From 72264661fdc86049dec1e17b6b8906eb6b86e658 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Mon, 3 Jun 2019 16:02:49 +0100 Subject: [PATCH 1/9] GWindow: Add SerenityKeys minimum functionality --- Applications/About/main.cpp | 1 + LibGUI/GAbstractButton.h | 1 + LibGUI/GButton.cpp | 4 ++ LibGUI/GWidget.h | 1 + LibGUI/GWindow.cpp | 97 +++++++++++++++++++++++++++++++++++-- LibGUI/GWindow.h | 9 ++++ 6 files changed, 109 insertions(+), 4 deletions(-) diff --git a/Applications/About/main.cpp b/Applications/About/main.cpp index e05fd3a7fc..e6c22966cb 100644 --- a/Applications/About/main.cpp +++ b/Applications/About/main.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include int main(int argc, char** argv) diff --git a/LibGUI/GAbstractButton.h b/LibGUI/GAbstractButton.h index fdbb6a7a8c..40f9869f28 100644 --- a/LibGUI/GAbstractButton.h +++ b/LibGUI/GAbstractButton.h @@ -26,6 +26,7 @@ public: virtual void click() = 0; virtual const char* class_name() const override { return "GAbstractButton"; } virtual bool accepts_focus() const override { return true; } + virtual bool accepts_keyboard_select() const { return true; } protected: explicit GAbstractButton(GWidget* parent); diff --git a/LibGUI/GButton.cpp b/LibGUI/GButton.cpp index 62e521702d..4522a9b7fc 100644 --- a/LibGUI/GButton.cpp +++ b/LibGUI/GButton.cpp @@ -64,6 +64,10 @@ void GButton::click() on_click(*this); } +/*bool GButton::accepts_keyboard_select() const { + return is_enabled(); +}*/ + void GButton::set_action(GAction& action) { m_action = action.make_weak_ptr(); diff --git a/LibGUI/GWidget.h b/LibGUI/GWidget.h index 6e73c7e4c1..11524fb005 100644 --- a/LibGUI/GWidget.h +++ b/LibGUI/GWidget.h @@ -106,6 +106,7 @@ public: void update(const Rect&); virtual bool accepts_focus() const { return false; } + virtual bool accepts_keyboard_select() const { return false; } bool is_focused() const; void set_focus(bool); diff --git a/LibGUI/GWindow.cpp b/LibGUI/GWindow.cpp index cccb2fda24..a015152242 100644 --- a/LibGUI/GWindow.cpp +++ b/LibGUI/GWindow.cpp @@ -230,6 +230,19 @@ void GWindow::event(CEvent& event) for (auto& rect : rects) m_main_widget->event(*make(rect)); + if (m_keybind_mode) { + //If we're in keybind mode indicate widgets in m_potential_keybind_widgets + GPainter painter(*m_main_widget); + painter.draw_text(Rect(20,20,20,20), m_entered_keybind.characters(), TextAlignment::TopLeft, Color::Green); + + for (auto& keypair: m_hashed_potential_keybind_widgets) { + auto widget = keypair.value; + auto rect = Rect(widget->x()-5, widget->y()-5, 12, 12); + painter.draw_text(rect, keypair.key.characters(), TextAlignment::TopLeft, Color::Black); + } + + } + if (m_double_buffering_enabled) flip(rects); else if (created_new_backing_store) @@ -251,10 +264,44 @@ void GWindow::event(CEvent& event) } if (event.type() == GEvent::KeyUp || event.type() == GEvent::KeyDown) { - if (m_focused_widget) - return m_focused_widget->event(event); - if (m_main_widget) - return m_main_widget->event(event); + auto keyevent = static_cast(event); + if (keyevent.logo() && event.type() == GEvent::KeyUp) { + if (m_keybind_mode) { + m_keybind_mode = false; + } else { + m_keybind_mode = true; + find_keyboard_selectable(); + m_entered_keybind = ""; + } + update(); + return; + } + + 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(); + + 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 + } + //m_entered_keybind.append(keyevent.text()); + update(); + } else { + if (m_focused_widget) + return m_focused_widget->event(event); + if (m_main_widget) + return m_main_widget->event(event); + } return; } @@ -296,6 +343,44 @@ void GWindow::event(CEvent& event) CObject::event(event); } +void GWindow::find_keyboard_selectable() { + m_potential_keybind_widgets.clear(); + 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; + char keybind_buffer[buffer_length]; + for (size_t i = 0; i < buffer_length-1; i++) { + keybind_buffer[i] = 'a'; + } + keybind_buffer[buffer_length-1] = '\0'; + + for (auto& widget: m_potential_keybind_widgets) { + m_hashed_potential_keybind_widgets.set(String(keybind_buffer), widget); + + for (size_t i = 0; i < buffer_length-1; i++) { + if (keybind_buffer[i] >= 'z') { + keybind_buffer[i] = 'a'; + } else { + keybind_buffer[i]++; + break; + } + } + } +} + +void GWindow::find_keyboard_selectable_children(GWidget* widget) { + //Rather than painting immediately we need a step to collect all the keybinds first + widget -> for_each_child_widget([&] (auto& child) { + if (child.accepts_keyboard_select()) { + //auto rect = Rect(child.x()-5, child.y()-5, 10, 10); + m_potential_keybind_widgets.append(&child); + find_keyboard_selectable_children(&child); + } + return IterationDecision::Continue; + }); +} + bool GWindow::is_visible() const { return false; @@ -305,6 +390,10 @@ void GWindow::update(const Rect& a_rect) { if (!m_window_id) return; + + //We probably shouldn't clear the buffer on updates + //find_keyboard_selectable(); + for (auto& pending_rect : m_pending_paint_event_rects) { if (pending_rect.contains(a_rect)) { #ifdef UPDATE_COALESCING_DEBUG diff --git a/LibGUI/GWindow.h b/LibGUI/GWindow.h index 200bec1a77..1415fadd03 100644 --- a/LibGUI/GWindow.h +++ b/LibGUI/GWindow.h @@ -1,12 +1,15 @@ #pragma once #include +#include +#include #include #include #include #include #include +class GPainter; class GWidget; class GWMEvent; @@ -130,6 +133,8 @@ protected: private: virtual bool is_window() const override final { return true; } + void find_keyboard_selectable(); + void find_keyboard_selectable_children(GWidget* widget); Retained create_backing_bitmap(const Size&); void set_current_backing_bitmap(GraphicsBitmap&, bool flush_immediately = false); void flip(const Vector& dirty_rects); @@ -159,4 +164,8 @@ private: bool m_resizable { true }; bool m_fullscreen { false }; bool m_show_titlebar { true }; + bool m_keybind_mode { false }; + String m_entered_keybind; + Vector m_potential_keybind_widgets; + HashMap m_hashed_potential_keybind_widgets; }; From 053f41f4f97b4c5d701761418969a82b2d48992d Mon Sep 17 00:00:00 2001 From: faissaloo Date: Mon, 3 Jun 2019 18:13:41 +0100 Subject: [PATCH 2/9] 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; }; From d449cda3d8c39d8108ee5fd29ae9c8922f67a176 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Mon, 3 Jun 2019 21:04:50 +0100 Subject: [PATCH 3/9] GWindow: Highlight for multicharacter SerenityKeys --- LibGUI/GWindow.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/LibGUI/GWindow.cpp b/LibGUI/GWindow.cpp index b3412269c9..61b5d284e3 100644 --- a/LibGUI/GWindow.cpp +++ b/LibGUI/GWindow.cpp @@ -233,12 +233,21 @@ void GWindow::event(CEvent& event) if (m_keybind_mode) { //If we're in keybind mode indicate widgets in m_potential_keybind_widgets GPainter painter(*m_main_widget); - painter.draw_text(Rect(20,20,20,20), m_entered_keybind.characters(), TextAlignment::TopLeft, Color::Green); for (auto& keypair: m_hashed_potential_keybind_widgets) { auto widget = keypair.value; auto rect = Rect(widget->x()-5, widget->y()-5, 12, 12); - painter.draw_text(rect, keypair.key.characters(), TextAlignment::TopLeft, Color::Black); + bool could_be_keybind = true; + for (size_t i = 0; i < m_entered_keybind.length(); i++) { + if (keypair.key.characters()[i] != m_entered_keybind.characters()[i]) { + could_be_keybind = false; + } + } + if (could_be_keybind) { + painter.draw_text(rect, keypair.key.characters(), TextAlignment::TopLeft, Color::Black); + painter.draw_text(rect, m_entered_keybind.characters(), TextAlignment::TopLeft, Color::Green); + } + } } @@ -280,7 +289,6 @@ void GWindow::event(CEvent& event) if (m_keybind_mode) { 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(); @@ -297,7 +305,6 @@ void GWindow::event(CEvent& event) } else if (m_entered_keybind.length() >= m_max_keybind_length) { m_keybind_mode = false; } - //m_entered_keybind.append(keyevent.text()); update(); } } else { From 185bff6714904c8d6daf698bfff7187143b12796 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Sat, 8 Jun 2019 15:13:16 +0100 Subject: [PATCH 4/9] GWindow: SerenityKeys refactor --- LibGUI/GWindow.cpp | 132 ++++++++++++++++++++++----------------------- LibGUI/GWindow.h | 5 +- 2 files changed, 68 insertions(+), 69 deletions(-) diff --git a/LibGUI/GWindow.cpp b/LibGUI/GWindow.cpp index 61b5d284e3..fdbc6e608a 100644 --- a/LibGUI/GWindow.cpp +++ b/LibGUI/GWindow.cpp @@ -230,27 +230,7 @@ void GWindow::event(CEvent& event) for (auto& rect : rects) m_main_widget->event(*make(rect)); - if (m_keybind_mode) { - //If we're in keybind mode indicate widgets in m_potential_keybind_widgets - GPainter painter(*m_main_widget); - - for (auto& keypair: m_hashed_potential_keybind_widgets) { - auto widget = keypair.value; - auto rect = Rect(widget->x()-5, widget->y()-5, 12, 12); - bool could_be_keybind = true; - for (size_t i = 0; i < m_entered_keybind.length(); i++) { - if (keypair.key.characters()[i] != m_entered_keybind.characters()[i]) { - could_be_keybind = false; - } - } - if (could_be_keybind) { - painter.draw_text(rect, keypair.key.characters(), TextAlignment::TopLeft, Color::Black); - painter.draw_text(rect, m_entered_keybind.characters(), TextAlignment::TopLeft, Color::Green); - } - - } - - } + paint_keybinds(); if (m_double_buffering_enabled) flip(rects); @@ -288,30 +268,29 @@ void GWindow::event(CEvent& event) if (m_keybind_mode) { if (event.type() == GEvent::KeyUp) { - StringBuilder builder; - builder.append(m_entered_keybind); - builder.append(keyevent.text()); - m_entered_keybind = builder.to_string(); + StringBuilder builder; + 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 - } else if (m_entered_keybind.length() >= m_max_keybind_length) { - m_keybind_mode = false; - } - update(); + 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); + } else if (m_entered_keybind.length() >= m_max_keybind_length) { + m_keybind_mode = false; + } + update(); } } else { if (m_focused_widget) - return m_focused_widget->event(event); + return m_focused_widget->event(event); if (m_main_widget) - return m_main_widget->event(event); + return m_main_widget->event(event); } return; } @@ -354,40 +333,59 @@ void GWindow::event(CEvent& event) CObject::event(event); } -void GWindow::find_keyboard_selectable() { - m_potential_keybind_widgets.clear(); - m_hashed_potential_keybind_widgets.clear(); - find_keyboard_selectable_children(m_main_widget); +void GWindow::paint_keybinds() { + if (m_keybind_mode) { + GPainter painter(*m_main_widget); - 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'; - } - keybind_buffer[buffer_length-1] = '\0'; - - for (auto& widget: m_potential_keybind_widgets) { - m_hashed_potential_keybind_widgets.set(String(keybind_buffer), widget); - - for (size_t i = 0; i < buffer_length-1; i++) { - if (keybind_buffer[i] >= 'z') { - keybind_buffer[i] = 'a'; - } else { - keybind_buffer[i]++; - break; + for (auto& keypair: m_hashed_potential_keybind_widgets) { + auto widget = keypair.value; + auto rect = Rect(widget->x()-5, widget->y()-5, 12, 12); + bool could_be_keybind = true; + for (size_t i = 0; i < m_entered_keybind.length(); i++) { + if (keypair.key.characters()[i] != m_entered_keybind.characters()[i]) { + could_be_keybind = false; + } + } + if (could_be_keybind) { + painter.draw_text(rect, keypair.key.characters(), TextAlignment::TopLeft, Color::Black); + painter.draw_text(rect, m_entered_keybind.characters(), TextAlignment::TopLeft, Color::Green); + } } - } } } -void GWindow::find_keyboard_selectable_children(GWidget* widget) { - //Rather than painting immediately we need a step to collect all the keybinds first +void GWindow::find_keyboard_selectable() { + Vector potential_keybind_widgets; + m_hashed_potential_keybind_widgets.clear(); + find_keyboard_selectable_children(m_main_widget, potential_keybind_widgets); + + m_max_keybind_length = ceil_div(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'; + } + keybind_buffer[buffer_length-1] = '\0'; + + for (auto& widget: potential_keybind_widgets) { + m_hashed_potential_keybind_widgets.set(String(keybind_buffer), widget); + + for (size_t i = 0; i < buffer_length-1; i++) { + if (keybind_buffer[i] >= 'z') { + keybind_buffer[i] = 'a'; + } else { + keybind_buffer[i]++; + break; + } + } + } +} + +void GWindow::find_keyboard_selectable_children(GWidget* widget, Vector &potential_keybind_widgets) { widget -> for_each_child_widget([&] (auto& child) { if (child.accepts_keyboard_select()) { - //auto rect = Rect(child.x()-5, child.y()-5, 10, 10); - m_potential_keybind_widgets.append(&child); - find_keyboard_selectable_children(&child); + potential_keybind_widgets.append(&child); + find_keyboard_selectable_children(&child, potential_keybind_widgets); } return IterationDecision::Continue; }); diff --git a/LibGUI/GWindow.h b/LibGUI/GWindow.h index 434756ba3a..5ced9f7572 100644 --- a/LibGUI/GWindow.h +++ b/LibGUI/GWindow.h @@ -133,8 +133,10 @@ protected: private: virtual bool is_window() const override final { return true; } + void paint_keybinds(); + void find_keyboard_selectable(); - void find_keyboard_selectable_children(GWidget* widget); + void find_keyboard_selectable_children(GWidget* widget, Vector &potential_keybind_widgets); Retained create_backing_bitmap(const Size&); void set_current_backing_bitmap(GraphicsBitmap&, bool flush_immediately = false); void flip(const Vector& dirty_rects); @@ -167,6 +169,5 @@ private: 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; }; From 6e0e9481a4b01c7bc69ebf18ff86daa71f1f0b44 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Sun, 9 Jun 2019 17:01:25 +0100 Subject: [PATCH 5/9] GWindow: Make SerenityKeys labels look better --- LibGUI/GWindow.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/LibGUI/GWindow.cpp b/LibGUI/GWindow.cpp index fdbc6e608a..284ce123b6 100644 --- a/LibGUI/GWindow.cpp +++ b/LibGUI/GWindow.cpp @@ -339,16 +339,21 @@ void GWindow::paint_keybinds() { for (auto& keypair: m_hashed_potential_keybind_widgets) { auto widget = keypair.value; - auto rect = Rect(widget->x()-5, widget->y()-5, 12, 12); bool could_be_keybind = true; for (size_t i = 0; i < m_entered_keybind.length(); i++) { if (keypair.key.characters()[i] != m_entered_keybind.characters()[i]) { could_be_keybind = false; } } + if (could_be_keybind) { - painter.draw_text(rect, keypair.key.characters(), TextAlignment::TopLeft, Color::Black); - painter.draw_text(rect, m_entered_keybind.characters(), TextAlignment::TopLeft, Color::Green); + auto rect = Rect(widget->x()-5, widget->y()-5, 4+Font::default_font().width(keypair.key), 16); + auto highlight_rect = Rect(widget->x()-3, widget->y()-5, 0, 16); + + painter.fill_rect(rect, Color::LightGray); + painter.draw_rect(rect, Color::Black, false); + painter.draw_text(rect, keypair.key.characters(), TextAlignment::Center, Color::Black); + painter.draw_text(highlight_rect, m_entered_keybind.characters(), TextAlignment::CenterLeft, Color::MidGray); } } } @@ -401,9 +406,6 @@ void GWindow::update(const Rect& a_rect) if (!m_window_id) return; - //We probably shouldn't clear the buffer on updates - //find_keyboard_selectable(); - for (auto& pending_rect : m_pending_paint_event_rects) { if (pending_rect.contains(a_rect)) { #ifdef UPDATE_COALESCING_DEBUG From 50fd9ed38383d0d28180a556ec9300823aaeb490 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Sat, 15 Jun 2019 21:53:25 +0100 Subject: [PATCH 6/9] GWindow: Hide SerenityKeys when window is deselected --- LibGUI/GWindow.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/LibGUI/GWindow.cpp b/LibGUI/GWindow.cpp index 284ce123b6..a84d55a0ed 100644 --- a/LibGUI/GWindow.cpp +++ b/LibGUI/GWindow.cpp @@ -296,6 +296,11 @@ void GWindow::event(CEvent& event) } if (event.type() == GEvent::WindowBecameActive || event.type() == GEvent::WindowBecameInactive) { + if (event.type() == GEvent::WindowBecameInactive && m_keybind_mode) { + m_keybind_mode = false; + update(); + } + m_is_active = event.type() == GEvent::WindowBecameActive; if (m_main_widget) m_main_widget->event(event); From 770907f90caad109dc57529843a3e9c21c25fcfc Mon Sep 17 00:00:00 2001 From: faissaloo Date: Sun, 16 Jun 2019 16:55:39 +0100 Subject: [PATCH 7/9] GWindow: Cleanup --- LibGUI/GWindow.cpp | 34 +++++++++++++++++----------------- LibGUI/GWindow.h | 4 +--- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/LibGUI/GWindow.cpp b/LibGUI/GWindow.cpp index 9ef6ce4c90..0cc561c156 100644 --- a/LibGUI/GWindow.cpp +++ b/LibGUI/GWindow.cpp @@ -3,6 +3,7 @@ #include "GEventLoop.h" #include "GWidget.h" #include +#include #include #include #include @@ -350,27 +351,26 @@ void GWindow::event(CEvent& event) } void GWindow::paint_keybinds() { - if (m_keybind_mode) { - GPainter painter(*m_main_widget); + if (!m_keybind_mode) return; + GPainter painter(*m_main_widget); - for (auto& keypair: m_hashed_potential_keybind_widgets) { - auto widget = keypair.value; - bool could_be_keybind = true; - for (size_t i = 0; i < m_entered_keybind.length(); i++) { - if (keypair.key.characters()[i] != m_entered_keybind.characters()[i]) { - could_be_keybind = false; - } + for (auto& keypair: m_hashed_potential_keybind_widgets) { + auto widget = keypair.value; + bool could_be_keybind = true; + for (size_t i = 0; i < m_entered_keybind.length(); i++) { + if (keypair.key.characters()[i] != m_entered_keybind.characters()[i]) { + could_be_keybind = false; } + } - if (could_be_keybind) { - auto rect = Rect(widget->x()-5, widget->y()-5, 4+Font::default_font().width(keypair.key), 16); - auto highlight_rect = Rect(widget->x()-3, widget->y()-5, 0, 16); + if (could_be_keybind) { + auto rect = Rect(widget->x()-5, widget->y()-5, 4+Font::default_font().width(keypair.key), 16); + auto highlight_rect = Rect(widget->x()-3, widget->y()-5, 0, 16); - painter.fill_rect(rect, Color::LightGray); - painter.draw_rect(rect, Color::Black, false); - painter.draw_text(rect, keypair.key.characters(), TextAlignment::Center, Color::Black); - painter.draw_text(highlight_rect, m_entered_keybind.characters(), TextAlignment::CenterLeft, Color::MidGray); - } + painter.fill_rect(rect, Color::LightGray); + painter.draw_rect(rect, Color::Black, false); + painter.draw_text(rect, keypair.key.characters(), TextAlignment::Center, Color::Black); + painter.draw_text(highlight_rect, m_entered_keybind.characters(), TextAlignment::CenterLeft, Color::MidGray); } } } diff --git a/LibGUI/GWindow.h b/LibGUI/GWindow.h index 957a50ed93..aa09e6646a 100644 --- a/LibGUI/GWindow.h +++ b/LibGUI/GWindow.h @@ -2,14 +2,12 @@ #include #include -#include #include #include #include #include #include -class GPainter; class GWidget; class GWMEvent; @@ -168,6 +166,6 @@ private: bool m_show_titlebar { true }; bool m_keybind_mode { false }; String m_entered_keybind; - size_t m_max_keybind_length; + size_t m_max_keybind_length { 0 }; HashMap m_hashed_potential_keybind_widgets; }; From 55e115b0cd0826db2a324c3ea0ee83d54e4ba298 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Sun, 16 Jun 2019 21:01:51 +0100 Subject: [PATCH 8/9] LibGUI: clang-format --- LibGUI/GAbstractButton.cpp | 2 +- LibGUI/GButton.cpp | 5 ++-- LibGUI/GWindow.cpp | 48 +++++++++++++++++++++----------------- LibGUI/GWindow.h | 2 +- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/LibGUI/GAbstractButton.cpp b/LibGUI/GAbstractButton.cpp index 27b0fefb32..61aaacfed3 100644 --- a/LibGUI/GAbstractButton.cpp +++ b/LibGUI/GAbstractButton.cpp @@ -31,7 +31,7 @@ void GAbstractButton::set_checked(bool checked) m_checked = checked; if (is_exclusive() && checked) { - parent_widget()->for_each_child_of_type([&] (auto& sibling) { + parent_widget()->for_each_child_of_type([&](auto& sibling) { if (!sibling.is_exclusive() || !sibling.is_checkable() || !sibling.is_checked()) return IterationDecision::Continue; sibling.m_checked = false; diff --git a/LibGUI/GButton.cpp b/LibGUI/GButton.cpp index ea84af5976..203ef664e6 100644 --- a/LibGUI/GButton.cpp +++ b/LibGUI/GButton.cpp @@ -66,8 +66,9 @@ void GButton::click() on_click(*this); } -bool GButton::accepts_keyboard_select() const { - return is_enabled(); +bool GButton::accepts_keyboard_select() const +{ + return is_enabled(); } void GButton::set_action(GAction& action) diff --git a/LibGUI/GWindow.cpp b/LibGUI/GWindow.cpp index 0cc561c156..837c29620a 100644 --- a/LibGUI/GWindow.cpp +++ b/LibGUI/GWindow.cpp @@ -288,7 +288,7 @@ void GWindow::event(CEvent& event) 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(); + 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); @@ -309,8 +309,8 @@ void GWindow::event(CEvent& event) if (event.type() == GEvent::WindowBecameActive || event.type() == GEvent::WindowBecameInactive) { if (event.type() == GEvent::WindowBecameInactive && m_keybind_mode) { - m_keybind_mode = false; - update(); + m_keybind_mode = false; + update(); } m_is_active = event.type() == GEvent::WindowBecameActive; @@ -350,11 +350,13 @@ void GWindow::event(CEvent& event) CObject::event(event); } -void GWindow::paint_keybinds() { - if (!m_keybind_mode) return; +void GWindow::paint_keybinds() +{ + if (!m_keybind_mode) + return; GPainter painter(*m_main_widget); - for (auto& keypair: m_hashed_potential_keybind_widgets) { + for (auto& keypair : m_hashed_potential_keybind_widgets) { auto widget = keypair.value; bool could_be_keybind = true; for (size_t i = 0; i < m_entered_keybind.length(); i++) { @@ -364,8 +366,8 @@ void GWindow::paint_keybinds() { } if (could_be_keybind) { - auto rect = Rect(widget->x()-5, widget->y()-5, 4+Font::default_font().width(keypair.key), 16); - auto highlight_rect = Rect(widget->x()-3, widget->y()-5, 0, 16); + auto rect = Rect(widget->x() - 5, widget->y() - 5, 4 + Font::default_font().width(keypair.key), 16); + auto highlight_rect = Rect(widget->x() - 3, widget->y() - 5, 0, 16); painter.fill_rect(rect, Color::LightGray); painter.draw_rect(rect, Color::Black, false); @@ -375,23 +377,24 @@ void GWindow::paint_keybinds() { } } -void GWindow::find_keyboard_selectable() { +void GWindow::find_keyboard_selectable() +{ Vector potential_keybind_widgets; m_hashed_potential_keybind_widgets.clear(); find_keyboard_selectable_children(m_main_widget, potential_keybind_widgets); - m_max_keybind_length = ceil_div(potential_keybind_widgets.size(), ('z'-'a')); + m_max_keybind_length = ceil_div(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++) { + for (size_t i = 0; i < buffer_length - 1; i++) { keybind_buffer[i] = 'a'; } - keybind_buffer[buffer_length-1] = '\0'; + keybind_buffer[buffer_length - 1] = '\0'; - for (auto& widget: potential_keybind_widgets) { + for (auto& widget : potential_keybind_widgets) { m_hashed_potential_keybind_widgets.set(String(keybind_buffer), widget); - for (size_t i = 0; i < buffer_length-1; i++) { + for (size_t i = 0; i < buffer_length - 1; i++) { if (keybind_buffer[i] >= 'z') { keybind_buffer[i] = 'a'; } else { @@ -402,14 +405,15 @@ void GWindow::find_keyboard_selectable() { } } -void GWindow::find_keyboard_selectable_children(GWidget* widget, Vector &potential_keybind_widgets) { - widget -> for_each_child_widget([&] (auto& child) { - if (child.accepts_keyboard_select()) { - potential_keybind_widgets.append(&child); - find_keyboard_selectable_children(&child, potential_keybind_widgets); - } - return IterationDecision::Continue; - }); +void GWindow::find_keyboard_selectable_children(GWidget* widget, Vector& potential_keybind_widgets) +{ + widget->for_each_child_widget([&](auto& child) { + if (child.accepts_keyboard_select()) { + potential_keybind_widgets.append(&child); + find_keyboard_selectable_children(&child, potential_keybind_widgets); + } + return IterationDecision::Continue; + }); } bool GWindow::is_visible() const diff --git a/LibGUI/GWindow.h b/LibGUI/GWindow.h index aa09e6646a..988b81ffb1 100644 --- a/LibGUI/GWindow.h +++ b/LibGUI/GWindow.h @@ -134,7 +134,7 @@ private: void paint_keybinds(); void find_keyboard_selectable(); - void find_keyboard_selectable_children(GWidget* widget, Vector &potential_keybind_widgets); + void find_keyboard_selectable_children(GWidget* widget, Vector& potential_keybind_widgets); Retained create_backing_bitmap(const Size&); void set_current_backing_bitmap(GraphicsBitmap&, bool flush_immediately = false); void flip(const Vector& dirty_rects); From 54005e69425f0390d700a8e4cad6da736e83a5b3 Mon Sep 17 00:00:00 2001 From: faissaloo Date: Sun, 16 Jun 2019 21:07:55 +0100 Subject: [PATCH 9/9] GWindow: Get rid of superflous variable --- LibGUI/GWindow.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/LibGUI/GWindow.cpp b/LibGUI/GWindow.cpp index 837c29620a..cf13d8a24c 100644 --- a/LibGUI/GWindow.cpp +++ b/LibGUI/GWindow.cpp @@ -288,10 +288,9 @@ void GWindow::event(CEvent& event) 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); + 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); + event = make(GEvent::MouseUp, Point(), 0, GMouseButton::Left, 0, 0); found_widget->value->event(*event); } else if (m_entered_keybind.length() >= m_max_keybind_length) { m_keybind_mode = false;