mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-23 09:51:57 -05:00
LibGUI: Refactor the keyboard activation code a bit to use WeakPtr<GWidget>.
This commit is contained in:
parent
41bcabd9aa
commit
da475ce3f5
6 changed files with 43 additions and 39 deletions
|
@ -29,7 +29,7 @@ public:
|
||||||
virtual void click() = 0;
|
virtual void click() = 0;
|
||||||
virtual const char* class_name() const override { return "GAbstractButton"; }
|
virtual const char* class_name() const override { return "GAbstractButton"; }
|
||||||
virtual bool accepts_focus() const override { return true; }
|
virtual bool accepts_focus() const override { return true; }
|
||||||
virtual bool accepts_keyboard_select() const { return true; }
|
virtual bool supports_keyboard_activation() const { return true; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
explicit GAbstractButton(GWidget* parent);
|
explicit GAbstractButton(GWidget* parent);
|
||||||
|
|
|
@ -66,7 +66,7 @@ void GButton::click()
|
||||||
on_click(*this);
|
on_click(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GButton::accepts_keyboard_select() const
|
bool GButton::supports_keyboard_activation() const
|
||||||
{
|
{
|
||||||
return is_enabled();
|
return is_enabled();
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,7 @@ public:
|
||||||
|
|
||||||
virtual const char* class_name() const override { return "GButton"; }
|
virtual const char* class_name() const override { return "GButton"; }
|
||||||
virtual bool accepts_focus() const override { return true; }
|
virtual bool accepts_focus() const override { return true; }
|
||||||
virtual bool accepts_keyboard_select() const;
|
virtual bool supports_keyboard_activation() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void paint_event(GPaintEvent&) override;
|
virtual void paint_event(GPaintEvent&) override;
|
||||||
|
|
|
@ -102,7 +102,7 @@ public:
|
||||||
void update(const Rect&);
|
void update(const Rect&);
|
||||||
|
|
||||||
virtual bool accepts_focus() const { return false; }
|
virtual bool accepts_focus() const { return false; }
|
||||||
virtual bool accepts_keyboard_select() const { return false; }
|
virtual bool supports_keyboard_activation() const { return false; }
|
||||||
|
|
||||||
bool is_focused() const;
|
bool is_focused() const;
|
||||||
void set_focus(bool);
|
void set_focus(bool);
|
||||||
|
|
|
@ -271,7 +271,7 @@ void GWindow::event(CEvent& event)
|
||||||
m_keybind_mode = false;
|
m_keybind_mode = false;
|
||||||
} else {
|
} else {
|
||||||
m_keybind_mode = true;
|
m_keybind_mode = true;
|
||||||
find_keyboard_selectable();
|
collect_keyboard_activation_targets();
|
||||||
m_entered_keybind = "";
|
m_entered_keybind = "";
|
||||||
}
|
}
|
||||||
update();
|
update();
|
||||||
|
@ -285,8 +285,8 @@ void GWindow::event(CEvent& event)
|
||||||
builder.append(keyevent.text());
|
builder.append(keyevent.text());
|
||||||
m_entered_keybind = builder.to_string();
|
m_entered_keybind = builder.to_string();
|
||||||
|
|
||||||
auto found_widget = m_hashed_potential_keybind_widgets.find(m_entered_keybind);
|
auto found_widget = m_keyboard_activation_targets.find(m_entered_keybind);
|
||||||
if (found_widget != m_hashed_potential_keybind_widgets.end()) {
|
if (found_widget != m_keyboard_activation_targets.end()) {
|
||||||
m_keybind_mode = false;
|
m_keybind_mode = false;
|
||||||
auto event = make<GMouseEvent>(GEvent::MouseDown, Point(), 0, GMouseButton::Left, 0, 0);
|
auto event = make<GMouseEvent>(GEvent::MouseDown, Point(), 0, GMouseButton::Left, 0, 0);
|
||||||
found_widget->value->event(*event);
|
found_widget->value->event(*event);
|
||||||
|
@ -355,66 +355,70 @@ void GWindow::paint_keybinds()
|
||||||
return;
|
return;
|
||||||
GPainter painter(*m_main_widget);
|
GPainter painter(*m_main_widget);
|
||||||
|
|
||||||
for (auto& keypair : m_hashed_potential_keybind_widgets) {
|
for (auto& keypair : m_keyboard_activation_targets) {
|
||||||
auto widget = keypair.value;
|
if (!keypair.value)
|
||||||
|
continue;
|
||||||
|
auto& widget = *keypair.value;
|
||||||
bool could_be_keybind = true;
|
bool could_be_keybind = true;
|
||||||
for (size_t i = 0; i < m_entered_keybind.length(); i++) {
|
for (int i = 0; i < m_entered_keybind.length(); ++i) {
|
||||||
if (keypair.key.characters()[i] != m_entered_keybind.characters()[i]) {
|
if (keypair.key.characters()[i] != m_entered_keybind.characters()[i]) {
|
||||||
could_be_keybind = false;
|
could_be_keybind = false;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (could_be_keybind) {
|
if (could_be_keybind) {
|
||||||
auto rect = Rect(widget->x() - 5, widget->y() - 5, 4 + Font::default_font().width(keypair.key), 16);
|
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);
|
Rect highlight_rect { widget.x() - 3, widget.y() - 5, 0, 16 };
|
||||||
|
|
||||||
painter.fill_rect(rect, Color::LightGray);
|
painter.fill_rect(rect, Color::LightGray);
|
||||||
painter.draw_rect(rect, Color::Black, false);
|
painter.draw_rect(rect, Color::Black);
|
||||||
painter.draw_text(rect, keypair.key.characters(), TextAlignment::Center, Color::Black);
|
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.draw_text(highlight_rect, m_entered_keybind.characters(), TextAlignment::CenterLeft, Color::MidGray);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GWindow::find_keyboard_selectable()
|
static void collect_keyboard_activation_targets_impl(GWidget& widget, Vector<GWidget*>& targets)
|
||||||
{
|
{
|
||||||
Vector<GWidget*> potential_keybind_widgets;
|
widget.for_each_child_widget([&](auto& child) {
|
||||||
m_hashed_potential_keybind_widgets.clear();
|
if (child.supports_keyboard_activation()) {
|
||||||
find_keyboard_selectable_children(m_main_widget, potential_keybind_widgets);
|
targets.append(&child);
|
||||||
|
collect_keyboard_activation_targets_impl(child, targets);
|
||||||
|
}
|
||||||
|
return IterationDecision::Continue;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
m_max_keybind_length = ceil_div(potential_keybind_widgets.size(), ('z' - 'a'));
|
void GWindow::collect_keyboard_activation_targets()
|
||||||
|
{
|
||||||
|
m_keyboard_activation_targets.clear();
|
||||||
|
if (!m_main_widget)
|
||||||
|
return;
|
||||||
|
|
||||||
|
Vector<GWidget*> targets;
|
||||||
|
collect_keyboard_activation_targets_impl(*m_main_widget, targets);
|
||||||
|
|
||||||
|
m_max_keybind_length = ceil_div(targets.size(), ('z' - 'a'));
|
||||||
size_t buffer_length = m_max_keybind_length + 1;
|
size_t buffer_length = m_max_keybind_length + 1;
|
||||||
char keybind_buffer[buffer_length];
|
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[i] = 'a';
|
||||||
}
|
|
||||||
keybind_buffer[buffer_length - 1] = '\0';
|
keybind_buffer[buffer_length - 1] = '\0';
|
||||||
|
|
||||||
for (auto& widget : potential_keybind_widgets) {
|
for (auto& widget : targets) {
|
||||||
m_hashed_potential_keybind_widgets.set(String(keybind_buffer), widget);
|
m_keyboard_activation_targets.set(String(keybind_buffer), widget->make_weak_ptr());
|
||||||
|
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') {
|
if (keybind_buffer[i] >= 'z') {
|
||||||
keybind_buffer[i] = 'a';
|
keybind_buffer[i] = 'a';
|
||||||
} else {
|
} else {
|
||||||
keybind_buffer[i]++;
|
++keybind_buffer[i];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GWindow::find_keyboard_selectable_children(GWidget* widget, Vector<GWidget*>& 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
|
bool GWindow::is_visible() const
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -133,8 +133,8 @@ private:
|
||||||
|
|
||||||
void paint_keybinds();
|
void paint_keybinds();
|
||||||
|
|
||||||
void find_keyboard_selectable();
|
void collect_keyboard_activation_targets();
|
||||||
void find_keyboard_selectable_children(GWidget* widget, Vector<GWidget*>& potential_keybind_widgets);
|
|
||||||
Retained<GraphicsBitmap> create_backing_bitmap(const Size&);
|
Retained<GraphicsBitmap> create_backing_bitmap(const Size&);
|
||||||
void set_current_backing_bitmap(GraphicsBitmap&, bool flush_immediately = false);
|
void set_current_backing_bitmap(GraphicsBitmap&, bool flush_immediately = false);
|
||||||
void flip(const Vector<Rect, 32>& dirty_rects);
|
void flip(const Vector<Rect, 32>& dirty_rects);
|
||||||
|
@ -167,5 +167,5 @@ private:
|
||||||
bool m_keybind_mode { false };
|
bool m_keybind_mode { false };
|
||||||
String m_entered_keybind;
|
String m_entered_keybind;
|
||||||
size_t m_max_keybind_length { 0 };
|
size_t m_max_keybind_length { 0 };
|
||||||
HashMap<String, GWidget*> m_hashed_potential_keybind_widgets;
|
HashMap<String, WeakPtr<GWidget>> m_keyboard_activation_targets;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue