Module: UI::EventDispatcher

Included in:
Dialog
Defined in:
library/general/src/lib/ui/event_dispatcher.rb

Overview

Provides switch between event_loop and dispatching to handlers. A #handle_event method can be defined to delegate some events.

Examples:

simple OK/cancel dialog

class OKDialog
  include Yast::UIShortcuts
  include Yast::Logger
  include UI::EventDispatcher
  Yast.import "UI"

  def run
    return nil unless Yast::UI.OpenDialog(
      HBox(
        PushButton(Id(:ok), "OK"),
        PushButton(Id(:cancel), "Cancel")
      )
    )
    begin
      return event_loop
    ensure
       Yast::UI.CloseDialog
    end
  end

  def ok_handler
    finish_dialog(:ok)
    log.info "OK button pressed"
  end

  def handle_event(input)
    widget.handler(input)
  end
end

Instance Method Summary collapse

Instance Method Details

#cancel_handlerObject

Default handler for cancel which can be also 'x' on dialog window



104
105
106
# File 'library/general/src/lib/ui/event_dispatcher.rb', line 104

def cancel_handler
  finish_dialog
end

#event_loopObject

Does UI event dispatching.

Returns:

  • value from exit_dialog method.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'library/general/src/lib/ui/event_dispatcher.rb', line 39

def event_loop
  Yast.import "UI"
  @_finish_dialog_flag = false

  loop do
    input = user_input

    if respond_to?(:"#{input}_handler")
      public_send(:"#{input}_handler")
    elsif respond_to?(:handle_event)
      public_send(:handle_event, input)
    else
      raise "Unknown action #{input}"
    end

    return @_finish_dialog_value if @_finish_dialog_flag
  end
end

#finish_dialog(return_value = nil) ⇒ Object

Set internal flag to not continue with processing other UI inputs

Parameters:

  • return_value (Object) (defaults to: nil)

    value to return from event_loop



98
99
100
101
# File 'library/general/src/lib/ui/event_dispatcher.rb', line 98

def finish_dialog(return_value = nil)
  @_finish_dialog_flag = true
  @_finish_dialog_value = return_value
end

#user_inputObject

Reads input for next event dispath Can be redefined to modify the way of getting user input, like introducing a timeout. Default implementation uses Yast::UI.UserInput which waits indefinitely for user input.

end

Examples:

use user input with timeout

class OKDialog
 include Yast::UIShortcuts
 include Yast::Logger
 include UI::EventDispatcher
 Yast.import "UI"

 def user_input
   Yast::UI.TimeoutUserInput(1000)
 end

 def run
   return nil unless Yast::UI.OpenDialog(
     HBox(
       PushButton(Id(:ok), "OK"),
       PushButton(Id(:cancel), "Cancel")
     )
   )
   begin
     return event_loop
   ensure
      Yast::UI.CloseDialog
   end
 end

 def ok_handler
   finish_dialog(:ok)
   log.info "OK button pressed"
 end


92
93
94
# File 'library/general/src/lib/ui/event_dispatcher.rb', line 92

def user_input
  Yast::UI.UserInput
end