Connect add and remove buttons

This commit is contained in:
jaby 2025-02-03 21:00:12 +00:00
parent f7ae70b901
commit a9fd194672
3 changed files with 56 additions and 8 deletions

View File

@ -1,10 +1,46 @@
// Prevent console window in addition to Slint window in Windows release builds when, e.g., starting the app via file manager. Ignored on other platforms.
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use slint::Model;
use std::{cell::RefCell, rc::Rc};
slint::include_modules!();
fn main() -> Result<(), slint::PlatformError> {
let main_window = MainWindow::new()?;
struct GUIElements {
tab_vram_file_list: Rc<slint::VecModel<slint::StandardListViewItem>>
}
impl GUIElements {
pub fn new(main_window: &MainWindow) -> GUIElements {
let tab_vram_file_list:Vec<slint::StandardListViewItem> = main_window.get_main_tab_vram_file_list().iter().collect();
let tab_vram_file_list = Rc::new(slint::VecModel::from(tab_vram_file_list));
main_window.set_main_tab_vram_file_list(tab_vram_file_list.clone().into());
GUIElements{tab_vram_file_list}
}
pub fn add_new_vram_file(&mut self, file: &str) {
self.tab_vram_file_list.push(slint::StandardListViewItem::from(file));
}
pub fn remove_vram_file(&mut self, idx: usize) {
self.tab_vram_file_list.remove(idx);
}
}
fn main() -> Result<(), slint::PlatformError> {
let main_window = MainWindow::new()?;
let gui_elements = Rc::new(RefCell::new(GUIElements::new(&main_window)));
let gui_elements_cloned = gui_elements.clone();
main_window.on_main_tab_add_file_clicked(move || {
gui_elements_cloned.borrow_mut().add_new_vram_file("Planschi");
});
let gui_elements_cloned = gui_elements.clone();
main_window.on_main_tab_remove_file_clicked(move |idx: i32| {
if idx >= 0 {
gui_elements_cloned.borrow_mut().remove_vram_file(idx as usize);
}
});
main_window.run()
}

View File

@ -4,6 +4,11 @@ import { MainTab } from "./tab/main-tab.slint";
import { TabWidget } from "std-widgets.slint";
export component MainWindow inherits Window {
in-out property main_tab_vram_file_list <=> tab.vram_files;
callback main_tab_add_file_clicked <=> tab.add_file_clicked;
callback main_tab_remove_file_clicked <=> tab.remove_file_clicked;
title: "TIM Tool 0.1.0";
width: tab_widget.width;
height: tab_widget.height;

View File

@ -8,8 +8,11 @@ struct VRAMImage {
}
export component MainTab inherits Rectangle {
in property <[StandardListViewItem]> vram_file_list: [];
in property <[VRAMImage]> vram_images: [];
in-out property <[StandardListViewItem]> vram_files: [];
in-out property <[VRAMImage]> vram_images: [];
callback add_file_clicked();
callback remove_file_clicked(int);
width: group.width + group.x*2;
height: group.height + group.y*2 + 32px;
@ -49,19 +52,23 @@ export component MainTab inherits Rectangle {
VerticalLayout {
alignment: start;
padding: 4px;
StandardListView {
width: background_image.width/2;
vram_files_list := StandardListView {
width: background_image.width/2;
height: 128px;
model: root.vram_file_list;
model: root.vram_files;
}
HorizontalLayout {
padding: 4px;
Button {
text: "Add File";
clicked => {root.add_file_clicked();}
}
Button {
text: "Remove File";
clicked => {
root.remove_file_clicked(vram_files_list.current_item);
vram_files_list.current-item = -1;
}
}
}
}