Skip to content

Commit 3ebdb5e

Browse files
asliturkawesomekling
authored andcommitted
Application: KeyboardMapper, make button to like a real key
Change key paint to more like a real key :)
1 parent 8733da6 commit 3ebdb5e

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

Applications/KeyboardMapper/KeyButton.cpp

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "KeyButton.h"
2828
#include <LibGUI/Button.h>
2929
#include <LibGUI/Painter.h>
30+
#include <LibGUI/Window.h>
3031
#include <LibGfx/Font.h>
3132
#include <LibGfx/Palette.h>
3233

@@ -39,25 +40,30 @@ void KeyButton::paint_event(GUI::PaintEvent& event)
3940
GUI::Painter painter(*this);
4041
painter.add_clip_rect(event.rect());
4142

42-
auto content_rect = rect().shrunken(5, 5);
43+
auto cont_rect = rect();
4344
auto& font = this->font();
4445

45-
Gfx::StylePainter::paint_button(painter, rect(), palette(), Gfx::ButtonStyle::Normal, is_being_pressed(), is_hovered(), is_checked(), true);
46+
Color color;
47+
if (m_pressed) {
48+
color = Color::Cyan;
49+
} else if (!is_enabled()) {
50+
color = Color::LightGray;
51+
} else {
52+
color = Color::White;
53+
}
4654

47-
if (m_pressed)
48-
painter.fill_rect(content_rect, Color::Cyan);
49-
else if (!is_enabled())
50-
painter.fill_rect(content_rect, Color::from_rgb(0x8C7272));
55+
painter.fill_rect(cont_rect, Color::Black);
56+
painter.fill_rect({ cont_rect.x() + 1, cont_rect.y() + 1, cont_rect.width() - 2, cont_rect.height() - 2 }, Color::from_rgb(0x999999));
57+
painter.fill_rect({ cont_rect.x() + 6, cont_rect.y() + 3, cont_rect.width() - 12, cont_rect.height() - 12 }, Color::from_rgb(0x8C7272));
58+
painter.fill_rect({ cont_rect.x() + 7, cont_rect.y() + 4, cont_rect.width() - 14, cont_rect.height() - 14 }, color);
5159

5260
if (!text().is_empty()) {
5361
Gfx::IntRect text_rect { 0, 0, font.width(text()), font.glyph_height() };
54-
text_rect.align_within(content_rect, Gfx::TextAlignment::Center);
55-
56-
auto clipped_rect = rect().intersected(this->rect());
62+
text_rect.align_within({ cont_rect.x() + 7, cont_rect.y() + 4, cont_rect.width() - 14, cont_rect.height() - 14 }, Gfx::TextAlignment::Center);
5763

58-
painter.draw_text(clipped_rect, text(), font, Gfx::TextAlignment::Center, palette().button_text(), Gfx::TextElision::Right);
64+
painter.draw_text(text_rect, text(), font, Gfx::TextAlignment::Center, palette().button_text(), Gfx::TextElision::Right);
5965
if (is_focused())
60-
painter.draw_rect(clipped_rect.inflated(6, 4), palette().focus_outline());
66+
painter.draw_rect(text_rect.inflated(6, 4), palette().focus_outline());
6167
}
6268
}
6369

@@ -66,3 +72,26 @@ void KeyButton::click(unsigned)
6672
if (on_click)
6773
on_click();
6874
}
75+
76+
void KeyButton::mousemove_event(GUI::MouseEvent& event)
77+
{
78+
if (!is_enabled())
79+
return;
80+
81+
Gfx::IntRect c = { rect().x() + 7, rect().y() + 4, rect().width() - 14, rect().height() - 14 };
82+
83+
if (c.contains(event.position())) {
84+
window()->set_override_cursor(GUI::StandardCursor::Hand);
85+
return;
86+
}
87+
window()->set_override_cursor(GUI::StandardCursor::Arrow);
88+
89+
AbstractButton::mousemove_event(event);
90+
}
91+
92+
void KeyButton::leave_event(Core::Event& event)
93+
{
94+
window()->set_override_cursor(GUI::StandardCursor::Arrow);
95+
AbstractButton::leave_event(event);
96+
}
97+

Applications/KeyboardMapper/KeyButton.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class KeyButton : public GUI::AbstractButton {
4040

4141
protected:
4242
virtual void click(unsigned modifiers = 0) override;
43+
virtual void leave_event(Core::Event&) override;
44+
virtual void mousemove_event(GUI::MouseEvent&) override;
4345
virtual void paint_event(GUI::PaintEvent&) override;
4446

4547
private:

0 commit comments

Comments
 (0)