Skip to content

Commit

Permalink
Add EventDispatcher#event_handler
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Jun 2, 2017
1 parent 20b72b6 commit ef38070
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
47 changes: 43 additions & 4 deletions library/general/src/lib/ui/event_dispatcher.rb
Expand Up @@ -37,14 +37,53 @@ def event_loop

loop do
input = user_input
raise "Unknown action #{input}" unless respond_to?(:"#{input}_handler")

send(:"#{input}_handler")

event_handler(input)
return @_finish_dialog_value if @_finish_dialog_flag
end
end

# General dialog events handler.
# Can be redefined to modify the way of managing events, for example when
# some events need to be delegated to a widget.
# @example delegate events
# class OKDialog
# include Yast::UIShortcuts
# include UI::EventDispatcher
# Yast.import "UI"
#
# def initialize
# @widget = Widget.new
# 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 event_handler(input)
# @widget.handler(input)
# case input
# when :ok
# finish_dialog(:ok)
# when :cancel
# finish_dialog(:cancel)
# end
# end
# end
def event_handler(input)
raise "Unknown action #{input}" unless respond_to?(:"#{input}_handler")
send(:"#{input}_handler")
end

# 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.
Expand Down
17 changes: 17 additions & 0 deletions library/general/test/event_dispatcher_test.rb
Expand Up @@ -32,6 +32,16 @@ def user_input
end
end

class DispatcherEventHandlerTestDialog
include Yast::UIShortcuts
include UI::EventDispatcher
Yast.import "UI"

def event_handler(_input)
finish_dialog(:always_cancel)
end
end

describe UI::EventDispatcher do
subject { DispatcherTestDialog.new }

Expand Down Expand Up @@ -68,6 +78,13 @@ def mock_ui_events(*events)

dialog.event_loop
end

it "uses custom event_handler to manage events" do
mock_ui_events(:ok)

dialog = DispatcherEventHandlerTestDialog.new
expect(dialog.event_loop).to eq(:always_cancel)
end
end

describe "#cancel_handler" do
Expand Down

0 comments on commit ef38070

Please sign in to comment.