Skip to content

Commit

Permalink
create a desktop shortcut on first launch
Browse files Browse the repository at this point in the history
  • Loading branch information
hahn-kev committed Dec 3, 2024
1 parent 88afbf5 commit fa2e4a9
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
11 changes: 10 additions & 1 deletion backend/FwLite/FwLiteDesktop/FwLiteDesktopKernel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Windows.ApplicationModel;
using System.Runtime.InteropServices;
using Windows.ApplicationModel;
using FwLiteDesktop.ServerBridge;
using FwLiteShared.Auth;
using LcmCrdt;
Expand Down Expand Up @@ -44,9 +45,17 @@ public static void AddFwLiteDesktopServices(this IServiceCollection services,
services.AddSingleton<IMauiInitializeService>(_ => _.GetRequiredService<ServerManager>());
services.AddHttpClient();
if (IsPackagedApp)
{
services.AddSingleton<IMauiInitializeService, AppUpdateService>();
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
services.AddSingleton<IMauiInitializeService, WindowsShortcutService>();
}
}

services.AddSingleton<IHostEnvironment>(_ => _.GetRequiredService<ServerManager>().WebServices.GetRequiredService<IHostEnvironment>());
services.AddSingleton<IPreferences>(Preferences.Default);
services.AddSingleton<IVersionTracking>(VersionTracking.Default);
configuration.Add<ServerConfigSource>(source => source.ServerManager = serverManager);
services.AddOptions<LocalWebAppConfig>().BindConfiguration("LocalWebApp");
logging.AddFile(Path.Combine(baseDataPath, "app.log"));
Expand Down
4 changes: 4 additions & 0 deletions backend/FwLite/FwLiteDesktop/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public static MauiApp CreateMauiApp()
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
builder.ConfigureEssentials(essentialsBuilder =>
{
essentialsBuilder.UseVersionTracking();
});
builder.ConfigureLifecycleEvents(events => events.AddWindows(windowsEvents =>
{
windowsEvents.OnClosed((window, args) =>
Expand Down
61 changes: 61 additions & 0 deletions backend/FwLite/FwLiteDesktop/WindowsShortcutService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using Windows.ApplicationModel;

namespace FwLiteDesktop;

public class WindowsShortcutService(IVersionTracking versionTracking) : IMauiInitializeService
{
public void Initialize(IServiceProvider services)
{
if (!FwLiteDesktopKernel.IsPackagedApp || !versionTracking.IsFirstLaunchEver || !RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return;
var package = Package.Current;
IShellLink link = (IShellLink)new ShellLink();
link.SetPath($@"shell:AppsFolder\{package.Id.FamilyName}!App");
link.SetIconLocation(Path.Combine(package.InstalledLocation.Path, "logo_light.ico"), 0);
var file = (IPersistFile)link;
file.Save(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Fieldworks Lite.lnk"),
false);
}


[ComImport]
[Guid("00021401-0000-0000-C000-000000000046")]
internal class ShellLink
{
}

[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("000214F9-0000-0000-C000-000000000046")]
internal interface IShellLink
{
void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile,
int cchMaxPath,
out IntPtr pfd,
int fFlags);

void GetIDList(out IntPtr ppidl);
void SetIDList(IntPtr pidl);
void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName);
void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath);
void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath);
void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
void GetHotkey(out short pwHotkey);
void SetHotkey(short wHotkey);
void GetShowCmd(out int piShowCmd);
void SetShowCmd(int iShowCmd);

void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath,
int cchIconPath,
out int piIcon);

void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon);
void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved);
void Resolve(IntPtr hwnd, int fFlags);
void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
}
}

0 comments on commit fa2e4a9

Please sign in to comment.