Skip to content

Commit

Permalink
Add FileManagerLauncher implementation for macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
equeim committed Dec 6, 2023
1 parent af6ddf6 commit 032afca
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -242,19 +242,24 @@ if (UNIX)
target_sources(
tremotesf_objects
PRIVATE
filemanagerlauncher_fallback.cpp
ipc/ipcclient_socket.cpp
ipc/ipcserver_socket.cpp
ipc/ipcserver_socket.h
ui/notificationscontroller_fallback.cpp
)

if (APPLE)
target_sources(
tremotesf_objects
PRIVATE
macoshelpers.h
macoshelpers.mm
filemanagerlauncher_macos.mm
)
else ()
target_sources(
tremotesf_objects
PRIVATE
filemanagerlauncher_fallback.cpp
)
endif ()
endif ()
Expand Down
59 changes: 59 additions & 0 deletions src/filemanagerlauncher_macos.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// SPDX-FileCopyrightText: 2015-2023 Alexey Rochev
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "filemanagerlauncher.h"

#include <AppKit/NSWorkspace.h>
#include <QFileInfo>
#include <QWidget>

#include "log/log.h"

namespace tremotesf {
namespace {
class MacosFileManagerLauncher final : public impl::FileManagerLauncher {
Q_OBJECT

public:
MacosFileManagerLauncher() = default;

protected:
void launchFileManagerAndSelectFiles(
std::vector<FilesInDirectory> filesToSelect, QPointer<QWidget> parentWidget
) override {
auto* const workspace = [NSWorkspace sharedWorkspace];
NSMutableArray<NSURL*>* const fileUrls = [NSMutableArray arrayWithCapacity:filesToSelect.size()];
std::vector<QString> fallbackDirectories{};
for (const auto& [dirPath, files] : filesToSelect) {
for (const auto& filePath : files) {
// activateFileViewerSelectingURLs won't work is file does not exist
if (QFileInfo::exists(filePath)) {
auto* const url = [NSURL fileURLWithPath:filePath.toNSString()];
[fileUrls addObject:url];
} else {
if (std::find(fallbackDirectories.begin(), fallbackDirectories.end(), dirPath) ==
fallbackDirectories.end()) {
fallbackDirectories.push_back(dirPath);
}
}
}
}
if (const auto count = [fileUrls count]; count != 0) {
logInfo("Executing NSWorkspace.activateFileViewerSelectingURLs for {} files", count);
[workspace activateFileViewerSelectingURLs:fileUrls];
}
for (const auto& dirPath : fallbackDirectories) {
fallbackForDirectory(dirPath, parentWidget.get());
}
emit done();
}
};
}

namespace impl {
FileManagerLauncher* FileManagerLauncher::createInstance() { return new MacosFileManagerLauncher(); }
}
}

#include "filemanagerlauncher_macos.moc"

0 comments on commit 032afca

Please sign in to comment.