marche plus ou moins, avec des rc dans tous les sens
This commit is contained in:
parent
5bbb922641
commit
0014db45fa
29
src/main.rs
29
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user