diff options
author | Andreas Kling <kling@serenityos.org> | 2020-12-26 15:34:49 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-12-26 16:09:02 +0100 |
commit | bdff88d8d5cfaf6effbe51c6ae9dac37c9d73564 (patch) | |
tree | be4fe3704bea880a7f89e9dd972e41339f5269ce /Libraries | |
parent | 6b6594e3d0bdf2407fce89e2c5315fa7a023d928 (diff) | |
download | serenity-bdff88d8d5cfaf6effbe51c6ae9dac37c9d73564.zip |
LibGUI: Make the LinkLabel widget keyboard-friendly
Make it tab-focusable and activate it with the return key. :^)
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibGUI/Label.cpp | 18 | ||||
-rw-r--r-- | Libraries/LibGUI/Label.h | 2 | ||||
-rw-r--r-- | Libraries/LibGUI/LinkLabel.cpp | 16 | ||||
-rw-r--r-- | Libraries/LibGUI/LinkLabel.h | 1 |
4 files changed, 29 insertions, 8 deletions
diff --git a/Libraries/LibGUI/Label.cpp b/Libraries/LibGUI/Label.cpp index 60e831b4b7..5268ad3f61 100644 --- a/Libraries/LibGUI/Label.cpp +++ b/Libraries/LibGUI/Label.cpp @@ -77,6 +77,17 @@ void Label::set_text(String text) did_change_text(); } +Gfx::IntRect Label::text_rect() const +{ + int indent = 0; + if (frame_thickness() > 0) + indent = font().glyph_width('x') / 2; + auto rect = frame_inner_rect(); + rect.move_by(indent, 0); + rect.set_width(rect.width() - indent * 2); + return rect; +} + void Label::paint_event(PaintEvent& event) { Frame::paint_event(event); @@ -94,12 +105,7 @@ void Label::paint_event(PaintEvent& event) } if (text().is_empty()) return; - int indent = 0; - if (frame_thickness() > 0) - indent = font().glyph_width('x') / 2; - auto text_rect = frame_inner_rect(); - text_rect.move_by(indent, 0); - text_rect.set_width(text_rect.width() - indent * 2); + auto text_rect = this->text_rect(); if (is_enabled()) { painter.draw_text(text_rect, text(), m_text_alignment, palette().color(foreground_role()), Gfx::TextElision::Right); diff --git a/Libraries/LibGUI/Label.h b/Libraries/LibGUI/Label.h index c98c1ffec6..df17f28545 100644 --- a/Libraries/LibGUI/Label.h +++ b/Libraries/LibGUI/Label.h @@ -53,6 +53,8 @@ public: bool is_autosize() const { return m_autosize; } void set_autosize(bool); + Gfx::IntRect text_rect() const; + protected: explicit Label(String text = {}); diff --git a/Libraries/LibGUI/LinkLabel.cpp b/Libraries/LibGUI/LinkLabel.cpp index cf40f94082..a948554f81 100644 --- a/Libraries/LibGUI/LinkLabel.cpp +++ b/Libraries/LibGUI/LinkLabel.cpp @@ -37,6 +37,7 @@ LinkLabel::LinkLabel(String text) : Label(move(text)) { set_foreground_role(Gfx::ColorRole::Link); + set_focus_policy(FocusPolicy::TabFocus); } void LinkLabel::mousedown_event(MouseEvent& event) @@ -47,14 +48,25 @@ void LinkLabel::mousedown_event(MouseEvent& event) } } +void LinkLabel::keydown_event(KeyEvent& event) +{ + Label::keydown_event(event); + if (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Space) { + if (on_click) + on_click(); + } +} + void LinkLabel::paint_event(PaintEvent& event) { Label::paint_event(event); GUI::Painter painter(*this); if (m_hovered) - painter.draw_line({ 0, rect().bottom() }, { font().width(text()), rect().bottom() }, - Widget::palette().link()); + painter.draw_line({ 0, rect().bottom() }, { font().width(text()), rect().bottom() }, palette().link()); + + if (is_focused()) + painter.draw_focus_rect(text_rect(), palette().focus_outline()); } void LinkLabel::enter_event(Core::Event& event) diff --git a/Libraries/LibGUI/LinkLabel.h b/Libraries/LibGUI/LinkLabel.h index 41b924a772..fa666141a5 100644 --- a/Libraries/LibGUI/LinkLabel.h +++ b/Libraries/LibGUI/LinkLabel.h @@ -44,6 +44,7 @@ private: virtual void resize_event(ResizeEvent&) override; virtual void enter_event(Core::Event&) override; virtual void leave_event(Core::Event&) override; + virtual void keydown_event(KeyEvent&) override; virtual void did_change_text() override; |