From 183005da531e27793f20f806b07878639850bc9b Mon Sep 17 00:00:00 2001 From: Eduardo Flores Date: Fri, 1 Nov 2024 20:41:08 +0000 Subject: [PATCH] feat: about section - users can now specify information about their app and render it. --- Cargo.toml | 1 + src/app/cosmic.rs | 5 +++ src/app/mod.rs | 106 ++++++++++++++++++++++++++++++++++++++++++-- src/widget/about.rs | 42 ++++++++++++++++++ src/widget/mod.rs | 3 ++ 5 files changed, 154 insertions(+), 3 deletions(-) create mode 100644 src/widget/about.rs diff --git a/Cargo.toml b/Cargo.toml index 3f225059..995c7d22 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -97,6 +97,7 @@ image = { version = "0.25.1", optional = true } lazy_static = "1.4.0" libc = { version = "0.2.155", optional = true } mime = { version = "0.3.17", optional = true } +open = "5.3.0" palette = "0.7.3" rfd = { version = "0.14.0", optional = true } rustix = { version = "0.38.34", features = [ diff --git a/src/app/cosmic.rs b/src/app/cosmic.rs index a2b229e6..c5a30e20 100644 --- a/src/app/cosmic.rs +++ b/src/app/cosmic.rs @@ -78,6 +78,7 @@ pub enum Message { /// Tracks updates to window suggested size. #[cfg(feature = "applet")] SuggestedBounds(Option), + OpenUrl(String), } #[derive(Default)] @@ -661,6 +662,10 @@ impl Cosmic { let core = self.app.core_mut(); core.applet.suggested_bounds = b; } + Message::OpenUrl(url) => match open::that_detached(url) { + Ok(_) => (), + Err(err) => tracing::error!("{err}"), + }, _ => {} } diff --git a/src/app/mod.rs b/src/app/mod.rs index 8554c8b3..c9daef02 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -52,13 +52,13 @@ use std::borrow::Cow; pub use self::command::Task; pub use self::core::Core; pub use self::settings::Settings; -use crate::prelude::*; use crate::theme::THEME; use crate::widget::{ container, context_drawer, horizontal_space, id_container, menu, nav_bar, popover, }; +use crate::{prelude::*, widget}; use apply::Apply; -use iced::window; +use iced::{window, Alignment}; use iced::{Length, Subscription}; pub use message::Message; use url::Url; @@ -67,10 +67,14 @@ use { iced_futures::futures::channel::mpsc::{Receiver, Sender}, iced_futures::futures::SinkExt, std::any::TypeId, - std::collections::HashMap, zbus::{interface, proxy, zvariant::Value}, }; +#[cfg(feature = "desktop")] +use crate::widget::About; +#[cfg(any(feature = "single-instance", feature = "desktop"))] +use std::collections::HashMap; + pub(crate) fn iced_settings( settings: Settings, flags: App::Flags, @@ -591,6 +595,102 @@ where panic!("no view for window {id:?}"); } + #[cfg(feature = "desktop")] + /// Provides information about the application. + fn about(&self) -> Option { + None + } + + fn about_view(&self) -> Element { + if let Some(about) = self.about() { + let spacing = crate::theme::active().cosmic().spacing; + + let application_name = about.application_name.map(widget::text::title3); + let application_icon = about + .application_icon + .map(|icon| crate::desktop::IconSource::Name(icon).as_cosmic_icon()); + + let mut links: HashMap = HashMap::new(); + + if let Some(website) = about.website { + links.insert("Website".into(), website); + } + + if let Some(repository_url) = about.repository_url { + links.insert("Repository".into(), repository_url); + } + + if let Some(support_url) = about.support_url { + links.insert("Support".into(), support_url); + } + + let links_section = self.maintainer_section(links, "Links"); + let developers_section = self.maintainer_section(about.developers, "Developers"); + let designers_section = self.maintainer_section(about.designers, "Designers"); + let artists_section = self.maintainer_section(about.artists, "Artists"); + let translators_section = self.maintainer_section(about.translators, "Translators"); + let documenters_section = self.maintainer_section(about.documenters, "Documenters"); + + let developer_name = about.developer_name.map(widget::text); + let version = about.version.map(widget::button::standard); + let license = about.license_type.map(widget::button::standard); + let copyright = about.copyright.map(widget::text::body); + let comments = about.comments.map(widget::text::body); + + widget::column() + .push_maybe(application_icon) + .push_maybe(application_name) + .push_maybe(developer_name) + .push( + widget::row() + .push_maybe(version) + .push_maybe(license) + .spacing(spacing.space_xxs), + ) + .push_maybe(links_section) + .push_maybe(developers_section) + .push_maybe(designers_section) + .push_maybe(artists_section) + .push_maybe(translators_section) + .push_maybe(documenters_section) + .push_maybe(comments) + .push_maybe(copyright) + .align_x(Alignment::Center) + .spacing(spacing.space_xxs) + .width(Length::Fill) + .into() + } else { + crate::widget::text("Test").into() + } + } + + fn maintainer_section<'a>( + &'a self, + list: HashMap, + title: &'a str, + ) -> Option> { + let developers_section = if list.is_empty() { + None + } else { + let developers: Vec> = list + .into_iter() + .map(|(name, email)| { + widget::button::link(name) + .on_press(crate::app::cosmic::Message::OpenUrl(format!( + "mailto:{email}" + ))) + .into() + }) + .collect(); + let mut developers_section = widget::settings::section().title(title); + for developer in developers { + developers_section = developers_section.add(developer); + } + Some(developers_section) + }; + developers_section + } + /// Overrides the default style for applications fn style(&self) -> Option { None diff --git a/src/widget/about.rs b/src/widget/about.rs new file mode 100644 index 00000000..4d870500 --- /dev/null +++ b/src/widget/about.rs @@ -0,0 +1,42 @@ +use crate::{widget, Element}; + +#[cfg(feature = "desktop")] +use std::collections::HashMap; + +#[cfg(feature = "desktop")] +#[derive(Debug, Default, Clone, derive_setters::Setters)] +#[setters(prefix = "set_", into, strip_option)] +pub struct About { + /// The application's name. + pub application_name: Option, + /// The application's icon name. + pub application_icon: Option, + /// Artists who contributed to the application. + pub artists: HashMap, + /// Comments about the application. + pub comments: Option, + /// The application's copyright. + pub copyright: Option, + /// Designers who contributed to the application. + pub designers: HashMap, + /// Name of the application's developer. + pub developer_name: Option, + /// Developers who contributed to the application. + pub developers: HashMap, + /// Documenters who contributed to the application. + pub documenters: HashMap, + /// The license text. + pub license: Option, + /// The license from a list of known licenses. + pub license_type: Option, + /// The URL of the application’s support page. + pub support_url: Option, + /// The URL of the application’s repository. + pub repository_url: Option, + /// Translators who contributed to the application. + pub translators: HashMap, + /// The application’s version. + pub version: Option, + /// The application’s website. + pub website: Option, +} diff --git a/src/widget/mod.rs b/src/widget/mod.rs index ecce1a86..8ad7190f 100644 --- a/src/widget/mod.rs +++ b/src/widget/mod.rs @@ -361,3 +361,6 @@ pub mod tooltip { pub mod warning; #[doc(inline)] pub use warning::*; + +pub mod about; +pub use about::*;