Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement native method for PresentFileExternally in MacOS #6365

Merged
merged 4 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions osu.Framework/Platform/MacOS/MacOSGameHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Handlers;
using osu.Framework.Input.Handlers.Mouse;
using osu.Framework.Logging;
using osu.Framework.Platform.MacOS.Native;

namespace osu.Framework.Platform.MacOS
{
Expand Down Expand Up @@ -51,6 +53,29 @@ protected override void Swap()
Renderer.WaitUntilIdle();
}

public override bool PresentFileExternally(string filename)
{
string folderPath = Path.GetDirectoryName(filename);

if (folderPath == null)
{
Logger.Log($"Failed to get directory for {filename}", level: LogLevel.Debug);
return false;
}

if (!File.Exists(filename) && !Directory.Exists(filename))
{
Logger.Log($"Cannot find file for '{filename}'", level: LogLevel.Debug);

// Open the folder without the file selected if we can't find the file
OpenFileExternally(folderPath);
return true;
}

Finder.OpenFolderAndSelectItem(filename);
return true;
}

protected override IEnumerable<InputHandler> CreateAvailableInputHandlers()
{
var handlers = base.CreateAvailableInputHandlers();
Expand Down
20 changes: 20 additions & 0 deletions osu.Framework/Platform/MacOS/Native/Finder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Threading.Tasks;

namespace osu.Framework.Platform.MacOS.Native
{
internal static class Finder
{
private static readonly NSWorkspace shared_workspace = NSWorkspace.SharedWorkspace();

internal static void OpenFolderAndSelectItem(string filename)
{
Task.Run(() =>
cdwcgt marked this conversation as resolved.
Show resolved Hide resolved
{
shared_workspace.SelectFile(filename);
});
}
}
}
25 changes: 25 additions & 0 deletions osu.Framework/Platform/MacOS/Native/NSWorkspace.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;

namespace osu.Framework.Platform.MacOS.Native
{
internal readonly struct NSWorkspace
{
internal IntPtr Handle { get; }

private static readonly IntPtr class_pointer = Class.Get("NSWorkspace");
private static readonly IntPtr sel_shared_workspace = Selector.Get("sharedWorkspace");
private static readonly IntPtr sel_select_file = Selector.Get("selectFile:inFileViewerRootedAtPath:");

internal NSWorkspace(IntPtr handle)
{
Handle = handle;
}

internal static NSWorkspace SharedWorkspace() => new NSWorkspace(Cocoa.SendIntPtr(class_pointer, sel_shared_workspace));

internal bool SelectFile(string file) => Cocoa.SendBool(Handle, sel_select_file, Cocoa.ToNSString(file));
}
}
Loading