1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 23:38:12 +00:00

LibGUI: Handle Action keyboard shortcuts in Widget keydown

Widgets can now prevent shortcut Actions from being called, which
allows text input keydown handlers to override single key shortcuts.
This commit is contained in:
Zaggy1024 2022-10-24 19:05:40 -05:00 committed by Sam Atkins
parent 8e7c7e0a2a
commit 967dfa7956
6 changed files with 72 additions and 13 deletions

View file

@ -454,6 +454,37 @@ void Window::handle_multi_paint_event(MultiPaintEvent& event)
ConnectionToWindowServer::the().async_did_finish_painting(m_window_id, rects);
}
void Window::propagate_shortcuts_up_to_application(KeyEvent& event, Widget* widget)
{
VERIFY(event.type() == Event::KeyDown);
auto shortcut = Shortcut(event.modifiers(), event.key());
Action* action = nullptr;
if (widget) {
VERIFY(widget->window() == this);
do {
action = widget->action_for_shortcut(shortcut);
if (action)
break;
widget = widget->parent_widget();
} while (widget);
}
if (!action)
action = action_for_shortcut(shortcut);
if (!action)
action = Application::the()->action_for_shortcut(shortcut);
if (action) {
action->process_event(*this, event);
return;
}
event.ignore();
}
void Window::handle_key_event(KeyEvent& event)
{
if (!m_focused_widget && event.type() == Event::KeyDown && event.key() == Key_Tab && !event.ctrl() && !event.alt() && !event.super()) {
@ -465,9 +496,16 @@ void Window::handle_key_event(KeyEvent& event)
return default_return_key_widget()->dispatch_event(event, this);
if (m_focused_widget)
return m_focused_widget->dispatch_event(event, this);
if (m_main_widget)
return m_main_widget->dispatch_event(event, this);
m_focused_widget->dispatch_event(event, this);
else if (m_main_widget)
m_main_widget->dispatch_event(event, this);
if (event.is_accepted())
return;
// Only process shortcuts if this is a keydown event.
if (event.type() == Event::KeyDown)
propagate_shortcuts_up_to_application(event, nullptr);
}
void Window::handle_resize_event(ResizeEvent& event)