marche plus ou moins, avec des rc dans tous les sens

This commit is contained in:
justine 2024-08-20 17:02:28 +02:00
parent 5bbb922641
commit 0014db45fa

View File

@ -2,6 +2,7 @@ use gtk::prelude::*;
use gtk::{glib, Application, ApplicationWindow, Button, Image}; use gtk::{glib, Application, ApplicationWindow, Button, Image};
use std::cell::Cell; use std::cell::Cell;
use std::fs; use std::fs;
use std::rc::Rc;
extern crate anyhow; extern crate anyhow;
@ -56,10 +57,21 @@ fn build_ui(application: &Application) {
.margin_end(12) .margin_end(12)
.build(); .build();
let button_decrease = Button::builder()
.label("Prev")
.margin_top(12)
.margin_bottom(12)
.margin_start(12)
.margin_end(12)
.build();
// A list of pictures with a mutable index ref // A list of pictures with a mutable index ref
let pics = get_pictures_in_folder(".".to_string()).unwrap(); let pics = get_pictures_in_folder(".".to_string()).unwrap();
let sp = Rc::new(pics.clone());
let nbr: usize = 0; let nbr: usize = 0;
//Une copie pour papa, une copie pour maman... C'est chiant
let curr_pic = Cell::new(nbr); let curr_pic = Cell::new(nbr);
let curr_pic2 = Cell::clone(&curr_pic);
// Create the image // Create the image
let image = Image::from_file(pics[curr_pic.get()].clone()); let image = Image::from_file(pics[curr_pic.get()].clone());
@ -67,17 +79,28 @@ fn build_ui(application: &Application) {
image.set_vexpand(true); image.set_vexpand(true);
image.set_size_request(-1, -1); image.set_size_request(-1, -1);
// Connect the button's clicked signal to update the image // Connect the buttons' clicked signal to update the image
let image_clone = image.clone(); // Clone the image widget reference for the closure let image_clone = image.clone(); // Clone the image widget reference for the closure
let shared_pics = Rc::clone(&sp);
button_increase.connect_clicked(move |_| { button_increase.connect_clicked(move |_| {
let new_index = (curr_pic.get() + 1) % pics.len(); // Ensure the index wraps around let new_index = (curr_pic.get() + 1) % shared_pics.len(); // Ensure the index wraps around
curr_pic.set(new_index); // Update the current picture index curr_pic.set(new_index); // Update the current picture index
image_clone.set_from_file(Some(pics[curr_pic.get()].clone())); // Update the image image_clone.set_from_file(Some(shared_pics[curr_pic.get()].clone())); // Update the image
println!("{:?}", curr_pic.get()); println!("{:?}", curr_pic.get());
}); });
let image_clone = image.clone();
let shared_pics = Rc::clone(&sp);
button_decrease.connect_clicked(move |_| {
let new_index = (curr_pic2.get().checked_sub(1).unwrap_or(0)); // Ensure the index wraps around
curr_pic2.set(new_index); // Update the current picture index
image_clone.set_from_file(Some(shared_pics[curr_pic2.get()].clone())); // Update the image
println!("{:?}", curr_pic2.get());
});
// Put the widgets in the container // Put the widgets in the container
vbox.append(&button_increase); vbox.append(&button_increase);
vbox.append(&button_decrease);
vbox.append(&image); vbox.append(&image);
// Create a window // Create a window