summaryrefslogtreecommitdiff
path: root/Servers
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-04-06 04:08:09 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-04-06 04:08:09 +0200
commitf12573cb6319b1ee30fdabb2264e8b63bec030e1 (patch)
tree342843598b81bc1bdde0950792679d601c7fb6a9 /Servers
parent22c204af6648364eb9c9a13d32e148a48f682555 (diff)
downloadserenity-f12573cb6319b1ee30fdabb2264e8b63bec030e1.zip
Add a slight hover highlight to GButton and WSButton. :^)
Diffstat (limited to 'Servers')
-rw-r--r--Servers/WindowServer/WSButton.cpp26
-rw-r--r--Servers/WindowServer/WSButton.h1
-rw-r--r--Servers/WindowServer/WSWindowFrame.cpp2
-rw-r--r--Servers/WindowServer/WSWindowManager.cpp9
-rw-r--r--Servers/WindowServer/WSWindowManager.h2
5 files changed, 31 insertions, 9 deletions
diff --git a/Servers/WindowServer/WSButton.cpp b/Servers/WindowServer/WSButton.cpp
index 40f393895d..70ef677ca9 100644
--- a/Servers/WindowServer/WSButton.cpp
+++ b/Servers/WindowServer/WSButton.cpp
@@ -20,7 +20,7 @@ void WSButton::paint(Painter& painter)
{
PainterStateSaver saver(painter);
painter.translate(relative_rect().location());
- StylePainter::paint_button(painter, rect(), ButtonStyle::Normal, m_pressed);
+ StylePainter::paint_button(painter, rect(), ButtonStyle::Normal, m_pressed, m_hovered);
auto x_location = rect().center();
x_location.move_by(-(m_bitmap->width() / 2), -(m_bitmap->height() / 2));
if (m_pressed)
@@ -39,13 +39,6 @@ void WSButton::on_mouse_event(const WSMouseEvent& event)
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;
@@ -56,6 +49,23 @@ void WSButton::on_mouse_event(const WSMouseEvent& event)
}
if (old_pressed != m_pressed)
wm.invalidate(screen_rect());
+ return;
+ }
+
+ if (event.type() == WSMessage::MouseMove) {
+ bool old_hovered = m_hovered;
+ m_hovered = rect().contains(event.position());
+ wm.set_hovered_button(m_hovered ? this : nullptr);
+ if (old_hovered != m_hovered)
+ wm.invalidate(screen_rect());
+ dbgprintf("move, hov=%d, rect=%s, evpos=%s\n", m_hovered, rect().to_string().characters(), event.position().to_string().characters());
+ }
+
+ if (event.type() == WSMessage::MouseMove && event.buttons() & (unsigned)MouseButton::Left) {
+ bool old_pressed = m_pressed;
+ m_pressed = m_hovered;
+ if (old_pressed != m_pressed)
+ wm.invalidate(screen_rect());
}
}
diff --git a/Servers/WindowServer/WSButton.h b/Servers/WindowServer/WSButton.h
index 569b9f21f5..47937b420a 100644
--- a/Servers/WindowServer/WSButton.h
+++ b/Servers/WindowServer/WSButton.h
@@ -34,4 +34,5 @@ private:
Retained<CharacterBitmap> m_bitmap;
bool m_pressed { false };
bool m_visible { true };
+ bool m_hovered { false };
};
diff --git a/Servers/WindowServer/WSWindowFrame.cpp b/Servers/WindowServer/WSWindowFrame.cpp
index 6e9b8726f2..ab6b70d6c1 100644
--- a/Servers/WindowServer/WSWindowFrame.cpp
+++ b/Servers/WindowServer/WSWindowFrame.cpp
@@ -239,7 +239,7 @@ void WSWindowFrame::on_mouse_event(const WSMouseEvent& event)
for (auto& button : m_buttons) {
if (button->relative_rect().contains(event.position()))
- return button->on_mouse_event(event);
+ return button->on_mouse_event(event.translated(-button->relative_rect().location()));
}
if (event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left)
wm.start_window_drag(m_window, event.translated(rect().location()));
diff --git a/Servers/WindowServer/WSWindowManager.cpp b/Servers/WindowServer/WSWindowManager.cpp
index 8e7b9c096e..567d1968bf 100644
--- a/Servers/WindowServer/WSWindowManager.cpp
+++ b/Servers/WindowServer/WSWindowManager.cpp
@@ -623,6 +623,10 @@ void WSWindowManager::process_mouse_event(const WSMouseEvent& event, WSWindow*&
if (m_cursor_tracking_button)
return m_cursor_tracking_button->on_mouse_event(event.translated(-m_cursor_tracking_button->screen_rect().location()));
+ // This is quite hackish, but it's how the WSButton hover effect is implemented.
+ if (m_hovered_button && event.type() == WSMessage::MouseMove)
+ m_hovered_button->on_mouse_event(event.translated(-m_hovered_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()) {
@@ -1099,3 +1103,8 @@ const WSCursor& WSWindowManager::active_cursor() const
return *m_arrow_cursor;
}
+
+void WSWindowManager::set_hovered_button(WSButton* button)
+{
+ m_hovered_button = button;
+}
diff --git a/Servers/WindowServer/WSWindowManager.h b/Servers/WindowServer/WSWindowManager.h
index 185a04af9b..b67466405a 100644
--- a/Servers/WindowServer/WSWindowManager.h
+++ b/Servers/WindowServer/WSWindowManager.h
@@ -101,6 +101,7 @@ public:
void set_active_window(WSWindow*);
void set_cursor_tracking_button(WSButton* button) { m_cursor_tracking_button = button; }
+ void set_hovered_button(WSButton*);
private:
void process_mouse_event(const WSMouseEvent&, WSWindow*& event_window);
@@ -206,6 +207,7 @@ private:
String m_username;
WSButton* m_cursor_tracking_button { nullptr };
+ WSButton* m_hovered_button { nullptr };
};
template<typename Callback>