Skip to content

Commit

Permalink
Add always on top toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanLua committed Aug 17, 2023
1 parent b5c49d2 commit e6793ae
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Fluent Auto Clicker/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public App()
services.AddSingleton<IAppNotificationService, AppNotificationService>();
services.AddSingleton<ILocalSettingsService, LocalSettingsService>();
services.AddSingleton<IThemeSelectorService, ThemeSelectorService>();
services.AddSingleton<IAlwaysOnTopService, AlwaysOnTopService>();
services.AddTransient<INavigationViewService, NavigationViewService>();
services.AddSingleton<IActivationService, ActivationService>();
Expand Down
17 changes: 17 additions & 0 deletions src/Fluent Auto Clicker/Contracts/Services/IAlwaysOnTopService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.UI.Xaml;

namespace Fluent_Auto_Clicker.Contracts.Services;

public interface IAlwaysOnTopService
{
bool AlwaysOnTop
{
get;
}

Task InitializeAsync();

Task SetAlwaysOnTopAsync(bool AlwaysOnTop);

Task SetRequestedAlwaysOnTopAsync();
}
6 changes: 5 additions & 1 deletion src/Fluent Auto Clicker/Services/ActivationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ public class ActivationService : IActivationService
private readonly ActivationHandler<LaunchActivatedEventArgs> _defaultHandler;
private readonly IEnumerable<IActivationHandler> _activationHandlers;
private readonly IThemeSelectorService _themeSelectorService;
private readonly IAlwaysOnTopService _alwaysOnTopService;
private UIElement? _shell = null;

public ActivationService(ActivationHandler<LaunchActivatedEventArgs> defaultHandler, IEnumerable<IActivationHandler> activationHandlers, IThemeSelectorService themeSelectorService)
public ActivationService(ActivationHandler<LaunchActivatedEventArgs> defaultHandler, IEnumerable<IActivationHandler> activationHandlers, IThemeSelectorService themeSelectorService, IAlwaysOnTopService alwaysOnTopService)
{
_defaultHandler = defaultHandler;
_activationHandlers = activationHandlers;
_themeSelectorService = themeSelectorService;
_alwaysOnTopService = alwaysOnTopService;
}

public async Task ActivateAsync(object activationArgs)
Expand Down Expand Up @@ -61,12 +63,14 @@ private async Task HandleActivationAsync(object activationArgs)
private async Task InitializeAsync()
{
await _themeSelectorService.InitializeAsync().ConfigureAwait(false);
await _alwaysOnTopService.InitializeAsync().ConfigureAwait(false);
await Task.CompletedTask;
}

private async Task StartupAsync()
{
await _themeSelectorService.SetRequestedThemeAsync();
await _alwaysOnTopService.SetRequestedAlwaysOnTopAsync();
await Task.CompletedTask;
}
}
55 changes: 55 additions & 0 deletions src/Fluent Auto Clicker/Services/AlwaysOnTopService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Fluent_Auto_Clicker.Contracts.Services;
using Fluent_Auto_Clicker.Helpers;

using Microsoft.UI.Xaml;
using Param_RootNamespace.Helpers;

namespace Fluent_Auto_Clicker.Services;

public class AlwaysOnTopService : IAlwaysOnTopService
{
private const string SettingsKey = "AlwaysOnTopEnabled";

public bool AlwaysOnTop { get; set; } = true;

private readonly ILocalSettingsService _localSettingsService;

public AlwaysOnTopService(ILocalSettingsService localSettingsService)
{
_localSettingsService = localSettingsService;
}

public async Task InitializeAsync()
{
AlwaysOnTop = await LoadAlwaysOnTopSettingAsync();
await Task.CompletedTask;
}

public async Task SetAlwaysOnTopAsync(bool topmost)
{
AlwaysOnTop = topmost;

await SetRequestedAlwaysOnTopAsync();
await SaveAlwaysOnTopSettingAsync(AlwaysOnTop);
}

public async Task SetRequestedAlwaysOnTopAsync()
{
if (App.MainWindow is Window mainWindow)
{
mainWindow.SetIsAlwaysOnTop(AlwaysOnTop);
}

await Task.CompletedTask;
}

private async Task<bool> LoadAlwaysOnTopSettingAsync()
{
return await _localSettingsService.ReadSettingAsync<bool>(SettingsKey);
}

private async Task SaveAlwaysOnTopSettingAsync(bool alwaysOnTopEnabled)
{
await _localSettingsService.SaveSettingAsync(SettingsKey, alwaysOnTopEnabled);
}
}
23 changes: 22 additions & 1 deletion src/Fluent Auto Clicker/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ namespace Fluent_Auto_Clicker.ViewModels;
public class SettingsViewModel : ObservableRecipient
{
private readonly IThemeSelectorService _themeSelectorService;
private readonly IAlwaysOnTopService _alwaysOnTopService;
private ElementTheme _elementTheme;
private string _versionDescription;
private bool _alwaysOnTop;

public bool AlwaysOnTop
{
get => _alwaysOnTop;
set => SetProperty(ref _alwaysOnTop, value);
}

public ElementTheme ElementTheme
{
Expand All @@ -36,9 +44,17 @@ public ICommand SwitchThemeCommand
get;
}

public SettingsViewModel(IThemeSelectorService themeSelectorService)
public ICommand SwitchAlwaysOnTopCommand
{

get;
}

public SettingsViewModel(IThemeSelectorService themeSelectorService, IAlwaysOnTopService alwaysOnTopService)
{
_themeSelectorService = themeSelectorService;
_alwaysOnTopService = alwaysOnTopService;
_alwaysOnTop = _alwaysOnTopService.AlwaysOnTop;
_elementTheme = _themeSelectorService.Theme;
_versionDescription = GetVersionDescription();

Expand All @@ -51,6 +67,11 @@ public SettingsViewModel(IThemeSelectorService themeSelectorService)
await _themeSelectorService.SetThemeAsync(param);
}
});
SwitchAlwaysOnTopCommand = new RelayCommand<bool>(
(param) =>
{
AlwaysOnTop = param;
});
}

private static string GetVersionDescription()
Expand Down
5 changes: 4 additions & 1 deletion src/Fluent Auto Clicker/Views/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@
<TextBlock x:Uid="Settings_Window" />

<StackPanel Margin="{StaticResource XSmallTopMargin}">
<CheckBox x:Uid="Settings_Window_AlwaysOnTop" Content="Always on top" IsChecked="{x:Null}" />
<CheckBox
Command="{x:Bind ViewModel.SwitchAlwaysOnTopCommand}"
x:Uid="Settings_Window_AlwaysOnTop"
IsChecked="{x:Bind ViewModel.AlwaysOnTop, Mode=OneWay}" />
<CheckBox x:Uid="Settings_Window_HideWhenMinimized" Content="Hide when minimized" />
</StackPanel>
</StackPanel>
Expand Down

0 comments on commit e6793ae

Please sign in to comment.