code from a month ago i didnt push yet
This commit is contained in:
2025-07-31 01:45:24 +02:00
parent 4e7e30ef84
commit f1f6cedec8
3 changed files with 47 additions and 13 deletions
+2
View File
@@ -0,0 +1,2 @@
431960
target
+1 -1
View File
@@ -4,5 +4,5 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
gtk = { version = "0.9.6", package = "gtk4", features = ["v4_16"] } gtk = { version = "0.10.0", package = "gtk4", features = ["v4_16"] }
walkdir = "2" walkdir = "2"
+44 -12
View File
@@ -1,8 +1,11 @@
//use dirs::config_dir;
use gtk::prelude::*; use gtk::prelude::*;
use gtk::{ use gtk::{
Application, ApplicationWindow, Box as GtkBox, Button, FileChooserAction, FileChooserDialog, Application, ApplicationWindow, Box as GtkBox, Button, FileChooserAction, FileChooserDialog,
Image, Orientation, PolicyType, ResponseType, ScrolledWindow, Image, Orientation, PolicyType, ResponseType, ScrolledWindow,
}; };
use std::env;
use std::fs::{self};
use std::path::PathBuf; use std::path::PathBuf;
use std::process::{Child, Command}; use std::process::{Child, Command};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
@@ -19,6 +22,17 @@ fn build_ui(app: &Application) {
current_process: None, current_process: None,
})); }));
let scrolled_window = ScrolledWindow::builder()
.hscrollbar_policy(PolicyType::Never)
.vscrollbar_policy(PolicyType::Automatic)
.build();
scrolled_window.set_hexpand(true);
scrolled_window.set_vexpand(true);
if let Some(image_dir) = load_config_dir() {
load_images(&image_dir, &scrolled_window, Arc::clone(&app_state));
}
let window = ApplicationWindow::builder() let window = ApplicationWindow::builder()
.application(app) .application(app)
.title("Linux Wallpaper Engine GUI") .title("Linux Wallpaper Engine GUI")
@@ -90,22 +104,49 @@ fn build_ui(app: &Application) {
window.present(); window.present();
} }
fn load_config_dir() -> Option<PathBuf> {
let config_home = env::var("XDG_CONFIG_HOME")
.unwrap_or_else(|_| format!("{}/.config", env::var("HOME").unwrap()));
let config_path = PathBuf::from(format!("{}/linux-wallpaperengine/config.toml", config_home));
if config_path.exists() {
if let Ok(content) = fs::read_to_string(&config_path) {
for line in content.lines() {
if line.starts_with("image_dir") {
if let Some(dir_str) = line.split('\"').nth(1) {
let path = PathBuf::from(dir_str);
if path.exists() && path.is_dir() {
return Some(path);
}
}
}
}
}
}
None
}
fn load_images( fn load_images(
image_dir: &PathBuf, image_dir: &PathBuf,
scrolled_window: &ScrolledWindow, scrolled_window: &ScrolledWindow,
app_state: Arc<Mutex<AppState>>, app_state: Arc<Mutex<AppState>>,
) { ) {
// DISPLAY IMAGES
let all_rows_box = GtkBox::new(Orientation::Vertical, 5); let all_rows_box = GtkBox::new(Orientation::Vertical, 5);
let mut row_box = GtkBox::new(Orientation::Horizontal, 5); let mut row_box = GtkBox::new(Orientation::Horizontal, 5);
let mut images_in_row = 0; let mut images_in_row = 0;
for entry in WalkDir::new(image_dir) let image_dir = load_config_dir().unwrap_or_else(|| image_dir.clone());
// read config file to get image_dir if it exists in config
for entry in WalkDir::new(&image_dir)
.max_depth(3) .max_depth(3)
.into_iter() .into_iter()
.filter_map(Result::ok) .filter_map(Result::ok)
{ {
let path = entry.path().to_path_buf(); let path = entry.path().to_path_buf();
if path.file_name().map_or(false, |name| name == "preview.jpg") { if path.file_name().is_some_and(|name| name == "preview.jpg") {
let image = Image::from_file(&path); let image = Image::from_file(&path);
image.set_pixel_size(150); image.set_pixel_size(150);
@@ -115,15 +156,11 @@ fn load_images(
button.connect_clicked(move |_| { button.connect_clicked(move |_| {
let command_path = path_clone.to_string_lossy().replace("/preview.jpg", ""); let command_path = path_clone.to_string_lossy().replace("/preview.jpg", "");
// First terminate any existing process
let mut state = app_state_clone.lock().unwrap(); let mut state = app_state_clone.lock().unwrap();
if let Some(mut child) = state.current_process.take() { if let Some(mut child) = state.current_process.take() {
println!("Terminating previous wallpaper process"); println!("Terminating previous wallpaper process");
let _ = child.kill(); let _ = child.kill();
} }
// Launch the command as a child process
let child = Command::new("linux-wallpaperengine") let child = Command::new("linux-wallpaperengine")
.arg("--use-angle=GL") .arg("--use-angle=GL")
.arg("--screen-root=DP-2") .arg("--screen-root=DP-2")
@@ -132,7 +169,7 @@ fn load_images(
.arg("--silent") .arg("--silent")
.arg(&command_path) .arg(&command_path)
.spawn(); .spawn();
//let status = child.wait().expect("Failed to wait on child process");
match child { match child {
Ok(child_process) => { Ok(child_process) => {
println!("Started wallpaper engine with: {}", command_path); println!("Started wallpaper engine with: {}", command_path);
@@ -141,10 +178,8 @@ fn load_images(
Err(e) => println!("Failed to start wallpaper engine: {}", e), Err(e) => println!("Failed to start wallpaper engine: {}", e),
} }
}); });
row_box.append(&button); row_box.append(&button);
images_in_row += 1; images_in_row += 1;
if images_in_row == 10 { if images_in_row == 10 {
all_rows_box.append(&row_box); all_rows_box.append(&row_box);
row_box = GtkBox::new(Orientation::Horizontal, 5); row_box = GtkBox::new(Orientation::Horizontal, 5);
@@ -152,14 +187,11 @@ fn load_images(
} }
} }
} }
if images_in_row > 0 { if images_in_row > 0 {
all_rows_box.append(&row_box); all_rows_box.append(&row_box);
} }
scrolled_window.set_child(Some(&all_rows_box)); scrolled_window.set_child(Some(&all_rows_box));
} }
pub fn main() -> gtk::glib::ExitCode { pub fn main() -> gtk::glib::ExitCode {
let app = Application::builder().application_id(APP_ID).build(); let app = Application::builder().application_id(APP_ID).build();
app.connect_activate(build_ui); app.connect_activate(build_ui);