Skip to content

Commit

Permalink
Merge pull request #34 from FaithBeam/install
Browse files Browse the repository at this point in the history
Add Start Menu Installer
  • Loading branch information
FaithBeam authored Oct 20, 2024
2 parents 5b8cdbc + 9e83eba commit 5047f82
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace YMouseButtonControl.Core.Services.StartMenuInstaller;

public interface IStartMenuInstallerService
{
bool InstallStatus();
void Install();
void Uninstall();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using ReactiveUI;
using YMouseButtonControl.Core.Services.Logging;
using YMouseButtonControl.Core.Services.Settings;
using YMouseButtonControl.Core.Services.StartMenuInstaller;
using YMouseButtonControl.Core.Services.Theme;
using YMouseButtonControl.Core.ViewModels.Models;
using YMouseButtonControl.DataAccess.Models;
Expand All @@ -19,18 +20,22 @@ public class GlobalSettingsDialogViewModel : DialogBase, IGlobalSettingsDialogVi
{
private SettingBoolVm _startMinimizedSetting;
private bool _loggingEnabled;
private bool _startMenuChecked;
private SettingIntVm _themeSetting;
private ObservableCollection<ThemeVm> _themeCollection;
private ThemeVm _selectedTheme;
private readonly ObservableAsPropertyHelper<bool>? _applyIsExec;

public GlobalSettingsDialogViewModel(
IStartMenuInstallerService startMenuInstallerService,
IEnableLoggingService enableLoggingService,
ISettingsService settingsService,
IThemeService themeService
)
: base(themeService)
{
StartMenuEnabled = !OperatingSystem.IsMacOS();
_startMenuChecked = StartMenuEnabled && startMenuInstallerService.InstallStatus();
_startMinimizedSetting =
settingsService.GetSetting("StartMinimized") as SettingBoolVm
?? throw new Exception("Error retrieving StartMinimized setting");
Expand All @@ -54,16 +59,20 @@ IThemeService themeService
x => x.LoggingEnabled,
selector: val => val != enableLoggingService.GetLoggingState()
);
var startMenuChanged = this.WhenAnyValue(
x => x.StartMenuChecked,
selector: val => StartMenuEnabled && val != startMenuInstallerService.InstallStatus()
);
var themeChanged = this.WhenAnyValue(
x => x.ThemeSetting.IntValue,
selector: val =>
settingsService.GetSetting("Theme") is not SettingIntVm curVal
|| curVal.IntValue != val
);

var applyIsExecObs = this.WhenAnyValue(x => x.AppIsExec);
var canSave = startMinimizedChanged
.Merge(loggingChanged)
.Merge(startMenuChanged)
.Merge(applyIsExecObs)
.Merge(themeChanged);
ApplyCommand = ReactiveCommand.Create(
Expand All @@ -80,6 +89,22 @@ IThemeService themeService
enableLoggingService.DisableLogging();
}
}

if (
StartMenuEnabled
&& StartMenuChecked != startMenuInstallerService.InstallStatus()
)
{
if (StartMenuChecked)
{
startMenuInstallerService.Install();
}
else
{
startMenuInstallerService.Uninstall();
}
}

using var trn = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);
settingsService.UpdateSetting(StartMinimized);
settingsService.UpdateSetting(ThemeSetting);
Expand All @@ -92,6 +117,14 @@ IThemeService themeService

private bool AppIsExec => _applyIsExec?.Value ?? false;

public bool StartMenuChecked
{
get => _startMenuChecked;
set => this.RaiseAndSetIfChanged(ref _startMenuChecked, value);
}

public bool StartMenuEnabled { get; init; }

