finish? lock functionality

This commit is contained in:
Penelope Gwen 2025-09-18 16:12:55 -07:00
parent 652c26c712
commit f3538fe267
3 changed files with 55 additions and 19 deletions

View file

@ -24,11 +24,17 @@ pub struct Programs {
pub command: String,
pub arguments: Vec<String>
}
#[derive(Serialize,Deserialize,Clone,Debug,Default)]
pub struct LockConf {
pub blur: f32,
pub scale: f32,
}
#[derive(Serialize,Deserialize,Default)]
pub struct Config {
pub title_length: usize,
pub window_icons: Vec<WindowIcon>,
pub wallpaper_path: String,
pub programs: HashMap<String, Programs>,
pub lock: LockConf,
pub profiles: HashMap<String, Profile>
}

View file

@ -1,7 +1,7 @@
use std::{env::consts, fs::{self, exists}, io, ops::Sub, path::{Path, PathBuf}};
use std::{env::consts, fs::{self, exists, DirBuilder}, io, ops::Sub, path::{Path, PathBuf}};
use walkdir::WalkDir;
use image::imageops::GaussianBlurParameters;
use image::{imageops::GaussianBlurParameters, DynamicImage};
use serde::{Deserialize, Serialize};
use serde_json::json;
use sha2::{Sha256, Digest};
@ -9,7 +9,7 @@ use swayipc::Connection;
use xdg::BaseDirectories;
use shellexpand::tilde;
use crate::config::Config;
use crate::config::{Config, LockConf};
#[derive(Serialize,Deserialize,Debug)]
pub struct WallpaperCache {
@ -17,11 +17,33 @@ pub struct WallpaperCache {
pub hash: String
}
pub fn lock_screen(config: &Config, mut sway_connection: Connection) {
pub fn generate_image(orig_img_data: DynamicImage,gen_image_path: PathBuf, lock_conf: LockConf) {// Result<(), ImageError> {
//let img_data = image::open(image_path).unwrap();
let w = orig_img_data.width() as f32 * lock_conf.scale;
let h = orig_img_data.height() as f32 * lock_conf.scale;
let blurred_img_data = orig_img_data
.resize(w as u32, h as u32, image::imageops::FilterType::Nearest)
.blur_advanced(GaussianBlurParameters::new_from_sigma(lock_conf.blur));
//let gen_image_path = cache_dir.clone().unwrap().join(format!("sway-profiles-rs/lock/{}.jpg",output.name));
match blurred_img_data.save(gen_image_path) {
Ok(_) => println!("Image saved successfully"),
Err(e) => println!("error: {:?}",e),
}
}
pub fn lock_screen(config: &Config, mut sway_connection: Connection, force_bg_render: bool) -> Result<Vec<Result<(), swayipc::Error>>, swayipc::Error> {
let wallpaper_root_path = tilde(&config.wallpaper_path).to_string();
let wallpaper_root_path = Path::new(&wallpaper_root_path);
let cache_dir = BaseDirectories::with_prefix("sway-profiles-rs").cache_home;
let wp_cache_dir = cache_dir.clone().unwrap().join("sway-profiles-rs/lock/");
match DirBuilder::new().recursive(true).create(&wp_cache_dir) {
Ok(o) => {
println!("created directory ({:?})",o)
},
Err(e) => {
println!("error: {:?}",e)
}
}
let mut wp_hash_json: Vec<WallpaperCache> = vec![];
let hashes_json_path = wp_cache_dir.join("hashes.json");
@ -43,16 +65,19 @@ pub fn lock_screen(config: &Config, mut sway_connection: Connection) {
let hash = format!("{:x}",sha256.finalize());
wp_hash_array.push(WallpaperCache { display: output.name.clone(), hash: hash.clone() });
if let Some(output_saved_hash) = wp_hash_json.iter().find(|x|x.display == output.name) && output_saved_hash.hash.ne(&hash) {
let img_data = image::open(image_path).unwrap();
let blurred_img_data = img_data
.resize((img_data.width() as f64 * 0.75) as u32, (img_data.height() as f64 * 0.75) as u32, image::imageops::FilterType::Nearest)
.blur_advanced(GaussianBlurParameters::new_from_sigma(10.0));
let blurred_image_path = cache_dir.clone().unwrap().join(format!("sway-profiles-rs/lock/{}.jpg",output.name));
let _ = blurred_img_data.save(blurred_image_path);
// if hashes_json_path.exists() {
let wallpaper_data = image::open(&image_path).unwrap();
let wallpaper_cache_path = cache_dir.as_ref().unwrap().join(format!("sway-profiles-rs/lock/{}.jpg",output.name));
match wp_hash_json.iter().find(|x|x.display == output.name) {
Some (saved_hash) => {
if !(saved_hash.hash == hash && wallpaper_cache_path.exists()) || force_bg_render {
generate_image(wallpaper_data,wallpaper_cache_path, config.lock.clone())
};
},
None => generate_image(wallpaper_data, wallpaper_cache_path, config.lock.clone())
}
}
let gtklock_modules_dir = "/usr/lib/".to_owned() + consts::ARCH + "-linux-gnu/gtklock/";
let gtklock_modules_dir = format!("/usr/lib/{}-linux-gnu/gtklock/",consts::ARCH);//.to_owned() + consts::ARCH + "-linux-gnu/gtklock/";
let gktlock_modules_path = Path::new(&gtklock_modules_dir);
let mut gtklock_args: Vec<String> = vec![];
for file in WalkDir::new(gktlock_modules_path).into_iter() {//.find(|x|x.as_ref().unwrap().file_name().to_str().unwrap().ends_with("-module.so")) {
@ -64,10 +89,11 @@ pub fn lock_screen(config: &Config, mut sway_connection: Connection) {
}
let new_json = json!(wp_hash_array);
let _ = std::fs::write::<PathBuf, String>(hashes_json_path, new_json.to_string());
let mut gtklock_command = "exec gtklock".to_owned();
let mut gtklock_command = "exec echo gtklock".to_owned();
for a in gtklock_args {
gtklock_command = gtklock_command + " " + &a;
}
println!("{:?}",gtklock_command);
let _ = sway_connection.run_command(gtklock_command);
// let _ = sway_connection.run_command(gtklock_command);
sway_connection.run_command(gtklock_command)
}

View file

@ -1,7 +1,5 @@
use std::process::exit;
use clap::{Parser,Subcommand,ArgAction};
use swayipc::{Connection, Event, EventType, Fallible};
@ -54,7 +52,10 @@ enum Commands {
program: String
},
/// Set up blurred wallpaper for screen lock, and load gtklock modules
Lock,
Lock {
#[arg(short, long, action = ArgAction::SetTrue)]
force_render_background: Option<bool>,
},
//Rename,
Profile,
Shortcuts {
@ -110,8 +111,11 @@ fn main() -> Fallible<()> {
let _ = sway_connection.run_command(swaymsg_command);
}
},
Commands::Lock => {
lock_screen(&config, sway_connection);
Commands::Lock { force_render_background } => {
match lock_screen(&config, sway_connection, force_render_background.unwrap()) {
Ok(o) => println!("Screen locked successfully{:?}",o.first().unwrap()),
Err(e) => println!("{:?}",e),
};
},
//Commands::Rename => todo!(),
Commands::Profile => {