LibGUI+WindowServer: Add support for per-GWidget context menus.

You can now simply assign a GMenu as a GWidget's context menu and it will
automagically pop up on right click. :^)
This commit is contained in:
Andreas Kling 2019-04-12 17:10:30 +02:00
parent 8feecf6c77
commit c06a3bdeb4
Notes: sideshowbarker 2024-07-19 14:44:12 +09:00
12 changed files with 102 additions and 8 deletions

View file

@ -7,7 +7,7 @@
#include <SharedGraphics/GraphicsBitmap.h>
#include <LibGUI/GPainter.h>
#include <LibGUI/GApplication.h>
#include <LibGUI/GMenu.h>
#include <unistd.h>
GWidget::GWidget(GWidget* parent)
@ -161,12 +161,14 @@ void GWidget::handle_mouseup_event(GMouseEvent& event)
return;
// It's a click.. but is it a doubleclick?
// FIXME: This needs improvement.
int elapsed_since_last_click = m_click_clock.elapsed();
dbgprintf("Click clock elapsed: %d\n", m_click_clock.elapsed());
if (elapsed_since_last_click < 250) {
doubleclick_event(event);
} else {
m_click_clock.start();
if (m_click_clock.is_valid()) {
int elapsed_since_last_click = m_click_clock.elapsed();
dbgprintf("Click clock elapsed: %d\n", m_click_clock.elapsed());
if (elapsed_since_last_click < 250) {
doubleclick_event(event);
} else {
m_click_clock.start();
}
}
}
@ -174,6 +176,13 @@ void GWidget::handle_mousedown_event(GMouseEvent& event)
{
if (accepts_focus())
set_focus(true);
if (event.button() == GMouseButton::Right) {
if (m_context_menu) {
m_context_menu->popup(screen_relative_rect().location().translated(event.position()));
return;
}
}
// FIXME: Maybe the click clock should be per-button.
if (!m_click_clock.is_valid())
m_click_clock.start();
mousedown_event(event);
@ -416,3 +425,10 @@ void GWidget::set_enabled(bool enabled)
m_enabled = enabled;
update();
}
void GWidget::set_context_menu(OwnPtr<GMenu>&& context_menu)
{
// FIXME: Support switching context menus.
ASSERT(!m_context_menu);
m_context_menu = move(context_menu);
}