Add native helper to open a directory

This commit is contained in:
Jean Pierre Cimalando 2021-02-22 08:06:06 +01:00
parent 7627f7c074
commit bd13260046
3 changed files with 41 additions and 11 deletions

View file

@ -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 <gio/gio.h>
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

View file

@ -7,3 +7,4 @@
#pragma once
bool openFileInExternalEditor(const char *filename);
bool openDirectoryInExplorer(const char *filename);

View file

@ -11,20 +11,24 @@
#import <CoreServices/CoreServices.h>
#import <Foundation/Foundation.h>
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