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:
Andreas Kling 2019-04-05 21:53:45 +02:00
parent 0d60c56b51
commit 0fc3ccaa52
6 changed files with 56 additions and 8 deletions

View file

@ -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());
}

View file

@ -7,16 +7,18 @@
class CharacterBitmap;
class Painter;
class WSMouseEvent;
class WSWindowFrame;
class WSButton {
class WSButton final {
public:
WSButton(Retained<CharacterBitmap>&&, Function<void()>&& on_click_handler);
WSButton(WSWindowFrame&, Retained<CharacterBitmap>&&, Function<void()>&& on_click_handler);
~WSButton();
Rect relative_rect() const { return m_relative_rect; }
void set_relative_rect(const Rect& rect) { m_relative_rect = rect; }
Rect rect() const { return { { }, m_relative_rect.size() }; }
Rect screen_rect() const;
void paint(Painter&);
@ -27,6 +29,7 @@ public:
bool is_visible() const { return m_visible; }
private:
WSWindowFrame& m_frame;
Rect m_relative_rect;
Retained<CharacterBitmap> m_bitmap;
bool m_pressed { false };

View file

@ -32,7 +32,7 @@ WSWindowFrame::WSWindowFrame(WSWindow& window)
if (!s_close_button_bitmap)
s_close_button_bitmap = &CharacterBitmap::create_from_ascii(s_close_button_bitmap_data, s_close_button_bitmap_width, s_close_button_bitmap_height).leak_ref();
m_buttons.append(make<WSButton>(*s_close_button_bitmap, [this] {
m_buttons.append(make<WSButton>(*this, *s_close_button_bitmap, [this] {
m_window.on_message(WSMessage(WSMessage::WindowCloseRequest));
}));
}
@ -181,6 +181,11 @@ Rect WSWindowFrame::rect() const
return frame_rect_for_window_type(m_window.type(), m_window.rect());
}
void WSWindowFrame::invalidate_title_bar()
{
WSWindowManager::the().invalidate(title_bar_rect().translated(rect().location()));
}
void WSWindowFrame::notify_window_rect_changed(const Rect& old_rect, const Rect& new_rect)
{
int window_button_width = 15;

View file

@ -17,6 +17,7 @@ public:
void paint(Painter&);
void on_mouse_event(const WSMouseEvent&);
void notify_window_rect_changed(const Rect& old_rect, const Rect& new_rect);
void invalidate_title_bar();
private:
Rect title_bar_rect() const;

View file

@ -16,7 +16,8 @@
#include <time.h>
#include <SharedGraphics/StylePainter.h>
#include <SharedGraphics/PNGLoader.h>
#include "WSCursor.h"
#include <WindowServer/WSCursor.h>
#include <WindowServer/WSButton.h>
//#define DEBUG_COUNTERS
//#define RESIZE_DEBUG
@ -614,6 +615,9 @@ void WSWindowManager::process_mouse_event(const WSMouseEvent& event, WSWindow*&
if (process_ongoing_window_resize(event, event_window))
return;
if (m_cursor_tracking_button)
return m_cursor_tracking_button->on_mouse_event(event.translated(-m_cursor_tracking_button->screen_rect().location()));
HashTable<WSWindow*> windows_who_received_mouse_event_due_to_cursor_tracking;
for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {

View file

@ -25,6 +25,7 @@ class WSWindow;
class WSClientConnection;
class WSWindowSwitcher;
class GraphicsBitmap;
class WSButton;
enum class ResizeDirection { None, Left, UpLeft, Up, UpRight, Right, DownRight, Down, DownLeft };
@ -98,6 +99,7 @@ public:
const WSCursor& move_cursor() const { return *m_move_cursor; }
void set_active_window(WSWindow*);
void set_cursor_tracking_button(WSButton* button) { m_cursor_tracking_button = button; }
private:
void process_mouse_event(const WSMouseEvent&, WSWindow*& event_window);
@ -202,6 +204,7 @@ private:
CircularQueue<float, 30> m_cpu_history;
String m_username;
WSButton* m_cursor_tracking_button { nullptr };
};
template<typename Callback>