Basic suspend/shutdown/reboot works!
This commit is contained in:
parent
fa03b0553b
commit
329c6006c3
4 changed files with 90 additions and 1 deletions
|
|
@ -3,6 +3,8 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate relm4_macros;
|
extern crate relm4_macros;
|
||||||
|
|
||||||
|
pub mod ui;
|
||||||
|
|
||||||
use gtk4::{gio::ApplicationFlags, prelude::*, Orientation, Separator};
|
use gtk4::{gio::ApplicationFlags, prelude::*, Orientation, Separator};
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use tokio::runtime::Runtime;
|
use tokio::runtime::Runtime;
|
||||||
|
|
@ -26,6 +28,7 @@ fn build_ui(application: >k4::Application) {
|
||||||
.default_height(300)
|
.default_height(300)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
let system_section = ui::system::build();
|
||||||
view! {
|
view! {
|
||||||
main_box = gtk4::Box {
|
main_box = gtk4::Box {
|
||||||
set_orientation: Orientation::Vertical,
|
set_orientation: Orientation::Vertical,
|
||||||
|
|
@ -33,7 +36,10 @@ fn build_ui(application: >k4::Application) {
|
||||||
set_margin_top: 20,
|
set_margin_top: 20,
|
||||||
set_margin_bottom: 20,
|
set_margin_bottom: 20,
|
||||||
set_margin_start: 24,
|
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));
|
window.set_child(Some(&main_box));
|
||||||
|
|
|
||||||
4
applets/cosmic-applet-power/src/ui.rs
Normal file
4
applets/cosmic-applet-power/src/ui.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||||
|
|
||||||
|
pub mod session;
|
||||||
|
pub mod system;
|
||||||
1
applets/cosmic-applet-power/src/ui/session.rs
Normal file
1
applets/cosmic-applet-power/src/ui/session.rs
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||||
78
applets/cosmic-applet-power/src/ui/system.rs
Normal file
78
applets/cosmic-applet-power/src/ui/system.rs
Normal file
|
|
@ -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
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue