Skip to content

Commit

Permalink
Xmoji: Add single-instance to settings dialog
Browse files Browse the repository at this point in the history
Handle changes to the configuration by starting/stopping SingleInstance
as required.
  • Loading branch information
Zirias committed Oct 8, 2024
1 parent fcc2dc9 commit 079b681
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 2 deletions.
33 changes: 33 additions & 0 deletions src/bin/xmoji/translations/xmoji-ui-de.def
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 24 additions & 0 deletions src/bin/xmoji/translations/xmoji-ui.def.in
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
58 changes: 56 additions & 2 deletions src/bin/xmoji/xmoji.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ typedef struct Xmoji
TabBox *tabs;
FlowGrid *searchGrid;
FlowGrid *recentGrid;
Dropdown *instanceBox;
Dropdown *scaleBox;
Dropdown *injectFlagsBox;
SpinBox *waitBeforeBox;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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",
Expand Down Expand Up @@ -700,14 +737,31 @@ 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);
TextLabel_setText(label, TR(tr, XMU_txt_scale));
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));
Expand Down

0 comments on commit 079b681

Please sign in to comment.