mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 04:07:45 +00:00
LibGUI: Add GAction class and make GMenu deal in actions rather than strings.
This commit is contained in:
parent
a5a7ea3d1e
commit
3085e400bc
9 changed files with 103 additions and 43 deletions
23
LibGUI/GAction.cpp
Normal file
23
LibGUI/GAction.cpp
Normal file
|
@ -0,0 +1,23 @@
|
|||
#include <LibGUI/GAction.h>
|
||||
|
||||
GAction::GAction(const String& text, const String& custom_data, Function<void(const GAction&)> on_activation_callback)
|
||||
: m_text(text)
|
||||
, on_activation(move(on_activation_callback))
|
||||
, m_custom_data(custom_data)
|
||||
{
|
||||
}
|
||||
|
||||
GAction::GAction(const String& text, Function<void(const GAction&)> on_activation_callback)
|
||||
: GAction(text, String(), move(on_activation_callback))
|
||||
{
|
||||
}
|
||||
|
||||
GAction::~GAction()
|
||||
{
|
||||
}
|
||||
|
||||
void GAction::activate()
|
||||
{
|
||||
if (on_activation)
|
||||
on_activation(*this);
|
||||
}
|
23
LibGUI/GAction.h
Normal file
23
LibGUI/GAction.h
Normal file
|
@ -0,0 +1,23 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/AKString.h>
|
||||
#include <AK/Function.h>
|
||||
|
||||
class GAction {
|
||||
public:
|
||||
GAction(const String& text, Function<void(const GAction&)> = nullptr);
|
||||
GAction(const String& text, const String& custom_data = String(), Function<void(const GAction&)> = nullptr);
|
||||
~GAction();
|
||||
|
||||
String text() const { return m_text; }
|
||||
String custom_data() const { return m_custom_data; }
|
||||
|
||||
Function<void(GAction&)> on_activation;
|
||||
|
||||
void activate();
|
||||
|
||||
private:
|
||||
String m_text;
|
||||
String m_custom_data;
|
||||
};
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
#include "GEvent.h"
|
||||
#include "GObject.h"
|
||||
#include "GWindow.h"
|
||||
#include <LibGUI/GAction.h>
|
||||
#include <LibGUI/GNotifier.h>
|
||||
#include <LibGUI/GMenu.h>
|
||||
#include <LibC/unistd.h>
|
||||
|
@ -156,8 +157,8 @@ void GEventLoop::handle_menu_event(const GUI_Event& event)
|
|||
dbgprintf("GEventLoop received event for invalid window ID %d\n", event.window_id);
|
||||
return;
|
||||
}
|
||||
if (menu->on_item_activation)
|
||||
menu->on_item_activation(event.menu.identifier);
|
||||
if (auto* action = menu->action_at(event.menu.identifier))
|
||||
action->activate();
|
||||
return;
|
||||
}
|
||||
ASSERT_NOT_REACHED();
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#include <LibGUI/GAction.h>
|
||||
#include <LibGUI/GMenu.h>
|
||||
#include <LibC/gui.h>
|
||||
#include <AK/HashMap.h>
|
||||
|
@ -32,26 +33,38 @@ GMenu::~GMenu()
|
|||
}
|
||||
}
|
||||
|
||||
void GMenu::add_item(unsigned identifier, const String& text)
|
||||
void GMenu::add_action(OwnPtr<GAction>&& action)
|
||||
{
|
||||
m_items.append({ identifier, text });
|
||||
m_items.append(make<GMenuItem>(move(action)));
|
||||
}
|
||||
|
||||
void GMenu::add_separator()
|
||||
{
|
||||
m_items.append(GMenuItem(GMenuItem::Separator));
|
||||
m_items.append(make<GMenuItem>(GMenuItem::Separator));
|
||||
}
|
||||
|
||||
int GMenu::realize_menu()
|
||||
{
|
||||
m_menu_id = gui_menu_create(m_name.characters());
|
||||
ASSERT(m_menu_id > 0);
|
||||
for (auto& item : m_items) {
|
||||
if (item.type() == GMenuItem::Separator)
|
||||
for (size_t i = 0; i < m_items.size(); ++i) {
|
||||
auto& item = *m_items[i];
|
||||
if (item.type() == GMenuItem::Separator) {
|
||||
gui_menu_add_separator(m_menu_id);
|
||||
else if (item.type() == GMenuItem::Text)
|
||||
gui_menu_add_item(m_menu_id, item.identifier(), item.text().characters());
|
||||
continue;
|
||||
}
|
||||
if (item.type() == GMenuItem::Action) {
|
||||
auto& action = *item.action();
|
||||
gui_menu_add_item(m_menu_id, i, action.text().characters());
|
||||
}
|
||||
}
|
||||
all_menus().set(m_menu_id, this);
|
||||
return m_menu_id;
|
||||
}
|
||||
|
||||
GAction* GMenu::action_at(size_t index)
|
||||
{
|
||||
if (index >= m_items.size())
|
||||
return nullptr;
|
||||
return m_items[index]->action();
|
||||
}
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
#include <AK/Function.h>
|
||||
#include <AK/Vector.h>
|
||||
|
||||
class GAction;
|
||||
|
||||
class GMenu {
|
||||
public:
|
||||
explicit GMenu(const String& name);
|
||||
|
@ -11,7 +13,9 @@ public:
|
|||
|
||||
static GMenu* from_menu_id(int);
|
||||
|
||||
void add_item(unsigned identifier, const String& text);
|
||||
GAction* action_at(size_t);
|
||||
|
||||
void add_action(OwnPtr<GAction>&&);
|
||||
void add_separator();
|
||||
|
||||
Function<void(unsigned)> on_item_activation;
|
||||
|
@ -23,5 +27,5 @@ private:
|
|||
|
||||
int m_menu_id { 0 };
|
||||
String m_name;
|
||||
Vector<GMenuItem> m_items;
|
||||
Vector<OwnPtr<GMenuItem>> m_items;
|
||||
};
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
#include <LibGUI/GMenuItem.h>
|
||||
#include <LibGUI/GAction.h>
|
||||
|
||||
GMenuItem::GMenuItem(Type type)
|
||||
: m_type(type)
|
||||
{
|
||||
}
|
||||
|
||||
GMenuItem::GMenuItem(unsigned identifier, const String& text)
|
||||
: m_type(Text)
|
||||
, m_identifier(identifier)
|
||||
, m_text(text)
|
||||
GMenuItem::GMenuItem(OwnPtr<GAction>&& action)
|
||||
: m_type(Action)
|
||||
, m_action(move(action))
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -2,21 +2,25 @@
|
|||
|
||||
#include <AK/AKString.h>
|
||||
|
||||
class GAction;
|
||||
|
||||
class GMenuItem {
|
||||
public:
|
||||
enum Type { Invalid, Text, Separator };
|
||||
enum Type { Invalid, Action, Separator };
|
||||
|
||||
explicit GMenuItem(Type);
|
||||
GMenuItem(unsigned identifier, const String& text);
|
||||
explicit GMenuItem(OwnPtr<GAction>&&);
|
||||
~GMenuItem();
|
||||
|
||||
Type type() const { return m_type; }
|
||||
String text() const { return m_text; }
|
||||
String text() const;
|
||||
const GAction* action() const { return m_action.ptr(); }
|
||||
GAction* action() { return m_action.ptr(); }
|
||||
unsigned identifier() const { return m_identifier; }
|
||||
|
||||
private:
|
||||
Type m_type { Invalid };
|
||||
unsigned m_identifier { 0 };
|
||||
String m_text;
|
||||
OwnPtr<GAction> m_action;
|
||||
};
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@ LIBGUI_OBJS = \
|
|||
GMenu.o \
|
||||
GMenuItem.o \
|
||||
GApplication.o \
|
||||
GAction.o \
|
||||
GWindow.o
|
||||
|
||||
OBJS = $(SHAREDGRAPHICS_OBJS) $(LIBGUI_OBJS)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue