Skip to content

Commit b49f695

Browse files
committed
DRY and future proof by making a custom widget
1 parent 2b55a3e commit b49f695

File tree

4 files changed

+45
-41
lines changed

4 files changed

+45
-41
lines changed

src/Views/PreferencesView.vala

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
*/
77

88
public class Jorts.PreferencesView : Gtk.Box {
9-
public Gtk.Switch scribbly_toggle;
10-
public Gtk.Switch hidebar_toggle;
119
//public Gtk.Button reset_button;
1210
private Granite.Toast toast;
1311
public Gtk.Button close_button;
@@ -41,43 +39,23 @@
4139

4240
debug ("Built UI. Lets do connects and binds");
4341

44-
var scribbly_box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 6);
42+
var scribbly_box = new Jorts.SettingsSwitch (
43+
_("Make unfocused notes unreadable"),
44+
_("If enabled, unfocused sticky notes become unreadable to protect their content from peeking eyes (Ctrl+H)"),
45+
"scribbly-mode-active");
4546

46-
scribbly_toggle = new Gtk.Switch () {
47-
halign = Gtk.Align.END,
48-
hexpand = true,
49-
valign = Gtk.Align.CENTER,
50-
};
51-
52-
var scribbly_label = new Granite.HeaderLabel (_("Make unfocused notes unreadable")) {
53-
mnemonic_widget = scribbly_toggle,
54-
secondary_text = _("If enabled, unfocused sticky notes become unreadable to protect their content from peeking eyes (Ctrl+H)")
55-
};
56-
57-
scribbly_box.append (scribbly_label);
58-
scribbly_box.append (scribbly_toggle);
5947
settingsbox.append (scribbly_box);
6048

6149

6250
/*************************************************/
6351
/* hidebar Toggle */
6452
/*************************************************/
6553

66-
var hidebar_box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 6);
67-
68-
hidebar_toggle = new Gtk.Switch () {
69-
halign = Gtk.Align.END,
70-
hexpand = true,
71-
valign = Gtk.Align.CENTER,
72-
};
73-
74-
var hidebar_label = new Granite.HeaderLabel (_("Hide buttons")) {
75-
mnemonic_widget = hidebar_toggle,
76-
secondary_text = _("If enabled, hides the bottom bar in sticky notes. Keyboard shortcuts will still function (Ctrl+T)")
77-
};
54+
var hidebar_box = new Jorts.SettingsSwitch (
55+
_("Hide buttons"),
56+
_("If enabled, hides the bottom bar in sticky notes. Keyboard shortcuts will still function (Ctrl+T)"),
57+
"hide-bar");
7858

79-
hidebar_box.append (hidebar_label);
80-
hidebar_box.append (hidebar_toggle);
8159
settingsbox.append (hidebar_box);
8260

8361

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* SPDX-License-Identifier: GPL-3.0-or-later
3+
* SPDX-FileCopyrightText: 2017-2024 Lains
4+
* 2025 Stella & Charlie (teamcons.carrd.co)
5+
* 2025 Contributions from the ellie_Commons community (github.com/ellie-commons/)
6+
*/
7+
8+
/**
9+
* Switch and its explanatory text
10+
*/
11+
public class Jorts.SettingsSwitch : Gtk.Box {
12+
13+
public SettingsSwitch (string name, string explanation, string key) {
14+
orientation = Gtk.Orientation.HORIZONTAL;
15+
spacing = 6;
16+
17+
var toggle = new Gtk.Switch () {
18+
halign = Gtk.Align.END,
19+
hexpand = true,
20+
valign = Gtk.Align.CENTER,
21+
};
22+
23+
var label = new Granite.HeaderLabel (name) {
24+
mnemonic_widget = toggle,
25+
secondary_text = explanation
26+
};
27+
28+
append (label);
29+
append (toggle);
30+
31+
Application.gsettings.bind (
32+
key,
33+
toggle, "active",
34+
SettingsBindFlags.DEFAULT);
35+
}
36+
}

src/Windows/PreferenceWindow.vala

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,6 @@ public class Jorts.PreferenceWindow : Gtk.ApplicationWindow {
7979
/* CONNECTS AND BINDS */
8080
/***************************************************/
8181

82-
Application.gsettings.bind (
83-
"scribbly-mode-active",
84-
prefview.scribbly_toggle, "active",
85-
SettingsBindFlags.DEFAULT);
86-
87-
88-
Application.gsettings.bind (
89-
"hide-bar",
90-
prefview.hidebar_toggle, "active",
91-
SettingsBindFlags.DEFAULT);
92-
9382
//prefview.reset_button.clicked.connect (on_reset);
9483
prefview.close_button.clicked.connect (() => {close ();});
9584

src/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ sources = files (
2020
'Widgets' / 'PopoverWidgets' / 'ColorBox.vala',
2121
'Widgets' / 'PopoverWidgets' / 'MonospaceBox.vala',
2222
'Widgets' / 'PopoverWidgets' / 'ZoomBox.vala',
23+
'Widgets' / 'PreferencesWidgets' / 'SettingsSwitch.vala',
2324

2425
'Views' / 'PopoverView.vala',
2526
'Views' / 'PreferencesView.vala',

0 commit comments

Comments
 (0)