From 079b681a95fff1edb9aa846e84b8f6c0512d8e6f Mon Sep 17 00:00:00 2001 From: Felix Palmen Date: Tue, 8 Oct 2024 09:46:13 +0200 Subject: [PATCH] Xmoji: Add single-instance to settings dialog Handle changes to the configuration by starting/stopping SingleInstance as required. --- src/bin/xmoji/translations/xmoji-ui-de.def | 33 ++++++++++++ src/bin/xmoji/translations/xmoji-ui.def.in | 24 +++++++++ src/bin/xmoji/xmoji.c | 58 +++++++++++++++++++++- 3 files changed, 113 insertions(+), 2 deletions(-) diff --git a/src/bin/xmoji/translations/xmoji-ui-de.def b/src/bin/xmoji/translations/xmoji-ui-de.def index 6615f71..3ba2b60 100644 --- a/src/bin/xmoji/translations/xmoji-ui-de.def +++ b/src/bin/xmoji/translations/xmoji-ui-de.def @@ -142,6 +142,39 @@ Xmoji settings Xmoji Einstellungen . +$w$instanceDesc +This allows to enforce running only one instance of Xmoji. +In "Single" mode, trying to run a second instance as the same +user on the same machine will bring the already running +instance to the top of the current desktop. +In "Multi" mode, many instance can run at the same time. +. +Mit dieser Einstellung kann erzwungen werden, dass nur eine +Instanz von Xmoji läuft. +Im "Einzeln" Modus wird die bereits laufende Instanz in den +Vordergrund gebracht, wenn der gleiche Benutzer auf der gleichen +Maschine versucht, eine weitere Instanz zu starten. +Im "Beliebig" Modus können viele Instanzen gleichzeitig laufen. +. + +$w$instance +Instance mode: +. +Instanzmodus: +. + +$w$instanceSingle +Single +. +Einzeln +. + +$w$instanceMulti +Multi +. +Beliebig +. + $w$scaleDesc Scale factor for the size of the emoji font. Tiny means the default text character size. diff --git a/src/bin/xmoji/translations/xmoji-ui.def.in b/src/bin/xmoji/translations/xmoji-ui.def.in index 25870aa..7d2ca4b 100644 --- a/src/bin/xmoji/translations/xmoji-ui.def.in +++ b/src/bin/xmoji/translations/xmoji-ui.def.in @@ -73,6 +73,30 @@ $c$settingsDlgTitle Xmoji settings . +$w$instanceDesc +. +This allows to enforce running only one instance of Xmoji. +In "Single" mode, trying to run a second instance as the same +user on the same machine will bring the already running +instance to the top of the current desktop. +In "Multi" mode, many instance can run at the same time. +. + +$w$instance +. +Instance mode: +. + +$w$instanceSingle +. +Single +. + +$w$instanceMulti +. +Multi +. + $w$scaleDesc . Scale factor for the size of the emoji font. diff --git a/src/bin/xmoji/xmoji.c b/src/bin/xmoji/xmoji.c index 7545e6b..6726903 100644 --- a/src/bin/xmoji/xmoji.c +++ b/src/bin/xmoji/xmoji.c @@ -57,6 +57,7 @@ typedef struct Xmoji TabBox *tabs; FlowGrid *searchGrid; FlowGrid *recentGrid; + Dropdown *instanceBox; Dropdown *scaleBox; Dropdown *injectFlagsBox; SpinBox *waitBeforeBox; @@ -205,6 +206,39 @@ static void onhistorychanged(void *receiver, void *sender, void *args) Widget_invalidate(self->recentGrid); } +static void onsingleinstancechanged(void *receiver, void *sender, void *args) +{ + (void)sender; + + Xmoji *self = receiver; + + int singleInstance = Config_singleInstance(self->config); + if (singleInstance) + { + if (!SingleInstance_start(self->instance)) + { + onquit(self, 0, 0); + return; + } + } + else SingleInstance_stop(self->instance); + + ConfigChangedEventArgs *ea = args; + if (ea->external) + { + Dropdown_select(self->instanceBox, !singleInstance); + } +} + +static void oninstanceboxchanged(void *receiver, void *sender, void *args) +{ + (void)sender; + + Xmoji *self = receiver; + unsigned *val = args; + Config_setSingleInstance(self->config, !*val); +} + static void onscalechanged(void *receiver, void *sender, void *args) { (void)sender; @@ -377,6 +411,8 @@ static int startup(void *app) /* Initialize runtime configuration */ self->config = Config_create(self->cfgfile); + PSC_Event_register(Config_singleInstanceChanged(self->config), self, + onsingleinstancechanged, 0); PSC_Event_register(Config_scaleChanged(self->config), self, onscalechanged, 0); PSC_Event_register(Config_injectorFlagsChanged(self->config), self, @@ -391,7 +427,8 @@ static int startup(void *app) self->instance = SingleInstance_create(); PSC_Event_register(SingleInstance_secondary(self->instance), self, onsecondary, 0); - if (!SingleInstance_start(self->instance)) return -1; + if (Config_singleInstance(self->config) + && !SingleInstance_start(self->instance)) return -1; /* Load translations */ Translator *tr = Translator_create("xmoji-ui", @@ -700,6 +737,23 @@ static int startup(void *app) Widget_setFontResName(settingsDlg, 0, 0, 0); table = Table_create(settingsDlg); + row = TableRow_create(table); + Widget_setTooltip(row, TR(tr, XMU_txt_instanceDesc), 0); + label = TextLabel_create("instanceLabel", row); + TextLabel_setText(label, TR(tr, XMU_txt_instance)); + Widget_setAlign(label, AH_RIGHT|AV_MIDDLE); + Widget_show(label); + HBox_addWidget(row, label); + Dropdown *dd = Dropdown_create("instanceBox", row); + Dropdown_addOption(dd, TR(tr, XMU_txt_instanceSingle)); + Dropdown_addOption(dd, TR(tr, XMU_txt_instanceMulti)); + Dropdown_select(dd, !Config_singleInstance(self->config)); + Widget_show(dd); + HBox_addWidget(row, dd); + self->instanceBox = dd; + PSC_Event_register(Dropdown_selected(dd), self, oninstanceboxchanged, 0); + Widget_show(row); + Table_addRow(table, row); row = TableRow_create(table); Widget_setTooltip(row, TR(tr, XMU_txt_scaleDesc), 0); label = TextLabel_create("scaleLabel", row); @@ -707,7 +761,7 @@ static int startup(void *app) Widget_setAlign(label, AH_RIGHT|AV_MIDDLE); Widget_show(label); HBox_addWidget(row, label); - Dropdown *dd = Dropdown_create("scaleBox", row); + dd = Dropdown_create("scaleBox", row); Dropdown_addOption(dd, TR(tr, XMU_txt_scaleTiny)); Dropdown_addOption(dd, TR(tr, XMU_txt_scaleSmall)); Dropdown_addOption(dd, TR(tr, XMU_txt_scaleMedium));