From f1f6cedec8e065b9ad889a8cb259bc49681f6cd7 Mon Sep 17 00:00:00 2001 From: KeksNino Date: Thu, 31 Jul 2025 01:45:24 +0200 Subject: [PATCH] updated code from a month ago i didnt push yet --- .gitignore | 2 ++ Cargo.toml | 2 +- src/main.rs | 56 +++++++++++++++++++++++++++++++++++++++++------------ 3 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a441ace --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +431960 +target diff --git a/Cargo.toml b/Cargo.toml index 8557605..313d152 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,5 +4,5 @@ version = "0.1.0" edition = "2021" [dependencies] -gtk = { version = "0.9.6", package = "gtk4", features = ["v4_16"] } +gtk = { version = "0.10.0", package = "gtk4", features = ["v4_16"] } walkdir = "2" diff --git a/src/main.rs b/src/main.rs index 6415398..a8dfc81 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,11 @@ +//use dirs::config_dir; use gtk::prelude::*; use gtk::{ Application, ApplicationWindow, Box as GtkBox, Button, FileChooserAction, FileChooserDialog, Image, Orientation, PolicyType, ResponseType, ScrolledWindow, }; +use std::env; +use std::fs::{self}; use std::path::PathBuf; use std::process::{Child, Command}; use std::sync::{Arc, Mutex}; @@ -19,6 +22,17 @@ fn build_ui(app: &Application) { 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() .application(app) .title("Linux Wallpaper Engine GUI") @@ -90,22 +104,49 @@ fn build_ui(app: &Application) { window.present(); } +fn load_config_dir() -> Option { + 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( image_dir: &PathBuf, scrolled_window: &ScrolledWindow, app_state: Arc>, ) { + // DISPLAY IMAGES let all_rows_box = GtkBox::new(Orientation::Vertical, 5); let mut row_box = GtkBox::new(Orientation::Horizontal, 5); 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) .into_iter() .filter_map(Result::ok) { 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); image.set_pixel_size(150); @@ -115,15 +156,11 @@ fn load_images( button.connect_clicked(move |_| { let command_path = path_clone.to_string_lossy().replace("/preview.jpg", ""); - - // First terminate any existing process let mut state = app_state_clone.lock().unwrap(); if let Some(mut child) = state.current_process.take() { println!("Terminating previous wallpaper process"); let _ = child.kill(); } - - // Launch the command as a child process let child = Command::new("linux-wallpaperengine") .arg("--use-angle=GL") .arg("--screen-root=DP-2") @@ -132,7 +169,7 @@ fn load_images( .arg("--silent") .arg(&command_path) .spawn(); - + //let status = child.wait().expect("Failed to wait on child process"); match child { Ok(child_process) => { println!("Started wallpaper engine with: {}", command_path); @@ -141,10 +178,8 @@ fn load_images( Err(e) => println!("Failed to start wallpaper engine: {}", e), } }); - row_box.append(&button); images_in_row += 1; - if images_in_row == 10 { all_rows_box.append(&row_box); row_box = GtkBox::new(Orientation::Horizontal, 5); @@ -152,14 +187,11 @@ fn load_images( } } } - if images_in_row > 0 { all_rows_box.append(&row_box); } - scrolled_window.set_child(Some(&all_rows_box)); } - pub fn main() -> gtk::glib::ExitCode { let app = Application::builder().application_id(APP_ID).build(); app.connect_activate(build_ui);