From c237a9af45331f4b76ef6fa94bfb827cc708dcd5 Mon Sep 17 00:00:00 2001 From: Jaby Date: Sat, 15 Feb 2025 11:16:49 +0100 Subject: [PATCH] Redirect to file dialog --- src/Tools/tim_tool/src/gui/file_tab.rs | 8 +++-- src/Tools/tim_tool/src/gui/main_tab.rs | 8 ----- src/Tools/tim_tool/src/main.rs | 26 +++++------------ src/Tools/tim_tool/ui/app-window.slint | 13 +++++++-- src/Tools/tim_tool/ui/tab/file-tab.slint | 37 ++++++++++++------------ 5 files changed, 43 insertions(+), 49 deletions(-) diff --git a/src/Tools/tim_tool/src/gui/file_tab.rs b/src/Tools/tim_tool/src/gui/file_tab.rs index 49c29a87..1e966325 100644 --- a/src/Tools/tim_tool/src/gui/file_tab.rs +++ b/src/Tools/tim_tool/src/gui/file_tab.rs @@ -1,5 +1,5 @@ use crate::MainWindow; -use super::MainWindowRef; +use super::{GUIElementsRef, MainWindowRef, main_tab::MainTab}; pub struct FileTab { main_window: MainWindowRef @@ -10,10 +10,12 @@ impl FileTab { FileTab{main_window} } - pub fn on_browse_file(&self, mut function: impl FnMut(&MainWindow) + 'static) { + pub fn on_browse_file(&self, gui_elements: GUIElementsRef, mut function: impl FnMut(&MainWindow, &mut MainTab) + 'static) { let main_window_cloned = self.main_window.clone(); + let gui_cloned = gui_elements.clone(); + self.main_window.borrow().on_file_tab_browse_convert_image(move || { - function(&main_window_cloned.borrow()); + function(&main_window_cloned.borrow(), &mut gui_cloned.borrow_mut().main_tab); }); } } \ No newline at end of file diff --git a/src/Tools/tim_tool/src/gui/main_tab.rs b/src/Tools/tim_tool/src/gui/main_tab.rs index 5c45adc3..7d8e8c95 100644 --- a/src/Tools/tim_tool/src/gui/main_tab.rs +++ b/src/Tools/tim_tool/src/gui/main_tab.rs @@ -87,12 +87,4 @@ impl MainTab { function(&mut gui_cloned.borrow_mut().main_tab, &main_window_cloned.borrow(), idx); }); } - - pub fn on_add_file(&self, gui_elements: GUIElementsRef, mut function: impl FnMut(&mut MainTab, &MainWindow) + 'static) { - let main_window_cloned = self.main_window.clone(); - let gui_cloned = gui_elements.clone(); - self.main_window.borrow().on_main_tab_add_file_clicked(move || { - function(&mut gui_cloned.borrow_mut().main_tab, &main_window_cloned.borrow()); - }); - } } \ No newline at end of file diff --git a/src/Tools/tim_tool/src/main.rs b/src/Tools/tim_tool/src/main.rs index 65c6861b..db34e439 100644 --- a/src/Tools/tim_tool/src/main.rs +++ b/src/Tools/tim_tool/src/main.rs @@ -32,29 +32,14 @@ fn setup_main_tab(gui_elements_ref: Rc>) { main_tab.remove_vram_file(idx as usize); } }); - - gui_elements.main_tab.on_add_file(gui_elements_ref.clone(), move |main_tab, _main_window| { - let file = FileDialog::new() - .add_filter("Images", &["png", "bmp", "jpeg"]) - .add_filter("All", &[""]) - .set_title("Add TIM image") - .pick_file(); - - if let Some(file) = file { - if let Result::Err(error) = main_tab.add_new_vram_file(file) { - MessageDialog::new().set_title("Loading Image failed").set_level(rfd::MessageLevel::Error).set_description(error.to_string()).show(); - } - } - }); } fn setup_file_tab(gui_elements_ref: Rc>) { let gui_elements = gui_elements_ref.borrow(); - gui_elements.file_tab.on_browse_file(move |main_window| { - let file = FileDialog::new() - .add_filter("Images", &["png", "bmp", "jpeg"]) - .add_filter("All", &[""]) + gui_elements.file_tab.on_browse_file(gui_elements_ref.clone(), move |main_window, main_tab| { + let file = FileDialog::new() + .add_filter("Images (.png; .bmp; .jpeg)", &["png", "bmp", "jpeg"]) .set_title("Select image file") .pick_file(); @@ -62,6 +47,11 @@ fn setup_file_tab(gui_elements_ref: Rc>) { if let Some(file_path) = file.to_str() { main_window.set_file_tab_browse_path(SharedString::from(file_path)); } + + if let Result::Err(error) = main_tab.add_new_vram_file(file) { + MessageDialog::new().set_title("Loading Image failed").set_level(rfd::MessageLevel::Error).set_description(error.to_string()).show(); + } + main_window.invoke_change_to_main(); } }); } \ No newline at end of file diff --git a/src/Tools/tim_tool/ui/app-window.slint b/src/Tools/tim_tool/ui/app-window.slint index 5ace34ad..2e408a2b 100644 --- a/src/Tools/tim_tool/ui/app-window.slint +++ b/src/Tools/tim_tool/ui/app-window.slint @@ -1,5 +1,5 @@ import { AboutTab } from "./tab/about-tab.slint"; -import { FileTab } from "./tab/file-tab.slint"; +import { FileTab, State } from "./tab/file-tab.slint"; import { MainTab } from "./tab/main-tab.slint"; import { TabWidget } from "std-widgets.slint"; @@ -9,7 +9,6 @@ export component MainWindow inherits Window { in-out property main_tab_vram_file_list <=> main_tab.vram_files; in-out property main_tab_vram_images <=> main_tab.vram_images; - callback main_tab_add_file_clicked <=> main_tab.add_file_clicked; callback main_tab_remove_file_clicked <=> main_tab.remove_file_clicked; callback move_vram_image <=> main_tab.move_vram_image; @@ -40,6 +39,7 @@ export component MainWindow inherits Window { main_tab := MainTab { x: 0px; y: 0px; + add_file_clicked => {root.change_to_load_file()} } } Tab { @@ -47,4 +47,13 @@ export component MainWindow inherits Window { AboutTab {} } } + + public function change_to_load_file() { + file_tab.state = State.ConvertImage; + tab_widget.current-index = 0; + } + + public function change_to_main() { + tab_widget.current-index = 1; + } } \ No newline at end of file diff --git a/src/Tools/tim_tool/ui/tab/file-tab.slint b/src/Tools/tim_tool/ui/tab/file-tab.slint index d265f56f..e42a928f 100644 --- a/src/Tools/tim_tool/ui/tab/file-tab.slint +++ b/src/Tools/tim_tool/ui/tab/file-tab.slint @@ -1,11 +1,17 @@ import { Button, TabWidget, LineEdit, GroupBox } from "std-widgets.slint"; -enum State { - OpenProject, - SaveProject +export enum State { + Project, + ConvertImage, } -component OpenProjectWidget inherits Rectangle { +component ProjectWidget inherits Rectangle { + Text { + text: "!!Planschbecken!!"; + } +} + +component ConvertImageWidget inherits Rectangle { in-out property path; callback browse_clicked(); @@ -43,17 +49,11 @@ component OpenProjectWidget inherits Rectangle { } } -component SaveProjectWidget inherits Rectangle { - Text { - text: "!!Planschbecken!!"; - } -} - export component FileTab inherits Rectangle { in-out property conv_image_path; + in-out property state; callback conv_image_browse_clicked; - property state; x: 0px; y: 0px; @@ -65,15 +65,15 @@ export component FileTab inherits Rectangle { alignment: start; Button { - text: "Open project"; + text: "Projects"; clicked => { - root.state = State.OpenProject; + root.state = State.Project; } } Button { - text: "Save project"; + text: "Add Image"; clicked => { - root.state = State.SaveProject; + root.state = State.ConvertImage; } } } @@ -81,14 +81,15 @@ export component FileTab inherits Rectangle { VerticalLayout { padding: 4px; alignment: start; - if root.state == State.OpenProject : OpenProjectWidget { + if root.state == State.Project : ProjectWidget { + } + if root.state == State.ConvertImage : ConvertImageWidget { path <=> root.conv_image_path; browse_clicked => { root.conv_image_browse_clicked(); } } - if root.state == State.SaveProject : SaveProjectWidget { - } + } } } \ No newline at end of file