Area input and redraws
A drawing callback displays state. Change state in input callbacks or timers, then call queue_redraw_all to schedule the next draw. queue_redraw_all does not draw immediately.
x = 80.0
y = 80.0
brush = UIng::Area::Draw::Brush.new(:solid, 0.2, 0.4, 0.8, 1.0)
handler = UIng::Area::Handler.new do
draw do |_area, params|
params.context.fill_path(brush) do |path|
path.add_rectangle(x, y, 24, 24)
end
end
mouse_event do |area, event|
x = event.x
y = event.y
area.queue_redraw_all
end
key_event do |area, event|
handled = !event.up? && event.ext_key == UIng::Area::ExtKey::Left
x -= 8 if handled
area.queue_redraw_all if handled
handled
end
scroll_event do |area, event|
puts "Scroll: #{event.delta_x}, #{event.delta_y}"
true # Consume the event; return false for normal scrolling in a scrolling Area.
end
end
area = UIng::Area.new(handler)
Input callbacks
mouse_eventreceives coordinates, button state, and modifiers.mouse_crossedreceives pointer enter/leave changes.drag_brokencan restore state when a drag is interrupted.key_eventreturnstruewhen it handled the key andfalseotherwise.scroll_eventreceives wheel and trackpad movement indelta_xanddelta_y; callhandler.scroll_event(nil)to clear it.
Scroll deltas are signed device amounts, not drawing coordinates. Their size varies by device and platform. Positive values mean right and down.
Call area.focus to request keyboard focus after attaching it to a visible window. Check area.focused? if needed; the native window system may reject the request.
Call begin_user_window_move and begin_user_window_resize only inside mouse_event while event.down != 0. For animation, avoid creating drawing resources every frame and redraw only after state changes.
area_events.cr visualizes keyboard and mouse input. area_analog_clock.cr and reversi.cr combine state with redraws.