Skip to content

Commit

Permalink
add buttons at bottom
Browse files Browse the repository at this point in the history
  • Loading branch information
nnyyxxxx committed Oct 17, 2024
1 parent ec21f3f commit 38eb21b
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 4 deletions.
67 changes: 67 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ lazy_static = "1.4.0"
parking_lot = "0.12.1"
rayon = "1.7"
num_cpus = "1.15"
rand = "0.8"

[profile.release]
lto = true
Expand Down
82 changes: 78 additions & 4 deletions src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use glib::ControlFlow;
use gtk::gdk::Texture;
use gtk::gdk_pixbuf::Pixbuf;
use gtk::{
gdk, gio, glib, prelude::*, Application, ApplicationWindow, Button, FlowBox, Image,
ScrolledWindow,
gdk, gio, glib, prelude::*, Application, ApplicationWindow, Box as GtkBox, Button, FlowBox,
Image, MenuButton, PopoverMenu, ScrolledWindow,
};
use parking_lot::Mutex;
use rand::seq::SliceRandom;
use rayon::prelude::*;
use std::{
cell::RefCell,
Expand Down Expand Up @@ -144,9 +145,28 @@ pub fn build_ui(app: &Application) {
}
});

let main_box = gtk::Box::new(gtk::Orientation::Vertical, 5);
main_box.append(&choose_folder_button);
let sort_button = MenuButton::new();
sort_button.set_label("Sort");
let sort_menu = gio::Menu::new();
sort_menu.append(Some("Date"), Some("app.sort_by_date"));
sort_menu.append(Some("Name"), Some("app.sort_by_name"));
let popover = PopoverMenu::from_model(Some(&sort_menu));
sort_button.set_popover(Some(&popover));

let random_button = Button::with_label("Random");
let exit_button = Button::with_label("Exit");

let bottom_box = GtkBox::new(gtk::Orientation::Horizontal, 10);
bottom_box.set_margin_top(10);
bottom_box.set_margin_bottom(10);
bottom_box.set_halign(gtk::Align::Center);
bottom_box.append(&sort_button);
bottom_box.append(&random_button);
bottom_box.append(&exit_button);

let main_box = GtkBox::new(gtk::Orientation::Vertical, 0);
main_box.append(&scrolled_window);
main_box.append(&bottom_box);

window.set_child(Some(&main_box));

Expand All @@ -163,6 +183,29 @@ pub fn build_ui(app: &Application) {
}
});

let sort_by_date_action = gio::SimpleAction::new("sort_by_date", None);
sort_by_date_action.connect_activate(|_, _| {
println!("Sorting by date");
});
app.add_action(&sort_by_date_action);

let sort_by_name_action = gio::SimpleAction::new("sort_by_name", None);
sort_by_name_action.connect_activate(|_, _| {
println!("Sorting by name");
});
app.add_action(&sort_by_name_action);

let flowbox_clone = Rc::clone(&flowbox_ref);
let image_loader_clone = Rc::clone(&image_loader);
random_button.connect_clicked(move |_| {
set_random_wallpaper(&flowbox_clone, &image_loader_clone);
});

let app_clone = app.clone();
exit_button.connect_clicked(move |_| {
app_clone.quit();
});

window.present();
}

Expand Down Expand Up @@ -283,3 +326,34 @@ fn save_last_path(path: &Path) {
let content = format!("[Settings]\nfolder = {}", path.to_str().unwrap_or(""));
let _ = fs::write(config_path, content);
}

fn set_random_wallpaper(_flowbox: &Rc<RefCell<FlowBox>>, image_loader: &Rc<RefCell<ImageLoader>>) {
let image_loader = image_loader.borrow();
if let Some(current_folder) = &image_loader.current_folder {
if let Ok(entries) = fs::read_dir(current_folder) {
let images: Vec<_> = entries
.filter_map(|entry| {
entry.ok().and_then(|e| {
let path = e.path();
if path.is_file()
&& matches!(
path.extension().and_then(|e| e.to_str()),
Some("png" | "jpg" | "jpeg")
)
{
Some(path)
} else {
None
}
})
})
.collect();

if let Some(random_image) = images.choose(&mut rand::thread_rng()) {
if let Some(path_str) = random_image.to_str() {
crate::set_wallpaper(path_str);
}
}
}
}
}

0 comments on commit 38eb21b

Please sign in to comment.