added scrolling

This commit is contained in:
KeksNino
2025-03-20 18:50:40 +01:00
parent 2aa48eda50
commit bf6a39f70d
+41 -45
View File
@@ -1,44 +1,39 @@
use gtk::prelude::*; use gtk::prelude::*;
use gtk::FileChooserDialog;
use gtk::{ use gtk::{
glib, Application, ApplicationWindow, Box as GtkBox, Button, FileChooserAction, Image, Application, ApplicationWindow, Box as GtkBox, Button, FileChooserAction, FileChooserDialog,
Orientation, ResponseType, Image, Orientation, PolicyType, ResponseType, ScrolledWindow,
}; };
use std::fs;
use std::path::Path;
use walkdir::WalkDir; use walkdir::WalkDir;
const APP_ID: &str = "org.gtk_rs.HelloWorld"; const APP_ID: &str = "org.gtk_rs.Example";
pub fn main() -> glib::ExitCode {
let app = Application::builder().application_id(APP_ID).build();
app.connect_activate(build_ui);
app.run()
}
fn build_ui(app: &Application) { fn build_ui(app: &Application) {
// Create a window first
let window = ApplicationWindow::builder() let window = ApplicationWindow::builder()
.application(app) .application(app)
.title("Linux WallpaperEngine GUI") .title("Images in Rows of 5")
.default_width(800)
.default_height(600)
.build(); .build();
// Create a button let wrapper_box = GtkBox::new(Orientation::Vertical, 5);
let button = Button::builder() wrapper_box.set_hexpand(true);
.label("Folder") wrapper_box.set_vexpand(true);
.margin_top(2)
.margin_bottom(2) let button = Button::with_label("Folder");
.margin_start(2) wrapper_box.append(&button);
.margin_end(2)
let scrolled_window = ScrolledWindow::builder()
.hscrollbar_policy(PolicyType::Never)
.vscrollbar_policy(PolicyType::Automatic)
.build(); .build();
scrolled_window.set_hexpand(true);
scrolled_window.set_vexpand(true);
let main_box = GtkBox::new(Orientation::Horizontal, 5); let all_rows_box = GtkBox::new(Orientation::Vertical, 5);
let image_dir = "/media/gamedisk4/SteamLibrary/steamapps/workshop/content/431960/"; let image_dir = "/home/user/Desktop/LinuxWallpaperEngineGUI";
// let image_dir = "/home/user/Desktop/LinuxWallpaperEngineGUI"; let mut row_box = GtkBox::new(Orientation::Horizontal, 5);
let mut images_in_row = 0;
let mut column_box = GtkBox::new(Orientation::Vertical, 5);
let mut images_in_column = 0;
for entry in WalkDir::new(image_dir) for entry in WalkDir::new(image_dir)
.max_depth(3) .max_depth(3)
@@ -46,28 +41,31 @@ fn build_ui(app: &Application) {
.filter_map(Result::ok) .filter_map(Result::ok)
{ {
let path = entry.path(); let path = entry.path();
if path.file_name().map_or(false, |name| name == "preview.jpg") { if path.file_name().map_or(false, |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);
column_box.append(&image); row_box.append(&image);
images_in_column += 1; images_in_row += 1;
if images_in_column == 5 { if images_in_row == 10 {
main_box.append(&column_box); all_rows_box.append(&row_box);
column_box = GtkBox::new(Orientation::Vertical, 5); row_box = GtkBox::new(Orientation::Horizontal, 5);
images_in_column = 0; images_in_row = 0;
} }
} }
} }
if images_in_column > 0 {
main_box.append(&column_box); if images_in_row > 0 {
all_rows_box.append(&row_box);
} }
scrolled_window.set_child(Some(&all_rows_box));
wrapper_box.append(&scrolled_window);
let window_weak = window.downgrade(); let window_weak = window.downgrade();
button.connect_clicked(move |_| { button.connect_clicked(move |_| {
// Get a strong reference back from the weak window
if let Some(window) = window_weak.upgrade() { if let Some(window) = window_weak.upgrade() {
let dialog = FileChooserDialog::new( let dialog = FileChooserDialog::new(
Some("Select Workshop Folder"), Some("Select Workshop Folder"),
@@ -78,8 +76,6 @@ fn build_ui(app: &Application) {
("Open", ResponseType::Accept), ("Open", ResponseType::Accept),
], ],
); );
// Use a response callback rather than dialog.run()
dialog.connect_response(move |dialog, response| { dialog.connect_response(move |dialog, response| {
if response == ResponseType::Accept { if response == ResponseType::Accept {
if let Some(file_path) = dialog.file().and_then(|f| f.path()) { if let Some(file_path) = dialog.file().and_then(|f| f.path()) {
@@ -88,16 +84,16 @@ fn build_ui(app: &Application) {
} }
dialog.close(); dialog.close();
}); });
// Present the dialog (non-blocking)
dialog.present(); dialog.present();
} }
}); });
let wrapper_box = GtkBox::new(Orientation::Vertical, 5);
wrapper_box.append(&button);
wrapper_box.append(&main_box);
window.set_child(Some(&wrapper_box)); window.set_child(Some(&wrapper_box));
window.present(); window.present();
} }
pub fn main() -> gtk::glib::ExitCode {
let app = Application::builder().application_id(APP_ID).build();
app.connect_activate(build_ui);
app.run()
}