From 053f41f4f97b4c5d701761418969a82b2d48992d Mon Sep 17 00:00:00 2001 From: faissaloo Date: Mon, 3 Jun 2019 18:13:41 +0100 Subject: [PATCH] GWindow: Leave SerenityKey mode if non-existent keybind is used --- LibGUI/GButton.cpp | 4 ++-- LibGUI/GButton.h | 1 + LibGUI/GWindow.cpp | 39 ++++++++++++++++++++++----------------- LibGUI/GWindow.h | 1 + 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/LibGUI/GButton.cpp b/LibGUI/GButton.cpp index 4522a9b7fc..46f49a61ab 100644 --- a/LibGUI/GButton.cpp +++ b/LibGUI/GButton.cpp @@ -64,9 +64,9 @@ void GButton::click() on_click(*this); } -/*bool GButton::accepts_keyboard_select() const { +bool GButton::accepts_keyboard_select() const { return is_enabled(); -}*/ +} void GButton::set_action(GAction& action) { diff --git a/LibGUI/GButton.h b/LibGUI/GButton.h index 1f62dc8d39..ae37ccf355 100644 --- a/LibGUI/GButton.h +++ b/LibGUI/GButton.h @@ -33,6 +33,7 @@ public: virtual const char* class_name() const override { return "GButton"; } virtual bool accepts_focus() const override { return true; } + virtual bool accepts_keyboard_select() const; protected: virtual void paint_event(GPaintEvent&) override; diff --git a/LibGUI/GWindow.cpp b/LibGUI/GWindow.cpp index a015152242..b3412269c9 100644 --- a/LibGUI/GWindow.cpp +++ b/LibGUI/GWindow.cpp @@ -278,24 +278,28 @@ void GWindow::event(CEvent& event) } if (m_keybind_mode) { - StringBuilder builder; - //Y u no work - builder.append(m_entered_keybind); - builder.append(keyevent.text()); - m_entered_keybind = builder.to_string(); + if (event.type() == GEvent::KeyUp) { + StringBuilder builder; + //Y u no work + builder.append(m_entered_keybind); + builder.append(keyevent.text()); + m_entered_keybind = builder.to_string(); - auto found_widget = m_hashed_potential_keybind_widgets.find(m_entered_keybind); - if (found_widget != m_hashed_potential_keybind_widgets.end()) { - m_keybind_mode = false; - const auto& point = Point(); - auto event = make(GEvent::MouseDown, point, 0, GMouseButton::Left, 0, 0); - found_widget->value->event(*event); - event = make(GEvent::MouseUp, point, 0, GMouseButton::Left, 0, 0); - found_widget->value->event(*event); - //Call click on the found widget + auto found_widget = m_hashed_potential_keybind_widgets.find(m_entered_keybind); + if (found_widget != m_hashed_potential_keybind_widgets.end()) { + m_keybind_mode = false; + const auto& point = Point(); + auto event = make(GEvent::MouseDown, point, 0, GMouseButton::Left, 0, 0); + found_widget->value->event(*event); + event = make(GEvent::MouseUp, point, 0, GMouseButton::Left, 0, 0); + found_widget->value->event(*event); + //Call click on the found widget + } else if (m_entered_keybind.length() >= m_max_keybind_length) { + m_keybind_mode = false; + } + //m_entered_keybind.append(keyevent.text()); + update(); } - //m_entered_keybind.append(keyevent.text()); - update(); } else { if (m_focused_widget) return m_focused_widget->event(event); @@ -348,7 +352,8 @@ void GWindow::find_keyboard_selectable() { m_hashed_potential_keybind_widgets.clear(); find_keyboard_selectable_children(m_main_widget); - size_t buffer_length = ceil_div(m_potential_keybind_widgets.size(), ('z'-'a'))+1; + m_max_keybind_length = ceil_div(m_potential_keybind_widgets.size(), ('z'-'a')); + size_t buffer_length = m_max_keybind_length + 1; char keybind_buffer[buffer_length]; for (size_t i = 0; i < buffer_length-1; i++) { keybind_buffer[i] = 'a'; diff --git a/LibGUI/GWindow.h b/LibGUI/GWindow.h index 1415fadd03..434756ba3a 100644 --- a/LibGUI/GWindow.h +++ b/LibGUI/GWindow.h @@ -166,6 +166,7 @@ private: bool m_show_titlebar { true }; bool m_keybind_mode { false }; String m_entered_keybind; + size_t m_max_keybind_length; Vector m_potential_keybind_widgets; HashMap m_hashed_potential_keybind_widgets; };