Skip to content

Commit

Permalink
Updated to quit app if no visible window left.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlynxZhou committed Jan 12, 2025
1 parent a0ff3d1 commit 7288516
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 21 deletions.
23 changes: 17 additions & 6 deletions showmethekey-gtk/smtk-app-win.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "glib-object.h"
#include <gtk/gtk.h>
#include <adwaita.h>
#include <glib/gi18n.h>
Expand Down Expand Up @@ -34,6 +35,10 @@ struct _SmtkAppWin {
};
G_DEFINE_TYPE(SmtkAppWin, smtk_app_win, ADW_TYPE_APPLICATION_WINDOW)

enum { SIG_KEYS_WIN_DESTROY, N_SIGNALS };

static unsigned int obj_signals[N_SIGNALS] = { 0 };

static SmtkKeyMode smtk_app_win_get_mode(SmtkAppWin *win)
{
int mode_id =
Expand Down Expand Up @@ -103,6 +108,8 @@ static void smtk_app_win_keys_win_on_destroy(SmtkAppWin *win,
gtk_switch_set_active(GTK_SWITCH(win->clickable_switch), true);
gtk_switch_set_active(GTK_SWITCH(win->pause_switch), false);
smtk_app_win_enable(win);

g_signal_emit_by_name(win, "keys-win-destroy");
}
}

Expand Down Expand Up @@ -152,12 +159,12 @@ void smtk_app_win_activate(SmtkAppWin *win)
gtk_switch_set_active(GTK_SWITCH(win->keys_win_switch), false);
return;
}
g_signal_connect_object(win->keys_win, "pause",
G_CALLBACK(smtk_app_win_toggle_pause_switch),
win, G_CONNECT_SWAPPED);
g_signal_connect_object(win->keys_win, "destroy",
G_CALLBACK(smtk_app_win_keys_win_on_destroy),
win, G_CONNECT_SWAPPED);
g_signal_connect_swapped(win->keys_win, "pause",
G_CALLBACK(smtk_app_win_toggle_pause_switch),
win);
g_signal_connect_swapped(win->keys_win, "destroy",
G_CALLBACK(smtk_app_win_keys_win_on_destroy),
win);

gtk_window_present(GTK_WINDOW(win->keys_win));

Expand Down Expand Up @@ -548,6 +555,10 @@ static void smtk_app_win_class_init(SmtkAppWinClass *win_class)
gtk_widget_class_bind_template_callback(
GTK_WIDGET_CLASS(win_class),
smtk_app_win_on_keymap_selector_selected);

obj_signals[SIG_KEYS_WIN_DESTROY] = g_signal_new(
"keys-win-destroy", SMTK_TYPE_APP_WIN, G_SIGNAL_RUN_LAST, 0,
NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}

GtkWidget *smtk_app_win_new(SmtkApp *app)
Expand Down
25 changes: 19 additions & 6 deletions showmethekey-gtk/smtk-app.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "glib-object.h"
#include <gtk/gtk.h>
#include <adwaita.h>
#include <glib/gi18n.h>
Expand Down Expand Up @@ -92,12 +93,7 @@ static void quit_action(GSimpleAction *action, GVariant *parameter,
{
SmtkApp *app = SMTK_APP(user_data);

if (app->win != NULL) {
gtk_window_destroy(GTK_WINDOW(app->win));
app->win = NULL;
}

g_application_quit(G_APPLICATION(app));
smtk_app_quit(app);
}

static void smtk_app_init(SmtkApp *app)
Expand Down Expand Up @@ -146,8 +142,13 @@ static void smtk_app_activate(GApplication *g_app)
if (!app->clickable_opt)
smtk_app_win_toggle_clickable_switch(
SMTK_APP_WIN(app->win));
// Quit if no visible window left.
if (app->app_win_opt)
gtk_window_present(GTK_WINDOW(app->win));
else
g_signal_connect_swapped(app->win, "keys-win-destroy",
G_CALLBACK(smtk_app_quit),
app);
if (app->keys_win_opt)
smtk_app_win_activate(SMTK_APP_WIN(app->win));
}
Expand Down Expand Up @@ -251,3 +252,15 @@ SmtkApp *smtk_app_new(void)
return g_object_new(SMTK_TYPE_APP, "application-id",
"one.alynx.showmethekey", NULL);
}

void smtk_app_quit(SmtkApp *app)
{
g_return_if_fail(app != NULL);

if (app->win != NULL) {
gtk_window_destroy(GTK_WINDOW(app->win));
app->win = NULL;
}

g_application_quit(G_APPLICATION(app));
}
1 change: 1 addition & 0 deletions showmethekey-gtk/smtk-app.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ G_BEGIN_DECLS
G_DECLARE_FINAL_TYPE(SmtkApp, smtk_app, SMTK, APP, AdwApplication)

SmtkApp *smtk_app_new(void);
void smtk_app_quit(SmtkApp *app);

G_END_DECLS

Expand Down
16 changes: 7 additions & 9 deletions showmethekey-gtk/smtk-keys-win.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,16 +364,14 @@ static void smtk_keys_win_constructed(GObject *object)
// `win->error` is set so just return.
if (win->emitter == NULL)
goto out;
g_signal_connect_object(
g_signal_connect_swapped(
win->emitter, "error-cli-exit",
G_CALLBACK(smtk_keys_win_emitter_on_error_cli_exit), win,
G_CONNECT_SWAPPED);
g_signal_connect_object(win->emitter, "key",
G_CALLBACK(smtk_keys_win_emitter_on_key), win,
G_CONNECT_SWAPPED);
g_signal_connect_object(win->emitter, "pause",
G_CALLBACK(smtk_keys_win_emitter_on_pause), win,
G_CONNECT_SWAPPED);
G_CALLBACK(smtk_keys_win_emitter_on_error_cli_exit), win);
g_signal_connect_swapped(win->emitter, "key",
G_CALLBACK(smtk_keys_win_emitter_on_key), win);
g_signal_connect_swapped(win->emitter, "pause",
G_CALLBACK(smtk_keys_win_emitter_on_pause),
win);

smtk_keys_emitter_start_async(win->emitter, &win->error);
if (win->error != NULL)
Expand Down

2 comments on commit 7288516

@Nicknamely
Copy link

@Nicknamely Nicknamely commented on 7288516 Jan 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't seem to work.

recording_2025-01-12_10-40-52.mp4

@AlynxZhou
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Nicknamely That works on my system, are you using the latest version (1.18.1)? Maybe you forget to rebuild it.

Please sign in to comment.