Class: Flammarion::Engraving

Inherits:
Object
  • Object
show all
Includes:
Revelator, Writeable
Defined in:
lib/flammarion.rb

Constant Summary

Constants included from Revelator

Revelator::CHROME_PATH

Instance Attribute Summary collapse

Attributes included from Writeable

#front_end

Instance Method Summary collapse

Methods included from Writeable

#break, #button, #button_box, #callback_link, #checkbox, #clear, #embedded_button, #gets, #hide, #highlight, #html, #input, #live_reload_template, #map, #markdown, #plot, #puts, #replace, #script, #send, #show, #status, #style, #subpane, #table, #template

Methods included from Revelator

#open_a_window, #open_a_window_on_windows, #which

Constructor Details

#initialize(options = {}) ⇒ Engraving

Returns a new instance of Engraving.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/flammarion.rb', line 27

def initialize(options = {})
  @chrome = OpenStruct.new
  @sockets = []
  @actions = {}
  @front_end = self
  @pane_name = "default"
  @on_connect = options[:on_connect]
  @ignore_old = options.fetch(:ignore_old, false)
  @on_disconnect = options[:on_disconnect]
  @exit_on_disconnect = options.fetch(:exit_on_disconnect, false)

  start_server
  @window_id = @@server.register_window(self)
  open_a_window unless options[:no_chrome]
  @callbacks = {}
  wait_for_a_connection unless options[:no_wait]

  at_exit {close} if options.fetch(:close_on_exit, false)
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



25
26
27
# File 'lib/flammarion.rb', line 25

def actions
  @actions
end

#callbacksObject

Returns the value of attribute callbacks.



25
26
27
# File 'lib/flammarion.rb', line 25

def callbacks
  @callbacks
end

#chromeObject (readonly)

Returns the value of attribute chrome.



24
25
26
# File 'lib/flammarion.rb', line 24

def chrome
  @chrome
end

#on_connectObject

Returns the value of attribute on_connect.



25
26
27
# File 'lib/flammarion.rb', line 25

def on_connect
  @on_connect
end

#on_disconnectObject

Returns the value of attribute on_disconnect.



25
26
27
# File 'lib/flammarion.rb', line 25

def on_disconnect
  @on_disconnect
end

#socketsObject

Returns the value of attribute sockets.



25
26
27
# File 'lib/flammarion.rb', line 25

def sockets
  @sockets
end

Instance Method Details

#alert(text) ⇒ Object



110
111
112
# File 'lib/flammarion.rb', line 110

def alert(text)
  send_json(action:'alert', text:text)
end

#closeObject



128
129
130
# File 'lib/flammarion.rb', line 128

def close
  send_json({action:'close'})
end

#disconnect(ws) ⇒ Object



47
48
49
50
51
# File 'lib/flammarion.rb', line 47

def disconnect(ws)
  @sockets.delete ws
  exit 0 if @exit_on_disconnect
  @on_disconnect.call if @on_disconnect
end

#get_save_pathObject



137
138
139
140
141
142
143
# File 'lib/flammarion.rb', line 137

def get_save_path
  if Gem.win_platform?
    `powershell "Add-Type -AssemblyName System.windows.forms|Out-Null;$f=New-Object System.Windows.Forms.SaveFileDialog;$f.InitialDirectory='%cd%';$f.Filter='All Files (*.*)|*.*';$f.showHelp=$true;$f.ShowDialog()|Out-Null;$f.FileName"`.strip
  else
    `zenity --file-selection --save --confirm-overwrite`.strip
  end
end

#layout(file) ⇒ Object



123
124
125
126
# File 'lib/flammarion.rb', line 123

def layout(file)
  data = Slim::Template.new(file).render
  send_json({action:'layout', data:data})
end

#live_reload_layout(file) ⇒ Object



132
133
134
135
# File 'lib/flammarion.rb', line 132

def live_reload_layout(file)
  layout(file); yield if block_given?
  FileWatcher.new(file).watch {|file| layout(file); yield if block_given? }
end

#make_idObject



77
78
79
80
81
# File 'lib/flammarion.rb', line 77

def make_id
  @id ||= 0
  @id += 1
  "i#{@id}"
end

#orientation=(orientation) ⇒ Object

Raises:

  • (ArgumentError)


114
115
116
117
# File 'lib/flammarion.rb', line 114

def orientation=(orientation)
  raise ArgumentError.new("Orientation must be :horizontal or :vertical") unless [:horizontal, :vertical].include?(orientation)
  send_json({action:'reorient', orientation:orientation})
end

#pane(name) ⇒ Object



106
107
108
# File 'lib/flammarion.rb', line 106

def pane(name)
  return Pane.new(self, name)
end

#process_message(msg) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/flammarion.rb', line 53

def process_message(msg)
  @last_msg = msg
  m = {}
  begin
    m = JSON.parse(msg)
  rescue JSON::ParserError
    log "Invalid JSON String"
    return
  end

  case m["action"]
  when 'callback'
    callback = @callbacks[m['id']]
    unless callback.nil?
      if callback.arity == 1
        callback.call(m)
      else
        callback.call
      end
    end
  end
  @actions[m["action"]].call(m) if @actions.include?(m["action"])
end

#send_json(val) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/flammarion.rb', line 97

def send_json(val)
  if @sockets.empty? then
    open_a_window
    wait_for_a_connection
  end
  @sockets.each{|ws| ws.send val.to_json}
  nil
end

#serverObject



87
# File 'lib/flammarion.rb', line 87

def server; @@server; end

#start_serverObject



83
84
85
# File 'lib/flammarion.rb', line 83

def start_server
  @@server ||= Server.new
end

#title(str) ⇒ Object



119
120
121
# File 'lib/flammarion.rb', line 119

def title(str)
  send_json({action:'title', title:str})
end

#wait_for_a_connectionObject



89
90
91
# File 'lib/flammarion.rb', line 89

def wait_for_a_connection
  sleep 0.5 while @sockets.empty?
end

#window_open?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/flammarion.rb', line 93

def window_open?
  not @sockets.empty?
end