public SettingBoolVm StartMinimized
{
get => _startMinimizedSetting;
Expand Down
49 changes: 49 additions & 0 deletions YMouseButtonControl.Linux/Services/StartMenuInstallerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using YMouseButtonControl.Core.Services.StartMenuInstaller;

namespace YMouseButtonControl.Linux.Services;

public class StartMenuInstallerService : IStartMenuInstallerService
{
private const string DesktopFile = """
[Desktop Entry]
Type=Application
Exec={0}
Path={1}
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name=YMouseButtonControl
Comment=YMouseButtonControl
""";

private readonly string _localShare = Environment.GetFolderPath(
Environment.SpecialFolder.LocalApplicationData
);

private readonly string _desktopFilePath;

public StartMenuInstallerService()
{
var applicationsDir = Path.Combine(_localShare, "applications");
_desktopFilePath = Path.Combine(applicationsDir, "YMouseButtonControl.desktop");
}

public bool InstallStatus() =>
File.Exists(_desktopFilePath)
&& File.ReadAllText(_desktopFilePath).Contains($"Exec={GetCurExePath()}");

public void Install() =>
File.WriteAllText(
_desktopFilePath,
string.Format(DesktopFile, GetCurExePath(), GetCurExeParentPath())
);

public void Uninstall() => File.Delete(_desktopFilePath);

private static string GetCurExeParentPath() =>
Path.GetDirectoryName(GetCurExePath())
?? throw new Exception("Error retrieving parent of process path");

private static string GetCurExePath() =>
Environment.ProcessPath ?? throw new Exception("Error retrieving process path");
}
21 changes: 21 additions & 0 deletions YMouseButtonControl.MacOS/Services/StartMenuInstaller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using YMouseButtonControl.Core.Services.StartMenuInstaller;

namespace YMouseButtonControl.MacOS.Services;

public class StartMenuInstaller : IStartMenuInstallerService
{
public bool InstallStatus()
{
throw new System.NotImplementedException();
}

public void Install()
{
throw new System.NotImplementedException();
}

public void Uninstall()
{
throw new System.NotImplementedException();
}
}
70 changes: 70 additions & 0 deletions YMouseButtonControl.Windows/Services/StartMenuInstallerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.IO;
using WindowsShortcutFactory;
using YMouseButtonControl.Core.Services.StartMenuInstaller;

namespace YMouseButtonControl.Windows.Services;

public class StartMenuInstallerService : IStartMenuInstallerService
{
private readonly string _roamingAppDataFolder = Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData
);

private readonly string _roamingYMouseButtonsFolder;

private readonly string _roamingYmouseButtonsShortcutPath;

public StartMenuInstallerService()
{
_roamingYMouseButtonsFolder = Path.Combine(
_roamingAppDataFolder,
"Microsoft",
"Windows",
"Start Menu",
"Programs",
"YMouseButtonControl"
);
_roamingYmouseButtonsShortcutPath = Path.Combine(
_roamingYMouseButtonsFolder,
"YMouseButtonControl.lnk"
);
}

public bool InstallStatus()
{
if (!File.Exists(_roamingYmouseButtonsShortcutPath))
{
return false;
}
using var shortcut = WindowsShortcut.Load(_roamingYmouseButtonsShortcutPath);
return shortcut.Path == GetCurExePath();
}

public void Install()
{
if (File.Exists(_roamingYmouseButtonsShortcutPath))
{
File.Delete(_roamingYmouseButtonsShortcutPath);
}

if (!Directory.Exists(_roamingYMouseButtonsFolder))
{
Directory.CreateDirectory(_roamingYMouseButtonsFolder);
}

using var shortcut = new WindowsShortcut();
shortcut.Path = GetCurExePath();
shortcut.WorkingDirectory = GetCurExeParentPath();
shortcut.Save(_roamingYmouseButtonsShortcutPath);
}

public void Uninstall() => File.Delete(_roamingYmouseButtonsShortcutPath);

private static string GetCurExeParentPath() =>
Path.GetDirectoryName(GetCurExePath())
?? throw new Exception("Error retrieving parent of process path");

private static string GetCurExePath() =>
Environment.ProcessPath ?? throw new Exception("Error retrieving process path");
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
</PackageReference>
<PackageReference Include="ReactiveUI" Version="20.1.1" />
<PackageReference Include="System.Drawing.Common" Version="8.0.8" />
<PackageReference Include="WindowsShortcutFactory" Version="1.2.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using YMouseButtonControl.Core.Services.Processes;
using YMouseButtonControl.Core.Services.Profiles;
using YMouseButtonControl.Core.Services.Settings;
using YMouseButtonControl.Core.Services.StartMenuInstaller;
using YMouseButtonControl.Core.Services.StartupInstaller;
using YMouseButtonControl.Core.Services.Theme;

Expand Down Expand Up @@ -53,6 +54,7 @@ private static void RegisterLinuxServices(IServiceCollection services)
{
services
.AddScoped<IStartupInstallerService, Linux.Services.StartupInstallerService>()
.AddScoped<IStartMenuInstallerService, Linux.Services.StartMenuInstallerService>()
.AddScoped<IProcessMonitorService, Linux.Services.ProcessMonitorService>()
.AddScoped<IBackgroundTasksRunner, Linux.Services.BackgroundTasksRunner>();
if (Environment.GetEnvironmentVariable("XDG_SESSION_TYPE") == "x11")
Expand All @@ -69,6 +71,7 @@ private static void RegisterLinuxServices(IServiceCollection services)
private static void RegisterWindowsServices(IServiceCollection services)
{
services
.AddScoped<IStartMenuInstallerService, Windows.Services.StartMenuInstallerService>()
.AddScoped<IStartupInstallerService, Windows.Services.StartupInstallerService>()
.AddScoped<IProcessMonitorService, Windows.Services.ProcessMonitorService>()
.AddScoped<ICurrentWindowService, Windows.Services.CurrentWindowService>()
Expand All @@ -79,6 +82,7 @@ private static void RegisterMacOsServices(IServiceCollection services)
{
services
.AddScoped<IStartupInstallerService, MacOS.Services.StartupInstallerService>()
.AddScoped<IStartMenuInstallerService, MacOS.Services.StartMenuInstaller>()
.AddScoped<IProcessMonitorService, MacOS.Services.ProcessMonitorService>()
.AddScoped<ICurrentWindowService, MacOS.Services.CurrentWindowService>()
.AddScoped<IBackgroundTasksRunner, MacOS.Services.BackgroundTasksRunner>();
Expand Down
24 changes: 19 additions & 5 deletions YMouseButtonControl/Views/Dialogs/GlobalSettingsDialog.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,32 @@
<mainWindow:GlobalSettingsDialogViewModel />
</Design.DataContext>

<Grid Margin="6" RowDefinitions="Auto,Auto,Auto,*">
<CheckBox Grid.Row="0" Content="Start Minimized"
<Grid Margin="6" RowDefinitions="Auto,Auto,Auto,Auto,*">
<CheckBox Grid.Row="0"
Content="Start Minimized"
IsChecked="{Binding StartMinimized.BoolValue}" />
<CheckBox Grid.Row="1" Content="Logging"
<CheckBox Grid.Row="1"
Content="Start Menu"
IsEnabled="{Binding StartMenuEnabled}"
IsChecked="{Binding StartMenuChecked}">
<ToolTip.Tip>
<TextBlock>
Add YMouseButtonControl to the start menu.
<LineBreak />
Disabled for macOS.
</TextBlock>
</ToolTip.Tip>
</CheckBox>
<CheckBox Grid.Row="2"
Content="Logging"
IsChecked="{Binding LoggingEnabled}">
<ToolTip.Tip>
<TextBlock>
Whether or not logging to file YMouseButtonControl.log is performed. Requires a restart.
</TextBlock>
</ToolTip.Tip>
</CheckBox>
<StackPanel Grid.Row="2">
<StackPanel Grid.Row="3">
<Label Content="Theme" />
<ComboBox ItemsSource="{Binding ThemeCollection}"
SelectedItem="{Binding SelectedTheme}">
Expand All @@ -47,7 +61,7 @@
</TextBlock>
</ToolTip.Tip>
</StackPanel>
<StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Bottom">
<StackPanel Grid.Row="4" Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Bottom">
<Button Content="Apply" Command="{Binding ApplyCommand}" />
<Button Content="Cancel" Click="Cancel_OnClick"></Button>
</StackPanel>
Expand Down
2 changes: 1 addition & 1 deletion YMouseButtonControl/YMouseButtonControl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<!--Avalonia doesen't support TrimMode=link currently,but we are working on that https://github.com/AvaloniaUI/Avalonia/issues/6892 -->
<TrimMode>copyused</TrimMode>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<LangVersion>10</LangVersion>
<LangVersion>default</LangVersion>
<AssemblyVersion>0.1.0</AssemblyVersion>
<FileVersion>0.1.0</FileVersion>
<Version>0.1.0</Version>
Expand Down

0 comments on commit 5047f82

Please sign in to comment.