Skip to content
This repository was archived by the owner on Jan 7, 2025. It is now read-only.

Commit 96094f8

Browse files
authored
Merge pull request #24 from govnorice/rust
rewritten in rust + use dbus
2 parents 7757a16 + 5c40c14 commit 96094f8

23 files changed

+243
-343
lines changed

.gitignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
cmake-build-debug
2-
.idea
3-
.vscode
1+
.vscode
2+
/target

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/powerdialog.iml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CMakeLists.txt

Lines changed: 0 additions & 27 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

images/images.h

Lines changed: 0 additions & 21 deletions
This file was deleted.

main.cpp

Lines changed: 0 additions & 109 deletions
This file was deleted.

modules/CssManager.cpp

Lines changed: 0 additions & 37 deletions
This file was deleted.

modules/CssManager.h

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/images.rs

Lines changed: 12 additions & 0 deletions
Large diffs are not rendered by default.

src/main.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
pub mod widgets;
2+
pub mod images;
3+
pub mod modules;
4+
5+
use images::IMAGES;
6+
7+
use modules::css_manager::CssManager;
8+
9+
use gtk::{Application, ApplicationWindow, Box};
10+
use gtk::Orientation::Horizontal;
11+
use gtk::prelude::{ApplicationExt, ApplicationExtManual, BoxExt, ContainerExt, WidgetExt};
12+
13+
use widgets::{action::ActionWidget};
14+
use std::env;
15+
16+
fn main() {
17+
let app = Application::builder()
18+
.application_id("org.govnorice.powerdialog")
19+
.build();
20+
21+
app.connect_activate(|app| {
22+
let win = ApplicationWindow::builder()
23+
.application(app)
24+
.default_width(400)
25+
.default_height(80)
26+
.title("powerdialog")
27+
.build();
28+
29+
let container = Box::new(Horizontal, 0);
30+
31+
let poweroff_widget = ActionWidget::init(IMAGES.poweroff, "poweroff");
32+
let suspend_widget = ActionWidget::init(IMAGES.suspend, "suspend");
33+
let reboot_widget = ActionWidget::init(IMAGES.reboot, "reboot");
34+
let logout_widget = ActionWidget::init(IMAGES.logout, "logout");
35+
36+
container.pack_start(&poweroff_widget, false, false, 0);
37+
container.pack_start(&suspend_widget, false, false, 0);
38+
container.pack_start(&reboot_widget, false, false, 0);
39+
container.pack_start(&logout_widget, false, false, 0);
40+
41+
win.add(&container);
42+
43+
// CSS
44+
let cm = CssManager::new();
45+
let style = "* {
46+
color: white;
47+
}
48+
window {
49+
background: black;
50+
}
51+
image {
52+
margin: 0;
53+
padding: 0;
54+
padding: 10px;
55+
}
56+
button {
57+
margin-right: 3em;
58+
padding: 0;
59+
background: image(transparent);
60+
border: none;
61+
border-radius: 100%;
62+
}
63+
button:focus {
64+
outline: none;
65+
}
66+
#poweroff {
67+
margin-left: 2.8em;
68+
}
69+
70+
#poweroff:hover, #poweroff:focus {
71+
background: image(red);
72+
}
73+
#suspend:hover, #suspend:focus {
74+
background: image(blue);
75+
}
76+
#reboot:hover, #reboot:focus {
77+
background: image(green);
78+
}
79+
#logout:hover, #logout:focus {
80+
background: image(#720cad);
81+
}";
82+
cm.load_from_data(style.as_bytes());
83+
if let Some(home_dir) = env::var_os("HOME") {
84+
if let Some(home_path) = home_dir.to_str() {
85+
let path: String = home_path.to_string() + "/.config/powerdialog/styles.css";
86+
cm.load_from_file(&path);
87+
} else {
88+
println!("Failed to convert HOME environment variable to string");
89+
}
90+
} else {
91+
println!("HOME environment variable is not set");
92+
}
93+
win.show_all();
94+
});
95+
96+
app.run();
97+
}

src/modules/css_manager.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use gtk::{CssProvider, STYLE_PROVIDER_PRIORITY_APPLICATION, StyleContext};
2+
use gtk::prelude::{CssProviderExt};
3+
4+
pub struct CssManager {
5+
provider: CssProvider,
6+
provider2: CssProvider,
7+
}
8+
9+
impl CssManager {
10+
pub fn new() -> CssManager {
11+
CssManager {
12+
provider: CssProvider::new(),
13+
provider2: CssProvider::new(),
14+
}
15+
}
16+
pub fn load_from_file(&self, path: &str) {
17+
if let Err(err) = self.provider.load_from_path(path) {
18+
eprintln!("Error loading CSS from {}: {}", path, err);
19+
} else {
20+
StyleContext::add_provider_for_screen(&gdk::Screen::default().unwrap(), &self.provider, STYLE_PROVIDER_PRIORITY_APPLICATION);
21+
println!("CSS loaded successfully")
22+
}
23+
}
24+
pub fn load_from_data(&self, style: &[u8]) {
25+
if let Err(err) = self.provider2.load_from_data(style) {
26+
eprintln!("Error loading style from data: {}", err);
27+
} else {
28+
StyleContext::add_provider_for_screen(&gdk::Screen::default().unwrap(), &self.provider2, STYLE_PROVIDER_PRIORITY_APPLICATION);
29+
println!("Style from data loaded successfully")
30+
}
31+
}
32+
}

src/modules/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod css_manager;

0 commit comments

Comments
 (0)