add gpower

This commit is contained in:
LordMZTE 2022-04-20 17:08:54 +02:00
parent 9a029efec2
commit 8afc64adb9
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
6 changed files with 160 additions and 0 deletions

View file

@ -54,6 +54,9 @@ bindsym $mod+Return exec TERMINAL=alacritty i3-sensible-terminal
# kill focused window
bindsym $mod+Shift+q kill
# open gpower
bindsym $mod+Shift+p exec gpower
# change focus
bindsym $mod+h focus left
bindsym $mod+j focus down

2
scripts/gpower/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
target/
Cargo.lock

View file

@ -0,0 +1,9 @@
[package]
name = "gpower"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
gtk4 = "0.4.7"

90
scripts/gpower/src/gui.rs Normal file
View file

@ -0,0 +1,90 @@
use std::{
cell::Cell,
rc::Rc,
thread::{self, JoinHandle},
};
use gtk4::{prelude::*, Inhibit};
use crate::handler::{self, Action};
pub fn on_activate(handle_out: Rc<Cell<Option<JoinHandle<()>>>>, app: &gtk4::Application) {
let win = gtk4::ApplicationWindow::new(app);
win.set_modal(true);
win.set_resizable(false);
win.set_icon_name(Some("system-shutdown"));
let content = gtk4::Box::new(gtk4::Orientation::Horizontal, 20);
content.set_margin_start(20);
content.set_margin_end(20);
content.set_margin_top(20);
content.set_margin_bottom(20);
win.set_child(Some(&content));
let key_event_controller = gtk4::EventControllerKey::new();
content.add_controller(&key_event_controller);
let win_ = win.clone();
key_event_controller.connect_key_pressed(move |_, key, _, _| {
if key == gtk4::gdk::Key::Escape {
win_.close();
Inhibit(true)
} else {
Inhibit(false)
}
});
content.append(&power_button(
handle_out.clone(),
win.clone(),
Action::Shutdown,
"system-shutdown",
"Shutdown",
));
content.append(&power_button(
handle_out.clone(),
win.clone(),
Action::Reboot,
"system-reboot",
"Reboot",
));
content.append(&power_button(
handle_out.clone(),
win.clone(),
Action::Suspend,
"system-suspend",
"Suspend",
));
content.append(&power_button(
handle_out,
win.clone(),
Action::Hibernate,
"system-hibernate",
"Hibernate",
));
win.show();
}
fn power_button(
handle_out: Rc<Cell<Option<JoinHandle<()>>>>,
win: gtk4::ApplicationWindow,
action: Action,
icon: &str,
text: &str,
) -> gtk4::Box {
let vbox = gtk4::Box::new(gtk4::Orientation::Vertical, 2);
let btn = gtk4::Button::new();
vbox.append(&btn);
btn.set_child(Some(
&gtk4::Image::builder()
.icon_name(icon)
.pixel_size(60)
.build(),
));
btn.connect_clicked(move |_| {
handle_out.set(Some(thread::spawn(move || handler::run_action(action))));
win.close();
});
vbox.append(&gtk4::Label::new(Some(text)));
vbox
}

View file

@ -0,0 +1,33 @@
use std::process::Command;
#[derive(Copy, Clone)]
pub enum Action {
Shutdown,
Reboot,
Suspend,
Hibernate,
}
impl Action {
fn as_command(&self) -> Command {
let mut cmd = Command::new("systemctl");
match self {
Action::Shutdown => cmd.arg("poweroff"),
Action::Reboot => cmd.arg("reboot"),
Action::Suspend => cmd.arg("suspend"),
Action::Hibernate => cmd.arg("hibernate"),
};
cmd
}
}
pub fn run_action(cmd: Action) {
match cmd.as_command().spawn() {
Ok(mut c) => {
let _ = c.wait();
},
Err(e) => eprintln!("Error spawning child process: {:?}", e),
}
}

View file

@ -0,0 +1,23 @@
use std::{cell::Cell, rc::Rc};
use gtk4::prelude::*;
mod gui;
mod handler;
fn main() {
let app = gtk4::Application::new(
Some("de.mzte.gpower"),
gtk4::gio::ApplicationFlags::FLAGS_NONE,
);
let handle = Rc::new(Cell::new(None));
let handle_ = handle.clone();
app.connect_activate(move |app| gui::on_activate(handle_.clone(), app));
app.run();
if let Some(handle) = handle.take() {
let _ = handle.join();
}
}