fjordgard

A desktop clock application
Log | Files | Refs | README | LICENSE

commit d83a58eee15ecac710c466e1dfa4ad01bab2a58f
parent c59a0e19630c91da1126d9f88494e83ec969d6c7
Author: Sylvia Ivory <git@sivory.net>
Date:   Fri, 20 Jun 2025 02:03:39 -0700

Allow settings to function on web

Diffstat:
Msrc/config.rs | 9+++++----
Msrc/main.rs | 83++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------
Msrc/settings.rs | 7++++++-
3 files changed, 79 insertions(+), 20 deletions(-)

diff --git a/src/config.rs b/src/config.rs @@ -82,10 +82,10 @@ impl Config { #[cfg(target_arch = "wasm32")] fn get_storage() -> anyhow::Result<web_sys::Storage> { - let window = web_sys::window() - .ok_or_else(|| anyhow::anyhow!("expected window"))?; + let window = web_sys::window().ok_or_else(|| anyhow::anyhow!("expected window"))?; - window.local_storage() + window + .local_storage() .map_err(|_| anyhow::anyhow!("expected local_storage"))? .ok_or_else(|| anyhow::anyhow!("expected local_storage")) } @@ -106,7 +106,8 @@ impl Config { let storage = Self::get_storage()?; let config = serde_json::to_string(self)?; - storage.set_item("config", &config) + storage + .set_item("config", &config) .map_err(|_| anyhow::anyhow!("failed to save config"))?; Ok(()) diff --git a/src/main.rs b/src/main.rs @@ -8,14 +8,13 @@ use fjordgard_weather::{ MeteoClient, model::{CurrentVariable, Forecast, ForecastOptions}, }; +#[cfg(not(target_arch = "wasm32"))] +use iced::font::Weight; use iced::{ - Color, Element, Font, Length, Size, Subscription, Task, - time, + Color, Element, Font, Length, Size, Subscription, Task, time, widget::{center, column, container, horizontal_space, row, stack, text}, window, }; -#[cfg(not(target_arch = "wasm32"))] -use iced::font::Weight; use background::BackgroundHandle; use config::{BackgroundMode, Config}; @@ -36,6 +35,7 @@ pub struct Fjordgard { format_parsed: Vec<Item<'static>>, settings_window: Option<settings::Settings>, + settings_id: Option<window::Id>, main_window: window::Id, main_window_size: Size, @@ -57,7 +57,7 @@ pub enum Message { Media(MediaControl), OpenSettings, - SettingsOpened, + SettingsOpened(window::Id), MainWindowOpened, WindowClosed(window::Id), WindowResized((window::Id, Size)), @@ -69,15 +69,22 @@ pub enum Message { ForecastUpdate(Box<Result<Forecast, String>>), } +#[cfg(target_arch = "wasm32")] +fn window_open(_settings: window::Settings) -> (window::Id, Task<window::Id>) { + let id = window::Id::unique(); + + (id, Task::done(id)) +} + impl Fjordgard { fn new() -> (Self, Task<Message>) { let settings = window::Settings::default(); let main_window_size = settings.size; - // #[cfg(not(target_arch = "wasm32"))] + #[cfg(not(target_arch = "wasm32"))] let (id, open) = window::open(settings); - // #[cfg(target_arch = "wasm32")] - // let (id, open) = wasm::window_open(settings); + #[cfg(target_arch = "wasm32")] + let (id, open) = window_open(settings); let config = Config::load().unwrap(); @@ -99,6 +106,7 @@ impl Fjordgard { format_parsed, settings_window: None, + settings_id: None, main_window: id, main_window_size, @@ -114,6 +122,7 @@ impl Fjordgard { ) } + #[cfg(not(target_arch = "wasm32"))] fn title(&self, window_id: window::Id) -> String { if window_id == self.main_window { String::from("Fjordgard") @@ -122,6 +131,11 @@ impl Fjordgard { } } + #[cfg(target_arch = "wasm32")] + fn title(&self) -> String { + String::from("Fjordgard") + } + fn update(&mut self, msg: Message) -> Task<Message> { match msg { Message::Tick(time) => { @@ -142,18 +156,22 @@ impl Fjordgard { }, Message::OpenSettings => { if self.settings_window.is_none() { + #[cfg(not(target_arch = "wasm32"))] let (_id, open) = window::open(window::Settings { level: window::Level::AlwaysOnTop, size: Size::new(350.0, 450.0), ..Default::default() }); + #[cfg(target_arch = "wasm32")] + let (_id, open) = window_open(window::Settings::default()); + self.settings_window = Some(settings::Settings::new( self.config.clone(), self.meteo.clone(), )); - open.map(|_| Message::SettingsOpened) + open.map(Message::SettingsOpened) } else { Task::none() } @@ -204,6 +222,24 @@ impl Fjordgard { Message::Settings(settings::Message::ToBackground(msg)) => { Task::done(Message::Background(msg)) } + Message::Settings(settings::Message::CloseSettings) => { + #[cfg_attr(target_arch = "wasm32", allow(unused_variables))] + if let Some(id) = self.settings_id { + self.settings_id = None; + self.settings_window = None; + + #[cfg(not(target_arch = "wasm32"))] + { + window::close(id) + } + #[cfg(target_arch = "wasm32")] + { + Task::none() + } + } else { + Task::none() + } + } Message::Settings(msg) => { if let Some(settings) = &mut self.settings_window { settings.update(msg).map(Message::Settings) @@ -212,8 +248,9 @@ impl Fjordgard { } } Message::Background(msg) => self.background.update(msg).map(Message::Background), - Message::SettingsOpened => { + Message::SettingsOpened(id) => { debug!("settings window opened"); + self.settings_id = Some(id); Task::none() } Message::MainWindowOpened => { @@ -343,6 +380,7 @@ impl Fjordgard { } } + #[cfg(not(target_arch = "wasm32"))] fn view(&self, window_id: window::Id) -> Element<Message> { if self.main_window == window_id { self.view_main() @@ -355,6 +393,15 @@ impl Fjordgard { } } + #[cfg(target_arch = "wasm32")] + fn view(&self) -> Element<Message> { + if let Some(settings) = &self.settings_window { + settings.view().map(Message::Settings) + } else { + self.view_main() + } + } + fn view_main(&self) -> Element<Message> { #[cfg_attr(target_arch = "wasm32", allow(unused_mut))] let mut bold = Font::DEFAULT; @@ -419,15 +466,21 @@ impl Fjordgard { fn main() -> iced::Result { #[cfg(not(target_arch = "wasm32"))] - env_logger::init(); + { + env_logger::init(); + + iced::daemon(Fjordgard::title, Fjordgard::update, Fjordgard::view) + .subscription(Fjordgard::subscription) + .run_with(Fjordgard::new) + } #[cfg(target_arch = "wasm32")] { std::panic::set_hook(Box::new(console_error_panic_hook::hook)); console_log::init_with_level(log::Level::Info).unwrap(); - } - iced::daemon(Fjordgard::title, Fjordgard::update, Fjordgard::view) - .subscription(Fjordgard::subscription) - .run_with(Fjordgard::new) + iced::application(Fjordgard::title, Fjordgard::update, Fjordgard::view) + .subscription(Fjordgard::subscription) + .run_with(Fjordgard::new) + } } diff --git a/src/settings.rs b/src/settings.rs @@ -68,6 +68,7 @@ pub enum Message { #[cfg(not(target_arch = "wasm32"))] FileSelected(Option<FileHandle>), Save, + CloseSettings, Committed, Saved(Result<(), String>), @@ -471,7 +472,11 @@ impl Settings { 64.0 * (self.location_results.len().clamp(0, 1) as f32) )) .width(Length::Fill), - button("Save").on_press_maybe(save_message) + row![ + button("Save").on_press_maybe(save_message), + button("Close").on_press(Message::CloseSettings), + ] + .spacing(5) ] .spacing(10), )