1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 02:07:34 +00:00

Make buttons unpress when the cursor leaves the button rect.

Implement this functionality by adding global cursor tracking.
It's currently only possible for one GWidget per GWindow to track the cursor.
This commit is contained in:
Andreas Kling 2019-01-27 08:48:34 +01:00
parent 15fad649ea
commit 069d21ed7f
15 changed files with 105 additions and 7 deletions

View file

@ -206,6 +206,7 @@ public:
int gui$set_window_title(int window_id, const char* title, size_t size); int gui$set_window_title(int window_id, const char* title, size_t size);
int gui$get_window_rect(int window_id, GUI_Rect*); int gui$get_window_rect(int window_id, GUI_Rect*);
int gui$set_window_rect(int window_id, const GUI_Rect*); int gui$set_window_rect(int window_id, const GUI_Rect*);
int gui$set_global_cursor_tracking_enabled(int window_id, bool enabled);
DisplayInfo get_display_info(); DisplayInfo get_display_info();

View file

@ -244,3 +244,16 @@ int Process::gui$set_window_rect(int window_id, const GUI_Rect* rect)
WSMessageLoop::the().server_process().request_wakeup(); WSMessageLoop::the().server_process().request_wakeup();
return 0; return 0;
} }
int Process::gui$set_global_cursor_tracking_enabled(int window_id, bool enabled)
{
if (window_id < 0)
return -EINVAL;
auto it = m_windows.find(window_id);
if (it == m_windows.end())
return -EBADWINDOW;
auto& window = *(*it).value;
WSWindowLocker locker(window);
window.set_global_cursor_tracking_enabled(enabled);
return 0;
}

View file

