mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 00:37:35 +00:00
LibGUI: Refactor context menus to be event-driven instead of declarative.
The declarative approach had way too many limitations. This patch adds a context menu event that can be hooked to prepare a custom context menu on demand just-in-time. :^)
This commit is contained in:
parent
e74b5bc054
commit
a747a10eab
5 changed files with 37 additions and 27 deletions
|
@ -29,6 +29,7 @@ public:
|
|||
FocusIn,
|
||||
FocusOut,
|
||||
WindowCloseRequest,
|
||||
ContextMenu,
|
||||
WM_WindowRemoved,
|
||||
WM_WindowStateChanged,
|
||||
WM_WindowIconChanged,
|
||||
|
@ -141,6 +142,23 @@ private:
|
|||
Size m_size;
|
||||
};
|
||||
|
||||
class GContextMenuEvent final : public GEvent {
|
||||
public:
|
||||
explicit GContextMenuEvent(const Point& position, const Point& screen_position)
|
||||
: GEvent(GEvent::ContextMenu)
|
||||
, m_position(position)
|
||||
, m_screen_position(screen_position)
|
||||
{
|
||||
}
|
||||
|
||||
const Point& position() const { return m_position; }
|
||||
const Point& screen_position() const { return m_screen_position; }
|
||||
|
||||
private:
|
||||
Point m_position;
|
||||
Point m_screen_position;
|
||||
};
|
||||
|
||||
class GShowEvent final : public GEvent {
|
||||
public:
|
||||
GShowEvent()
|
||||
|
|
|
@ -178,18 +178,14 @@ void GWidget::handle_mousedown_event(GMouseEvent& event)
|
|||
{
|
||||
if (accepts_focus())
|
||||
set_focus(true);
|
||||
if (event.button() == GMouseButton::Right) {
|
||||
if (m_context_menu) {
|
||||
if (m_context_menu_mode == ContextMenuMode::PassthroughMouseEvent)
|
||||
mousedown_event(event);
|
||||
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);
|
||||
if (event.button() == GMouseButton::Right) {
|
||||
GContextMenuEvent c_event(event.position(), screen_relative_rect().location().translated(event.position()));
|
||||
context_menu_event(c_event);
|
||||
}
|
||||
}
|
||||
|
||||
void GWidget::handle_enter_event(CEvent& event)
|
||||
|
@ -253,6 +249,10 @@ void GWidget::mousemove_event(GMouseEvent&)
|
|||
{
|
||||
}
|
||||
|
||||
void GWidget::context_menu_event(GContextMenuEvent&)
|
||||
{
|
||||
}
|
||||
|
||||
void GWidget::focusin_event(CEvent&)
|
||||
{
|
||||
}
|
||||
|
@ -437,14 +437,6 @@ void GWidget::set_enabled(bool enabled)
|
|||
update();
|
||||
}
|
||||
|
||||
void GWidget::set_context_menu(OwnPtr<GMenu>&& context_menu, ContextMenuMode mode)
|
||||
{
|
||||
// FIXME: Support switching context menus.
|
||||
ASSERT(!m_context_menu);
|
||||
m_context_menu = move(context_menu);
|
||||
m_context_menu_mode = mode;
|
||||
}
|
||||
|
||||
void GWidget::move_to_front()
|
||||
{
|
||||
auto* parent = parent_widget();
|
||||
|
|
|
@ -42,11 +42,6 @@ public:
|
|||
bool is_enabled() const { return m_enabled; }
|
||||
void set_enabled(bool);
|
||||
|
||||
enum class ContextMenuMode { SwallowMouseEvent, PassthroughMouseEvent };
|
||||
|
||||
const GMenu* context_menu() const { return m_context_menu.ptr(); }
|
||||
void set_context_menu(OwnPtr<GMenu>&&, ContextMenuMode = ContextMenuMode::SwallowMouseEvent);
|
||||
|
||||
virtual void event(CEvent&) override;
|
||||
virtual void paint_event(GPaintEvent&);
|
||||
virtual void resize_event(GResizeEvent&);
|
||||
|
@ -59,6 +54,7 @@ public:
|
|||
virtual void mouseup_event(GMouseEvent&);
|
||||
virtual void click_event(GMouseEvent&);
|
||||
virtual void doubleclick_event(GMouseEvent&);
|
||||
virtual void context_menu_event(GContextMenuEvent&);
|
||||
virtual void focusin_event(CEvent&);
|
||||
virtual void focusout_event(CEvent&);
|
||||
virtual void enter_event(CEvent&);
|
||||
|
@ -206,6 +202,4 @@ private:
|
|||
bool m_enabled { true };
|
||||
|
||||
CElapsedTimer m_click_clock;
|
||||
OwnPtr<GMenu> m_context_menu;
|
||||
ContextMenuMode m_context_menu_mode { ContextMenuMode::SwallowMouseEvent };
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue