File picker cleanup
This commit is contained in:
@@ -246,7 +246,6 @@ project(":desktop"){
|
|||||||
implementation project(":core")
|
implementation project(":core")
|
||||||
implementation arcModule("extensions:profiling")
|
implementation arcModule("extensions:profiling")
|
||||||
implementation arcModule("extensions:discord")
|
implementation arcModule("extensions:discord")
|
||||||
implementation arcModule("natives:natives-filedialogs")
|
|
||||||
implementation arcModule("natives:natives-desktop")
|
implementation arcModule("natives:natives-desktop")
|
||||||
implementation arcModule("natives:natives-freetype-desktop")
|
implementation arcModule("natives:natives-freetype-desktop")
|
||||||
|
|
||||||
@@ -336,7 +335,6 @@ project(":core"){
|
|||||||
api arcModule("extensions:g3d")
|
api arcModule("extensions:g3d")
|
||||||
api arcModule("extensions:fx")
|
api arcModule("extensions:fx")
|
||||||
api arcModule("extensions:arcnet")
|
api arcModule("extensions:arcnet")
|
||||||
implementation arcModule("extensions:filedialogs")
|
|
||||||
api "com.github.Anuken:rhino:$rhinoVersion"
|
api "com.github.Anuken:rhino:$rhinoVersion"
|
||||||
if(localArc && debugged()) api arcModule("extensions:recorder")
|
if(localArc && debugged()) api arcModule("extensions:recorder")
|
||||||
if(localArc) api arcModule(":extensions:packer")
|
if(localArc) api arcModule(":extensions:packer")
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package mindustry.core;
|
package mindustry.core;
|
||||||
|
|
||||||
import arc.*;
|
import arc.*;
|
||||||
import arc.filedialogs.*;
|
|
||||||
import arc.files.*;
|
import arc.files.*;
|
||||||
import arc.func.*;
|
import arc.func.*;
|
||||||
import arc.math.*;
|
import arc.math.*;
|
||||||
@@ -15,7 +14,6 @@ import mindustry.type.*;
|
|||||||
import mindustry.ui.dialogs.*;
|
import mindustry.ui.dialogs.*;
|
||||||
import rhino.*;
|
import rhino.*;
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
|
|
||||||
import static mindustry.Vars.*;
|
import static mindustry.Vars.*;
|
||||||
@@ -142,11 +140,20 @@ public interface Platform{
|
|||||||
* @param title The title of the native dialog
|
* @param title The title of the native dialog
|
||||||
*/
|
*/
|
||||||
default void showFileChooser(boolean open, String title, String extension, Cons<Fi> cons){
|
default void showFileChooser(boolean open, String title, String extension, Cons<Fi> cons){
|
||||||
if(OS.isWindows || OS.isMac || (OS.isLinux && !OS.isAndroid)){
|
defaultFileDialog(open, title, extension, cons);
|
||||||
showNativeFileChooser(open, title, cons, extension);
|
}
|
||||||
}else{
|
|
||||||
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<Fi> cons, String... extensions){
|
||||||
|
defaultMultiFileChooser(cons, extensions);
|
||||||
|
}
|
||||||
|
|
||||||
|
default void showFileChooser(boolean open, String extension, Cons<Fi> cons){
|
||||||
|
showFileChooser(open, open ? "@open": "@save", extension, cons);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void defaultFileDialog(boolean open, String title, String extension, Cons<Fi> cons){
|
static void defaultFileDialog(boolean open, String title, String extension, Cons<Fi> cons){
|
||||||
@@ -159,91 +166,10 @@ public interface Platform{
|
|||||||
}).show();
|
}).show();
|
||||||
}
|
}
|
||||||
|
|
||||||
default void showFileChooser(boolean open, String extension, Cons<Fi> 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<Fi> 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<Fi> cons, String... extensions){
|
static void defaultMultiFileChooser(Cons<Fi> cons, String... extensions){
|
||||||
new FileChooser("@open", file -> Structs.contains(extensions, file.extension().toLowerCase()), true, cons).show();
|
new FileChooser("@open", file -> Structs.contains(extensions, file.extension().toLowerCase()), true, cons).show();
|
||||||
}
|
}
|
||||||
|
|
||||||
default void showNativeFileChooser(boolean open, String title, Cons<Fi> 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. */
|
/** Hide the app. Android only. */
|
||||||
default void hide(){
|
default void hide(){
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -297,7 +297,16 @@ public class DesktopLauncher extends ClientLauncher{
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void showNativeFileChooser(boolean open, String title, Cons<Fi> cons, String... shownExtensions){
|
public void showFileChooser(boolean open, String title, String extension, Cons<Fi> cons){
|
||||||
|
showNativeFileChooser(open, cons, extension);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void showMultiFileChooser(Cons<Fi> cons, String... extensions){
|
||||||
|
showNativeFileChooser(true, cons, extensions);
|
||||||
|
}
|
||||||
|
|
||||||
|
void showNativeFileChooser(boolean open, Cons<Fi> cons, String... shownExtensions){
|
||||||
String[] ext = shownExtensions == null || shownExtensions.length == 0 ? new String[]{""} : shownExtensions;
|
String[] ext = shownExtensions == null || shownExtensions.length == 0 ? new String[]{""} : shownExtensions;
|
||||||
|
|
||||||
SDL_DialogFileFilter.Buffer filters = SDL_DialogFileFilter.calloc(ext.length);
|
SDL_DialogFileFilter.Buffer filters = SDL_DialogFileFilter.calloc(ext.length);
|
||||||
@@ -340,7 +349,7 @@ public class DesktopLauncher extends ClientLauncher{
|
|||||||
if(open){
|
if(open){
|
||||||
SDLDialog.SDL_ShowOpenFileDialog(callback, 0, ((SdlApplication)Core.app).getWindow(), filters, FileChooser.getLastDirectory().absolutePath(), false);
|
SDLDialog.SDL_ShowOpenFileDialog(callback, 0, ((SdlApplication)Core.app).getWindow(), filters, FileChooser.getLastDirectory().absolutePath(), false);
|
||||||
}else{
|
}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();
|
filters.free();
|
||||||
|
|||||||
@@ -138,6 +138,11 @@ public class IOSLauncher extends IOSApplication.Delegate{
|
|||||||
UIApplication.getSharedApplication().getKeyWindow().getRootViewController().presentViewController(cont, true, () -> {});
|
UIApplication.getSharedApplication().getKeyWindow().getRootViewController().presentViewController(cont, true, () -> {});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void showMultiFileChooser(Cons<Fi> cons, String... extensions){
|
||||||
|
showFileChooser(true, extensions[0], cons);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Context getScriptContext(){
|
public Context getScriptContext(){
|
||||||
Context context = Context.getCurrentContext();
|
Context context = Context.getCurrentContext();
|
||||||
|
|||||||
Reference in New Issue
Block a user