@ -217,6 +217,8 @@ static dword handle(RegisterDump& regs, dword function, dword arg1, dword arg2,
return current->sys$read_tsc((dword*)arg1, (dword*)arg2); return current->sys$read_tsc((dword*)arg1, (dword*)arg2);
case Syscall::SC_gui_notify_paint_finished: case Syscall::SC_gui_notify_paint_finished:
return current->gui$notify_paint_finished((int)arg1, (const GUI_Rect*)arg2); return current->gui$notify_paint_finished((int)arg1, (const GUI_Rect*)arg2);
case Syscall::SC_gui_set_global_cursor_tracking_enabled:
return current->gui$set_global_cursor_tracking_enabled((int)arg1, (bool)arg2);
default: default:
kprintf("<%u> int0x80: Unknown function %u requested {%x, %x, %x}\n", current->pid(), function, arg1, arg2, arg3); kprintf("<%u> int0x80: Unknown function %u requested {%x, %x, %x}\n", current->pid(), function, arg1, arg2, arg3);
break; break;

View file

@ -81,6 +81,7 @@
__ENUMERATE_SYSCALL(gui_get_window_rect) \ __ENUMERATE_SYSCALL(gui_get_window_rect) \
__ENUMERATE_SYSCALL(gui_set_window_rect) \ __ENUMERATE_SYSCALL(gui_set_window_rect) \
__ENUMERATE_SYSCALL(gui_notify_paint_finished) \ __ENUMERATE_SYSCALL(gui_notify_paint_finished) \
__ENUMERATE_SYSCALL(gui_set_global_cursor_tracking_enabled) \
#ifdef SERENITY #ifdef SERENITY

View file

@ -56,3 +56,9 @@ int gui_notify_paint_finished(int window_id, const GUI_Rect* rect)
int rc = syscall(SC_gui_notify_paint_finished, window_id, rect); int rc = syscall(SC_gui_notify_paint_finished, window_id, rect);
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
int gui_set_global_cursor_tracking_enabled(int window_id, bool enabled)
{
int rc = syscall(SC_gui_set_global_cursor_tracking_enabled, window_id, enabled);
__RETURN_WITH_ERRNO(rc, rc, -1);
}

View file

@ -14,6 +14,7 @@ int gui_get_window_title(int window_id, char*, size_t);
int gui_set_window_title(int window_id, const char*, size_t); int gui_set_window_title(int window_id, const char*, size_t);
int gui_get_window_rect(int window_id, GUI_Rect*); int gui_get_window_rect(int window_id, GUI_Rect*);
int gui_set_window_rect(int window_id, const GUI_Rect*); int gui_set_window_rect(int window_id, const GUI_Rect*);
int gui_set_global_cursor_tracking_enabled(int window_id, bool);
__END_DECLS __END_DECLS

View file

@ -65,12 +65,24 @@ void GButton::paint_event(GPaintEvent&)
} }
} }
void GButton::mousemove_event(GMouseEvent& event)
{
if (m_tracking_cursor) {
bool being_pressed = rect().contains(event.position());
if (being_pressed != m_being_pressed) {
m_being_pressed = being_pressed;
update();
}
}
GWidget::mousemove_event(event);
}
void GButton::mousedown_event(GMouseEvent& event) void GButton::mousedown_event(GMouseEvent& event)
{ {
dbgprintf("Button::mouseDownEvent: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button()); dbgprintf("Button::mouseDownEvent: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
m_being_pressed = true; m_being_pressed = true;
m_tracking_cursor = true;
set_global_cursor_tracking(true);
update(); update();
GWidget::mousedown_event(event); GWidget::mousedown_event(event);
} }
@ -78,13 +90,15 @@ void GButton::mousedown_event(GMouseEvent& event)
void GButton::mouseup_event(GMouseEvent& event) void GButton::mouseup_event(GMouseEvent& event)
{ {
dbgprintf("Button::mouseUpEvent: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button()); dbgprintf("Button::mouseUpEvent: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
bool was_being_pressed = m_being_pressed;
m_being_pressed = false; m_being_pressed = false;
m_tracking_cursor = false;
set_global_cursor_tracking(false);
update(); update();
GWidget::mouseup_event(event); GWidget::mouseup_event(event);
if (was_being_pressed) {
if (on_click) if (on_click)
on_click(*this); on_click(*this);
}
} }

View file

@ -18,10 +18,12 @@ private:
virtual void paint_event(GPaintEvent&) override; virtual void paint_event(GPaintEvent&) override;
virtual void mousedown_event(GMouseEvent&) override; virtual void mousedown_event(GMouseEvent&) override;
virtual void mouseup_event(GMouseEvent&) override; virtual void mouseup_event(GMouseEvent&) override;
virtual void mousemove_event(GMouseEvent&) override;
virtual const char* class_name() const override { return "GButton"; } virtual const char* class_name() const override { return "GButton"; }
String m_caption; String m_caption;
bool m_being_pressed { false }; bool m_being_pressed { false };
bool m_tracking_cursor { false };
}; };

View file

@ -170,3 +170,19 @@ void GWidget::set_font(RetainPtr<Font>&& font)
else else
m_font = move(font); m_font = move(font);
} }
void GWidget::set_global_cursor_tracking(bool enabled)
{
auto* win = window();
if (!win)
return;
win->set_global_cursor_tracking_widget(enabled ? this : nullptr);
}
bool GWidget::global_cursor_tracking() const
{
auto* win = window();
if (!win)
return false;
return win->global_cursor_tracking_widget() == this;
}

View file

@ -88,6 +88,9 @@ public:
const Font& font() const { return *m_font; } const Font& font() const { return *m_font; }
void set_font(RetainPtr<Font>&&); void set_font(RetainPtr<Font>&&);
void set_global_cursor_tracking(bool);
bool global_cursor_tracking() const;
private: private:
GWindow* m_window { nullptr }; GWindow* m_window { nullptr };

View file

