Skip to content

Commit e9a2176

Browse files
committed
remove the hack of aliasing wait_for_event to wait_for_event_c
and implement the event loop in the native wait_for_event method instead
1 parent 48893c4 commit e9a2176

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

ext/ui/dialog.cc

+21-2
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,33 @@ resize(VALUE self)
9797
YEXCEPTION_CATCH
9898
}
9999

100+
/*
101+
* Extend wait_for_event with ability to process event in block.
102+
* @yield [event] pass raised event to block
103+
* @yield_param [UI::Event] event that occur
104+
* @yield_return [true,false] response if dialog should continue in waiting
105+
* for another event
106+
* @example change button label
107+
* dialog.wait_for_event do |event|
108+
* break unless event.is_a? UI::WidgetEvent
109+
* event.widget[:Label] = "Already pressed"
110+
* true
111+
* end
112+
*/
100113
static VALUE
101114
wait_for_event(VALUE self)
102115
{
103116
YEXCEPTION_TRY
117+
104118
YDialog *ptr = ui_unwrap_dialog(self);
105119
new CallbackFilter(ptr); //see filter documentation
106-
YEvent * ev = ptr->waitForEvent();
107-
return convert_event(ev);
120+
YEvent *ev = 0L;
121+
do {
122+
ev = ptr->waitForEvent();
123+
if (!rb_block_given_p())
124+
return convert_event(ev);
125+
} while (rb_yield(convert_event(ev)) != Qfalse);
126+
108127
YEXCEPTION_CATCH
109128
}
110129

lib/ui/dialog.rb

+2-20
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,9 @@
11
module UI
22
class Dialog
3-
alias_method :wait_for_event_c, :wait_for_event
3+
44
class << self
55
alias_method :current, :current_dialog
66
end
7-
# extend wait_for_event with ability to process event in block.
8-
# @yield [event] pass raised event to block
9-
# @yield_param [UI::Event] event that occur
10-
# @yield_return [true,false] response if dialog should continue in waiting
11-
# for another event
12-
# @example change button label
13-
# dialog.wait_for_event do |event|
14-
# break unless event.is_a? UI::WidgetEvent
15-
# event.widget[:Label] = "Already pressed"
16-
# true
17-
# end
18-
def wait_for_event(options={})
19-
if block_given?
20-
while (yield(wait_for_event_c)) do
21-
end
22-
else
23-
return wait_for_event_c
24-
end
25-
end
7+
268
end
279
end

0 commit comments

Comments
 (0)