Skip to content

Commit

Permalink
more implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nnyyxxxx committed Oct 18, 2024
1 parent 1518b53 commit 4a90c80
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 51 additions & 1 deletion src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,16 @@ pub fn build_ui(app: &Application) {
backend_combo.append(Some("swww"), "Swww");
backend_combo.append(Some("wallutils"), "Wallutils");
backend_combo.append(Some("feh"), "Feh");
backend_combo.set_active_id(Some("hyprpaper"));

let current_backend = *crate::CURRENT_BACKEND.lock();
let backend_id = match current_backend {
WallpaperBackend::Hyprpaper => "hyprpaper",
WallpaperBackend::Swaybg => "swaybg",
WallpaperBackend::Swww => "swww",
WallpaperBackend::Wallutils => "wallutils",
WallpaperBackend::Feh => "feh",
};
backend_combo.set_active_id(Some(backend_id));

backend_combo.connect_changed(|combo| {
if let Some(active_id) = combo.active_id() {
Expand Down Expand Up @@ -458,3 +467,44 @@ pub fn save_last_wallpaper(path: &str) {
let _ = fs::write(config_path, contents);
}
}

pub fn save_wallpaper_backend(backend: &WallpaperBackend) {
let config_path = shellexpand::tilde(CONFIG_FILE).into_owned();
if let Ok(mut contents) = fs::read_to_string(&config_path) {
let backend_str = match backend {
WallpaperBackend::Hyprpaper => "hyprpaper",
WallpaperBackend::Swaybg => "swaybg",
WallpaperBackend::Swww => "swww",
WallpaperBackend::Wallutils => "wallutils",
WallpaperBackend::Feh => "feh",
};
if let Some(line) = contents.lines().find(|line| line.starts_with("backend = ")) {
contents = contents.replace(line, &format!("backend = {}", backend_str));
} else {
contents.push_str(&format!("\nbackend = {}", backend_str));
}
let _ = fs::write(config_path, contents);
}
}

pub fn load_wallpaper_backend() -> Option<WallpaperBackend> {
let config_path = shellexpand::tilde(CONFIG_FILE).into_owned();
fs::File::open(config_path).ok().and_then(|mut file| {
let mut contents = String::new();
file.read_to_string(&mut contents).ok()?;
contents
.lines()
.find(|line| line.starts_with("backend = "))
.and_then(|line| {
let backend_str = line.trim_start_matches("backend = ");
match backend_str {
"hyprpaper" => Some(WallpaperBackend::Hyprpaper),
"swaybg" => Some(WallpaperBackend::Swaybg),
"swww" => Some(WallpaperBackend::Swww),
"wallutils" => Some(WallpaperBackend::Wallutils),
"feh" => Some(WallpaperBackend::Feh),
_ => None,
}
})
})
}
20 changes: 19 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ struct Cli {
fn main() {
let cli = Cli::parse();

let rt = Runtime::new().expect("Failed to create Tokio runtime");
let _guard = rt.enter();

load_wallpaper_backend();

if cli.restore {
restore_last_wallpaper();
return;
Expand Down Expand Up @@ -65,13 +70,19 @@ async fn set_wallpaper_internal(path: &str) -> Result<(), String> {
println!("Attempting to set wallpaper: {}", path);

let backend = *CURRENT_BACKEND.lock();
match backend {
let result = match backend {
WallpaperBackend::Hyprpaper => set_hyprpaper_wallpaper(path).await,
WallpaperBackend::Swaybg => set_swaybg_wallpaper(path).await,
WallpaperBackend::Swww => set_swww_wallpaper(path).await,
WallpaperBackend::Wallutils => set_wallutils_wallpaper(path).await,
WallpaperBackend::Feh => set_feh_wallpaper(path).await,
};

if result.is_ok() {
gui::save_wallpaper_backend(&backend);
}

result
}

async fn set_hyprpaper_wallpaper(path: &str) -> Result<(), String> {
Expand Down Expand Up @@ -234,6 +245,7 @@ pub fn set_wallpaper_backend(backend: WallpaperBackend) {
drop_all_wallpapers(previous_backend).await;
kill_previous_backend(previous_backend).await;
});
gui::save_wallpaper_backend(&backend);
}

async fn kill_previous_backend(backend: WallpaperBackend) {
Expand Down Expand Up @@ -282,3 +294,9 @@ fn restore_last_wallpaper() {
eprintln!("No last wallpaper found to restore");
}
}

pub fn load_wallpaper_backend() {
if let Some(backend) = gui::load_wallpaper_backend() {
*CURRENT_BACKEND.lock() = backend;
}
}

0 comments on commit 4a90c80

Please sign in to comment.