From 0fc3ccaa5281c4e895cb0d0d96bfe25360bcea32 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 5 Apr 2019 21:53:45 +0200 Subject: 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. --- Servers/WindowServer/WSButton.cpp | 40 ++++++++++++++++++++++++++++---- Servers/WindowServer/WSButton.h | 7 ++++-- Servers/WindowServer/WSWindowFrame.cpp | 7 +++++- Servers/WindowServer/WSWindowFrame.h | 1 + Servers/WindowServer/WSWindowManager.cpp | 6 ++++- Servers/WindowServer/WSWindowManager.h | 3 +++ 6 files changed, 56 insertions(+), 8 deletions(-) diff --git a/Servers/WindowServer/WSButton.cpp b/Servers/WindowServer/WSButton.cpp index 393f28e4e7..40f393895d 100644 --- a/Servers/WindowServer/WSButton.cpp +++ b/Servers/WindowServer/WSButton.cpp @@ -1,11 +1,13 @@ #include #include +#include #include #include #include -WSButton::WSButton(Retained&& bitmap, Function&& on_click_handler) +WSButton::WSButton(WSWindowFrame& frame, Retained&& bitmap, Function&& 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()); +} diff --git a/Servers/WindowServer/WSButton.h b/Servers/WindowServer/WSButton.h index 267dc6b012..569b9f21f5 100644 --- a/Servers/WindowServer/WSButton.h +++ b/Servers/WindowServer/WSButton.h @@ -7,16 +7,18 @@ class CharacterBitmap; class Painter; class WSMouseEvent; +class WSWindowFrame; -class WSButton { +class WSButton final { public: - WSButton(Retained&&, Function&& on_click_handler); + WSButton(WSWindowFrame&, Retained&&, Function&& 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 m_bitmap; bool m_pressed { false }; diff --git a/Servers/WindowServer/WSWindowFrame.cpp b/Servers/WindowServer/WSWindowFrame.cpp index b0109f42f6..ec08641d8d 100644 --- a/Servers/WindowServer/WSWindowFrame.cpp +++ b/Servers/WindowServer/WSWindowFrame.cpp @@ -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(*s_close_button_bitmap, [this] { + m_buttons.append(make(*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; diff --git a/Servers/WindowServer/WSWindowFrame.h b/Servers/WindowServer/WSWindowFrame.h index 360344c154..7ed541e4cc 100644 --- a/Servers/WindowServer/WSWindowFrame.h +++ b/Servers/WindowServer/WSWindowFrame.h @@ -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; diff --git a/Servers/WindowServer/WSWindowManager.cpp b/Servers/WindowServer/WSWindowManager.cpp index c81ba19619..da4f2026ca 100644 --- a/Servers/WindowServer/WSWindowManager.cpp +++ b/Servers/WindowServer/WSWindowManager.cpp @@ -16,7 +16,8 @@ #include #include #include -#include "WSCursor.h" +#include +#include //#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 windows_who_received_mouse_event_due_to_cursor_tracking; for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) { diff --git a/Servers/WindowServer/WSWindowManager.h b/Servers/WindowServer/WSWindowManager.h index a082aea302..1b0d152b79 100644 --- a/Servers/WindowServer/WSWindowManager.h +++ b/Servers/WindowServer/WSWindowManager.h @@ -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 m_cpu_history; String m_username; + WSButton* m_cursor_tracking_button { nullptr }; }; template -- cgit v1.2.3