From bd1326004654191b1d60eade4a179b75a20a3523 Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Mon, 22 Feb 2021 08:06:06 +0100 Subject: [PATCH] Add native helper to open a directory --- plugins/editor/src/editor/NativeHelpers.cpp | 29 +++++++++++++++++++-- plugins/editor/src/editor/NativeHelpers.h | 1 + plugins/editor/src/editor/NativeHelpers.mm | 22 +++++++++------- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/plugins/editor/src/editor/NativeHelpers.cpp b/plugins/editor/src/editor/NativeHelpers.cpp index ab760d73..a8cbb255 100644 --- a/plugins/editor/src/editor/NativeHelpers.cpp +++ b/plugins/editor/src/editor/NativeHelpers.cpp @@ -27,14 +27,29 @@ bool openFileInExternalEditor(const char *filename) return ShellExecuteExW(&info); } + +bool openDirectoryInExplorer(const char *filename) +{ + std::wstring path = fs::u8path(filename).wstring(); + + SHELLEXECUTEINFOW info; + memset(&info, 0, sizeof(info)); + + info.cbSize = sizeof(info); + info.lpVerb = L"explore"; + info.lpFile = path.c_str(); + info.nShow = SW_SHOW; + + return ShellExecuteExW(&info); +} #elif defined(__APPLE__) // implemented in NativeHelpers.mm #else #include -bool openFileInExternalEditor(const char *filename) +static bool openFileByMimeType(const char *filename, const char *mimetype) { - GAppInfo* appinfo = g_app_info_get_default_for_type("text/plain", FALSE); + GAppInfo* appinfo = g_app_info_get_default_for_type(mimetype, FALSE); if (!appinfo) return 1; @@ -47,4 +62,14 @@ bool openFileInExternalEditor(const char *filename) g_object_unref(appinfo); return success == TRUE; } + +bool openFileInExternalEditor(const char *filename) +{ + return openFileByMimeType(filename, "text/plain"); +} + +bool openDirectoryInExplorer(const char *filename) +{ + return openFileByMimeType(filename, "inode/directory"); +} #endif diff --git a/plugins/editor/src/editor/NativeHelpers.h b/plugins/editor/src/editor/NativeHelpers.h index b9c29bb6..3ca310f7 100644 --- a/plugins/editor/src/editor/NativeHelpers.h +++ b/plugins/editor/src/editor/NativeHelpers.h @@ -7,3 +7,4 @@ #pragma once bool openFileInExternalEditor(const char *filename); +bool openDirectoryInExplorer(const char *filename); diff --git a/plugins/editor/src/editor/NativeHelpers.mm b/plugins/editor/src/editor/NativeHelpers.mm index 9cb8cd28..7fea74e8 100644 --- a/plugins/editor/src/editor/NativeHelpers.mm +++ b/plugins/editor/src/editor/NativeHelpers.mm @@ -11,20 +11,24 @@ #import #import -bool openFileInExternalEditor(const char *fileNameUTF8) +static bool openFileWithApplication(const char *fileName, NSString *application) { - BOOL wasOpened = NO; + NSWorkspace* workspace = [NSWorkspace sharedWorkspace]; + NSString* fileNameNs = [NSString stringWithUTF8String:fileName]; + return [workspace openFile:fileNameNs withApplication:application] == YES; +} +bool openFileInExternalEditor(const char *fileName) +{ NSURL* applicationURL = (__bridge_transfer NSURL*)LSCopyDefaultApplicationURLForContentType( kUTTypePlainText, kLSRolesEditor, nil); - if (!applicationURL) + if (!applicationURL || ![applicationURL isFileURL]) return false; - if ([applicationURL isFileURL]) { - NSWorkspace* workspace = [NSWorkspace sharedWorkspace]; - NSString* fileName = [NSString stringWithUTF8String:fileNameUTF8]; - wasOpened = [workspace openFile:fileName withApplication:[applicationURL path]]; - } + return openFileWithApplication(fileName, [applicationURL path]); +} - return wasOpened == YES; +bool openDirectoryInExplorer(const char *fileName) +{ + return openFileWithApplication(fileName, @"Finder"); } #endif