Centered buttons

This commit is contained in:
justine 2024-08-20 17:25:28 +02:00
parent 9fd4bd5d3e
commit 06b091c87f

View File

@ -1,5 +1,6 @@
use crate::glib::clone;
use gtk::prelude::*; use gtk::prelude::*;
use gtk::{glib, Application, ApplicationWindow, Button, Image}; use gtk::{self, glib, Application, ApplicationWindow, Button, Image};
use std::cell::Cell; use std::cell::Cell;
use std::fs; use std::fs;
use std::rc::Rc; use std::rc::Rc;
@ -48,6 +49,11 @@ fn build_ui(application: &Application) {
// Create a vertical container // Create a vertical container
let mut vbox = gtk::Box::new(gtk::Orientation::Vertical, 0); let mut vbox = gtk::Box::new(gtk::Orientation::Vertical, 0);
// Create an horizontal container
let mut hbox = gtk::Box::new(gtk::Orientation::Horizontal, 0);
hbox.set_hexpand(true);
hbox.set_halign(gtk::Align::Center);
// Create the "Increase" button // Create the "Increase" button
let button_increase = Button::builder() let button_increase = Button::builder()
.label("Next") .label("Next")
@ -70,8 +76,7 @@ fn build_ui(application: &Application) {
let sp = Rc::new(pics.clone()); 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 //Une copie pour papa, une copie pour maman... C'est chiant
let curr_pic = Cell::new(nbr); let curr_pic = Rc::new(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());
@ -80,27 +85,42 @@ fn build_ui(application: &Application) {
image.set_size_request(-1, -1); image.set_size_request(-1, -1);
// Connect the buttons' 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); let shared_pics = Rc::clone(&sp);
button_increase.connect_clicked(move |_| { button_increase.connect_clicked(clone!(
let new_index = (curr_pic.get() + 1) % shared_pics.len(); // Ensure the index wraps around #[weak]
curr_pic.set(new_index); // Update the current picture index button_decrease,
image_clone.set_from_file(Some(shared_pics[curr_pic.get()].clone())); // Update the image #[weak]
println!("{:?}", curr_pic.get()); curr_pic,
}); #[weak]
image,
move |_| {
let new_index = (curr_pic.get() + 1) % shared_pics.len();
curr_pic.set(new_index);
image.set_from_file(Some(shared_pics[curr_pic.get()].clone()));
dbg!(new_index.clone());
println!("haaa");
}
));
let image_clone = image.clone();
let shared_pics = Rc::clone(&sp); let shared_pics = Rc::clone(&sp);
button_decrease.connect_clicked(move |_| { button_decrease.connect_clicked(clone!(
let new_index = (curr_pic2.get().checked_sub(1).unwrap_or(0)); // Ensure the index wraps around #[weak]
curr_pic2.set(new_index); // Update the current picture index button_decrease,
image_clone.set_from_file(Some(shared_pics[curr_pic2.get()].clone())); // Update the image #[weak]
println!("{:?}", curr_pic2.get()); image,
}); move |_| {
let new_index = curr_pic.get().checked_sub(1).unwrap_or(0);
curr_pic.set(new_index);
image.set_from_file(Some(shared_pics[curr_pic.get()].clone()));
dbg!(new_index.clone());
println!("haaa");
}
));
// Put the widgets in the container // Put the widgets in the containers
vbox.append(&button_increase); hbox.append(&button_decrease);
vbox.append(&button_decrease); hbox.append(&button_increase);
vbox.append(&hbox);
vbox.append(&image); vbox.append(&image);
// Create a window // Create a window