feat(about): editable hostname input

This commit is contained in:
Michael Aaron Murphy 2024-04-10 15:18:25 +02:00 • committed by Michael Murphy
parent 44a333d070
commit 9963844b58
6 changed files with 294 additions and 155 deletions

View file

@ -48,6 +48,9 @@ itoa = "1.0.10"
futures = { package = "futures-lite", version = "2.2.0" }
xkb-data = "0.1.0"
udev = "0.8.0"
hostname-validator = "1.1.1"
hostname1-zbus = "0.1.0"
zbus = { version = "3.15.2", features = ["tokio"] }
[dependencies.i18n-embed]
version = "0.14.1"

View file

@ -1,22 +1,26 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only
use cosmic::iced::Length;
use cosmic_settings_page::{self as page, section, Section};
use cosmic::widget::{list_column, settings, text};
use cosmic::{command, Command};
use cosmic::widget::{self, editable_input, list_column, settings, text};
use cosmic::{command, Apply, Command};
use cosmic_settings_system::about::Info;
use slotmap::SlotMap;
#[derive(Clone, Debug)]
pub enum Message {
HostnameEdit(bool),
HostnameInput(String),
HostnameSubmit,
Info(Box<Info>),
}
#[derive(Clone, Debug, Default)]
pub struct Page {
editing_device_name: bool,
info: Info,
// support_page: page::Entity,
}
impl page::AutoBind<crate::pages::Message> for Page {}
@ -50,6 +54,48 @@ impl page::Page<crate::pages::Message> for Page {
impl Page {
pub fn update(&mut self, message: Message) {
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");
}
});
}
}
Message::Info(info) => self.info = *info,
}
}
@ -63,11 +109,28 @@ fn device() -> Section<crate::pages::Message> {
])
.view::<Page>(|_binder, page, section| {
let desc = &section.descriptions;
let hostname_input = editable_input(
"",
&page.info.device_name,
page.editing_device_name,
Message::HostnameEdit,
)
.on_input(Message::HostnameInput)
.on_submit(Message::HostnameSubmit);
let hostname_row = widget::row::with_capacity(2)
.push(widget::horizontal_space(Length::Fill))
.push(hostname_input);
let device_name = settings::item::builder(&*desc[0])
.description(&*desc[1])
.control(text(&page.info.device_name));
.control(hostname_row);
list_column().add(device_name).into()
list_column()
.add(device_name)
.apply(cosmic::Element::from)
.map(crate::pages::Message::About)
})
}