Kaynağa Gözat

LibGUI: Allow GUI::Action to swallow key events when disabled

Sometimes an action should be disabled and the KeyEvent not posted to
the app's event loop nonetheless. In other words the action swallows the
KeyEvent without being activated.

Specific use-case: Terminal's Ctrl+Shift+{C,V}.
Linus Groh 4 yıl önce
ebeveyn
işleme
886fe7e69f

+ 4 - 0
Libraries/LibGUI/Action.h

@@ -132,6 +132,9 @@ public:
     }
     }
     void set_checked(bool);
     void set_checked(bool);
 
 
+    bool swallow_key_event_when_disabled() const { return m_swallow_key_event_when_disabled; }
+    void set_swallow_key_event_when_disabled(bool swallow) { m_swallow_key_event_when_disabled = swallow; }
+
     void register_button(Badge<Button>, Button&);
     void register_button(Badge<Button>, Button&);
     void unregister_button(Badge<Button>, Button&);
     void unregister_button(Badge<Button>, Button&);
     void register_menu_item(Badge<MenuItem>, MenuItem&);
     void register_menu_item(Badge<MenuItem>, MenuItem&);
@@ -159,6 +162,7 @@ private:
     bool m_enabled { true };
     bool m_enabled { true };
     bool m_checkable { false };
     bool m_checkable { false };
     bool m_checked { false };
     bool m_checked { false };
+    bool m_swallow_key_event_when_disabled { false };
     ShortcutScope m_scope { ShortcutScope::None };
     ShortcutScope m_scope { ShortcutScope::None };
 
 
     HashTable<Button*> m_buttons;
     HashTable<Button*> m_buttons;

+ 7 - 3
Libraries/LibGUI/WindowServerConnection.cpp

@@ -173,9 +173,13 @@ void WindowServerConnection::handle(const Messages::WindowClient::KeyDown& messa
 #endif
 #endif
     }
     }
 
 
-    if (action && action->is_enabled()) {
-        action->activate();
-        return;
+    if (action) {
+        if (action->is_enabled()) {
+            action->activate();
+            return;
+        }
+        if (action->swallow_key_event_when_disabled())
+            return;
     }
     }
 
 
     bool focused_widget_accepts_emoji_input = window->focused_widget() && window->focused_widget()->accepts_emoji_input();
     bool focused_widget_accepts_emoji_input = window->focused_widget() && window->focused_widget()->accepts_emoji_input();