macOS: Refactor computing of accepted extensions in file dialogs

Change-Id: Ie71db5a0ab66dd9d157b53297cbb9aba248fa8af
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
This commit is contained in:
Tor Arne Vestbø 2021-01-25 14:39:45 +01:00
parent 9366e034d9
commit 7f8a80ebf8

View File

@ -390,10 +390,21 @@ static const int kReturnCodeNotSet = -1;
m_popupButton.hidden = chooseDirsOnly; // TODO hide the whole sunken pane instead? m_popupButton.hidden = chooseDirsOnly; // TODO hide the whole sunken pane instead?
if (m_options->acceptMode() == QFileDialogOptions::AcceptSave) m_panel.allowedFileTypes = [self computeAllowedFileTypes];
[self recomputeAcceptableExtensionsForSave];
else // Explicitly show extensions if we detect a filter
m_panel.allowedFileTypes = nil; // delegate panel:shouldEnableURL: does the file filtering for NSOpenPanel // that has a multi-part extension. This prevents
// confusing situations where the user clicks e.g.
// 'foo.tar.gz' and 'foo.tar' is populated in the
// file name box, but when then clicking save macOS
// will warn that the file needs to end in .gz,
// due to thinking the user tried to save the file
// as a 'tar' file instead. Unfortunately this
// property can only be set before the panel is
// shown, so it will not have any effect when
// switching filters in an already opened dialog.
if (m_panel.allowedFileTypes.count > 2)
m_panel.extensionHidden = NO;
if (m_panel.visible) if (m_panel.visible)
[m_panel validateVisibleColumns]; [m_panel validateVisibleColumns];
@ -436,8 +447,11 @@ static const int kReturnCodeNotSet = -1;
reduced to their final part, as NSSavePanel does not deal reduced to their final part, as NSSavePanel does not deal
well with multi-part extensions. well with multi-part extensions.
*/ */
- (void)recomputeAcceptableExtensionsForSave - (NSArray<NSString*>*)computeAllowedFileTypes
{ {
if (m_options->acceptMode() != QFileDialogOptions::AcceptSave)
return nil; // panel:shouldEnableURL: does the file filtering for NSOpenPanel
QStringList fileTypes; QStringList fileTypes;
for (const QString &filter : *m_selectedNameFilter) { for (const QString &filter : *m_selectedNameFilter) {
if (!filter.startsWith(QLatin1String("*."))) if (!filter.startsWith(QLatin1String("*.")))
@ -451,24 +465,9 @@ static const int kReturnCodeNotSet = -1;
auto extensions = filter.split('.', Qt::SkipEmptyParts); auto extensions = filter.split('.', Qt::SkipEmptyParts);
fileTypes += extensions.last(); fileTypes += extensions.last();
// Explicitly show extensions if we detect a filter
// that has a multi-part extension. This prevents
// confusing situations where the user clicks e.g.
// 'foo.tar.gz' and 'foo.tar' is populated in the
// file name box, but when then clicking save macOS
// will warn that the file needs to end in .gz,
// due to thinking the user tried to save the file
// as a 'tar' file instead. Unfortunately this
// property can only be set before the panel is
// shown, so it will not have any effect when
// swithcing filters in an already opened dialog.
if (extensions.size() > 2)
m_panel.extensionHidden = NO;
} }
m_panel.allowedFileTypes = fileTypes.isEmpty() ? nil return fileTypes.isEmpty() ? nil : qt_mac_QStringListToNSMutableArray(fileTypes);
: qt_mac_QStringListToNSMutableArray(fileTypes);
} }
- (QString)removeExtensions:(const QString &)filter - (QString)removeExtensions:(const QString &)filter