diff options
author | Andreas Kling <kling@serenityos.org> | 2021-05-12 20:51:45 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-05-12 20:51:45 +0200 |
commit | 8c7bce818d7c394f14c19d1efd20ba998fc34232 (patch) | |
tree | 4a465a1b5ae333b2aa9ae3f0ecd9e4ac653611ab /Userland | |
parent | 91eda222087048fef131a1bc39bcda947bce2288 (diff) | |
download | serenity-8c7bce818d7c394f14c19d1efd20ba998fc34232.zip |
LibGUI: Move action-from-key-event code to a separate function
The logic that figures out which (if any) action should be activated
by a keydown event was getting a bit unwieldy. This patch moves it to
a separate helper function.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibGUI/WindowServerConnection.cpp | 47 |
1 files changed, 26 insertions, 21 deletions
diff --git a/Userland/Libraries/LibGUI/WindowServerConnection.cpp b/Userland/Libraries/LibGUI/WindowServerConnection.cpp index 81609ce748..4cf36d658c 100644 --- a/Userland/Libraries/LibGUI/WindowServerConnection.cpp +++ b/Userland/Libraries/LibGUI/WindowServerConnection.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> + * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -111,37 +111,42 @@ void WindowServerConnection::window_left(i32 window_id) Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowLeft)); } -void WindowServerConnection::key_down(i32 window_id, u32 code_point, u32 key, u32 modifiers, u32 scancode) +static Action* action_for_key_event(Window& window, KeyEvent const& event) { - auto* window = Window::from_window_id(window_id); - if (!window) - return; - - auto key_event = make<KeyEvent>(Event::KeyDown, (KeyCode)key, modifiers, code_point, scancode); - Action* action = nullptr; - - dbgln_if(KEYBOARD_SHORTCUTS_DEBUG, "Looking up action for {}", key_event->to_string()); - - if (auto* focused_widget = window->focused_widget()) { - for (auto* widget = focused_widget; widget && !action; widget = widget->parent_widget()) { - action = widget->action_for_key_event(*key_event); + dbgln_if(KEYBOARD_SHORTCUTS_DEBUG, "Looking up action for {}", event.to_string()); + for (auto* widget = window.focused_widget(); widget; widget = widget->parent_widget()) { + if (auto* action = widget->action_for_key_event(event)) { dbgln_if(KEYBOARD_SHORTCUTS_DEBUG, " > Focused widget {} gave action: {}", *widget, action); + return action; } } - if (!action) { - action = window->action_for_key_event(*key_event); - dbgln_if(KEYBOARD_SHORTCUTS_DEBUG, " > Asked window {}, got action: {}", *window, action); + if (auto* action = window.action_for_key_event(event)) { + dbgln_if(KEYBOARD_SHORTCUTS_DEBUG, " > Asked window {}, got action: {}", window, action); + return action; } // NOTE: Application-global shortcuts are ignored while a modal window is up. - if (!action && !window->is_modal()) { - action = Application::the()->action_for_key_event(*key_event); - dbgln_if(KEYBOARD_SHORTCUTS_DEBUG, " > Asked application, got action: {}", action); + if (!window.is_modal()) { + if (auto* action = Application::the()->action_for_key_event(event)) { + dbgln_if(KEYBOARD_SHORTCUTS_DEBUG, " > Asked application, got action: {}", action); + return action; + } } - if (action) { + return nullptr; +} + +void WindowServerConnection::key_down(i32 window_id, u32 code_point, u32 key, u32 modifiers, u32 scancode) +{ + auto* window = Window::from_window_id(window_id); + if (!window) + return; + + auto key_event = make<KeyEvent>(Event::KeyDown, (KeyCode)key, modifiers, code_point, scancode); + + if (auto* action = action_for_key_event(*window, *key_event)) { if (action->is_enabled()) { action->activate(); return; |