Class: WinGui::App

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

Overview

This class is a wrapper around Windows App

Constant Summary collapse

LAUNCH_TIMEOUT =
0.2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window_or_handle) ⇒ App

Returns a new instance of App.



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

def initialize(window_or_handle)
  @main_window = case window_or_handle
                   when Window
                     window_or_handle
                   when Integer
                     Window.new window_or_handle
                   else
                     raise WinGui::Errors::InitError, "Unable to create App from #{window_or_handle.inspect}"
                 end
end

Instance Attribute Details

#main_windowObject

Main App window (top level)



7
8
9
# File 'lib/win_gui/app.rb', line 7

def main_window
  @main_window
end

Class Method Details

.find(opts) ⇒ Object

Finds already launched Application. Either title or class for main window is obligatory. Returns nil if no such Application found. Options:

:title

main window title

:class

main window class

:timeout

timeout (seconds) finding main window

:raise

raise this exception instead of returning nil if nothing found



42
43
44
45
# File 'lib/win_gui/app.rb', line 42

def find(opts)
  main_window = Window.top_level(opts)
  main_window ? new(main_window) : nil
end

.launch(opts) ⇒ Object

Launch new Application. Expects executable path and options to find main Window. Options:

:path/:app_path

path to App’s executable file

:dir/:cd

change to this dir before launching App

:title

main window title

:class

main window class

:timeout

timeout (seconds) finding main window

:raise

raise this exception instead of returning nil if launched app window not found



56
57
58
59
60
61
62
63
64
65
# File 'lib/win_gui/app.rb', line 56

def launch(opts)
  app_path = opts.delete(:path) || opts.delete(:app_path)
  dir_path = opts.delete(:dir) || opts.delete(:cd)

  launch_app app_path, dir_path

  defaults = {timeout: LAUNCH_TIMEOUT,
              raise: WinGui::Errors::InitError.new("Unable to launch App with #{opts.inspect}")}
  find(defaults.merge opts)
end

Instance Method Details

#close(wait_timeout = nil) ⇒ Object Also known as: exit

Exits application (optionally waiting timeout seconds for main window to close)



21
22
23
24
25
26
27
28
29
# File 'lib/win_gui/app.rb', line 21

def close(wait_timeout=nil)
  @main_window.close
  if wait_timeout
    timeout(wait_timeout) do
      sleep SLEEP_DELAY while @main_window.window?
    end
  end
#      @main_window.wait_for_close(timeout) if timeout - does not work as Window#wait_for_close relies on window_visible?
end