Browse Source

implement quit menu item

CPP-GTK4
yggverse 4 months ago
parent
commit
a9ae983549
  1. 3
      Makefile
  2. 88
      src/app/browser/header/bar/menu/main/quit.cpp
  3. 50
      src/app/browser/header/bar/menu/main/quit.h

3
Makefile

@ -14,7 +14,8 @@ SRCS = src/main.cpp\ @@ -14,7 +14,8 @@ SRCS = src/main.cpp\
src/app/browser/header/bar.cpp\
src/app/browser/header/bar/title.cpp\
src/app/browser/header/bar/menu.cpp\
src/app/browser/header/bar/menu/main.cpp
src/app/browser/header/bar/menu/main.cpp\
src/app/browser/header/bar/menu/main/quit.cpp
OBJS = $(SRCS:.cpp=.o)

88
src/app/browser/header/bar/menu/main/quit.cpp

@ -0,0 +1,88 @@ @@ -0,0 +1,88 @@
#include "quit.h"
namespace app::browser::header::bar::menu::main
{
// Construct
Quit::Quit(
Main *main
) {
// Init dependencies
this->main = main;
// Init action object
this->action = g_simple_action_new(
Quit::ACTION_ID,
NULL
);
g_action_map_add_action(
G_ACTION_MAP(
this->main->menu->bar->header->browser->application
),
G_ACTION(
this->action
)
);
// Init action NS
gchar action[255];
g_snprintf(
action,
sizeof(
action
),
Quit::ACTION_NS,
Quit::ACTION_ID
);
// Init keyboard accelerators
// https://docs.gtk.org/gtk4/func.accelerator_parse.html
const gchar *accels[] = {
Quit::ACCEL_1, // First accelerator
Quit::ACCEL_2, // Second accelerator
NULL
};
gtk_application_set_accels_for_action(
GTK_APPLICATION(
this->main->menu->bar->header->browser->application
),
action,
accels
);
// Init menu item object
this->item = g_menu_item_new(
Quit::LABEL,
action
);
// Connect events
g_signal_connect(
G_SIMPLE_ACTION(
this->action
),
"activate",
G_CALLBACK(
Quit::_activate
),
G_APPLICATION(
this->main->menu->bar->header->browser->application
)
);
}
// Events
void Quit::_activate(
GSimpleAction* action,
GVariant* parameter,
gpointer user_data
) {
g_application_quit(
G_APPLICATION(
user_data
)
);
}
}

50
src/app/browser/header/bar/menu/main/quit.h

@ -0,0 +1,50 @@ @@ -0,0 +1,50 @@
#ifndef APP_BROWSER_HEADER_BAR_MENU_QUIT_H
#define APP_BROWSER_HEADER_BAR_MENU_QUIT_H
#include "../main.h"
namespace app::browser::header::bar::menu
{
class Main;
namespace main
{
class Quit
{
public:
// GTK
GMenuItem *item;
GSimpleAction *action;
// Dependencies
Main *main;
// Defaults
const gchar *LABEL = "Quit";
const gchar *ACCEL_1 = "<Control>q";
const gchar *ACCEL_2 = "<Control>Q";
const gchar *ACTION_NS = "app.%s";
const gchar *ACTION_ID = "browser.header.bar.menu.main.quit.activate";
// Construct
Quit(
Main *main
);
private:
// Events
static void _activate(
GSimpleAction* action,
GVariant* parameter,
gpointer user_data
);
};
};
};
#endif
Loading…
Cancel
Save