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 OpenDirectory and enable TopBar button #1675

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 1 addition & 2 deletions src/NexusMods.App.UI/Controls/TopBar/TopBarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ public TopBarViewModel(
var loggingSettings = settingsManager.Get<LoggingSettings>();
var logDirectory = loggingSettings.MainProcessLogFilePath.ToPath(fileSystem).Parent;
await osInterop.OpenDirectory(logDirectory);
}, // TODO: enable this once OpenDirectory has been implemented
Observable.Return(false));
});

GiveFeedbackCommand = ReactiveCommand.Create(() =>
{
Expand Down
13 changes: 7 additions & 6 deletions src/NexusMods.CrossPlatform/Process/AOSInterop.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using CliWrap;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NexusMods.Paths;

Expand Down Expand Up @@ -31,13 +30,15 @@ protected AOSInterop(ILoggerFactory loggerFactory, IProcessFactory processFactor
protected abstract Command CreateCommand(Uri uri);

/// <inheritdoc/>
public async Task OpenUrl(Uri url, bool fireAndForget = false, CancellationToken cancellationToken = default)
public async Task OpenUrl(Uri url, bool logOutput = false, bool fireAndForget = false, CancellationToken cancellationToken = default)
{
var command = CreateCommand(url);

// NOTE(erri120): don't log the process output of the browser
var isWeb = url.Scheme.Equals("http", StringComparison.OrdinalIgnoreCase) || url.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase);
var task = _processFactory.ExecuteAsync(command, logProcessOutput: !isWeb, cancellationToken: cancellationToken);
var shouldLogOutput = logOutput && !isWeb;

var task = _processFactory.ExecuteAsync(command, logProcessOutput: shouldLogOutput, cancellationToken: cancellationToken);

try
{
Expand All @@ -54,7 +55,7 @@ public async Task OpenUrl(Uri url, bool fireAndForget = false, CancellationToken
}

/// <inheritdoc />
public Task OpenFile(AbsolutePath filePath, bool fireAndForget = false, CancellationToken cancellationToken = default)
public Task OpenFile(AbsolutePath filePath, bool logOutput = false, bool fireAndForget = false, CancellationToken cancellationToken = default)
{
if (!filePath.FileExists)
{
Expand All @@ -66,15 +67,15 @@ public Task OpenFile(AbsolutePath filePath, bool fireAndForget = false, Cancella
}

/// <inheritdoc />
public Task OpenDirectory(AbsolutePath directoryPath, bool fireAndForget = false, CancellationToken cancellationToken = default)
public virtual Task OpenDirectory(AbsolutePath directoryPath, bool logOutput = false, bool fireAndForget = true, CancellationToken cancellationToken = default)
{
if (!directoryPath.DirectoryExists())
{
_logger.LogError("Unable to open directory that doesn't exist at `{Path}`", directoryPath);
return Task.CompletedTask;
}

throw new NotImplementedException();
return OpenUrl(new Uri($"file://{directoryPath.ToNativeSeparators(OSInformation.Shared)}"), logOutput, fireAndForget, cancellationToken);
}

/// <inheritdoc />
Expand Down
11 changes: 7 additions & 4 deletions src/NexusMods.CrossPlatform/Process/IOSInterop.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
using NexusMods.Paths;
using JetBrains.Annotations;
using NexusMods.Paths;

namespace NexusMods.CrossPlatform.Process;

/// <summary>
/// abstractions for functionality that has no platform independent implementation in .NET
/// </summary>
// ReSharper disable once InconsistentNaming
[PublicAPI]
public interface IOSInterop
{
/// <summary>
/// open a url in the default application based on the protocol
/// </summary>
/// <param name="url">URI to open</param>
/// <param name="logOutput">Log the process output</param>
/// <param name="fireAndForget">Start the process but don't wait for the completion</param>
/// <param name="cancellationToken">Cancellation token</param>
Task OpenUrl(Uri url, bool fireAndForget = false, CancellationToken cancellationToken = default);
Task OpenUrl(Uri url, bool logOutput = false, bool fireAndForget = false, CancellationToken cancellationToken = default);

/// <summary>
/// Opens the file with the registered default application.
/// </summary>
Task OpenFile(AbsolutePath filePath, bool fireAndForget = false, CancellationToken cancellationToken = default);
Task OpenFile(AbsolutePath filePath, bool logOutput = false, bool fireAndForget = true, CancellationToken cancellationToken = default);

/// <summary>
/// Opens the directory with the system explorer.
/// </summary>
Task OpenDirectory(AbsolutePath directoryPath, bool fireAndForget = false, CancellationToken cancellationToken = default);
Task OpenDirectory(AbsolutePath directoryPath, bool logOutput = false, bool fireAndForget = true, CancellationToken cancellationToken = default);

/// <summary>
/// Get the path to the current executable
Expand Down
Loading