mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 13:28:11 +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
|
@ -14,6 +14,7 @@
|
||||||
#include <LibGUI/GWidget.h>
|
#include <LibGUI/GWidget.h>
|
||||||
#include <LibGUI/GWindow.h>
|
#include <LibGUI/GWindow.h>
|
||||||
#include <LibGUI/GMenuBar.h>
|
#include <LibGUI/GMenuBar.h>
|
||||||
|
#include <LibGUI/GAction.h>
|
||||||
|
|
||||||
static void make_shell(int ptm_fd)
|
static void make_shell(int ptm_fd)
|
||||||
{
|
{
|
||||||
|
@ -95,38 +96,28 @@ int main(int argc, char** argv)
|
||||||
auto menubar = make<GMenuBar>();
|
auto menubar = make<GMenuBar>();
|
||||||
|
|
||||||
auto app_menu = make<GMenu>("Terminal");
|
auto app_menu = make<GMenu>("Terminal");
|
||||||
app_menu->add_item(0, "Quit");
|
app_menu->add_action(make<GAction>("Quit", String(), [] (const GAction&) {
|
||||||
app_menu->on_item_activation = [] (unsigned identifier) {
|
dbgprintf("Terminal: Quit menu activated!\n");
|
||||||
if (identifier == 0) {
|
GApplication::the().exit(0);
|
||||||
dbgprintf("Terminal: Quit menu activated!\n");
|
return;
|
||||||
GApplication::the().exit(0);
|
}));
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
menubar->add_menu(move(app_menu));
|
menubar->add_menu(move(app_menu));
|
||||||
|
|
||||||
auto font_menu = make<GMenu>("Font");
|
auto font_menu = make<GMenu>("Font");
|
||||||
font_menu->add_item(0, "Liza Thin");
|
auto handle_font_selection = [&terminal] (const GAction& action) {
|
||||||
font_menu->add_item(1, "Liza Regular");
|
terminal.set_font(Font::load_from_file(action.custom_data()));
|
||||||
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;
|
|
||||||
}
|
|
||||||
terminal.force_repaint();
|
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));
|
menubar->add_menu(move(font_menu));
|
||||||
|
|
||||||
auto help_menu = make<GMenu>("Help");
|
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));
|
menubar->add_menu(move(help_menu));
|
||||||
|
|
||||||
app.set_menubar(move(menubar));
|
app.set_menubar(move(menubar));
|
||||||
|
|
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 "GEvent.h"
|
||||||
#include "GObject.h"
|
#include "GObject.h"
|
||||||
#include "GWindow.h"
|
#include "GWindow.h"
|
||||||
|
#include <LibGUI/GAction.h>
|
||||||
#include <LibGUI/GNotifier.h>
|
#include <LibGUI/GNotifier.h>
|
||||||
#include <LibGUI/GMenu.h>
|
#include <LibGUI/GMenu.h>
|
||||||
#include <LibC/unistd.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);
|
dbgprintf("GEventLoop received event for invalid window ID %d\n", event.window_id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (menu->on_item_activation)
|
if (auto* action = menu->action_at(event.menu.identifier))
|
||||||
menu->on_item_activation(event.menu.identifier);
|
action->activate();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
#include <LibGUI/GAction.h>
|
||||||
#include <LibGUI/GMenu.h>
|
#include <LibGUI/GMenu.h>
|
||||||
#include <LibC/gui.h>
|
#include <LibC/gui.h>
|
||||||
#include <AK/HashMap.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()
|
void GMenu::add_separator()
|
||||||
{
|
{
|
||||||
m_items.append(GMenuItem(GMenuItem::Separator));
|
m_items.append(make<GMenuItem>(GMenuItem::Separator));
|
||||||
}
|
}
|
||||||
|
|
||||||
int GMenu::realize_menu()
|
int GMenu::realize_menu()
|
||||||
{
|
{
|
||||||
m_menu_id = gui_menu_create(m_name.characters());
|
m_menu_id = gui_menu_create(m_name.characters());
|
||||||
ASSERT(m_menu_id > 0);
|
ASSERT(m_menu_id > 0);
|
||||||
for (auto& item : m_items) {
|
for (size_t i = 0; i < m_items.size(); ++i) {
|
||||||
if (item.type() == GMenuItem::Separator)
|
auto& item = *m_items[i];
|
||||||
|
if (item.type() == GMenuItem::Separator) {
|
||||||
gui_menu_add_separator(m_menu_id);
|
gui_menu_add_separator(m_menu_id);
|
||||||
else if (item.type() == GMenuItem::Text)
|
continue;
|
||||||
gui_menu_add_item(m_menu_id, item.identifier(), item.text().characters());
|
}
|
||||||
|
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);
|
all_menus().set(m_menu_id, this);
|
||||||
return m_menu_id;
|
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/Function.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
|
|
||||||
|
class GAction;
|
||||||
|
|
||||||
class GMenu {
|
class GMenu {
|
||||||
public:
|
public:
|
||||||
explicit GMenu(const String& name);
|
explicit GMenu(const String& name);
|
||||||
|
@ -11,7 +13,9 @@ public:
|
||||||
|
|
||||||
static GMenu* from_menu_id(int);
|
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();
|
void add_separator();
|
||||||
|
|
||||||
Function<void(unsigned)> on_item_activation;
|
Function<void(unsigned)> on_item_activation;
|
||||||
|
@ -23,5 +27,5 @@ private:
|
||||||
|
|
||||||
int m_menu_id { 0 };
|
int m_menu_id { 0 };
|
||||||
String m_name;
|
String m_name;
|
||||||
Vector<GMenuItem> m_items;
|
Vector<OwnPtr<GMenuItem>> m_items;
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
#include <LibGUI/GMenuItem.h>
|
#include <LibGUI/GMenuItem.h>
|
||||||
|
#include <LibGUI/GAction.h>
|
||||||
|
|
||||||
GMenuItem::GMenuItem(Type type)
|
GMenuItem::GMenuItem(Type type)
|
||||||
: m_type(type)
|
: m_type(type)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
GMenuItem::GMenuItem(unsigned identifier, const String& text)
|
GMenuItem::GMenuItem(OwnPtr<GAction>&& action)
|
||||||
: m_type(Text)
|
: m_type(Action)
|
||||||
, m_identifier(identifier)
|
, m_action(move(action))
|
||||||
, m_text(text)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,21 +2,25 @@
|
||||||
|
|
||||||
#include <AK/AKString.h>
|
#include <AK/AKString.h>
|
||||||
|
|
||||||
|
class GAction;
|
||||||
|
|
||||||
class GMenuItem {
|
class GMenuItem {
|
||||||
public:
|
public:
|
||||||
enum Type { Invalid, Text, Separator };
|
enum Type { Invalid, Action, Separator };
|
||||||
|
|
||||||
explicit GMenuItem(Type);
|
explicit GMenuItem(Type);
|
||||||
GMenuItem(unsigned identifier, const String& text);
|
explicit GMenuItem(OwnPtr<GAction>&&);
|
||||||
~GMenuItem();
|
~GMenuItem();
|
||||||
|
|
||||||
Type type() const { return m_type; }
|
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; }
|
unsigned identifier() const { return m_identifier; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Type m_type { Invalid };
|
Type m_type { Invalid };
|
||||||
unsigned m_identifier { 0 };
|
unsigned m_identifier { 0 };
|
||||||
String m_text;
|
OwnPtr<GAction> m_action;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,7 @@ LIBGUI_OBJS = \
|
||||||
GMenu.o \
|
GMenu.o \
|
||||||
GMenuItem.o \
|
GMenuItem.o \
|
||||||
GApplication.o \
|
GApplication.o \
|
||||||
|
GAction.o \
|
||||||
GWindow.o
|
GWindow.o
|
||||||
|
|
||||||
OBJS = $(SHAREDGRAPHICS_OBJS) $(LIBGUI_OBJS)
|
OBJS = $(SHAREDGRAPHICS_OBJS) $(LIBGUI_OBJS)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue