Class: DuoRuby::Launcher

Inherits:
Object
  • Object
show all
Defined in:
lib/duoruby/launcher.rb

Overview

Starts the server in a child process and opens a native webview window on the main process’s main thread.

GTK requires all calls on the thread that called gtk_init (the OS main thread). The Async/Falcon server runs in a forked child process so each has full ownership of its own event loop. When the window is closed the child is terminated; when the child dies unexpectedly the window closes.

The window title defaults to DuoRuby.config.title, which the application can set in its duoruby.rb config file:

DuoRuby.configure { |c| c.title = "My App" }

Examples:

DuoRuby::Launcher.new(root: __dir__).run

Instance Method Summary collapse

Constructor Details

#initialize(root: Dir.pwd, host: "127.0.0.1", port: nil, title: nil, width: 1280, height: 800) ⇒ Launcher

Returns a new instance of Launcher.

Parameters:

  • root (String) (defaults to: Dir.pwd)

    application root directory

  • host (String) (defaults to: "127.0.0.1")

    server host (default: “127.0.0.1”)

  • port (Integer, nil) (defaults to: nil)

    server port; nil picks a free port automatically

  • title (String, nil) (defaults to: nil)

    window title; nil uses DuoRuby.config.title

  • width (Integer) (defaults to: 1280)

    window width in pixels (default: 1280)

  • height (Integer) (defaults to: 800)

    window height in pixels (default: 800)



30
31
32
33
34
35
36
37
38
# File 'lib/duoruby/launcher.rb', line 30

def initialize(root: Dir.pwd, host: "127.0.0.1", port: nil,
               title: nil, width: 1280, height: 800)
  @root   = File.expand_path(root)
  @host   = host
  @port   = port || free_port
  @title  = title
  @width  = width
  @height = height
end

Instance Method Details

#run(output: $stdout) ⇒ Object

Forks the Async server into a child process, then opens the native window on the main thread. Blocks until the window is closed, then terminates the server child.

Parameters:

  • output (IO) (defaults to: $stdout)

    where to print the launch banner (default: $stdout)



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/duoruby/launcher.rb', line 45

def run(output: $stdout)
  output.puts "launching http://#{@host}:#{@port}"

  server_pid = fork { run_server }

  wait_for_server

  title = @title || DuoRuby.config.title
  window = WebviewUtil::Window.new(title: title, width: @width, height: @height)
  window.navigate("http://#{@host}:#{@port}")
  window.run
ensure
  if server_pid
    Process.kill(:TERM, server_pid) rescue nil
    Process.waitpid(server_pid) rescue nil
  end
end