Commit 2319d847 authored by Felix Häcker's avatar Felix Häcker

Implement settings window; Refactor settings backend

parent 64064bb0
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "Inflector"
version = "0.11.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "aho-corasick"
version = "0.7.6"
......@@ -778,6 +769,14 @@ dependencies = [
"tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "heck"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-segmentation 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "http"
version = "0.1.18"
......@@ -1365,6 +1364,14 @@ dependencies = [
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "proc-macro2"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "protobuf"
version = "2.8.0"
......@@ -1415,6 +1422,14 @@ dependencies = [
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "quote"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "r2d2"
version = "0.8.5"
......@@ -1753,7 +1768,6 @@ dependencies = [
name = "shortwave"
version = "0.0.0"
dependencies = [
"Inflector 0.11.4 (registry+https://github.com/rust-lang/crates.io-index)",
"cairo-rs 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)",
"diesel 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -1787,6 +1801,8 @@ dependencies = [
"serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_urlencoded 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
"soup 0.1.0 (git+https://gitlab.gnome.org/haecker-felix/soup-rs)",
"strum 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)",
"strum_macros 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)",
"url 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"uuid 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)",
"xdg 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -1845,6 +1861,22 @@ dependencies = [
"bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "strum"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "strum_macros"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "syn"
version = "0.11.11"
......@@ -1865,6 +1897,16 @@ dependencies = [
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "syn"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "synom"
version = "0.11.3"
......@@ -2146,6 +2188,11 @@ dependencies = [
"smallvec 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "unicode-segmentation"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "unicode-xid"
version = "0.0.4"
......@@ -2156,6 +2203,11 @@ name = "unicode-xid"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "unicode-xid"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "url"
version = "1.7.2"
......@@ -2270,7 +2322,6 @@ version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum Inflector 0.11.4 (registry+https://github.com/rust-lang/crates.io-index)" = "fe438c63458706e03479442743baae6c88256498e6431708f6dfc520a26515d3"
"checksum aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "58fb5e95d83b38284460a5fda7d6470aa0b8844d283a0b614b8535e880800d2d"
"checksum arrayvec 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)" = "b8d73f9beda665eaa98ab9e4f7442bd4e7de6652587de55b2525e52e29c1b0ba"
"checksum atk 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "86b7499272acf036bb5820c6e346bbfb5acc5dceb104bc2c4fd7e6e33dfcde6a"
......@@ -2349,6 +2400,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum gtk 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "709f1074259d4685b96133f92b75c7f35b504715b0fcdc96ec95de2607296a60"
"checksum gtk-sys 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bbd9395497ae1d1915d1d6e522d51ae8745bf613906c34ac191c411250fc4025"
"checksum h2 0.1.26 (registry+https://github.com/rust-lang/crates.io-index)" = "a5b34c246847f938a410a03c5458c7fee2274436675e76d8b903c08efc29c462"
"checksum heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205"
"checksum http 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)" = "372bcb56f939e449117fb0869c2e8fd8753a8223d92a172c6e808cf123a5b6e4"
"checksum http-body 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6741c859c1b2463a423a1dbce98d418e6c3c3fc720fb0d45528657320920292d"
"checksum httparse 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9"
......@@ -2411,6 +2463,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum ppv-lite86 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e3cbf9f658cdb5000fcf6f362b8ea2ba154b9f146a61c7a20d647034c6b6561b"
"checksum pretty_env_logger 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "df8b3f4e0475def7d9c2e5de8e5a1306949849761e107b360d03e98eafaffd61"
"checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759"
"checksum proc-macro2 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "afdc77cc74ec70ed262262942ebb7dac3d479e9e5cfa2da1841c0806f6cdabcc"
"checksum protobuf 2.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8aefcec9f142b524d98fc81d07827743be89dd6586a1ba6ab21fa66a500b3fa5"
"checksum protobuf-codegen 2.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "31539be8028d6b9e8e1b3b7c74e2fa3555302e27b2cc20dbaee6ffba648f75e2"
"checksum protoc 2.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d60e39b07eb4039379829c55c11eba3fd8bd72b265b9ece8cc623b106908c08d"
......@@ -2418,6 +2471,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0"
"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
"checksum quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)" = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1"
"checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe"
"checksum r2d2 0.8.5 (registry+https://github.com/rust-lang/crates.io-index)" = "bc42ce75d9f4447fb2a04bbe1ed5d18dd949104572850ec19b164e274919f81b"
"checksum rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca"
"checksum rand 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d47eab0e83d9693d40f825f86948aa16eff6750ead4bdffc4ab95b8b3a7f052c"
......@@ -2462,8 +2516,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum soup-sys 0.1.0 (git+https://gitlab.gnome.org/haecker-felix/soup-rs)" = "<none>"
"checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8"
"checksum string 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d24114bfcceb867ca7f71a0d3fe45d45619ec47a6fbfa98cb14e14250bfa5d6d"
"checksum strum 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6138f8f88a16d90134763314e3fc76fa3ed6a7db4725d6acf9a3ef95a3188d22"
"checksum strum_macros 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0054a7df764039a6cd8592b9de84be4bec368ff081d203a7d5371cbfa8e65c81"
"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad"
"checksum syn 0.15.42 (registry+https://github.com/rust-lang/crates.io-index)" = "eadc09306ca51a40555dd6fc2b415538e9e18bc9f870e47b1a524a79fe2dcf5e"
"checksum syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "66850e97125af79138385e9b88339cbcd037e3f28ceab8c5ad98e64f0f1f80bf"
"checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6"
"checksum synstructure 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "02353edf96d6e4dc81aea2d8490a7e9db177bf8acb0e951c24940bf866cb313f"
"checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9"
......@@ -2489,8 +2546,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum ucd-util 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fa9b3b49edd3468c0e6565d85783f51af95212b6fa3986a5500954f00b460874"
"checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5"
"checksum unicode-normalization 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "141339a08b982d942be2ca06ff8b076563cbe223d1befd5450716790d44e2426"
"checksum unicode-segmentation 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1967f4cdfc355b37fd76d2a954fb2ed3871034eb4f26d60537d88795cfc332a9"
"checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc"
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
"checksum url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dd4e7c0d531266369519a4aa4f399d748bd37043b00bde1e4ff1f60a120b355a"
"checksum url 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "77ddaf52e65c6b81c56b7e957c0b1970f7937f21c5c6774c4e56fcb4e20b48c6"
"checksum utf8-ranges 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "9d50aa7650df78abf942826607c62468ce18d9019673d4a2ebe1865dbb96ffde"
......
......@@ -41,4 +41,5 @@ indexmap = { git = "https://github.com/bluss/indexmap.git", rev = "380e55c6a330b
gettext-rs= { version = "0.4", features = ["gettext-system"] }
failure = "0.1.5"
failure_derive = "0.1.5"
Inflector = "0.11.4"
strum = "0.16.0"
strum_macros = "0.16.0"
......@@ -27,7 +27,7 @@
"--share=network"
],
"env" : {
"RUSTFLAGS" : "--remap-path-prefix =../ --error-format=human",
"RUSTFLAGS" : "--remap-path-prefix =../",
"CARGO_HOME" : "/run/build/Shortwave/cargo",
"RUST_BACKTRACE" : "1",
"RUST_LOG" : "shortwave=debug"
......
......@@ -18,6 +18,7 @@
<file compressed="true" preprocess="xml-stripblanks">gtk/mini_controller.ui</file>
<file compressed="true" preprocess="xml-stripblanks">gtk/tile_button.ui</file>
<file compressed="true" preprocess="xml-stripblanks">gtk/search.ui</file>
<file compressed="true" preprocess="xml-stripblanks">gtk/settings_window.ui</file>
<file compressed="true">gtk/style.css</file>
......
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="3.0"/>
<object class="HdyPreferencesWindow" id="settings_window">
<property name="default_height">300</property>
<property name="default_width">640</property>
<property name="width_request">300</property>
<child>
<object class="HdyPreferencesPage">
<property name="icon_name">preferences-system-symbolic</property>
<property name="title">General</property>
<property name="visible">True</property>
<child>
<object class="HdyPreferencesGroup">
<property name="title" translatable="yes">Appearance</property>
<property name="visible">True</property>
<child>
<object class="HdyActionRow">
<property name="title" translatable="yes">Enable dark mode</property>
<property name="visible">True</property>
<child type="action">
<object class="GtkSwitch" id="dark_mode_button">
<property name="can_focus">True</property>
<property name="valign">center</property>
<property name="visible">True</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</interface>
......@@ -17,13 +17,14 @@ use crate::database::Library;
use crate::discover::StoreFront;
use crate::ui::{View, Window, Notification};
use crate::utils::{Order, Sorting};
use crate::settings;
use crate::settings::{Key, SettingsManager, SettingsWindow};
#[derive(Debug, Clone)]
pub enum Action {
ViewShowDiscover,
ViewShowLibrary,
ViewShowPlayer,
ViewShowSettings,
ViewShowNotification(Rc<Notification>),
ViewRaise,
ViewSetSorting(Sorting, Order),
......@@ -36,6 +37,7 @@ pub enum Action {
LibraryAddStations(Vec<Station>),
LibraryRemoveStations(Vec<Station>),
SearchFor(StationRequest), // TODO: is this neccessary?,
SettingsKeyChanged(Key),
}
pub struct App {
......@@ -48,6 +50,8 @@ pub struct App {
player: Player,
library: Library,
storefront: StoreFront,
settings: SettingsManager,
}
impl App {
......@@ -75,6 +79,9 @@ impl App {
window.discover_header_switcher.set_stack(Some(&storefront.discover_stack));
window.discover_bottom_switcher.set_stack(Some(&storefront.discover_stack));
// Create new SettingsManager which notifies about settings changes
let settings = SettingsManager::new(sender.clone());
// Help overlay
let builder = gtk::Builder::new_from_resource("/de/haeckerfelix/Shortwave/gtk/shortcuts.ui");
get_widget!(builder, gtk::ShortcutsWindow, shortcuts);
......@@ -88,6 +95,7 @@ impl App {
player,
library,
storefront,
settings,
});
glib::set_application_name(&config::NAME);
......@@ -103,6 +111,8 @@ impl App {
info!("{} ({}) ({})", config::NAME, config::APP_ID, config::VCS_TAG);
info!("Version: {} ({})", config::VERSION, config::PROFILE);
self.settings.list_keys();
let a = app.clone();
let receiver = self.receiver.borrow_mut().take().unwrap();
receiver.attach(None, move |action| a.process_action(action));
......@@ -123,6 +133,12 @@ impl App {
Self::show_about_dialog(window.clone());
});
// Preferences
let sender = self.sender.clone();
self.add_gaction("preferences", move |_, _| {
sender.send(Action::ViewShowSettings).unwrap();
});
// Search / Discover / add stations
let sender = self.sender.clone();
self.add_gaction("discover", move |_, _| {
......@@ -205,6 +221,7 @@ impl App {
Action::ViewShowDiscover => self.window.set_view(View::Discover),
Action::ViewShowLibrary => self.window.set_view(View::Library),
Action::ViewShowPlayer => self.window.set_view(View::Player),
Action::ViewShowSettings => self.show_settings_window(),
Action::ViewRaise => self.window.widget.present_with_time((glib::get_monotonic_time() / 1000) as u32),
Action::ViewShowNotification(notification) => self.window.show_notification(notification),
Action::ViewSetSorting(sorting, order) => self.library.set_sorting(sorting, order),
......@@ -217,10 +234,21 @@ impl App {
Action::LibraryAddStations(stations) => self.library.add_stations(stations),
Action::LibraryRemoveStations(stations) => self.library.remove_stations(stations),
Action::SearchFor(data) => self.storefront.search_for(data),
Action::SettingsKeyChanged(key) => {
match key {
Key::DarkMode => self.window.update_dark_mode(),
_ => (),
}
},
}
glib::Continue(true)
}
fn show_settings_window(&self) {
let settings_window = SettingsWindow::new(&self.window.widget);
settings_window.show();
}
fn show_about_dialog(window: gtk::ApplicationWindow) {
let vcs_tag = config::VCS_TAG;
let version_suffix: String = match config::PROFILE {
......@@ -271,7 +299,7 @@ impl App {
self.sender.send(Action::ViewShowNotification(spinner_notification.clone())).unwrap();
// Get actual stations from identifiers
let client = Client::new(Url::parse(&settings::get_string(settings::Key::ApiServer)).unwrap());
let client = Client::new(Url::parse(&SettingsManager::get_string(Key::ApiServer)).unwrap());
let sender = self.sender.clone();
let fut = client.get_stations_by_identifiers(ids).map(move |stations| {
spinner_notification.hide();
......
......@@ -17,7 +17,7 @@ use crate::audio::{PlaybackState, Song, SongBackend};
use crate::ui::Notification;
use crate::path;
use crate::utils;
use crate::settings;
use crate::settings::{Key, SettingsManager};
/////////////////////////////////////////////////////////////////////////////////////////////////
// //
......@@ -114,7 +114,7 @@ impl Player {
let gst_backend = self.gst_backend.clone();
let sender = self.sender.clone();
let client = Client::new(Url::parse(&settings::get_string(settings::Key::ApiServer)).unwrap());
let client = Client::new(Url::parse(&SettingsManager::get_string(Key::ApiServer)).unwrap());
// get asynchronously the stream url and play it
let fut = client.get_stream_url(station).map(move |station_url| {
match station_url {
......
......@@ -14,8 +14,6 @@ pub struct SongBackend {
songs: IndexMap<String, Song>,
save_count: usize,
sender: Sender<Action>,
}
// songs: IndexMap<String, Song>,
......@@ -30,7 +28,7 @@ impl SongBackend {
let listbox = SongListBox::new(sender.clone());
let songs = IndexMap::new();
let song_backend = Self { listbox, songs, save_count, sender };
let song_backend = Self { listbox, songs, save_count };
song_backend
}
......
......@@ -14,7 +14,7 @@ use crate::database::queries;
use crate::database::StationIdentifier;
use crate::ui::{StationFlowBox, Notification};
use crate::utils::{Order, Sorting};
use crate::settings;
use crate::settings::{Key, SettingsManager};
pub struct Library {
pub widget: gtk::Box,
......@@ -40,7 +40,7 @@ impl Library {
flowbox.set_sorting(Sorting::Name, Order::Ascending);
content_box.add(&flowbox.widget);
let client = Client::new(Url::parse(&settings::get_string(settings::Key::ApiServer)).unwrap());
let client = Client::new(Url::parse(&SettingsManager::get_string(Key::ApiServer)).unwrap());
let library = Self {
widget: library,
......
......@@ -9,7 +9,7 @@ use std::rc::Rc;
use crate::api::{Client, StationRequest};
use crate::app::Action;
use crate::ui::{StationFlowBox, Notification};
use crate::settings;
use crate::settings::{Key, SettingsManager};
pub struct Search {
pub widget: gtk::Box,
......@@ -27,7 +27,7 @@ impl Search {
let builder = gtk::Builder::new_from_resource("/de/haeckerfelix/Shortwave/gtk/search.ui");
get_widget!(builder, gtk::Box, search);
let client = Client::new(Url::parse(&settings::get_string(settings::Key::ApiServer)).unwrap());
let client = Client::new(Url::parse(&SettingsManager::get_string(Key::ApiServer)).unwrap());
get_widget!(builder, gtk::Box, results_box);
let flowbox = Rc::new(StationFlowBox::new(sender.clone()));
......
......@@ -15,6 +15,9 @@ extern crate diesel;
extern crate failure_derive;
#[macro_use]
extern crate diesel_migrations;
#[macro_use]
extern crate strum_macros;
use gettextrs::*;
#[macro_use]
......@@ -54,3 +57,4 @@ fn main() {
let app = App::new();
app.run(app.clone());
}
......@@ -72,7 +72,10 @@ sources = files(
'discover/storefront.rs',
'discover/tile_button.rs',
'settings/key.rs',
'settings/mod.rs',
'settings/settings_window.rs',
'settings/settings_manager.rs',
'ui/mod.rs',
'ui/notification.rs',
......
#[derive(Display, Debug, Clone, EnumString)]
#[strum(serialize_all = "kebab_case")]
pub enum Key {
/* Client Backend */
ApiServer,
/* User Interface */
DarkMode,
WindowWidth,
WindowHeight,
}
use gio::prelude::*;
use inflector::Inflector;
mod settings_window;
pub use settings_window::SettingsWindow;
use crate::config;
mod key;
pub use key::Key;
#[derive(Debug)]
pub enum Key {
/* Client Backend */
ApiServer,
mod settings_manager;
pub use settings_manager::SettingsManager;
/* User Interface */
DarkMode,
WindowWidth,
WindowHeight,
}
impl Key{
pub fn to_string(&self) -> String{
let string = format!("{:?}", self);
string.to_kebab_case()
}
}
fn get_settings() -> gio::Settings{
let app_id = config::APP_ID.trim_end_matches(".Devel");
gio::Settings::new(app_id)
}
pub fn get_string(key: Key) -> String {
let settings = get_settings();
settings.get_string(&key.to_string()).unwrap().to_string()
}
pub fn set_string(key: Key, value: String) {
let settings = get_settings();
settings.set_string(&key.to_string(), &value);
}
pub fn get_boolean(key: Key) -> bool {
let settings = get_settings();
settings.get_boolean(&key.to_string())
}
pub fn set_boolean(key: Key, value: bool) {
let settings = get_settings();
settings.set_boolean(&key.to_string(), value);
}
pub fn get_integer(key: Key) -> i32 {
let settings = get_settings();
settings.get_int(&key.to_string())
}
pub fn set_integer(key: Key, value: i32) {
let settings = get_settings();
settings.set_int(&key.to_string(), value);
}
use gio::prelude::*;
use glib::prelude::*;
use std::str::FromStr;
use crate::app::Action;
use crate::config;
use crate::settings::Key;
pub struct SettingsManager{
settings: gio::Settings,
}
impl SettingsManager{
pub fn new (sender: glib::Sender<Action>) -> Self {
let settings = Self::get_settings();
settings.connect_changed(move|_, key_str|{
let key: Key = Key::from_str(key_str).unwrap();
sender.send(Action::SettingsKeyChanged(key)).unwrap();
});
Self{settings}
}
pub fn list_keys(&self){
debug!("Settings values:");
let keys = self.settings.list_keys();
for key in keys{
let name = key.to_string();
let value = self.settings.get_value(&name).unwrap();
debug!(" \"{}\" -> {}", name, value);
}
}
fn get_settings() -> gio::Settings{
let app_id = config::APP_ID.trim_end_matches(".Devel");
gio::Settings::new(app_id)
}
pub fn bind_property<P: IsA<glib::Object>>(key: Key, object: &P, property: &str) {
let settings = Self::get_settings();
settings.bind(key.to_string().as_str(), object, property, gio::SettingsBindFlags::DEFAULT);
}
pub fn get_string(key: Key) -> String {
let settings = Self::get_settings();
settings.get_string(&key.to_string()).unwrap().to_string()
}
pub fn set_string(key: Key, value: String) {
let settings = Self::get_settings();
settings.set_string(&key.to_string(), &value);
}
pub fn get_boolean(key: Key) -> bool {
let settings = Self::get_settings();
settings.get_boolean(&key.to_string())
}
pub fn set_boolean(key: Key, value: bool) {
let settings = Self::get_settings();
settings.set_boolean(&key.to_string(), value);
}
pub fn get_integer(key: Key) -> i32 {
let settings = Self::get_settings();
settings.get_int(&key.to_string())
}
pub fn set_integer(key: Key, value: i32) {
let settings = Self::get_settings();
settings.set_int(&key.to_string(), value);
}
}
use gtk::prelude::*;
use libhandy::PreferencesWindow;
use crate::settings::{SettingsManager, Key};
pub struct SettingsWindow {
pub widget: PreferencesWindow,
builder: gtk::Builder,
}
impl SettingsWindow {
pub fn new(window: &gtk::ApplicationWindow) -> Self {
let builder = gtk::Builder::new_from_resource("/de/haeckerfelix/Shortwave/gtk/settings_window.ui");
get_widget!(builder, PreferencesWindow, settings_window);
settings_window.set_transient_for(Some(window));
let window = Self {
widget: settings_window,
builder,
};
window.setup_signals();
window
}
pub fn show(&self) {
self.widget.set_visible(true);
}
fn setup_signals(&self) {
get_widget!(self.builder, gtk::Switch, dark_mode_button);
SettingsManager::bind_property(Key::DarkMode, &dark_mode_button, "active");
}
}
......@@ -8,7 +8,7 @@ use std::rc::Rc;
use crate::app::Action;
use crate::config;
use crate::ui::Notification;
use crate::settings;
use crate::settings::{Key, SettingsManager};
#[derive(Debug, Clone, PartialEq)]
pub enum View {
......@@ -79,16 +79,13 @@ impl Window {
ctx.add_class("devel");
}
// Dark mode
let gtk_settings = gtk::Settings::get_default().unwrap();
gtk_settings.set_property_gtk_application_prefer_dark_theme(settings::get_boolean(settings::Key::DarkMode));
// Restore window geometry
let width = settings::get_integer(settings::Key::WindowWidth);
let height = settings::get_integer(settings::Key::WindowHeight);
let width = SettingsManager::get_integer(Key::WindowWidth);
let height = SettingsManager::get_integer(Key::WindowHeight);
window.widget.resize(width, height);
window.setup_signals();
window.update_dark_mode();
window
}
......@@ -135,8 +132,8 @@ impl Window {
let width = window.get_size().0;
let height = window.get_size().1;
settings::set_integer(settings::Key::WindowWidth, width);
settings::set_integer(settings::Key::WindowHeight, height);
SettingsManager::set_integer(Key::WindowWidth, width);
SettingsManager::set_integer(Key::WindowHeight, height);
Inhibit(false)
});
}
......@@ -212,6 +209,11 @@ impl Window {
}
}
pub fn update_dark_mode(&self){
let gtk_settings = gtk::Settings::get_default().unwrap();
gtk_settings.set_property_gtk_application_prefer_dark_theme(SettingsManager::get_boolean(Key::DarkMode));
}
pub fn set_view(&self, view: View) {
*self.current_view.borrow_mut() = view;
Self::update_view(self.current_view.borrow().clone(), self.builder.clone(), self.menu_builder.clone());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment