From a9ae983549e7d9bc7218bdd47438a54c56aeddd4 Mon Sep 17 00:00:00 2001 From: yggverse Date: Tue, 6 Aug 2024 03:36:25 +0300 Subject: [PATCH] implement quit menu item --- Makefile | 3 +- src/app/browser/header/bar/menu/main/quit.cpp | 88 +++++++++++++++++++ src/app/browser/header/bar/menu/main/quit.h | 50 +++++++++++ 3 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 src/app/browser/header/bar/menu/main/quit.cpp create mode 100644 src/app/browser/header/bar/menu/main/quit.h diff --git a/Makefile b/Makefile index 2e49bf6c..2a919bad 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/src/app/browser/header/bar/menu/main/quit.cpp b/src/app/browser/header/bar/menu/main/quit.cpp new file mode 100644 index 00000000..f340d669 --- /dev/null +++ b/src/app/browser/header/bar/menu/main/quit.cpp @@ -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 + ) + ); + } +} diff --git a/src/app/browser/header/bar/menu/main/quit.h b/src/app/browser/header/bar/menu/main/quit.h new file mode 100644 index 00000000..f1b150aa --- /dev/null +++ b/src/app/browser/header/bar/menu/main/quit.h @@ -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 = "q"; + const gchar *ACCEL_2 = "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 \ No newline at end of file