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::{glib, Application, ApplicationWindow, Button, Image};
use gtk::{self, glib, Application, ApplicationWindow, Button, Image};
use std::cell::Cell;
use std::fs;
use std::rc::Rc;
@ -48,6 +49,11 @@ fn build_ui(application: &Application) {
// Create a vertical container
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
let button_increase = Button::builder()
.label("Next")
@ -70,8 +76,7 @@ fn build_ui(application: &Application) {
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);
let curr_pic = Rc::new(Cell::new(nbr));
// Create the image
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);
// 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) % shared_pics.len(); // Ensure the index wraps around
curr_pic.set(new_index); // Update the current picture index
image_clone.set_from_file(Some(shared_pics[curr_pic.get()].clone())); // Update the image
println!("{:?}", curr_pic.get());
});
button_increase.connect_clicked(clone!(
#[weak]
button_decrease,
#[weak]
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);
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());
});
button_decrease.connect_clicked(clone!(
#[weak]
button_decrease,
#[weak]
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
vbox.append(&button_increase);
vbox.append(&button_decrease);
// Put the widgets in the containers
hbox.append(&button_decrease);
hbox.append(&button_increase);
vbox.append(&hbox);
vbox.append(&image);
// Create a window