Skip to content

Commit 12dd613

Browse files
Copilotvogella
andcommitted
Fix: Read filter selection directly from panel's accessory view
The previous fix relied on the cached popup instance variable, which may not be accessible or may have stale state in the delegate callback context. Now we read the filter selection directly from the panel's accessory view at the time the delegate method is called, ensuring we get the current selection that the user actually chose. This should fix the issue where the wrong extension (abcd instead of png) was still being appended even after the previous fix. Co-authored-by: vogella <[email protected]>
1 parent 065b79e commit 12dd613

File tree

1 file changed

+32
-1
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets

1 file changed

+32
-1
lines changed

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/FileDialog.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,39 @@ long panel_userEnteredFilename_confirmed (long id, long sel, long sender, long f
536536
* On newer macOS versions (Sequoia/Tahoe), the filter selection change callback
537537
* may not be called before this method, so we need to update selectedExtension
538538
* based on the current popup selection to ensure we append the correct extension.
539+
*
540+
* We need to get the popup from the panel's accessory view rather than using
541+
* the cached instance variable, as the delegate callback may have a different context.
539542
*/
540-
getSelectedExtensions();
543+
NSView accessoryView = panel.accessoryView();
544+
if (accessoryView != null && accessoryView.isKindOfClass(OS.class_NSPopUpButton)) {
545+
NSPopUpButton currentPopup = new NSPopUpButton(accessoryView.id);
546+
int currentFilterIndex = (int)currentPopup.indexOfSelectedItem();
547+
if (filterExtensions != null && 0 <= currentFilterIndex && currentFilterIndex < filterExtensions.length) {
548+
String exts = filterExtensions[currentFilterIndex];
549+
int index = exts.indexOf(EXTENSION_SEPARATOR);
550+
String[] extensions = index != -1 ? exts.split(";") : new String[] {exts};
551+
if (extensions.length > 0) {
552+
String ext = extensions[0];
553+
String filter = ext.trim();
554+
if (!filter.equals("*") && !filter.equals("*.*")) {
555+
if (filter.startsWith("*.")) {
556+
filter = filter.substring(2);
557+
} else if (filter.startsWith(".")) {
558+
filter = filter.substring(1);
559+
}
560+
// Handle multi-part extensions
561+
int i = filter.lastIndexOf(".");
562+
if (i != -1 && ((i + 1) < filter.length())) {
563+
filter = filter.substring(i + 1);
564+
}
565+
selectedExtension = filter;
566+
} else {
567+
selectedExtension = null;
568+
}
569+
}
570+
}
571+
}
541572
NSString filenameWithExtension = new NSString(filename);
542573
filenameWithExtension = appendSelectedExtension(filenameWithExtension);
543574
return filenameWithExtension.id;

0 commit comments

Comments
 (0)