diff --git a/applets/cosmic-applet-power/src/main.rs b/applets/cosmic-applet-power/src/main.rs index 6c4a7776..e65378ee 100644 --- a/applets/cosmic-applet-power/src/main.rs +++ b/applets/cosmic-applet-power/src/main.rs @@ -3,6 +3,8 @@ #[macro_use] extern crate relm4_macros; +pub mod ui; + use gtk4::{gio::ApplicationFlags, prelude::*, Orientation, Separator}; use once_cell::sync::Lazy; use tokio::runtime::Runtime; @@ -26,6 +28,7 @@ fn build_ui(application: >k4::Application) { .default_height(300) .build(); + let system_section = ui::system::build(); view! { main_box = gtk4::Box { set_orientation: Orientation::Vertical, @@ -33,7 +36,10 @@ fn build_ui(application: >k4::Application) { set_margin_top: 20, set_margin_bottom: 20, set_margin_start: 24, - set_margin_end: 24 + set_margin_end: 24, + append: first_separator = &Separator {}, + append: second_separator = &Separator {}, + append: &system_section } } window.set_child(Some(&main_box)); diff --git a/applets/cosmic-applet-power/src/ui.rs b/applets/cosmic-applet-power/src/ui.rs new file mode 100644 index 00000000..4e00afaf --- /dev/null +++ b/applets/cosmic-applet-power/src/ui.rs @@ -0,0 +1,4 @@ +// SPDX-License-Identifier: LGPL-3.0-or-later + +pub mod session; +pub mod system; diff --git a/applets/cosmic-applet-power/src/ui/session.rs b/applets/cosmic-applet-power/src/ui/session.rs new file mode 100644 index 00000000..40f1a64e --- /dev/null +++ b/applets/cosmic-applet-power/src/ui/session.rs @@ -0,0 +1 @@ +// SPDX-License-Identifier: LGPL-3.0-or-later diff --git a/applets/cosmic-applet-power/src/ui/system.rs b/applets/cosmic-applet-power/src/ui/system.rs new file mode 100644 index 00000000..89aa4a76 --- /dev/null +++ b/applets/cosmic-applet-power/src/ui/system.rs @@ -0,0 +1,78 @@ +// SPDX-License-Identifier: LGPL-3.0-or-later + +use crate::RT; +use gtk4::{prelude::*, Align, Button, IconSize, Image, Label, Orientation}; +use logind_zbus::manager::ManagerProxy; +use zbus::Connection; + +async fn restart() -> zbus::Result<()> { + let connection = Connection::system().await?; + let manager_proxy = ManagerProxy::new(&connection).await?; + manager_proxy.reboot(true).await +} + +async fn shut_down() -> zbus::Result<()> { + let connection = Connection::system().await?; + let manager_proxy = ManagerProxy::new(&connection).await?; + manager_proxy.power_off(true).await +} + +async fn suspend() -> zbus::Result<()> { + let connection = Connection::system().await?; + let manager_proxy = ManagerProxy::new(&connection).await?; + manager_proxy.suspend(true).await +} + +pub fn build() -> gtk4::Box { + let suspend_button = create_button("Suspend", "system-suspend-symbolic"); + let restart_button = create_button("Restart", "system-reboot-symbolic"); + let shut_down_button = create_button("Shut Down", "system-shutdown-symbolic"); + suspend_button.connect_clicked(|_| { + RT.spawn(async move { + suspend().await.expect("failed to suspend system"); + }); + }); + restart_button.connect_clicked(|_| { + RT.spawn(async move { + restart().await.expect("failed to reboot system"); + }); + }); + shut_down_button.connect_clicked(|_| { + RT.spawn(async move { + shut_down().await.expect("failed to shut down system"); + }); + }); + view! { + inner_box = gtk4::Box { + set_orientation: Orientation::Horizontal, + set_spacing: 24, + append: &suspend_button, + append: &restart_button, + append: &shut_down_button, + } + } + inner_box +} + +pub fn create_button(name: &str, icon_name: &str) -> Button { + view! { + button = Button { + set_child: inner_box = Some(>k4::Box) { + set_orientation: Orientation::Vertical, + set_spacing: 8, + set_margin_start: 8, + set_margin_end: 8, + set_margin_top: 8, + set_margin_bottom: 8, + append: icon = &Image { + set_icon_name: Some(icon_name), + set_icon_size: IconSize::Large + }, + append: label = &Label { + set_label: name + } + } + } + } + button +}