Module: ShenmeGUI

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

Defined Under Namespace

Modules: Control, FileDialog

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.elementsObject (readonly)

Returns the value of attribute elements.



10
11
12
# File 'lib/shenmegui/core.rb', line 10

def elements
  @elements
end

.socketObject

Returns the value of attribute socket.



9
10
11
# File 'lib/shenmegui/core.rb', line 9

def socket
  @socket
end

.thisObject (readonly)

Returns the value of attribute this.



10
11
12
# File 'lib/shenmegui/core.rb', line 10

def this
  @this
end

Class Method Details

.alert(msg) ⇒ Object



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

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

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



31
32
33
34
35
36
37
38
39
40
# File 'lib/shenmegui/core.rb', line 31

def app(params={}, &block)
  body(params, &block)
  #找一个空闲的端口,不太好看
  temp_server = TCPServer.open('localhost', 0)
  @port = temp_server.addr[1]
  temp_server.close
  app_dir = File.expand_path($PROGRAM_NAME).match(/(.+)\/.+/)[1]
  File.open("#{app_dir}/index.html", 'w'){ |f| f.write @elements[0].render(port: @port) }
  nil
end

.enable_debuggingObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/shenmegui/core.rb', line 55

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

  end
end

.get_open_file_name(params = {}) ⇒ Object



47
48
49
# File 'lib/shenmegui/core.rb', line 47

def get_open_file_name(params={})
  FileDialog.get_open_file_name(params)
end

.get_save_file_name(params = {}) ⇒ Object



51
52
53
# File 'lib/shenmegui/core.rb', line 51

def get_save_file_name(params={})
  FileDialog.get_save_file_name(params)
end

.handle(msg) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/shenmegui/core.rb', line 12

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_properties(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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/shenmegui/core.rb', line 73

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

        ws.onclose { puts "Connection closed" }

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

        @socket = ws
      end
    end
  end
  begin
    app_dir = File.expand_path($PROGRAM_NAME).match(/(.+)\/.+/)[1]
    index_path = "#{app_dir}/index.html"
    if Gem.win_platform?
      `start file:///#{index_path}`
    elsif Gem.platforms[1].os == 'linux'
      `xdg-open file:///#{index_path}`
    end
  rescue
  end

  ws_thread.join
rescue Interrupt
  puts 'bye~'
end