eclipse-platform / eclipse.platform.swt

Eclipse SWT
https://www.eclipse.org/swt/
Eclipse Public License 2.0
114 stars 130 forks source link

FileDialog.getFilterIndex returns -1 on KDE #171

Open Mailaender opened 2 years ago

Mailaender commented 2 years ago

This will return invalid results as in null which seems to be interpreted as 0:

https://github.com/eclipse-platform/eclipse.platform.swt/blob/29dcffed4d3928b1a4540dc0fc7f0d3bfb46de27/bundles/org.eclipse.swt/Eclipse%20SWT/gtk/org/eclipse/swt/widgets/FileDialog.java#L206

and then skip a whole chunk of code so filterIndex stays -1 or whatever you initially set it to if you set several filters with descriptions from which the user has to choose from with no way of selecting none.

As a workaround, you can try to parse the file ending from the returned file name and guess the filter, which is not a very good solution.

SWT version 3.119.0.v20220223-1102

iloveeclipse commented 2 years ago

Please provide a self containing snippet that demonstrates the problem.

Mailaender commented 2 years ago
/*******************************************************************************
 * Copyright (c) 2022 Lablicate GmbH.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Matthias Mailänder - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.snippets;

/*
 * FileDialog example snippet: prompt for a file name (to save)
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;

public class GitHubIssue171 {

public static void main (String [] args) {
    Display display = new Display ();
    Shell shell = new Shell (display);
    shell.setText("GitHub Issue 171");
    shell.open ();
    FileDialog dialog = new FileDialog (shell, SWT.SAVE);
    dialog.setFileName("Example");
    dialog.setText("Save As...");
    dialog.setOverwrite(true);
    String [] filterNames = new String [] {"Character Separated Values", "Excel Sheet", "Text File"};
    String [] filterExtensions = new String [] {"*.csv", "*.xlsx", "*.txt"};
    String filterPath = System.getProperty("user.home");
    dialog.setFilterNames (filterNames);
    dialog.setFilterExtensions (filterExtensions);
    dialog.setFilterPath (filterPath);
    System.out.println ("Save to: " + dialog.open ());
    int i = dialog.getFilterIndex();
    if (i == -1) {
        System.err.println ("Invalid file format selection");
    }
    else {
        System.out.println (filterNames[i] + "(" + filterExtensions[i] + ") selected.");
    }   
    while (!shell.isDisposed ()) {
        if (!display.readAndDispatch ()) display.sleep ();
    }
    display.dispose ();
}
}
joel-majano commented 2 years ago

I ran this snippet on Fedora 36 GTK 3.24.31 (latest swt master) and got expected behavior. I can only get the "Invalid file format selection" message to print if I press "ESC" while the FileDialog is open.

What GTK version/Linux version/Desktop environment are you running?

Mailaender commented 2 years ago

This happens on Ubuntu 20.04 based @KDE Neon which I currently don't have access to and @openSUSE Leap 15.3 KDE Frameworks 5.76.0 Qt 5.12.7 Gtk 3.24.20

akurtakov commented 2 years ago

Do you see KDE file dialog? If that's the case try starting with XDG_DESKTOP_PORTAL=0 and see whether you reproduce the issue.

Mailaender commented 2 years ago

I see a GTK dialog. The KDE Portal is only visible in sandboxed applications like https://github.com/flathub/info.portfolio_performance.PortfolioPerformance