1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 01:07:35 +00:00

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
12 changed files with 102 additions and 8 deletions

View file

@ -39,6 +39,17 @@ void GMenu::add_separator()
m_items.append(make<GMenuItem>(m_menu_id, GMenuItem::Separator));
}
void GMenu::popup(const Point& screen_position)
{
if (!m_menu_id)
realize_menu();
WSAPI_ClientMessage request;
request.type = WSAPI_ClientMessage::Type::PopupMenu;
request.menu.menu_id = m_menu_id;
request.menu.position = screen_position;
GEventLoop::post_message_to_server(request);
}
int GMenu::realize_menu()
{
WSAPI_ClientMessage request;

View file

@ -5,6 +5,7 @@
#include <AK/Vector.h>
class GAction;
class Point;
class GMenu {
public:
@ -18,6 +19,8 @@ public:
void add_action(Retained<GAction>&&);
void add_separator();
void popup(const Point& screen_position);
Function<void(unsigned)> on_item_activation;
private:

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

View file

@ -11,6 +11,7 @@
class GraphicsBitmap;
class GLayout;
class GMenu;
class GWindow;
enum class SizePolicy { Fixed, Fill };
@ -41,6 +42,9 @@ public:
bool is_enabled() const { return m_enabled; }
void set_enabled(bool);
const GMenu* context_menu() const { return m_context_menu.ptr(); }
void set_context_menu(OwnPtr<GMenu>&&);
virtual void event(CEvent&) override;
virtual void paint_event(GPaintEvent&);
virtual void resize_event(GResizeEvent&);
@ -186,4 +190,5 @@ private:
bool m_enabled { true };
CElapsedTimer m_click_clock;
OwnPtr<GMenu> m_context_menu;
};