From 7d88eeee7792323d8edd296b850eee8e006c5289 Mon Sep 17 00:00:00 2001 From: Anuken Date: Fri, 6 Feb 2026 01:30:16 -0500 Subject: [PATCH] File picker cleanup --- build.gradle | 2 - core/src/mindustry/core/Platform.java | 102 +++--------------- .../mindustry/desktop/DesktopLauncher.java | 13 ++- ios/src/mindustry/ios/IOSLauncher.java | 5 + 4 files changed, 30 insertions(+), 92 deletions(-) diff --git a/build.gradle b/build.gradle index 09e2bc8303..799bc7d521 100644 --- a/build.gradle +++ b/build.gradle @@ -246,7 +246,6 @@ project(":desktop"){ implementation project(":core") implementation arcModule("extensions:profiling") implementation arcModule("extensions:discord") - implementation arcModule("natives:natives-filedialogs") implementation arcModule("natives:natives-desktop") implementation arcModule("natives:natives-freetype-desktop") @@ -336,7 +335,6 @@ project(":core"){ api arcModule("extensions:g3d") api arcModule("extensions:fx") api arcModule("extensions:arcnet") - implementation arcModule("extensions:filedialogs") api "com.github.Anuken:rhino:$rhinoVersion" if(localArc && debugged()) api arcModule("extensions:recorder") if(localArc) api arcModule(":extensions:packer") diff --git a/core/src/mindustry/core/Platform.java b/core/src/mindustry/core/Platform.java index 81a3f35742..b617768205 100644 --- a/core/src/mindustry/core/Platform.java +++ b/core/src/mindustry/core/Platform.java @@ -1,7 +1,6 @@ package mindustry.core; import arc.*; -import arc.filedialogs.*; import arc.files.*; import arc.func.*; import arc.math.*; @@ -15,7 +14,6 @@ import mindustry.type.*; import mindustry.ui.dialogs.*; import rhino.*; -import java.io.*; import java.net.*; import static mindustry.Vars.*; @@ -142,11 +140,20 @@ public interface Platform{ * @param title The title of the native dialog */ default void showFileChooser(boolean open, String title, String extension, Cons cons){ - if(OS.isWindows || OS.isMac || (OS.isLinux && !OS.isAndroid)){ - showNativeFileChooser(open, title, cons, extension); - }else{ - defaultFileDialog(open, title, extension, cons); - } + defaultFileDialog(open, title, extension, cons); + } + + /** + * Show a file chooser for multiple file types. + * @param cons Selection listener + * @param extensions File extensions to filter + */ + default void showMultiFileChooser(Cons cons, String... extensions){ + defaultMultiFileChooser(cons, extensions); + } + + default void showFileChooser(boolean open, String extension, Cons cons){ + showFileChooser(open, open ? "@open": "@save", extension, cons); } static void defaultFileDialog(boolean open, String title, String extension, Cons cons){ @@ -159,91 +166,10 @@ public interface Platform{ }).show(); } - default void showFileChooser(boolean open, String extension, Cons cons){ - showFileChooser(open, open ? "@open": "@save", extension, cons); - } - - /** - * Show a file chooser for multiple file types. - * @param cons Selection listener - * @param extensions File extensions to filter - */ - default void showMultiFileChooser(Cons cons, String... extensions){ - if(mobile){ - showFileChooser(true, extensions[0], cons); - }else if(OS.isWindows || OS.isMac || (OS.isLinux && !OS.isAndroid)){ - showNativeFileChooser(true, "@open", cons, extensions); - }else{ - defaultMultiFileChooser(cons, extensions); - } - } - static void defaultMultiFileChooser(Cons cons, String... extensions){ new FileChooser("@open", file -> Structs.contains(extensions, file.extension().toLowerCase()), true, cons).show(); } - default void showNativeFileChooser(boolean open, String title, Cons cons, String... shownExtensions){ - String formatted = (title.startsWith("@") ? Core.bundle.get(title.substring(1)) : title).replaceAll("\"", "'"); - - //this should never happen unless someone is being dumb with the parameters - String[] ext = shownExtensions == null || shownExtensions.length == 0 ? new String[]{""} : shownExtensions; - - //native file dialog - Threads.daemon(() -> { - try{ - FileDialogs.loadNatives(); - - String result; - String[] patterns = new String[ext.length]; - for(int i = 0; i < ext.length; i++){ - patterns[i] = "*." + ext[i]; - } - - //on MacOS, .msav is not properly recognized until I put garbage into the array? - if(patterns.length == 1 && OS.isMac && open){ - patterns = new String[]{"", "*." + ext[0]}; - } - - if(open){ - result = FileDialogs.openFileDialog(formatted, FileChooser.getLastDirectory().absolutePath(), patterns, "." + ext[0] + " files", false); - }else{ - result = FileDialogs.saveFileDialog(formatted, FileChooser.getLastDirectory().child("file." + ext[0]).absolutePath(), patterns, "." + ext[0] + " files"); - } - - if(result == null) return; - - if(result.length() > 1 && result.contains("\n")){ - result = result.split("\n")[0]; - } - - //cancelled selection, ignore result - if(result.isEmpty() || result.equals("\n")) return; - if(result.endsWith("\n")) result = result.substring(0, result.length() - 1); - if(result.contains("\n")) throw new IOException("invalid input: \"" + result + "\""); - - Fi file = Core.files.absolute(result); - Core.app.post(() -> { - FileChooser.setLastDirectory(file.isDirectory() ? file : file.parent()); - - if(!open){ - cons.get(file.parent().child(file.nameWithoutExtension() + "." + ext[0])); - }else{ - cons.get(file); - } - }); - }catch(Throwable error){ - Log.err("Failure to execute native file chooser", error); - Core.app.post(() -> { - if(ext.length > 1){ - defaultMultiFileChooser(cons, ext); - }else{ - defaultFileDialog(open, title, ext[0], cons); - } - }); - } - }); - } - /** Hide the app. Android only. */ default void hide(){ } diff --git a/desktop/src/mindustry/desktop/DesktopLauncher.java b/desktop/src/mindustry/desktop/DesktopLauncher.java index 656cc521aa..91f0933b2a 100644 --- a/desktop/src/mindustry/desktop/DesktopLauncher.java +++ b/desktop/src/mindustry/desktop/DesktopLauncher.java @@ -297,7 +297,16 @@ public class DesktopLauncher extends ClientLauncher{ } @Override - public void showNativeFileChooser(boolean open, String title, Cons cons, String... shownExtensions){ + public void showFileChooser(boolean open, String title, String extension, Cons cons){ + showNativeFileChooser(open, cons, extension); + } + + @Override + public void showMultiFileChooser(Cons cons, String... extensions){ + showNativeFileChooser(true, cons, extensions); + } + + void showNativeFileChooser(boolean open, Cons cons, String... shownExtensions){ String[] ext = shownExtensions == null || shownExtensions.length == 0 ? new String[]{""} : shownExtensions; SDL_DialogFileFilter.Buffer filters = SDL_DialogFileFilter.calloc(ext.length); @@ -340,7 +349,7 @@ public class DesktopLauncher extends ClientLauncher{ if(open){ SDLDialog.SDL_ShowOpenFileDialog(callback, 0, ((SdlApplication)Core.app).getWindow(), filters, FileChooser.getLastDirectory().absolutePath(), false); }else{ - SDLDialog.SDL_ShowSaveFileDialog(callback, 0, ((SdlApplication)Core.app).getWindow(), filters, FileChooser.getLastDirectory().absolutePath()); + SDLDialog.SDL_ShowSaveFileDialog(callback, 0, ((SdlApplication)Core.app).getWindow(), filters, FileChooser.getLastDirectory().absolutePath() + "/" + "export." + ext[0]); } filters.free(); diff --git a/ios/src/mindustry/ios/IOSLauncher.java b/ios/src/mindustry/ios/IOSLauncher.java index 82a1200623..ad377c8397 100644 --- a/ios/src/mindustry/ios/IOSLauncher.java +++ b/ios/src/mindustry/ios/IOSLauncher.java @@ -138,6 +138,11 @@ public class IOSLauncher extends IOSApplication.Delegate{ UIApplication.getSharedApplication().getKeyWindow().getRootViewController().presentViewController(cont, true, () -> {}); } + @Override + public void showMultiFileChooser(Cons cons, String... extensions){ + showFileChooser(true, extensions[0], cons); + } + @Override public Context getScriptContext(){ Context context = Context.getCurrentContext();