Class: Webview::App

Inherits:
Object
  • Object
show all
Defined in:
lib/webview/app.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title: nil, width: nil, height: nil, resizable: nil, debug: false) ⇒ App



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/webview/app.rb', line 8

def initialize(title: nil, width: nil, height: nil, resizable: nil, debug: false)
  @options = {
    title: title,
    width: width,
    height: height,
    resizable: resizable,
    debug: debug
  }
  @options.delete_if { |k, v| v.nil? }
  @app_out = nil
  @app_err = nil
  @app_process = nil
end

Instance Attribute Details

#app_errObject (readonly)

Returns the value of attribute app_err.



6
7
8
# File 'lib/webview/app.rb', line 6

def app_err
  @app_err
end

#app_outObject (readonly)

Returns the value of attribute app_out.



6
7
8
# File 'lib/webview/app.rb', line 6

def app_out
  @app_out
end

#app_processObject (readonly)

Returns the value of attribute app_process.



6
7
8
# File 'lib/webview/app.rb', line 6

def app_process
  @app_process
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/webview/app.rb', line 6

def options
  @options
end

Instance Method Details

#closeObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/webview/app.rb', line 37

def close
  return true unless app_process
  app_out.close
  app_err.close

  pid = app_process.pid
  signal('QUIT')
  begin
    Timeout.timeout(3) { Process.wait(pid) }
  rescue Timeout::Error
    kill
  end

  @app_process = nil
end

#joinObject



53
54
55
56
57
# File 'lib/webview/app.rb', line 53

def join
  return unless app_process && app_process.alive?
  Process.wait(app_process.pid)
rescue Errno::ECHILD
end

#killObject



59
60
61
# File 'lib/webview/app.rb', line 59

def kill
  signal('TERM')
end

#open(url) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/webview/app.rb', line 22

def open(url)
  return true if @app_process
  cmd = [
    File.expand_path('ext/webview_app', ROOT_PATH),
    "-url '#{url}'"
  ]
  @options.each do |k, v|
    case v
    when true, false then cmd << "-#{k}" if v
    else cmd << "-#{k} '#{v}'"
    end
  end
  exec_cmd(cmd.join(' '))
end

#signal(name) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/webview/app.rb', line 63

def signal(name)
  return false unless app_process&.pid
  Process.kill(name, app_process.pid)
  true
rescue Errno::ECHILD, Errno::ESRCH
  false
end