Debian Codegraph @

src/properties_dialog.rs

raw symbols SCIP
1 /*
2 * Copyright 2020-2024 Andrey Kutejko <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (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
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * For more details see the file COPYING.
18 */
19
20 use crate::{
21 controls::controls::create_controls_list,
22 game_config::{GameConfig, GameConfigs},
23 properties::Properties,
24 theme::Theme,
25 themes,
26 };
27 use adw::prelude::*;
28 use gettextrs::gettext;
29 use gtk::{gio, glib};
30 use std::rc::Rc;
31
32 pub fn show_preferences(
33 parent_window: Option<&gtk::Window>,
34 game_configs: &GameConfigs,
35 themes: &gio::ListModel,
36 settings: &gio::Settings,
37 ) {
38 let window = adw::PreferencesDialog::builder().build();
39
40 /* The configuration page */
41 let configuration_page = adw::PreferencesPage::builder()
42 .title(gettext("Game"))
43 .icon_name("gamepad2")
44 .build();
45 configuration_page.add(&create_game_group(game_configs, settings));
46 configuration_page.add(&create_sound_group(settings));
47 window.add(&configuration_page);
48
49 /* The graphics page */
50 let graphics_page = adw::PreferencesPage::builder()
51 .title(gettext("Appearance"))
52 .icon_name("brush-monitor")
53 .build();
54 let appearance_group = adw::PreferencesGroup::builder().build();
55 appearance_group.add(&create_theme_picker(themes, settings));
56 appearance_group.add(&create_background_picker(settings));
57 graphics_page.add(&appearance_group);
58 window.add(&graphics_page);
59
60 /* The keyboard page */
61 let keyboard_page = adw::PreferencesPage::builder()
62 .title(gettext("Keyboard"))
63 .icon_name("keyboard-shortcuts")
64 .build();
65 keyboard_page.add(&create_controls_list(settings));
66 window.add(&keyboard_page);
67
68 window.present(parent_window);
69 }
70
71 fn create_game_group(
72 game_configs: &GameConfigs,
73 properties: &gio::Settings,
74 ) -> adw::PreferencesGroup {
75 let group = adw::PreferencesGroup::new();
76
77 let typemenu = create_game_config_picker(game_configs, &properties.selected_config());
78 typemenu.connect_selected_item_notify(glib::clone!(
79 #[weak]
80 properties,
81 move |cr| {
82 if let Some(selection) = cr
83 .selected_item()
84 .and_downcast::<glib::BoxedAnyObject>()
85 .map(|b| b.borrow::<Rc<GameConfig>>().clone())
86 {
87 properties.set_selected_config(selection.name);
88 }
89 }
90 ));
91 group.add(&typemenu);
92
93 let safe_chkbox = adw::SwitchRow::builder()
94 .title(gettext("_Use safe moves"))
95 .use_underline(true)
96 .subtitle(gettext(
97 "Prevent accidental moves that result in getting killed.",
98 ))
99 .active(properties.safe_moves())
100 .build();
101 group.add(&safe_chkbox);
102
103 let super_safe_chkbox = adw::SwitchRow::builder()
104 .title(gettext("U_se super safe moves"))
105 .use_underline(true)
106 .subtitle(gettext("Prevents all moves that result in getting killed."))
107 .active(properties.super_safe_moves())
108 .sensitive(properties.safe_moves())
109 .build();
110 group.add(&super_safe_chkbox);
111
112 super_safe_chkbox.connect_active_notify(glib::clone!(
113 #[weak]
114 properties,
115 move |cb| {
116 properties.set_super_safe_moves(cb.is_active());
117 }
118 ));
119 safe_chkbox.connect_active_notify(glib::clone!(
120 #[weak]
121 properties,
122 move |cb| {
123 properties.set_safe_moves(cb.is_active());
124 super_safe_chkbox.set_sensitive(properties.safe_moves());
125 }
126 ));
127
128 group
129 }
130
131 fn create_sound_group(settings: &gio::Settings) -> adw::PreferencesGroup {
132 let group = adw::PreferencesGroup::new();
133
134 let sound_chkbox = adw::SwitchRow::builder()
135 .title(gettext("_Enable sounds"))
136 .use_underline(true)
137 .subtitle(gettext(
138 "Play sounds for events like winning a level and dying.",
139 ))
140 .active(settings.sound())
141 .build();
142 sound_chkbox.connect_active_notify(glib::clone!(
143 #[weak]
144 settings,
145 move |cb| {
146 settings.set_sound(cb.is_active());
147 }
148 ));
149 group.add(&sound_chkbox);
150
151 group
152 }
153
154 fn create_game_config_picker(game_configs: &GameConfigs, current_config: &str) -> adw::ComboRow {
155 let model = gio::ListStore::new::<glib::BoxedAnyObject>();
156 let mut active_index = 0;
157 for (i, config) in game_configs.game_configs.iter().enumerate() {
158 model.append(&glib::BoxedAnyObject::new(config.clone()));
159 if config.name == current_config {
160 active_index = i as u32;
161 }
162 }
163
164 adw::ComboRow::builder()
165 .title(gettext("Game Type"))
166 .model(&model)
167 .expression(gtk::ClosureExpression::new::<String>(
168 &[] as &[gtk::Expression],
169 glib::closure!(|item: &glib::Object| {
170 item.downcast_ref::<glib::BoxedAnyObject>()
171 .map(|b| b.borrow::<Rc<GameConfig>>().display_name.to_owned())
172 .unwrap_or_default()
173 }),
174 ))
175 .selected(active_index)
176 .build()
177 }
178
179 fn create_theme_picker(themes: &gio::ListModel, settings: &gio::Settings) -> adw::ComboRow {
180 let index = themes::find_by_name(themes, &settings.theme())
181 .map(|r| r.0)
182 .unwrap_or_default();
183
184 let theme_picker = adw::ComboRow::builder()
185 .title(gettext("_Image theme:"))
186 .use_underline(true)
187 .model(themes)
188 .selected(index)
189 .expression(gtk::PropertyExpression::new(
190 Theme::static_type(),
191 gtk::Expression::NONE,
192 "name",
193 ))
194 .build();
195
196 theme_picker.connect_selected_item_notify(glib::clone!(
197 #[weak]
198 settings,
199 move |tp| {
200 if let Some(theme) = tp.selected_item().and_downcast::<Theme>() {
201 settings.set_theme(&theme.name());
202 }
203 }
204 ));
205
206 theme_picker
207 }
208
209 fn create_background_picker(settings: &gio::Settings) -> adw::ActionRow {
210 let color_dialog = gtk::ColorDialog::new();
211
212 let button = gtk::ColorDialogButton::builder()
213 .dialog(&color_dialog)
214 .rgba(&settings.bgcolour())
215 .build();
216 button.connect_rgba_notify(glib::clone!(
217 #[weak]
218 settings,
219 move |b| {
220 settings.set_bgcolour(b.rgba());
221 }
222 ));
223
224 let color_row = adw::ActionRow::builder()
225 .title(gettext("_Background color:"))
226 .use_underline(true)
227 .build();
228 color_row.add_suffix(&button);
229
230 color_row
231 }