Sha256: 1223f5522b97f77d822b1bbd883c439cc6bacf397949fa20e2dd4fa2d96229ec

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

require 'timeout'
require 'open3'

module Webview
  class App
    attr_reader :app_out, :app_err, :app_process, :options

    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? }
    end

    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

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

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

      @app_process = nil
    end

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

    def kill
      return unless app_process&.pid
      Process.kill('TERM', app_process.pid)
    end

    private

    def exec_cmd(cmd)
      app_in, @app_out, @app_err, @app_process = Open3.popen3(cmd)
      app_in.close
      if app_process && app_process.alive?
        true
      else
        false
      end
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
webview-0.1.0 lib/webview/app.rb