cosmic-applets/applets/cosmic-applet-power/src/main.rs

105 lines
3.2 KiB
Rust
Raw Normal View History

2022-05-26 11:59:09 -04:00
// SPDX-License-Identifier: GPL-3.0-or-later
2022-02-18 10:31:12 -05:00
#[macro_use]
extern crate relm4_macros;
pub mod session_manager;
2022-02-18 12:10:01 -05:00
pub mod ui;
2022-06-29 13:32:50 -07:00
use cosmic_panel_config::config::CosmicPanelConfig;
2022-06-10 18:10:26 -07:00
use gtk4::{gio::ApplicationFlags, glib, prelude::*, Align, Button, Label, Orientation, Separator};
2022-02-18 10:31:12 -05:00
use once_cell::sync::Lazy;
2022-06-10 18:10:26 -07:00
use std::process::Command;
2022-02-18 10:31:12 -05:00
use tokio::runtime::Runtime;
static RT: Lazy<Runtime> = Lazy::new(|| Runtime::new().expect("failed to build tokio runtime"));
fn main() {
let application = gtk4::Application::new(
Some("com.system76.cosmic.applets.power"),
ApplicationFlags::default(),
);
application.connect_activate(build_ui);
application.run();
}
fn build_ui(application: &gtk4::Application) {
2022-05-26 15:37:59 -04:00
let provider = gtk4::CssProvider::new();
provider.load_from_data(include_bytes!("style.css"));
gtk4::StyleContext::add_provider_for_display(
&gtk4::gdk::Display::default().expect("Could not connect to a display."),
&provider,
gtk4::STYLE_PROVIDER_PRIORITY_APPLICATION,
);
2022-02-18 10:31:12 -05:00
let window = gtk4::ApplicationWindow::builder()
.application(application)
.title("COSMIC Power Applet")
.decorated(false)
2022-05-26 15:37:59 -04:00
.resizable(false)
.width_request(1)
.height_request(1)
.css_classes(vec!["root_window".to_string()])
2022-02-18 10:31:12 -05:00
.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,
2022-05-26 15:37:59 -04:00
add_css_class: "icon_box",
}
}
button.set_child(Some(&image));
2022-02-21 13:02:50 -05:00
let session_section = ui::session::build();
2022-02-18 12:10:01 -05:00
let system_section = ui::system::build();
2022-02-18 10:31:12 -05:00
view! {
main_box = gtk4::Box {
set_orientation: Orientation::Vertical,
set_spacing: 10,
set_margin_top: 20,
set_margin_bottom: 20,
set_margin_start: 24,
2022-02-18 12:10:01 -05:00
set_margin_end: 24,
2022-06-10 18:10:26 -07:00
append: settings_button = &Button {
#[wrap(Some)]
set_child = &Label {
2022-06-10 18:10:26 -07:00
set_label: "Settings...",
set_halign: Align::Start,
set_hexpand: true
},
connect_clicked => move |_| {
let _ = Command::new("cosmic-settings").spawn();
}
},
append = &Separator {},
2022-02-21 13:02:50 -05:00
append: &session_section,
2022-02-18 12:10:01 -05:00
append: second_separator = &Separator {},
append: &system_section
2022-02-18 10:31:12 -05:00
}
}
popover.set_child(Some(&main_box));
2022-02-18 10:31:12 -05:00
icon_box.append(&button);
icon_box.append(&popover);
window.set_child(Some(&icon_box));
2022-02-18 10:31:12 -05:00
window.show();
}