Redirect to file dialog

This commit is contained in:
Jaby 2025-02-15 11:16:49 +01:00
parent 88c8923989
commit c237a9af45
5 changed files with 43 additions and 49 deletions

View File

@ -1,5 +1,5 @@
use crate::MainWindow; use crate::MainWindow;
use super::MainWindowRef; use super::{GUIElementsRef, MainWindowRef, main_tab::MainTab};
pub struct FileTab { pub struct FileTab {
main_window: MainWindowRef main_window: MainWindowRef
@ -10,10 +10,12 @@ impl FileTab {
FileTab{main_window} 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 main_window_cloned = self.main_window.clone();
let gui_cloned = gui_elements.clone();
self.main_window.borrow().on_file_tab_browse_convert_image(move || { 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);
}); });
} }
} }

View File

@ -87,12 +87,4 @@ impl MainTab {
function(&mut gui_cloned.borrow_mut().main_tab, &main_window_cloned.borrow(), idx); 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());
});
}
} }

View File

@ -32,29 +32,14 @@ fn setup_main_tab(gui_elements_ref: Rc<RefCell<GUIElements>>) {
main_tab.remove_vram_file(idx as usize); 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<RefCell<GUIElements>>) { fn setup_file_tab(gui_elements_ref: Rc<RefCell<GUIElements>>) {
let gui_elements = gui_elements_ref.borrow(); let gui_elements = gui_elements_ref.borrow();
gui_elements.file_tab.on_browse_file(move |main_window| { gui_elements.file_tab.on_browse_file(gui_elements_ref.clone(), move |main_window, main_tab| {
let file = FileDialog::new() let file = FileDialog::new()
.add_filter("Images", &["png", "bmp", "jpeg"]) .add_filter("Images (.png; .bmp; .jpeg)", &["png", "bmp", "jpeg"])
.add_filter("All", &[""])
.set_title("Select image file") .set_title("Select image file")
.pick_file(); .pick_file();
@ -62,6 +47,11 @@ fn setup_file_tab(gui_elements_ref: Rc<RefCell<GUIElements>>) {
if let Some(file_path) = file.to_str() { if let Some(file_path) = file.to_str() {
main_window.set_file_tab_browse_path(SharedString::from(file_path)); 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();
} }
}); });
} }

View File

@ -1,5 +1,5 @@
import { AboutTab } from "./tab/about-tab.slint"; 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 { MainTab } from "./tab/main-tab.slint";
import { TabWidget } from "std-widgets.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_file_list <=> main_tab.vram_files;
in-out property main_tab_vram_images <=> main_tab.vram_images; 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 main_tab_remove_file_clicked <=> main_tab.remove_file_clicked;
callback move_vram_image <=> main_tab.move_vram_image; callback move_vram_image <=> main_tab.move_vram_image;
@ -40,6 +39,7 @@ export component MainWindow inherits Window {
main_tab := MainTab { main_tab := MainTab {
x: 0px; x: 0px;
y: 0px; y: 0px;
add_file_clicked => {root.change_to_load_file()}
} }
} }
Tab { Tab {
@ -47,4 +47,13 @@ export component MainWindow inherits Window {
AboutTab {} 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;
}
} }

View File

@ -1,11 +1,17 @@
import { Button, TabWidget, LineEdit, GroupBox } from "std-widgets.slint"; import { Button, TabWidget, LineEdit, GroupBox } from "std-widgets.slint";
enum State { export enum State {
OpenProject, Project,
SaveProject ConvertImage,
} }
component OpenProjectWidget inherits Rectangle { component ProjectWidget inherits Rectangle {
Text {
text: "!!Planschbecken!!";
}
}
component ConvertImageWidget inherits Rectangle {
in-out property <string> path; in-out property <string> path;
callback browse_clicked(); callback browse_clicked();
@ -43,17 +49,11 @@ component OpenProjectWidget inherits Rectangle {
} }
} }
component SaveProjectWidget inherits Rectangle {
Text {
text: "!!Planschbecken!!";
}
}
export component FileTab inherits Rectangle { export component FileTab inherits Rectangle {
in-out property <string> conv_image_path; in-out property <string> conv_image_path;
in-out property <State> state;
callback conv_image_browse_clicked; callback conv_image_browse_clicked;
property <State> state;
x: 0px; x: 0px;
y: 0px; y: 0px;
@ -65,15 +65,15 @@ export component FileTab inherits Rectangle {
alignment: start; alignment: start;
Button { Button {
text: "Open project"; text: "Projects";
clicked => { clicked => {
root.state = State.OpenProject; root.state = State.Project;
} }
} }
Button { Button {
text: "Save project"; text: "Add Image";
clicked => { clicked => {
root.state = State.SaveProject; root.state = State.ConvertImage;
} }
} }
} }
@ -81,14 +81,15 @@ export component FileTab inherits Rectangle {
VerticalLayout { VerticalLayout {
padding: 4px; padding: 4px;
alignment: start; 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; path <=> root.conv_image_path;
browse_clicked => { browse_clicked => {
root.conv_image_browse_clicked(); root.conv_image_browse_clicked();
} }
} }
if root.state == State.SaveProject : SaveProjectWidget {
}
} }
} }
} }