From 1958341a725b590d7c5ca05ddaec8c66ddf4c11e Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Thu, 26 May 2022 12:52:34 -0400 Subject: [PATCH] refactor: power applet popover and icon button~ --- applets/cosmic-applet-power/Cargo.toml | 1 + applets/cosmic-applet-power/src/main.rs | 39 +++++++++++++++++++++---- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/applets/cosmic-applet-power/Cargo.toml b/applets/cosmic-applet-power/Cargo.toml index ad0b4668..7c0b62c2 100644 --- a/applets/cosmic-applet-power/Cargo.toml +++ b/applets/cosmic-applet-power/Cargo.toml @@ -13,3 +13,4 @@ once_cell = "1.9.0" relm4-macros = "0.4.1" tokio = { version = "1.15.0", features = ["full"] } zbus = "2.0.1" +cosmic-panel-config = {git = "https://github.com/pop-os/cosmic-panel", features = ["gtk4"]} diff --git a/applets/cosmic-applet-power/src/main.rs b/applets/cosmic-applet-power/src/main.rs index 12dfa7ce..8f55cff1 100644 --- a/applets/cosmic-applet-power/src/main.rs +++ b/applets/cosmic-applet-power/src/main.rs @@ -6,9 +6,10 @@ extern crate relm4_macros; pub mod session_manager; pub mod ui; -use gtk4::{gio::ApplicationFlags, prelude::*, Orientation, Separator}; +use gtk4::{glib, gio::ApplicationFlags, prelude::*, Orientation, Separator}; use once_cell::sync::Lazy; use tokio::runtime::Runtime; +use cosmic_panel_config::config::CosmicPanelConfig; static RT: Lazy = Lazy::new(|| Runtime::new().expect("failed to build tokio runtime")); @@ -25,10 +26,36 @@ fn build_ui(application: >k4::Application) { let window = gtk4::ApplicationWindow::builder() .application(application) .title("COSMIC Power Applet") - .default_width(400) - .default_height(300) + .default_width(1) + .default_height(1) + .decorated(false) .build(); + let config = CosmicPanelConfig::load_from_env().unwrap_or_default(); + let popover = gtk4::builders::PopoverBuilder::new() + .autohide(true) + .has_arrow(false) + .build(); + + let button = gtk4::Button::new(); + button.add_css_class("panel_icon"); + button.connect_clicked(glib::clone!(@weak popover => move |_| { + popover.show(); + })); + + // TODO cleanup + // TODO adjust battery icon based on charge + let image = gtk4::Image::from_icon_name("battery-full-symbolic"); + image.add_css_class("panel_icon"); + image.set_pixel_size(config.get_applet_icon_size().try_into().unwrap()); + view! { + icon_box = gtk4::Box { + set_orientation: Orientation::Vertical, + set_spacing: 0, + } + } + button.set_child(Some(&image)); + let session_section = ui::session::build(); let system_section = ui::system::build(); view! { @@ -39,13 +66,15 @@ fn build_ui(application: >k4::Application) { set_margin_bottom: 20, set_margin_start: 24, set_margin_end: 24, - append: first_separator = &Separator {}, append: &session_section, append: second_separator = &Separator {}, append: &system_section } } - window.set_child(Some(&main_box)); + popover.set_child(Some(&main_box)); + icon_box.append(&button); + icon_box.append(&popover); + window.set_child(Some(&icon_box)); window.show(); }