power: Use libcosmic-applet

This commit is contained in:
Ian Douglas Scott 2022-07-01 22:29:06 -07:00
parent b8bea240a8
commit f8e7685939
3 changed files with 31 additions and 76 deletions

2
Cargo.lock generated
View file

@ -411,9 +411,9 @@ dependencies = [
name = "cosmic-applet-power" name = "cosmic-applet-power"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"cosmic-panel-config",
"futures-util", "futures-util",
"gtk4", "gtk4",
"libcosmic-applet",
"logind-zbus", "logind-zbus",
"nix 0.24.1", "nix 0.24.1",
"once_cell", "once_cell",

View file

@ -7,10 +7,10 @@ license = "GPL-3.0-or-later"
[dependencies] [dependencies]
futures-util = "0.3.21" futures-util = "0.3.21"
gtk4 = { git = "https://github.com/gtk-rs/gtk4-rs" } gtk4 = { git = "https://github.com/gtk-rs/gtk4-rs" }
libcosmic-applet = { path = "../../libcosmic-applet" }
logind-zbus = "3.0.1" logind-zbus = "3.0.1"
nix = "0.24.1" nix = "0.24.1"
once_cell = "1.9.0" once_cell = "1.9.0"
relm4-macros = { git = "https://github.com/Relm4/Relm4.git", branch = "next" } relm4-macros = { git = "https://github.com/Relm4/Relm4.git", branch = "next" }
tokio = { version = "1.15.0", features = ["full"] } tokio = { version = "1.15.0", features = ["full"] }
zbus = "2.0.1" zbus = "2.0.1"
cosmic-panel-config = {git = "https://github.com/pop-os/cosmic-panel", features = ["gtk4"]}

View file

@ -6,8 +6,7 @@ extern crate relm4_macros;
pub mod session_manager; pub mod session_manager;
pub mod ui; pub mod ui;
use cosmic_panel_config::config::CosmicPanelConfig; use gtk4::{gio::ApplicationFlags, prelude::*, Align, Button, Label, Orientation, Separator};
use gtk4::{gio::ApplicationFlags, glib, prelude::*, Align, Button, Label, Orientation, Separator};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use std::process::Command; use std::process::Command;
use tokio::runtime::Runtime; use tokio::runtime::Runtime;
@ -24,54 +23,14 @@ fn main() {
} }
fn build_ui(application: &gtk4::Application) { fn build_ui(application: &gtk4::Application) {
let provider = gtk4::CssProvider::new(); view! {
provider.load_from_data(include_bytes!("style.css")); window = libcosmic_applet::Applet {
gtk4::StyleContext::add_provider_for_display( set_title: Some("COSMIC Power Applet"),
&gtk4::gdk::Display::default().expect("Could not connect to a display."), set_application: Some(application),
&provider,
gtk4::STYLE_PROVIDER_PRIORITY_APPLICATION,
);
let window = gtk4::ApplicationWindow::builder()
.application(application)
.title("COSMIC Power Applet")
.decorated(false)
.resizable(false)
.width_request(1)
.height_request(1)
.css_classes(vec!["root_window".to_string()])
.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 // TODO adjust battery icon based on charge
let image = gtk4::Image::from_icon_name("system-shutdown-symbolic"); set_button_icon_name: "system-shutdown-symbolic",
image.add_css_class("panel_icon"); #[wrap(Some)]
image.set_pixel_size(config.get_applet_icon_size().try_into().unwrap()); set_popover_child: main_box = &gtk4::Box {
view! {
icon_box = gtk4::Box {
set_orientation: Orientation::Vertical,
set_spacing: 0,
add_css_class: "icon_box",
}
}
button.set_child(Some(&image));
let session_section = ui::session::build();
let system_section = ui::system::build();
view! {
main_box = gtk4::Box {
set_orientation: Orientation::Vertical, set_orientation: Orientation::Vertical,
set_spacing: 10, set_spacing: 10,
set_margin_top: 20, set_margin_top: 20,
@ -90,15 +49,11 @@ fn build_ui(application: &gtk4::Application) {
} }
}, },
append = &Separator {}, append = &Separator {},
append: &session_section, append: &ui::session::build(),
append: second_separator = &Separator {}, append: second_separator = &Separator {},
append: &system_section append: &ui::system::build(),
}
} }
} }
popover.set_child(Some(&main_box));
icon_box.append(&button);
icon_box.append(&popover);
window.set_child(Some(&icon_box));
window.show(); window.show();
} }