From bb7cc2b5683d0141b91b229edc56e87a0bd45f85 Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Fri, 14 Mar 2025 02:56:49 +0100 Subject: [PATCH] fix(about): change hostname when input focus changes Closes #1047 --- cosmic-settings/src/app.rs | 4 +- cosmic-settings/src/pages/system/about.rs | 123 +++++++++++++++------- 2 files changed, 87 insertions(+), 40 deletions(-) diff --git a/cosmic-settings/src/app.rs b/cosmic-settings/src/app.rs index 4f1fd0c..ac9fef2 100644 --- a/cosmic-settings/src/app.rs +++ b/cosmic-settings/src/app.rs @@ -380,7 +380,9 @@ impl cosmic::Application for SettingsApp { } #[cfg(feature = "page-about")] crate::pages::Message::About(message) => { - page::update!(self.pages, message, system::about::Page); + if let Some(page) = self.pages.page_mut::() { + return page.update(message).map(Into::into); + } } crate::pages::Message::Appearance(message) => { diff --git a/cosmic-settings/src/pages/system/about.rs b/cosmic-settings/src/pages/system/about.rs index 9833da5..d48e2d3 100644 --- a/cosmic-settings/src/pages/system/about.rs +++ b/cosmic-settings/src/pages/system/about.rs @@ -11,16 +11,31 @@ use slotmap::SlotMap; #[derive(Clone, Debug)] pub enum Message { + Error(String), HostnameEdit(bool), HostnameInput(String), HostnameSubmit, + HostnameSuccess(String), Info(Box), } +impl From for crate::app::Message { + fn from(message: Message) -> Self { + crate::pages::Message::About(message).into() + } +} + +impl From for crate::pages::Message { + fn from(message: Message) -> Self { + crate::pages::Message::About(message) + } +} + #[derive(Clone, Debug, Default)] pub struct Page { entity: page::Entity, editing_device_name: bool, + hostname_input: String, info: Info, on_enter_handle: Option, } @@ -69,52 +84,82 @@ impl page::Page for Page { } impl Page { - pub fn update(&mut self, message: Message) { + pub fn update(&mut self, message: Message) -> cosmic::app::Task { match message { Message::HostnameEdit(editing) => { self.editing_device_name = editing; - } - Message::HostnameInput(hostname) => { - self.info.device_name = hostname; - } - - Message::HostnameSubmit => { - let hostname = &self.info.device_name; - if hostname_validator::is_valid(hostname) { - // TODO: display errors - self.editing_device_name = false; - let hostname = hostname.clone(); - tokio::task::spawn(async move { - let connection = match zbus::Connection::system().await { - Ok(conn) => conn, - Err(why) => { - tracing::error!(?why, "failed to establish connection to dbus"); - return; - } - }; - - let hostname1 = match hostname1_zbus::Hostname1Proxy::new(&connection).await - { - Ok(proxy) => proxy, - Err(why) => { - tracing::error!( - ?why, - "failed to connect to org.freedesktop.hostname1" - ); - return; - } - }; - - if let Err(why) = hostname1.set_static_hostname(&hostname, false).await { - tracing::error!(?why, "failed to set static hostname"); - } - }); + if !editing { + return self.hostname_submit(); } } - Message::Info(info) => self.info = *info, + Message::HostnameInput(hostname) => { + self.hostname_input = hostname; + } + + Message::HostnameSubmit => return self.hostname_submit(), + + Message::Info(info) => { + self.info = *info; + self.hostname_input = self.info.device_name.clone(); + } + + Message::Error(_why) => { + self.hostname_input = self.info.device_name.clone(); + // TODO: display errors + } + + Message::HostnameSuccess(name) => { + self.info.device_name = name; + } } + + Task::none() + } + + fn hostname_submit(&mut self) -> cosmic::app::Task { + if self.hostname_input == self.info.device_name { + return Task::none(); + } + + let hostname = &self.hostname_input; + if hostname_validator::is_valid(hostname) { + self.editing_device_name = false; + let hostname = hostname.clone(); + return cosmic::Task::future(async move { + let connection = match zbus::Connection::system().await { + Ok(conn) => conn, + Err(why) => { + tracing::error!(?why, "failed to establish connection to dbus"); + return Message::Error(String::from( + "failed to establish connection to dbus", + )); + } + }; + + let hostname1 = match hostname1_zbus::Hostname1Proxy::new(&connection).await { + Ok(proxy) => proxy, + Err(why) => { + tracing::error!(?why, "failed to connect to org.freedesktop.hostname1"); + return Message::Error(String::from( + "failed to connect to org.freedesktop.hostname1", + )); + } + }; + + if let Err(why) = hostname1.set_static_hostname(&hostname, false).await { + tracing::error!(?why, "failed to set static hostname"); + return Message::Error(String::from("failed to set static hostname")); + } + + Message::HostnameSuccess(hostname) + }) + .map(crate::app::Message::from) + .map(Into::into); + } + + Task::none() } } @@ -131,7 +176,7 @@ fn device() -> Section { let hostname_input = editable_input( "", - &page.info.device_name, + &page.hostname_input, page.editing_device_name, Message::HostnameEdit, )