Skip to content

Commit

Permalink
Suspend and restore the settings app state using JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
TolikPylypchuk committed Feb 3, 2024
1 parent bd5f367 commit 577616d
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 42 deletions.
4 changes: 2 additions & 2 deletions build/KeyboardSwitch.Build/macos/postinstall-uninstaller-pkg
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
dscl . -list /Users UniqueID | grep -v -e ^_ -e root -e daemon -e nobody | while read -r CURRENT_USER CURRENT_UID ; do
launchctl asuser $CURRENT_UID launchctl remove io.tolik.keyboardswitch

if [ -f "$(eval echo ~$CURRENT_USER)/.keyboard-switch/.setup-configured" ] ; then
rm "$(eval echo ~$CURRENT_USER)/.keyboard-switch/.setup-configured"
if [ -f "$(eval echo ~$CURRENT_USER)/Library/Application Support/Keyboard Switch/.setup-configured" ] ; then
rm "$(eval echo ~$CURRENT_USER)/Library/Application Support/Keyboard Switch/.setup-configured"
fi
done

Expand Down
1 change: 1 addition & 0 deletions src/KeyboardSwitch.Core/Settings/GlobalSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace KeyboardSwitch.Core.Settings;
public sealed class GlobalSettings
{
public string SettingsFilePath { get; set; } = String.Empty;
public string StateFilePath { get; set; } = String.Empty;
public string ServicePath { get; set; } = String.Empty;
public string InitialSetupFilePath { get; set; } = String.Empty;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="akavache" Version="9.1.20" />
<PackageReference Include="DynamicData" Version="8.3.27" />
<PackageReference Include="Fody" Version="6.8.0">
<PrivateAssets>all</PrivateAssets>
Expand Down
4 changes: 0 additions & 4 deletions src/KeyboardSwitch.Settings/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,8 @@ private async Task<MainWindow> CreateMainWindow(MainViewModel viewModel)

if (state.IsInitialized)
{
window.WindowStartupLocation = WindowStartupLocation.Manual;
window.Width = state.WindowWidth;
window.Height = state.WindowHeight;
window.Position = new PixelPoint(state.WindowX, state.WindowY);
window.WindowState = state.IsWindowMaximized ? WindowState.Maximized : WindowState.Normal;
}

Expand Down Expand Up @@ -235,8 +233,6 @@ private void SaveAppState()
{
state.WindowWidth = this.desktop.MainWindow.Width;
state.WindowHeight = this.desktop.MainWindow.Height;
state.WindowX = this.desktop.MainWindow.Position.X;
state.WindowY = this.desktop.MainWindow.Position.Y;
}

state.IsInitialized = true;
Expand Down
1 change: 1 addition & 0 deletions src/KeyboardSwitch.Settings/KeyboardSwitch.Settings.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<PackageReference Include="System.Interactive" Version="6.0.1" />
<PackageReference Include="System.Interactive.Async" Version="6.0.1" />
<PackageReference Include="System.Reactive" Version="6.0.0" />
<PackageReference Include="System.Text.Json" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
18 changes: 0 additions & 18 deletions src/KeyboardSwitch.Settings/State/AkavacheSuspensionDriver.cs

This file was deleted.

14 changes: 0 additions & 14 deletions src/KeyboardSwitch.Settings/State/AppState.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
namespace KeyboardSwitch.Settings.State;

using System.Runtime.Serialization;

[DataContract]
public class AppState
{
[DataMember]
public double WindowWidth { get; set; }

[DataMember]
public double WindowHeight { get; set; }

[DataMember]
public int WindowX { get; set; }

[DataMember]
public int WindowY { get; set; }

[DataMember]
public bool IsWindowMaximized { get; set; }

[DataMember]
public bool IsInitialized { get; set; }
}
2 changes: 1 addition & 1 deletion src/KeyboardSwitch.Settings/State/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ namespace KeyboardSwitch.Settings.State;
public static class Extensions
{
public static IServiceCollection AddSuspensionDriver(this IServiceCollection services) =>
services.AddSingleton<ISuspensionDriver, AkavacheSuspensionDriver<AppState>>();
services.AddSingleton<ISuspensionDriver, JsonSuspensionDriver>();
}
43 changes: 43 additions & 0 deletions src/KeyboardSwitch.Settings/State/JsonSuspensionDriver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace KeyboardSwitch.Settings.State;

using System.Text.Json;

using Microsoft.Extensions.Options;

internal sealed class JsonSuspensionDriver(IOptions<GlobalSettings> settings) : ISuspensionDriver
{
private readonly string file = Environment.ExpandEnvironmentVariables(settings.Value.StateFilePath);

public IObservable<Unit> InvalidateState()
{
if (File.Exists(this.file))
{
File.Delete(this.file);
}

return Observable.Return(Unit.Default);
}

public IObservable<object> LoadState()
{
if (!File.Exists(this.file))
{
return Observable.Return(new AppState());
}

return Observable.FromAsync(() => File.ReadAllTextAsync(this.file))
.Select(content => JsonSerializer.Deserialize(content, SourceGenerationContext.Default.AppState))
.Select(state => state ?? new AppState());
}

public IObservable<Unit> SaveState(object state)
{
if (state is AppState appState)
{
var content = JsonSerializer.Serialize(appState, SourceGenerationContext.Default.AppState);
return Observable.FromAsync(() => File.WriteAllTextAsync(this.file, content));
}

return Observable.Throw<Unit>(new InvalidCastException("AppState must be provided to SaveState"));
}
}
9 changes: 9 additions & 0 deletions src/KeyboardSwitch.Settings/State/SourceGenerationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace KeyboardSwitch.Settings.State;

using System.Text.Json.Serialization;

[JsonSerializable(typeof(AppState))]
[JsonSourceGenerationOptions(WriteIndented = true)]
internal partial class SourceGenerationContext : JsonSerializerContext
{
}
3 changes: 2 additions & 1 deletion src/KeyboardSwitch/appsettings.linux.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"Settings": {
"SettingsFilePath": "%HOME%/.keyboard-switch/settings.db",
"StateFilePath": "%HOME%/.config/keyboard-switch/state.json",
"ServicePath": "./KeyboardSwitch",
"InitialSetupFilePath": "%HOME%/.keyboard-switch/.setup-configured"
"InitialSetupFilePath": "%HOME%/.config/keyboard-switch/.setup-configured"
},
"Startup": {
"StartupFilePath": "%HOME%/.config/autostart/keyboard-switch.desktop"
Expand Down
3 changes: 2 additions & 1 deletion src/KeyboardSwitch/appsettings.macos.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"Settings": {
"SettingsFilePath": "%HOME%/.keyboard-switch/settings.db",
"StateFilePath": "%HOME%/Library/Application Support/Keyboard Switch/state.json",
"ServicePath": "io.tolik.keyboardswitch",
"InitialSetupFilePath": "%HOME%/.keyboard-switch/.setup-configured"
"InitialSetupFilePath": "%HOME%/Library/Application Support/Keyboard Switch/.setup-configured"
},
"Launchd": {
"ServiceName": "io.tolik.keyboardswitch",
Expand Down
1 change: 1 addition & 0 deletions src/KeyboardSwitch/appsettings.windows.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"Settings": {
"SettingsFilePath": "%LocalAppData%\\KeyboardSwitch\\settings.db",
"StateFilePath": "%LocalAppData%\\KeyboardSwitch\\state.json",
"ServicePath": ".\\KeyboardSwitch.exe",
"InitialSetupFilePath": "%LocalAppData%\\KeyboardSwitch\\.setup-configured"
},
Expand Down

0 comments on commit 577616d

Please sign in to comment.