Skip to content

Commit 466bfda

Browse files
authored
Merge pull request #10 from cassidyjames/modeswitch
Use ModeSwitch
2 parents 61ead74 + fab28a8 commit 466bfda

File tree

6 files changed

+169
-19
lines changed

6 files changed

+169
-19
lines changed

data/icons/writeas-bright-dark.png

-2.91 KB
Binary file not shown.

data/icons/[email protected]

-3.1 KB
Binary file not shown.

data/meson.build

-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ foreach i : icon_sizes
88
)
99
endforeach
1010

11-
install_data('icons/writeas-bright-dark.png',
12-
install_dir: join_paths(get_option('datadir'), 'icons', 'hicolor', '16x16', 'actions'))
13-
1411
install_data('com.github.writeas.writeas-gtk.desktop',
1512
install_dir: join_paths(get_option('datadir'), 'applications'))
1613
install_data('com.github.writeas.writeas-gtk.appdata.xml',

src/Widgets/ModeSwitch.vala

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/*
2+
* Copyright (c) 2018 elementary, Inc. (https://elementary.io)
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2 of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public
15+
* License along with this program; if not, write to the
16+
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17+
* Boston, MA 02110-1301 USA
18+
*/
19+
20+
// From https://github.com/elementary/granite/blob/2066b377226cf327cb2d5399b6b40a2d36d47b11/lib/Widgets/ModeSwitch.vala
21+
22+
/**
23+
* ModeSwitch is a selection control for choosing between two options that can be described with an icon.
24+
*
25+
* ''Example''<<BR>>
26+
* {{{
27+
* var gtk_settings = Gtk.Settings.get_default ();
28+
*
29+
* var mode_switch = new ModeSwitch.from_icon_name ("display-brightness-symbolic", "weather-clear-night-symbolic");
30+
* mode_switch.primary_icon_tooltip_text = _("Light background");
31+
* mode_switch.secondary_icon_tooltip_text = _("Dark background");
32+
* mode_switch.bind_property ("active", gtk_settings, "gtk_application_prefer_dark_theme");
33+
* }}}
34+
*/
35+
public class Granite.ModeSwitch : Gtk.Grid {
36+
/**
37+
* Whether the {@link Gtk.Switch} widget is pointing to the secondary icon or not.
38+
*/
39+
public bool active { get; set; }
40+
41+
/**
42+
* The {@link GLib.Icon} to use for the primary icon for the switch.
43+
*/
44+
public GLib.Icon primary_icon_gicon { get; construct set; }
45+
46+
/**
47+
* The icon name to use for the primary icon for the switch.
48+
*/
49+
public string primary_icon_name { get; construct set; }
50+
51+
/**
52+
* The contents of the tooltip on the primary icon.
53+
*/
54+
public string primary_icon_tooltip_text { get; set; }
55+
56+
/**
57+
* The {@link GLib.Icon} to use for the secondary icon for the switch.
58+
*/
59+
public GLib.Icon secondary_icon_gicon { get; construct set; }
60+
61+
/**
62+
* The icon name to use for the secondary icon for the switch.
63+
*/
64+
public string secondary_icon_name { get; construct set; }
65+
66+
/**
67+
* The contents of the tooltip on the secondary icon.
68+
*/
69+
public string secondary_icon_tooltip_text { get; set; }
70+
71+
/**
72+
* Constructs a new {@link Granite.ModeSwitch} using {@link GLib.Icon}s.
73+
*
74+
* @param primary_icon_gicon The {@link GLib.Icon} to use for the primary icon for the switch.
75+
* @param secondary_icon_gicon The {@link GLib.Icon} to use for the secondary icon for the switch.
76+
*/
77+
public ModeSwitch (GLib.Icon primary_icon_gicon, GLib.Icon secondary_icon_gicon) {
78+
Object (
79+
primary_icon_gicon: primary_icon_gicon,
80+
secondary_icon_gicon: secondary_icon_gicon
81+
);
82+
}
83+
84+
/**
85+
* Constructs a new {@link Granite.ModeSwitch} from icon names.
86+
*
87+
* @param primary_icon_name The icon name to use for the primary icon for the switch.
88+
* @param secondary_icon_name The icon name to use for the secondary icon for the switch.
89+
*/
90+
public ModeSwitch.from_icon_name (string primary_icon_name, string secondary_icon_name) {
91+
Object (
92+
primary_icon_gicon: new ThemedIcon (primary_icon_name),
93+
secondary_icon_gicon: new ThemedIcon (secondary_icon_name),
94+
primary_icon_name: primary_icon_name,
95+
secondary_icon_name: secondary_icon_name
96+
);
97+
}
98+
99+
construct {
100+
var primary_image = new Gtk.Image ();
101+
primary_image.pixel_size = 16;
102+
103+
var primary_icon_box = new Gtk.EventBox ();
104+
primary_icon_box.add_events (Gdk.EventMask.BUTTON_RELEASE_MASK);
105+
primary_icon_box.add (primary_image);
106+
107+
var mode_switch = new Gtk.Switch ();
108+
mode_switch.valign = Gtk.Align.CENTER;
109+
mode_switch.get_style_context ().add_class ("mode-switch");
110+
111+
var secondary_icon = new Gtk.Image ();
112+
secondary_icon.pixel_size = 16;
113+
114+
var secondary_icon_box = new Gtk.EventBox ();
115+
secondary_icon_box.add_events (Gdk.EventMask.BUTTON_RELEASE_MASK);
116+
secondary_icon_box.add (secondary_icon);
117+
118+
column_spacing = 6;
119+
add (primary_icon_box);
120+
add (mode_switch);
121+
add (secondary_icon_box);
122+
123+
bind_property ("primary-icon-gicon", primary_image, "gicon", GLib.BindingFlags.SYNC_CREATE);
124+
bind_property ("primary-icon-name", primary_image, "icon-name", GLib.BindingFlags.SYNC_CREATE);
125+
bind_property ("primary-icon-tooltip-text", primary_image, "tooltip-text");
126+
bind_property ("secondary-icon-gicon", secondary_icon, "gicon", GLib.BindingFlags.SYNC_CREATE);
127+
bind_property ("secondary-icon-name", secondary_icon, "icon_name", GLib.BindingFlags.SYNC_CREATE);
128+
bind_property ("secondary-icon-tooltip-text", secondary_icon, "tooltip-text");
129+
130+
this.notify["active"].connect (() => {
131+
if (Gtk.StateFlags.DIR_RTL in get_state_flags ()) {
132+
mode_switch.active = !active;
133+
} else {
134+
mode_switch.active = active;
135+
}
136+
});
137+
138+
mode_switch.notify["active"].connect (() => {
139+
if (Gtk.StateFlags.DIR_RTL in get_state_flags ()) {
140+
active = !mode_switch.active;
141+
} else {
142+
active = mode_switch.active;
143+
}
144+
});
145+
146+
primary_icon_box.button_release_event.connect (() => {
147+
active = false;
148+
return Gdk.EVENT_STOP;
149+
});
150+
151+
secondary_icon_box.button_release_event.connect (() => {
152+
active = true;
153+
return Gdk.EVENT_STOP;
154+
});
155+
}
156+
}
157+

src/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
executable('com.github.writeas.writeas-gtk',
22
'application.vala',
33
'window.vala',
4+
'Widgets/ModeSwitch.vala',
45

56
c_args: ['-include', 'config.h'],
67
link_args: '-lm',

src/window.vala

+11-16
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
public class WriteAs.MainWindow : Gtk.ApplicationWindow {
2020
private Gtk.TextView canvas;
2121
private Gtk.HeaderBar header;
22-
private Gtk.ToggleButton darkmode_button;
22+
private Granite.ModeSwitch darkmode_switch;
2323

2424
private static string data_dir = ".writeas";
2525
private static string version = "1.0.1";
@@ -142,24 +142,19 @@ public class WriteAs.MainWindow : Gtk.ApplicationWindow {
142142
});
143143
header.pack_end(publish_button);
144144

145-
darkmode_button = new Gtk.ToggleButton();
146-
darkmode_button.tooltip_text = _("Toggle dark theme");
147-
// NOTE the fallback icon is a bit of a meaning stretch, but it works.
148-
var icon_theme = Gtk.IconTheme.get_default();
149-
darkmode_button.image = new Gtk.Image.from_icon_name(
150-
icon_theme.has_icon("writeas-bright-dark") ?
151-
"writeas-bright-dark" : "weather-clear-night",
152-
Gtk.IconSize.LARGE_TOOLBAR);
153-
darkmode_button.draw_indicator = false;
145+
darkmode_switch = new Granite.ModeSwitch.from_icon_name ("display-brightness-symbolic", "weather-clear-night-symbolic");
146+
darkmode_switch.primary_icon_tooltip_text = ("Light theme");
147+
darkmode_switch.secondary_icon_tooltip_text = ("Dark theme");
148+
darkmode_switch.valign = Gtk.Align.CENTER;
154149
var settings = Gtk.Settings.get_default();
155-
darkmode_button.toggled.connect(() => {
156-
settings.gtk_application_prefer_dark_theme = darkmode_button.active;
157-
dark_mode = darkmode_button.active;
150+
darkmode_switch.notify["active"].connect(() => {
151+
settings.gtk_application_prefer_dark_theme = darkmode_switch.active;
152+
dark_mode = darkmode_switch.active;
158153
if (!is_initializing) theme_save();
159154

160155
canvas.grab_focus();
161156
});
162-
if (supports_dark_theme()) header.pack_end(darkmode_button);
157+
if (supports_dark_theme()) header.pack_end(darkmode_switch);
163158

164159
var fonts = new Gtk.MenuButton();
165160
fonts.tooltip_text = _("Change document font");
@@ -212,7 +207,7 @@ public class WriteAs.MainWindow : Gtk.ApplicationWindow {
212207
theme.load_from_file(get_data_dir() + "/prefs.ini", KeyFileFlags.NONE);
213208

214209
dark_mode = theme.get_boolean("Theme", "darkmode");
215-
darkmode_button.set_active(dark_mode);
210+
darkmode_switch.active = dark_mode;
216211
Gtk.Settings.get_default().gtk_application_prefer_dark_theme = dark_mode;
217212
font_size = theme.get_integer("Theme", "fontsize");
218213
font = theme.get_string("Post", "font");
@@ -338,7 +333,7 @@ public class WriteAs.MainWindow : Gtk.ApplicationWindow {
338333

339334
// Toggle theme with Ctrl+T
340335
accels.connect(Gdk.Key.T, Gdk.ModifierType.CONTROL_MASK, Gtk.AccelFlags.VISIBLE | Gtk.AccelFlags.LOCKED, (g,a,k,m) => {
341-
darkmode_button.set_active(!darkmode_button.get_active());
336+
darkmode_switch.active = !darkmode_switch.active;
342337
return true;
343338
});
344339

0 commit comments

Comments
 (0)