@ -72,6 +72,13 @@ void GWindow::set_rect(const Rect& a_rect)
void GWindow::event(GEvent& event) void GWindow::event(GEvent& event)
{ {
if (event.is_mouse_event()) { if (event.is_mouse_event()) {
if (m_global_cursor_tracking_widget) {
// FIXME: This won't work for widgets-within-widgets.
auto& mouse_event = static_cast<GMouseEvent&>(event);
Point local_point { mouse_event.x() - m_global_cursor_tracking_widget->relative_rect().x(), mouse_event.y() - m_global_cursor_tracking_widget->relative_rect().y() };
auto local_event = make<GMouseEvent>(event.type(), local_point, mouse_event.buttons(), mouse_event.button());
m_global_cursor_tracking_widget->event(*local_event);
}
if (!m_main_widget) if (!m_main_widget)
return; return;
auto& mouse_event = static_cast<GMouseEvent&>(event); auto& mouse_event = static_cast<GMouseEvent&>(event);
@ -158,3 +165,11 @@ void GWindow::set_focused_widget(GWidget* widget)
m_focused_widget->update(); m_focused_widget->update();
} }
} }
void GWindow::set_global_cursor_tracking_widget(GWidget* widget)
{
if (widget == m_global_cursor_tracking_widget.ptr())
return;
m_global_cursor_tracking_widget = widget ? widget->makeWeakPtr() : nullptr;
gui_set_global_cursor_tracking_enabled(m_window_id, widget != nullptr);
}

View file

@ -4,6 +4,7 @@
#include <SharedGraphics/Rect.h> #include <SharedGraphics/Rect.h>
#include <SharedGraphics/GraphicsBitmap.h> #include <SharedGraphics/GraphicsBitmap.h>
#include <AK/AKString.h> #include <AK/AKString.h>
#include <AK/WeakPtr.h>
class GWidget; class GWidget;
@ -48,11 +49,16 @@ public:
void update(const Rect& = Rect()); void update(const Rect& = Rect());
void set_global_cursor_tracking_widget(GWidget*);
GWidget* global_cursor_tracking_widget() { return m_global_cursor_tracking_widget.ptr(); }
const GWidget* global_cursor_tracking_widget() const { return m_global_cursor_tracking_widget.ptr(); }
private: private:
RetainPtr<GraphicsBitmap> m_backing; RetainPtr<GraphicsBitmap> m_backing;
int m_window_id { -1 }; int m_window_id { -1 };
bool m_is_active { false }; bool m_is_active { false };
GWidget* m_main_widget { nullptr }; GWidget* m_main_widget { nullptr };
GWidget* m_focused_widget { nullptr }; GWidget* m_focused_widget { nullptr };
WeakPtr<GWidget> m_global_cursor_tracking_widget;
}; };

View file

@ -123,3 +123,9 @@ void WSWindow::on_message(WSMessage& message)
m_process.gui_events().append(move(gui_event)); m_process.gui_events().append(move(gui_event));
} }
} }
void WSWindow::set_global_cursor_tracking_enabled(bool enabled)
{
dbgprintf("WSWindow{%p} global_cursor_tracking <- %u\n", enabled);
m_global_cursor_tracking_enabled = enabled;
}

View file

@ -42,6 +42,9 @@ public:
pid_t pid() const { return m_pid; } pid_t pid() const { return m_pid; }
void set_global_cursor_tracking_enabled(bool);
bool global_cursor_tracking() const { return m_global_cursor_tracking_enabled; }
// For InlineLinkedList. // For InlineLinkedList.
// FIXME: Maybe make a ListHashSet and then WSWindowManager can just use that. // FIXME: Maybe make a ListHashSet and then WSWindowManager can just use that.
WSWindow* m_next { nullptr }; WSWindow* m_next { nullptr };
@ -52,6 +55,7 @@ private:
String m_title; String m_title;
Rect m_rect; Rect m_rect;
bool m_is_being_dragged { false }; bool m_is_being_dragged { false };
bool m_global_cursor_tracking_enabled { false };
RetainPtr<GraphicsBitmap> m_backing; RetainPtr<GraphicsBitmap> m_backing;
Process& m_process; Process& m_process;

View file

@ -287,6 +287,14 @@ void WSWindowManager::process_mouse_event(WSMouseEvent& event)
} }
} }
for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
if (!window->global_cursor_tracking())
continue;
Point position { event.x() - window->rect().x(), event.y() - window->rect().y() };
auto local_event = make<WSMouseEvent>(event.type(), position, event.buttons(), event.button());
window->on_message(*local_event);
}
for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) { for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
if (title_bar_rect(window->rect()).contains(event.position())) { if (title_bar_rect(window->rect()).contains(event.position())) {
if (event.type() == WSMessage::MouseDown) { if (event.type() == WSMessage::MouseDown) {