From 0014db45fabaafa4cecfd94753cf27e6447b9425 Mon Sep 17 00:00:00 2001 From: justine Date: Tue, 20 Aug 2024 17:02:28 +0200 Subject: [PATCH] marche plus ou moins, avec des rc dans tous les sens --- src/main.rs | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 183fd7e..f8c5aa4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use gtk::prelude::*; use gtk::{glib, Application, ApplicationWindow, Button, Image}; use std::cell::Cell; use std::fs; +use std::rc::Rc; extern crate anyhow; @@ -56,10 +57,21 @@ fn build_ui(application: &Application) { .margin_end(12) .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 let pics = get_pictures_in_folder(".".to_string()).unwrap(); + let sp = Rc::new(pics.clone()); let nbr: usize = 0; + //Une copie pour papa, une copie pour maman... C'est chiant let curr_pic = Cell::new(nbr); + let curr_pic2 = Cell::clone(&curr_pic); // Create the image 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_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 shared_pics = Rc::clone(&sp); 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 - 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()); }); + 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 vbox.append(&button_increase); + vbox.append(&button_decrease); vbox.append(&image); // Create a window