LibGUI: Add GAction class and make GMenu deal in actions rather than strings.

This commit is contained in:
Andreas Kling 2019-02-12 14:09:48 +01:00
parent a5a7ea3d1e
commit 3085e400bc
Notes: sideshowbarker 2024-07-19 15:46:14 +09:00
9 changed files with 103 additions and 43 deletions

View file

@ -14,6 +14,7 @@
#include <LibGUI/GWidget.h>
#include <LibGUI/GWindow.h>
#include <LibGUI/GMenuBar.h>
#include <LibGUI/GAction.h>
static void make_shell(int ptm_fd)
{
@ -95,38 +96,28 @@ int main(int argc, char** argv)
auto menubar = make<GMenuBar>();
auto app_menu = make<GMenu>("Terminal");
app_menu->add_item(0, "Quit");
app_menu->on_item_activation = [] (unsigned identifier) {
if (identifier == 0) {
dbgprintf("Terminal: Quit menu activated!\n");
GApplication::the().exit(0);
return;
}
};
app_menu->add_action(make<GAction>("Quit", String(), [] (const GAction&) {
dbgprintf("Terminal: Quit menu activated!\n");
GApplication::the().exit(0);
return;
}));
menubar->add_menu(move(app_menu));
auto font_menu = make<GMenu>("Font");
font_menu->add_item(0, "Liza Thin");
font_menu->add_item(1, "Liza Regular");
font_menu->add_item(2, "Liza Bold");
font_menu->on_item_activation = [&terminal] (unsigned identifier) {
switch (identifier) {
case 0:
terminal.set_font(Font::load_from_file("/res/fonts/LizaThin8x10.font"));
break;
case 1:
terminal.set_font(Font::load_from_file("/res/fonts/LizaRegular8x10.font"));
break;
case 2:
terminal.set_font(Font::load_from_file("/res/fonts/LizaBold8x10.font"));
break;
}
auto handle_font_selection = [&terminal] (const GAction& action) {
terminal.set_font(Font::load_from_file(action.custom_data()));
terminal.force_repaint();
};
font_menu->add_action(make<GAction>("Liza Thin", "/res/fonts/LizaThin8x10.font", move(handle_font_selection)));
font_menu->add_action(make<GAction>("Liza Regular", "/res/fonts/LizaRegular8x10.font", move(handle_font_selection)));
font_menu->add_action(make<GAction>("Liza Bold", "/res/fonts/LizaBold8x10.font", move(handle_font_selection)));
menubar->add_menu(move(font_menu));
auto help_menu = make<GMenu>("Help");
help_menu->add_item(0, "About");
help_menu->add_action(make<GAction>("About", [] (const GAction&) {
dbgprintf("FIXME: Implement Help/About\n");
}));
menubar->add_menu(move(help_menu));
app.set_menubar(move(menubar));

23
LibGUI/GAction.cpp Normal file
View 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
View 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;
};

View file

@ -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();

View file

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

View file

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

View file

@ -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))
{
}

View file

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

View file

@ -25,6 +25,7 @@ LIBGUI_OBJS = \
GMenu.o \
GMenuItem.o \
GApplication.o \
GAction.o \
GWindow.o
OBJS = $(SHAREDGRAPHICS_OBJS) $(LIBGUI_OBJS)