improv(date-time): auto-enable NTP when setting timezone

This commit is contained in:
Michael Aaron Murphy 2024-08-07 18:58:16 +02:00
parent b3d97012f2
commit a9cf706ca2
No known key found for this signature in database
GPG key ID: B2732D4240C9212C
3 changed files with 54 additions and 51 deletions

View file

@ -480,6 +480,7 @@ impl Page {
} }
widget::column() widget::column()
.padding([2, 0])
.spacing(32) .spacing(32)
.push(search) .push(search)
.push(list) .push(list)

View file

@ -18,7 +18,6 @@ use icu::{
datetime::DateTimeFormatter, datetime::DateTimeFormatter,
locid::Locale, locid::Locale,
}; };
use itertools::Itertools;
use slab::Slab; use slab::Slab;
use slotmap::SlotMap; use slotmap::SlotMap;
pub use timedate_zbus::TimeDateProxy; pub use timedate_zbus::TimeDateProxy;
@ -156,30 +155,6 @@ impl page::Page<crate::pages::Message> for Page {
impl Page { impl Page {
pub fn update(&mut self, message: Message) -> Command<crate::Message> { pub fn update(&mut self, message: Message) -> Command<crate::Message> {
match message { match message {
Message::Automatic(enable) => {
self.ntp_enabled = enable;
tokio::task::spawn(async move {
let client = match zbus::Connection::system().await {
Ok(client) => client,
Err(why) => {
tracing::error!(?why, "zbus client error");
return;
}
};
let timedate_proxy = match TimeDateProxy::new(&client).await {
Ok(timedate_proxy) => timedate_proxy,
Err(why) => {
tracing::error!(?why, "zbus client error");
return;
}
};
_ = timedate_proxy.set_ntp(enable, true).await;
});
}
Message::TimezoneContext => { Message::TimezoneContext => {
self.timezone_search.clear(); self.timezone_search.clear();
self.timezone_context = true; self.timezone_context = true;
@ -251,9 +226,16 @@ impl Page {
Message::Error(why) => { Message::Error(why) => {
tracing::error!(why, "failed to set timezone"); tracing::error!(why, "failed to set timezone");
self.timezone_context = false;
return cosmic::command::message(crate::Message::CloseContextDrawer);
} }
Message::UpdateTime => self.update_local_time(), Message::UpdateTime => {
self.set_ntp(true);
self.update_local_time();
self.timezone_context = false;
return cosmic::command::message(crate::Message::CloseContextDrawer);
}
Message::Refresh(info) => { Message::Refresh(info) => {
self.ntp_enabled = info.ntp_enabled; self.ntp_enabled = info.ntp_enabled;
@ -269,6 +251,30 @@ impl Page {
Command::none() Command::none()
} }
fn set_ntp(&mut self, enable: bool) {
self.ntp_enabled = enable;
tokio::task::spawn(async move {
let client = match zbus::Connection::system().await {
Ok(client) => client,
Err(why) => {
tracing::error!(?why, "zbus client error");
return;
}
};
let timedate_proxy = match TimeDateProxy::new(&client).await {
Ok(timedate_proxy) => timedate_proxy,
Err(why) => {
tracing::error!(?why, "zbus client error");
return;
}
};
_ = timedate_proxy.set_ntp(enable, true).await;
});
}
fn timezone_context_view(&self) -> Element<'_, crate::pages::Message> { fn timezone_context_view(&self) -> Element<'_, crate::pages::Message> {
let search = widget::search_input(fl!("type-to-search"), &self.timezone_search) let search = widget::search_input(fl!("type-to-search"), &self.timezone_search)
.on_input(Message::TimezoneSearch) .on_input(Message::TimezoneSearch)
@ -285,9 +291,10 @@ impl Page {
} }
widget::column() widget::column()
.padding([2, 0])
.spacing(32) .spacing(32)
.push(search) .push(search)
.push(widget::container(list).apply(widget::container)) .push(list)
.apply(Element::from) .apply(Element::from)
.map(crate::pages::Message::DateAndTime) .map(crate::pages::Message::DateAndTime)
} }
@ -314,7 +321,6 @@ impl Page {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum Message { pub enum Message {
Automatic(bool),
Error(String), Error(String),
MilitaryTime(bool), MilitaryTime(bool),
None, None,
@ -341,13 +347,10 @@ fn date() -> Section<crate::pages::Message> {
.view::<Page>(move |_binder, page, section| { .view::<Page>(move |_binder, page, section| {
settings::view_section(&section.title) settings::view_section(&section.title)
.add( .add(
settings::item::builder(&*section.descriptions[auto]) settings::item::builder(&*section.descriptions[title])
.toggler(page.ntp_enabled, Message::Automatic), .description(fl!("time-date", "auto-ntp"))
.control(widget::text(&page.formatted_date)),
) )
.add(settings::item(
&*section.descriptions[title],
widget::text(&page.formatted_date),
))
.apply(cosmic::Element::from) .apply(cosmic::Element::from)
.map(crate::pages::Message::DateAndTime) .map(crate::pages::Message::DateAndTime)
}) })
@ -409,24 +412,22 @@ fn timezone() -> Section<crate::pages::Message> {
.title(fl!("time-zone")) .title(fl!("time-zone"))
.descriptions(descriptions) .descriptions(descriptions)
.view::<Page>(move |_binder, page, section| { .view::<Page>(move |_binder, page, section| {
let timezone_context_button = settings::item_row(vec![ let timezone_context_button = widget::row::with_capacity(2)
widget::text( .spacing(12)
page.timezone .push(
.map(|id| &*page.timezone_list[id]) widget::text(
.unwrap_or_default(), page.timezone
.map(|id| &*page.timezone_list[id])
.unwrap_or_default(),
)
.wrap(Wrap::Word),
) )
.wrap(Wrap::Word) .push(widget::icon::from_name("go-next-symbolic").size(16).icon())
.into(), .apply(widget::container)
widget::icon::from_name("go-next-symbolic") .style(cosmic::theme::Container::List)
.size(16) .apply(widget::button)
.icon() .style(cosmic::theme::Button::Transparent)
.into(), .on_press(Message::TimezoneContext);
])
.apply(widget::container)
.style(cosmic::theme::Container::List)
.apply(widget::button)
.style(cosmic::theme::Button::Transparent)
.on_press(Message::TimezoneContext);
settings::view_section(&section.title) settings::view_section(&section.title)
// Time zone select // Time zone select

View file

@ -523,6 +523,7 @@ time = Time & Language
time-date = Date & Time time-date = Date & Time
.desc = Time zone, automatic clock settings, and some time formatting. .desc = Time zone, automatic clock settings, and some time formatting.
.auto = Set automatically .auto = Set automatically
.auto-ntp = Date & time will update automatically when the time zone is set.
time-zone = Time Zone time-zone = Time Zone
.auto = Automatic time zone .auto = Automatic time zone