Skip to content

Commit

Permalink
Merge pull request #6365 from cdwcgt/PresentFileExternally-macos
Browse files Browse the repository at this point in the history
Implement native method for `PresentFileExternally` in MacOS
  • Loading branch information
peppy authored Aug 27, 2024
2 parents a58e1fe + 63e8cd2 commit 4e3ef47
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
Expand Down Expand Up @@ -78,7 +79,7 @@ protected override void LoadComplete()
),
new ButtonWithDescription
(
() => logStorage.PresentFileExternally(@"runtime.log"),
() => logStorage.PresentFileExternally(logStorage.GetFiles(string.Empty, "*runtime*").First()),
@"show runtime.log",
@"Opens: 'logs' Selected: 'runtime.log'"
),
Expand Down
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 <contact@ppy.sh>. 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(() =>
{
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 <contact@ppy.sh>. 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));
}
}

0 comments on commit 4e3ef47

Please sign in to comment.