Module: ShenmeGUI

Defined in:
lib/shenmegui/core.rb,
lib/shenmegui/controls.rb,
lib/shenmegui/file_dialog.rb

Defined Under Namespace

Modules: Control, FileDialog Classes: HookedArray, HookedString

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.elementsObject (readonly)

Returns the value of attribute elements.



44
45
46
# File 'lib/shenmegui/core.rb', line 44

def elements
  @elements
end

.socketObject

Returns the value of attribute socket.



43
44
45
# File 'lib/shenmegui/core.rb', line 43

def socket
  @socket
end

.thisObject (readonly)

Returns the value of attribute this.



44
45
46
# File 'lib/shenmegui/core.rb', line 44

def this
  @this
end

Class Method Details

.alert(msg) ⇒ Object



71
72
73
74
# File 'lib/shenmegui/core.rb', line 71

def alert(msg)
  data = {message: msg}
  @socket.send("alert:0->#{data.to_json}")
end

.app(params = {}, &block) ⇒ Object



65
66
67
68
69
# File 'lib/shenmegui/core.rb', line 65

def app(params={}, &block)
  body(params, &block)
  File.open('index.html', 'w'){ |f| f.write @elements[0].render }
  nil
end

.debug!Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/shenmegui/core.rb', line 86

def self.debug!
  Thread.new do
    ShenmeGUI.instance_eval do
      bind = binding
      while true
        begin
          command = $stdin.gets.chomp
          result = eval command, bind
          puts "=> #{result}"
        rescue
          puts "#{$!}"
        end
      end
    end

  end
end

.handle(msg) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/shenmegui/core.rb', line 46

def handle(msg)
  match_data = msg.match(/(.+?):(\d+)(?:->)?({.+?})?/)
  command = match_data[1].to_sym
  id = match_data[2].to_i
  target = @elements[id]
  data = JSON.parse(match_data[3]) if match_data[3]
  case command
    when :sync
      target.update(data)
    else
      event_lambda = target.events[command]
      @this = @elements[id]
      ShenmeGUI.instance_exec(&event_lambda) if event_lambda
      @this = nil

  end
  target
end

.start!Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/shenmegui/core.rb', line 104

def self.start!
  ws_thread = Thread.new do
    EM.run do
      EM::WebSocket.run(:host => "0.0.0.0", :port => 80) do |ws|
        ws.onopen do
          puts "WebSocket connection open"
          @elements.each { |e| e.add_events; e.sync }
        end

        ws.onclose { puts "Connection closed" }

        ws.onmessage do |msg|
          puts "Recieved message: #{msg}"
          handle msg
        end
        
        @socket = ws
      end
    end
  end

  index_path = "#{Dir.pwd}/index.html"
  `start file:///#{index_path}`

  ws_thread.join
rescue Interrupt
  puts 'bye~'
end