From 54d6959533ec78d5a462300ff30e12e652bef13c Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Thu, 23 Jan 2025 19:52:39 +0100 Subject: [PATCH] System File Chooser: - always use some window as owner (similar to `JFileChooser`) - Linux: use "Select" for approve button in directory selection --- .../flatlaf/util/SystemFileChooser.java | 22 +++++++++++-------- .../src/main/cpp/GtkFileChooser.cpp | 3 ++- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/SystemFileChooser.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/SystemFileChooser.java index 79c97f9d..53c52861 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/SystemFileChooser.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/SystemFileChooser.java @@ -17,6 +17,7 @@ package com.formdev.flatlaf.util; import java.awt.Component; +import java.awt.KeyboardFocusManager; import java.awt.SecondaryLoop; import java.awt.Toolkit; import java.awt.Window; @@ -617,8 +618,14 @@ public class SystemFileChooser } private int showDialogImpl( Component parent ) { + Window owner = (parent instanceof Window) + ? (Window) parent + : (parent != null) ? SwingUtilities.windowForComponent( parent ) : null; + if( owner == null ) + owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow(); + approveResult = APPROVE_OPTION; - File[] files = getProvider().showDialog( parent, this ); + File[] files = getProvider().showDialog( owner, this ); setSelectedFiles( files ); return (files != null) ? approveResult : CANCEL_OPTION; } @@ -640,7 +647,7 @@ public class SystemFileChooser //---- interface FileChooserProvider -------------------------------------- private interface FileChooserProvider { - File[] showDialog( Component parent, SystemFileChooser fc ); + File[] showDialog( Window owner, SystemFileChooser fc ); } //---- class SystemFileChooserProvider ------------------------------------ @@ -649,10 +656,7 @@ public class SystemFileChooser implements FileChooserProvider { @Override - public File[] showDialog( Component parent, SystemFileChooser fc ) { - Window owner = (parent instanceof Window) - ? (Window) parent - : (parent != null) ? SwingUtilities.windowForComponent( parent ) : null; + public File[] showDialog( Window owner, SystemFileChooser fc ) { AtomicReference filenamesRef = new AtomicReference<>(); // create secondary event look and invoke system file dialog on a new thread @@ -667,7 +671,7 @@ public class SystemFileChooser // fallback to Swing file chooser if system file dialog failed or is not available if( filenames == null ) - return new SwingFileChooserProvider().showDialog( parent, fc ); + return new SwingFileChooserProvider().showDialog( owner, fc ); // canceled? if( filenames.length == 0 ) @@ -1068,7 +1072,7 @@ public class SystemFileChooser implements FileChooserProvider { @Override - public File[] showDialog( Component parent, SystemFileChooser fc ) { + public File[] showDialog( Window owner, SystemFileChooser fc ) { JFileChooser chooser = new JFileChooser() { @Override public void approveSelection() { @@ -1139,7 +1143,7 @@ public class SystemFileChooser chooser.setCurrentDirectory( fc.getCurrentDirectory() ); chooser.setSelectedFile( fc.getSelectedFile() ); - if( chooser.showDialog( parent, null ) != JFileChooser.APPROVE_OPTION ) + if( chooser.showDialog( owner, null ) != JFileChooser.APPROVE_OPTION ) return null; return chooser.isMultiSelectionEnabled() diff --git a/flatlaf-natives/flatlaf-natives-linux/src/main/cpp/GtkFileChooser.cpp b/flatlaf-natives/flatlaf-natives-linux/src/main/cpp/GtkFileChooser.cpp index 1aacde3a..47a64a55 100644 --- a/flatlaf-natives/flatlaf-natives-linux/src/main/cpp/GtkFileChooser.cpp +++ b/flatlaf-natives/flatlaf-natives-linux/src/main/cpp/GtkFileChooser.cpp @@ -194,7 +194,8 @@ JNIEXPORT jobjectArray JNICALL Java_com_formdev_flatlaf_ui_FlatNativeLinuxLibrar selectFolder ? GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER : (open ? GTK_FILE_CHOOSER_ACTION_OPEN : GTK_FILE_CHOOSER_ACTION_SAVE), _("_Cancel"), GTK_RESPONSE_CANCEL, - (cokButtonLabel != NULL) ? cokButtonLabel : (open ? _("_Open") : _("_Save")), GTK_RESPONSE_ACCEPT, + (cokButtonLabel != NULL) ? cokButtonLabel + : (selectFolder ? _("_Select") : (open ? _("_Open") : _("_Save"))), GTK_RESPONSE_ACCEPT, NULL ); // marks end of buttons GtkFileChooser* chooser = GTK_FILE_CHOOSER( dialog );