mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 08:58:11 +00:00
WindowServer: Make WSButton behave more like a normal button.
Previously it would just close the window on MouseDown. Now we do the normal thing where we require a MouseUp inside the button rect before committing.
This commit is contained in:
parent
0d60c56b51
commit
0fc3ccaa52
6 changed files with 56 additions and 8 deletions
|
@ -1,11 +1,13 @@
|
|||
#include <WindowServer/WSButton.h>
|
||||
#include <WindowServer/WSMessage.h>
|
||||
#include <WindowServer/WSWindowManager.h>
|
||||
#include <SharedGraphics/Painter.h>
|
||||
#include <SharedGraphics/StylePainter.h>
|
||||
#include <SharedGraphics/CharacterBitmap.h>
|
||||
|
||||
WSButton::WSButton(Retained<CharacterBitmap>&& bitmap, Function<void()>&& on_click_handler)
|
||||
WSButton::WSButton(WSWindowFrame& frame, Retained<CharacterBitmap>&& bitmap, Function<void()>&& on_click_handler)
|
||||
: on_click(move(on_click_handler))
|
||||
, m_frame(frame)
|
||||
, m_bitmap(move(bitmap))
|
||||
{
|
||||
}
|
||||
|
@ -21,13 +23,43 @@ void WSButton::paint(Painter& painter)
|
|||
StylePainter::paint_button(painter, rect(), ButtonStyle::Normal, m_pressed);
|
||||
auto x_location = rect().center();
|
||||
x_location.move_by(-(m_bitmap->width() / 2), -(m_bitmap->height() / 2));
|
||||
if (m_pressed)
|
||||
x_location.move_by(1, 1);
|
||||
painter.draw_bitmap(x_location, *m_bitmap, Color::Black);
|
||||
}
|
||||
|
||||
void WSButton::on_mouse_event(const WSMouseEvent& event)
|
||||
{
|
||||
if (event.type() == WSMessage::MouseDown) {
|
||||
if (on_click)
|
||||
on_click();
|
||||
auto& wm = WSWindowManager::the();
|
||||
|
||||
if (event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left) {
|
||||
m_pressed = true;
|
||||
wm.set_cursor_tracking_button(this);
|
||||
wm.invalidate(screen_rect());
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.type() == WSMessage::MouseMove) {
|
||||
bool old_pressed = m_pressed;
|
||||
m_pressed = rect().contains(event.position());
|
||||
if (old_pressed != m_pressed)
|
||||
wm.invalidate(screen_rect());
|
||||
}
|
||||
|
||||
if (event.type() == WSMessage::MouseUp && event.button() == MouseButton::Left) {
|
||||
WSWindowManager::the().set_cursor_tracking_button(nullptr);
|
||||
bool old_pressed = m_pressed;
|
||||
m_pressed = false;
|
||||
if (rect().contains(event.position())) {
|
||||
if (on_click)
|
||||
on_click();
|
||||
}
|
||||
if (old_pressed != m_pressed)
|
||||
wm.invalidate(screen_rect());
|
||||
}
|
||||
}
|
||||
|
||||
Rect WSButton::screen_rect() const
|
||||
{
|
||||
return m_relative_rect.translated(m_frame.rect().location());
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue