Skip to content

Commit

Permalink
add check && change menu item desktop app
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Feb 21, 2024
1 parent 9bb3320 commit 5e1363b
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ public OpenEditorDesktopService(AppSettings appSettings,
return ( null, "UseLocalDesktop feature toggle is disabled", [] );
}

var subPathAndImageFormatList =
await _openEditorPreflight.PreflightAsync(subPaths, collections);
if ( !_openApplicationNativeService.DetectToUseOpenApplication() )
{
return ( null, "OpenEditor is not supported on this configuration", [] );
}

var subPathAndImageFormatList = await _openEditorPreflight
.PreflightAsync(subPaths, collections);

if ( subPathAndImageFormatList.Count == 0 )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,26 @@ namespace starsky.foundation.native.OpenApplicationNative.Interfaces;

public interface IOpenApplicationNativeService
{
bool? OpenApplicationAtUrl(List<(string, string)> fullPathAndApplicationUrl);
bool? OpenApplicationAtUrl(List<string> fullPaths, string applicationUrl);
/// <summary>
/// Check if the system is supported to open a file
/// Not all configurations are supported
/// </summary>
/// <returns>true is supported and false is not supported</returns>
bool DetectToUseOpenApplication();

/// <summary>
/// Open with Default Editor
/// Please check DetectToUseOpenApplication() before using this method
/// </summary>
/// <param name="fullPathAndApplicationUrl">List first item is fullFilePath, second is ApplicationUrl</param>
/// <returns>open = true, null is unsupported</returns>
bool? OpenApplicationAtUrl(List<(string fullFilePath, string applicationUrl)> fullPathAndApplicationUrl);

/// <summary>
/// Open with Default Editor
/// Please check DetectToUseOpenApplication() before using this method
/// </summary>
/// <param name="fullPaths">Paths on disk</param>
/// <returns>open = true, null is unsupported</returns>
bool? OpenDefault(List<string> fullPaths);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Runtime.InteropServices;
using starsky.foundation.injection;
using starsky.foundation.native.Helpers;
using starsky.foundation.native.OpenApplicationNative.Helpers;
Expand All @@ -8,12 +9,56 @@ namespace starsky.foundation.native.OpenApplicationNative;
[Service(typeof(IOpenApplicationNativeService), InjectionLifetime = InjectionLifetime.Scoped)]
public class OpenApplicationNativeService : IOpenApplicationNativeService
{
/// <summary>
/// Is Open File supported on this configuration
/// </summary>
/// <returns>true if supported, false if not supported</returns>
public bool DetectToUseOpenApplication()
{
return DetectToUseOpenApplicationInternal(RuntimeInformation.IsOSPlatform,
Environment.UserInteractive);
}

/// <summary>
/// Use to overwrite the RuntimeInformation.IsOSPlatform
/// </summary>
internal delegate bool IsOsPlatformDelegate(OSPlatform osPlatform);

/// <summary>
/// Is Open File supported on this configuration
/// </summary>
/// <param name="runtimeInformationIsOsPlatform">RuntimeInformation.IsOSPlatform</param>
/// <param name="environmentUserInteractive">Environment.UserInteractive</param>
/// <returns>true if supported, false if not supported</returns>
internal static bool DetectToUseOpenApplicationInternal(
IsOsPlatformDelegate runtimeInformationIsOsPlatform,
bool environmentUserInteractive)
{
// Linux is not supported yet
if ( runtimeInformationIsOsPlatform(OSPlatform.Linux) ||
runtimeInformationIsOsPlatform(OSPlatform.FreeBSD) )
{
return false;
}

// When running in Windows as Service it does not open the application
// On Mac OS it does open the application
if ( !environmentUserInteractive && runtimeInformationIsOsPlatform(OSPlatform.Windows) )
{
return false;
}

return true;
}


/// <summary>
/// Open file with specified application
/// </summary>
/// <param name="fullPathAndApplicationUrl">List first item is fullFilePath, second is ApplicationUrl</param>
/// <returns>true is operation succeed, false failed | null is platform unsupported</returns>
public bool? OpenApplicationAtUrl(List<(string, string)> fullPathAndApplicationUrl)
public bool? OpenApplicationAtUrl(
List<(string fullFilePath, string applicationUrl)> fullPathAndApplicationUrl)
{
if ( fullPathAndApplicationUrl.Count == 0 )
{
Expand Down Expand Up @@ -42,7 +87,7 @@ public class OpenApplicationNativeService : IOpenApplicationNativeService
/// <param name="fullPaths">full path style</param>
/// <param name="applicationUrl"> applicationUrl</param>
/// <returns>true is operation succeed, false failed | null is platform unsupported</returns>
public bool? OpenApplicationAtUrl(List<string> fullPaths, string applicationUrl)
internal static bool? OpenApplicationAtUrl(List<string> fullPaths, string applicationUrl)
{
var currentPlatform = OperatingSystemHelper.GetPlatform();
var macOsOpenResult = MacOsOpenUrl.OpenApplicationAtUrl(fullPaths,
Expand All @@ -55,7 +100,7 @@ public class OpenApplicationNativeService : IOpenApplicationNativeService
}

internal static List<(List<string>, string)> SortToOpenFilesByApplicationPath(
List<(string, string)> fullPathAndApplicationUrl)
List<(string fullFilePath, string applicationUrl)> fullPathAndApplicationUrl)
{
// Group applications by their names
var groupedApplications = fullPathAndApplicationUrl.GroupBy(x => x.Item2).ToList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ public class FakeIOpenApplicationNativeService : IOpenApplicationNativeService
{
private readonly List<string> _fullFilePaths;
private readonly string _applicationUrl;
private readonly bool _isSupported;

public FakeIOpenApplicationNativeService(List<string> fullPaths, string applicationUrl)
public FakeIOpenApplicationNativeService(List<string> fullPaths, string applicationUrl,
bool isSupported = true)
{
_fullFilePaths = fullPaths;
_applicationUrl = applicationUrl;
_isSupported = isSupported;
}

public string FindPath(List<string> fullPaths)
Expand All @@ -30,6 +33,11 @@ public string FindPath(List<string> fullPaths)
return fullFilePath;
}

public bool DetectToUseOpenApplication()
{
return _isSupported;
}

public bool? OpenApplicationAtUrl(List<(string, string)> fullPathAndApplicationUrl)
{
var filesByApplicationPath =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public async Task OpenAsync_stringInput_HappyFlow()
Assert.AreEqual("/test.jpg", list[0].SubPath);
Assert.AreEqual("test", list[0].AppPath);
}


[TestMethod]
public async Task OpenAsync_ListInput_HappyFlow()
{
Expand Down Expand Up @@ -149,4 +149,25 @@ public async Task OpenAsync_ListInput_UseLocalDesktop_Null()
Assert.AreEqual("UseLocalDesktop feature toggle is disabled", status);
Assert.AreEqual(0, list.Count);
}

[TestMethod]
public async Task OpenAsync_ListInput_UnSupportedPlatform()
{
var fakeService =
new FakeIOpenApplicationNativeService(new List<string>(), string.Empty, false);

var appSettings = new AppSettings { UseLocalDesktop = true };

var preflight = new FakeIOpenEditorPreflight(new List<PathImageFormatExistsAppPathModel>());

var service =
new OpenEditorDesktopService(appSettings, fakeService, preflight);

var (success, status, list) =
( await service.OpenAsync(new List<string> { "/test.jpg" }, true) );

Assert.IsNull(success);
Assert.AreEqual("OpenEditor is not supported on this configuration", status);
Assert.AreEqual(0, list.Count);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using starsky.foundation.native.OpenApplicationNative.Helpers;
using starsky.foundation.platform.Models;
using starskytest.FakeCreateAn.CreateFakeStarskyExe;
using starskytest.starsky.foundation.native.Helpers;

namespace starskytest.starsky.foundation.native.OpenApplicationNative;

Expand Down Expand Up @@ -105,7 +106,7 @@ public async Task Service_OpenDefault_HappyFlow__WindowsOnly()
[TestMethod]
public void OpenApplicationAtUrl_ZeroItems_SoFalse()
{
var result = new OpenApplicationNativeService().OpenApplicationAtUrl([], "app");
var result = OpenApplicationNativeService.OpenApplicationAtUrl([], "app");

// Linux and FreeBSD are not supported
if ( OperatingSystemHelper.GetPlatform() == OSPlatform.Linux ||
Expand Down Expand Up @@ -234,4 +235,65 @@ public void SortToOpenFilesByApplicationPath_MultipleApplications_ReturnsMultipl
Assert.IsTrue(result.Exists(x => x.Item2 == "app2"));
Assert.IsTrue(result.Exists(x => x.Item2 == "app3"));
}

[TestMethod]
public void DetectToUseOpenApplication_Default()
{
var result = new OpenApplicationNativeService().DetectToUseOpenApplication();

// Depending on the environment
if ( !Environment.UserInteractive && new AppSettings().IsWindows )
{
Assert.IsFalse(result);
return;
}

// Linux and FreeBSD are not supported
if ( OperatingSystemHelper.GetPlatform() == OSPlatform.Linux ||
OperatingSystemHelper.GetPlatform() == OSPlatform.FreeBSD )
{
Assert.IsFalse(result);
return;
}

Assert.IsTrue(result);
}

[TestMethod]
public void DetectToUseOpenApplicationInternal_Windows_AsWindowsService_InteractiveFalse()
{
var result =
OpenApplicationNativeService.DetectToUseOpenApplicationInternal(
FakeOsOverwrite.IsWindows,
false);
Assert.IsFalse(result);
}

[TestMethod]
public void DetectToUseOpenApplicationInternal_MacOS_AsLaunchService_InteractiveTrue()
{
var result =
OpenApplicationNativeService.DetectToUseOpenApplicationInternal(FakeOsOverwrite.IsMacOs,
false);
Assert.IsTrue(result);
}

[TestMethod]
public void DetectToUseOpenApplicationInternal_MacOS_Interactive_InteractiveTrue()
{
var result =
OpenApplicationNativeService.DetectToUseOpenApplicationInternal(FakeOsOverwrite.IsMacOs,
true);
Assert.IsTrue(result);
}


[TestMethod]
public void DetectToUseOpenApplicationInternal_Linux_Interactive_Interactive_False()
{
var result =
OpenApplicationNativeService.DetectToUseOpenApplicationInternal(FakeOsOverwrite.IsLinux,
true);
Assert.IsFalse(result);
}
}
17 changes: 4 additions & 13 deletions starskydesktop/src/app/menu/app-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,7 @@ function AppMenu() {
label: "Open in browser",
// eslint-disable-next-line @typescript-eslint/no-misused-promises
click: async () => {
await shell.openExternal(
BrowserWindow.getFocusedWindow().webContents.getURL()
);
await shell.openExternal(BrowserWindow.getFocusedWindow().webContents.getURL());
},
},
],
Expand Down Expand Up @@ -189,12 +187,7 @@ function AppMenu() {
},
{ role: "zoom" },
...(isMac
? [
{ type: "separator" },
{ role: "front" },
{ type: "separator" },
{ role: "window" },
]
? [{ type: "separator" }, { role: "front" }, { type: "separator" }, { role: "window" }]
: [{ role: "close" }]),
],
},
Expand All @@ -205,17 +198,15 @@ function AppMenu() {
label: "Documentation website",
// eslint-disable-next-line @typescript-eslint/no-misused-promises
click: async () => {
await shell.openExternal("https://docs.qdraw.nl/download");
await shell.openExternal("https://docs.qdraw.nl/docs/getting-started/first-steps");
},
},
{
label: "Release overview",
// Referenced from HealthCheckForUpdates
// eslint-disable-next-line @typescript-eslint/no-misused-promises
click: async () => {
await shell.openExternal(
"https://github.com/qdraw/starsky/releases/latest"
);
await shell.openExternal("https://github.com/qdraw/starsky/releases/latest");
},
},
],
Expand Down

0 comments on commit 5e1363b

Please sign in to comment.