From 9260ee87e1e55f8f35a0a87106f358430cf12f15 Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Sat, 12 Nov 2022 01:55:37 +0100 Subject: [PATCH] helpers --- Cargo.toml | 5 +- examples/cosmic-applet-graphics/Cargo.toml | 5 +- examples/cosmic-applet-graphics/src/dbus.rs | 5 +- examples/cosmic-applet-graphics/src/window.rs | 94 +++++++------------ src/applet/mod.rs | 65 +++++++++++++ src/lib.rs | 2 + src/theme/mod.rs | 2 +- 7 files changed, 112 insertions(+), 66 deletions(-) create mode 100644 src/applet/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 04d451a6..4711d770 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,10 +7,11 @@ edition = "2021" name = "cosmic" [features] -default = ["wayland"] +default = ["wgpu"] debug = ["iced/debug"] wayland = ["iced/wayland"] wgpu = ["iced/wgpu"] +applet = ["cosmic-panel-config", "sctk"] [dependencies] freedesktop-icons = "0.2.1" @@ -18,6 +19,8 @@ apply = "0.3.0" derive_setters = "0.1.5" lazy_static = "1.4.0" palette = "0.6.1" +cosmic-panel-config = {git = "https://github.com/pop-os/cosmic-panel", default-features = false, optional = true } +sctk = { package = "smithay-client-toolkit", git = "https://github.com/Smithay/client-toolkit", optional = true } [dependencies.cosmic-theme] git = "https://github.com/pop-os/cosmic-theme.git" diff --git a/examples/cosmic-applet-graphics/Cargo.toml b/examples/cosmic-applet-graphics/Cargo.toml index 35e34e9c..d712985f 100644 --- a/examples/cosmic-applet-graphics/Cargo.toml +++ b/examples/cosmic-applet-graphics/Cargo.toml @@ -7,8 +7,7 @@ edition = "2021" [dependencies] zbus = "3.4" -libcosmic = { path = "../..", default-features = false, features = ["debug", "wayland"] } -# cosmic-panel-config = {git = "https://github.com/pop-os/cosmic-panel" } -cosmic-panel-config = { path = "../../../cosmic-panel/cosmic-panel-config", default-features = false } +libcosmic = { path = "../..", default-features = false, features = ["debug", "wayland", "applet"] } +cosmic-panel-config = {git = "https://github.com/pop-os/cosmic-panel", default-features = false } iced_sctk = { git = "https://github.com/pop-os/iced-sctk" } sctk = { package = "smithay-client-toolkit", git = "https://github.com/Smithay/client-toolkit" } diff --git a/examples/cosmic-applet-graphics/src/dbus.rs b/examples/cosmic-applet-graphics/src/dbus.rs index ee0a3479..2ec4aa7d 100644 --- a/examples/cosmic-applet-graphics/src/dbus.rs +++ b/examples/cosmic-applet-graphics/src/dbus.rs @@ -20,7 +20,10 @@ use zbus::{dbus_proxy, Connection}; -#[dbus_proxy(interface = "com.system76.PowerDaemon", default_path = "/com/system76/PowerDaemon")] +#[dbus_proxy( + interface = "com.system76.PowerDaemon", + default_path = "/com/system76/PowerDaemon" +)] trait PowerDaemon { /// Balanced method fn balanced(&self) -> zbus::Result<()>; diff --git a/examples/cosmic-applet-graphics/src/window.rs b/examples/cosmic-applet-graphics/src/window.rs index 8d0895c3..75a78766 100644 --- a/examples/cosmic-applet-graphics/src/window.rs +++ b/examples/cosmic-applet-graphics/src/window.rs @@ -1,27 +1,17 @@ use crate::dbus::{self, PowerDaemonProxy}; -use crate::graphics::{get_current_graphics, Graphics, set_graphics}; -use cosmic::widget::{expander, icon, nav_bar, nav_bar_page, nav_bar_section}; +use crate::graphics::{get_current_graphics, set_graphics, Graphics}; +use cosmic::applet::{get_popup_settings, icon_button}; +use cosmic::separator; use cosmic::{ - iced::widget::{ - checkbox, column, container, horizontal_space, pick_list, progress_bar, radio, row, slider, - text, - }, - iced::{self, Alignment, Application, Color, Command, Length}, - iced_lazy::responsive, + iced::widget::{column, radio, text}, + iced::{self, Application, Command, Length}, iced_native::window, - iced_winit::window::{drag, maximize, minimize}, - list_view, list_view_item, list_view_row, list_view_section, scrollable, theme::{self, Theme}, - widget::{button, header_bar, list_box, list_row, list_view::*, toggler}, Element, }; -use cosmic::{iced_native, separator}; -use cosmic_panel_config::{PanelSize, PanelAnchor}; +use cosmic_panel_config::{PanelAnchor, PanelSize}; use iced_sctk::alignment::Horizontal; -use iced_sctk::command::platform_specific::wayland::popup::{SctkPopupSettings, SctkPositioner}; use iced_sctk::commands::popup::{destroy_popup, get_popup}; -use iced_sctk::{Point, Rectangle, Size}; -use sctk::reexports::protocols::xdg::shell::client::xdg_positioner::{Anchor, Gravity}; use zbus::Connection; #[derive(Default, Clone, Copy)] @@ -74,12 +64,12 @@ impl Application for Window { .unwrap_or(16); window.icon_size = pixels; window.anchor = std::env::var("COSMIC_PANEL_ANCHOR") - .ok() - .map(|size| match size.parse::() { - Ok(p) => p, - Err(_) => PanelAnchor::Top, - }) - .unwrap_or(PanelAnchor::Top); + .ok() + .map(|size| match size.parse::() { + Ok(p) => p, + Err(_) => PanelAnchor::Top, + }) + .unwrap_or(PanelAnchor::Top); ( window, Command::perform(dbus::init(), |dbus_init| Message::DBusInit(dbus_init)), @@ -96,9 +86,12 @@ impl Application for Window { dbg!(new_graphics_mode); if let Some((_, proxy)) = self.dbus.as_ref() { self.state = State::SettingGraphicsMode(new_graphics_mode); - return Command::perform(set_graphics(proxy.clone(), new_graphics_mode), move |success| { - Message::AppliedGraphicsMode(success.ok().map(|_| new_graphics_mode)) - },); + return Command::perform( + set_graphics(proxy.clone(), new_graphics_mode), + move |success| { + Message::AppliedGraphicsMode(success.ok().map(|_| new_graphics_mode)) + }, + ); } } Message::AppliedGraphicsMode(g) => { @@ -107,7 +100,7 @@ impl Application for Window { self.graphics_mode.replace(g); self.state = State::SelectGraphicsMode; } - }, + } Message::TogglePopup => { if let Some(p) = self.popup.take() { return destroy_popup(p); @@ -122,31 +115,8 @@ impl Application for Window { |cur_graphics| Message::CurrentGraphics(cur_graphics.ok()), )); } - let (anchor, gravity) = match self.anchor { - PanelAnchor::Left => (Anchor::Right, Gravity::Right), - PanelAnchor::Right => (Anchor::Left, Gravity::Left), - PanelAnchor::Top => (Anchor::Bottom, Gravity::Bottom), - PanelAnchor::Bottom => (Anchor::Top, Gravity::Top), - }; - commands.push(Command::batch(vec![get_popup(SctkPopupSettings { - parent: window::Id::new(0), - id: new_id, - positioner: SctkPositioner { - anchor, - gravity, - size: (200, 200), - anchor_rect: Rectangle { - x: 0, - y: 0, - width: 32 + self.icon_size as i32, - height: 16 + self.icon_size as i32, - }, - reactive: true, - ..Default::default() - }, - parent_size: None, - grab: true, - })])); + let popup_settings = get_popup_settings(window::Id::new(0), new_id, None, None); + commands.push(get_popup(popup_settings)); return Command::batch(commands); } } @@ -160,12 +130,13 @@ impl Application for Window { Err(err) => { dbg!(err); None - }, + } }) }, ); } Message::CurrentGraphics(g) => { + dbg!(g); if let Some(g) = g { self.graphics_mode.replace(g); } @@ -216,10 +187,14 @@ impl Application for Window { Graphics::Nvidia => "nvidia", Graphics::Compute => "compute", }; - column(vec![ - text(format!("Setting graphics mode to {graphics_str}...")).width(Length::Fill).horizontal_alignment(Horizontal::Center).into() - ]).into() - }, + column(vec![text(format!( + "Setting graphics mode to {graphics_str}..." + )) + .width(Length::Fill) + .horizontal_alignment(Horizontal::Center) + .into()]) + .into() + } }; column(vec![ text("Graphics Mode") @@ -251,12 +226,11 @@ impl Application for Window { unimplemented!() } - fn view_window(&self, id: window::Id) -> Element { + fn view_window(&self, _: window::Id) -> Element { // TODO use panel config crate after resolving version mismatch - button!(icon("input-gaming-symbolic", self.icon_size).style(theme::Svg::Accent)) - .on_press(Message::TogglePopup) - .into() + let btn = icon_button().on_press(Message::TogglePopup); + btn.into() } fn should_exit(&self) -> bool { diff --git a/src/applet/mod.rs b/src/applet/mod.rs new file mode 100644 index 00000000..4eac4476 --- /dev/null +++ b/src/applet/mod.rs @@ -0,0 +1,65 @@ +use cosmic_panel_config::{PanelSize, PanelAnchor}; +use iced::{widget::Button, Rectangle}; +use iced_native::{command::platform_specific::wayland::popup::{SctkPopupSettings, SctkPositioner}}; +use sctk::reexports::protocols::xdg::shell::client::xdg_positioner::{Anchor, Gravity}; + +use crate::{button, widget::icon}; + +pub fn icon_button<'a, M: 'a, Renderer>() -> Button<'a, M, Renderer> +where + Renderer::Theme: iced_native::svg::StyleSheet + iced_style::button::StyleSheet, + Renderer: iced_native::Renderer + iced_native::svg::Renderer + 'a, +{ + let pixels = std::env::var("COSMIC_PANEL_SIZE") + .ok() + .and_then(|size| match size.parse::() { + Ok(PanelSize::XL) => Some(64), + Ok(PanelSize::L) => Some(36), + Ok(PanelSize::M) => Some(24), + Ok(PanelSize::S) => Some(16), + Ok(PanelSize::XS) => Some(12), + Err(_) => Some(12), + }) + .unwrap_or(16); + button!(icon("input-gaming-symbolic", pixels)) +} + +pub fn get_popup_settings(parent: iced_native::window::Id, id: iced_native::window::Id, width_padding: Option, height_padding: Option) -> SctkPopupSettings { + let anchor = std::env::var("COSMIC_PANEL_ANCHOR") + .ok() + .map(|size| match size.parse::() { + Ok(p) => p, + Err(_) => PanelAnchor::Top, + }) + .unwrap_or(PanelAnchor::Top); + let pixels = std::env::var("COSMIC_PANEL_SIZE") + .ok() + .and_then(|size| match size.parse::() { + Ok(PanelSize::XL) => Some(64), + Ok(PanelSize::L) => Some(36), + Ok(PanelSize::M) => Some(24), + Ok(PanelSize::S) => Some(16), + Ok(PanelSize::XS) => Some(12), + Err(_) => Some(12), + }) + .unwrap_or(16); + let (anchor, gravity) = match anchor { + PanelAnchor::Left => (Anchor::Right, Gravity::Right), + PanelAnchor::Right => (Anchor::Left, Gravity::Left), + PanelAnchor::Top => (Anchor::Bottom, Gravity::Bottom), + PanelAnchor::Bottom => (Anchor::Top, Gravity::Top), + }; + SctkPopupSettings { parent, id, positioner: SctkPositioner { + anchor, + gravity, + size: (200, 200), + anchor_rect: Rectangle { + x: 0, + y: 0, + width: width_padding.unwrap_or(16) * 2 + pixels as i32, + height: height_padding.unwrap_or(8) * 2 + pixels as i32, + }, + reactive: true, + ..Default::default() + }, parent_size: None, grab: true } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index c6dd88f3..ec69fb4c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,8 @@ pub use iced_native; pub use iced_style; pub use iced_winit; +#[cfg(feature = "applet")] +pub mod applet; pub mod font; pub mod theme; pub mod widget; diff --git a/src/theme/mod.rs b/src/theme/mod.rs index acd385ec..3e9b1b96 100644 --- a/src/theme/mod.rs +++ b/src/theme/mod.rs @@ -429,7 +429,7 @@ impl pick_list::StyleSheet for Theme { impl radio::StyleSheet for Theme { type Style = (); - fn active(&self, _style: &Self::Style, is_checked: bool) -> radio::Appearance { + fn active(&self, _style: &Self::Style, _is_checked: bool) -> radio::Appearance { let palette = self.extended_palette(); radio::Appearance {