Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions src/SightAdapt/RuntimeCoordinator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ internal sealed class RuntimeCoordinator
private readonly Func<nint, ApplicationIdentity?> _resolveIdentity;
private readonly Action<string> _showNotification;
private readonly Action<bool> _synchronizeAutomaticMode;
private bool _committingSettings;

public RuntimeCoordinator(
SettingsCoordinator settingsCoordinator,
Expand Down Expand Up @@ -119,7 +120,7 @@ public void ToggleActiveApplicationProfile()
return;
}

var commit = _settingsCoordinator.Commit(settings =>
var commit = CommitSettings(settings =>
{
var result =
ApplicationProfileManagementService.Toggle(
Expand Down Expand Up @@ -148,6 +149,10 @@ public void ToggleActiveApplicationProfile()
{
ResumeAutomaticOperation();
}
else
{
HandleSettingsChanged();
}

_showNotification(result.IsEnabled
? result.WasCreated
Expand All @@ -159,7 +164,7 @@ public void ToggleActiveApplicationProfile()

public void SetAutomaticMode(bool enabled)
{
var commit = _settingsCoordinator.Commit(settings =>
var commit = CommitSettings(settings =>
AutomaticModeManagementService.Set(settings, enabled));

if (!commit.Succeeded)
Expand All @@ -173,6 +178,10 @@ public void SetAutomaticMode(bool enabled)
{
ResumeAutomaticOperation();
}
else
{
HandleSettingsChanged();
}
}

public void HandleForegroundWindowChanged(nint candidate)
Expand All @@ -191,6 +200,11 @@ public void HandleForegroundWindowChanged(nint candidate)

public void HandleSettingsChanged()
{
if (_committingSettings)
{
return;
}

if (_stateController.Current.Kind ==
ApplicationRunState.ManualActive)
{
Expand Down Expand Up @@ -237,7 +251,7 @@ public void EmergencyDisable()
_stateController.SetEmergency(
"All overlays were disabled.");

var commit = _settingsCoordinator.Commit(settings =>
var commit = CommitSettings(settings =>
AutomaticModeManagementService.Disable(settings));

if (commit.Succeeded)
Expand All @@ -261,6 +275,22 @@ public void DisableForExit()
_stateController.SetInactive();
}

private SettingsCommitResult<T> CommitSettings<T>(
Func<SightAdaptSettings, T> mutation)
{
ArgumentNullException.ThrowIfNull(mutation);

_committingSettings = true;
try
{
return _settingsCoordinator.Commit(mutation);
}
finally
{
_committingSettings = false;
}
}

private void ResumeAutomaticOperation()
{
if (_stateController.Current.Kind is
Expand Down Expand Up @@ -427,4 +457,4 @@ private void ShowCommitError(string? message)
? "Settings could not be changed."
: message);
}
}
}
6 changes: 3 additions & 3 deletions src/SightAdapt/SightAdapt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
<Copyright>Copyright © $(Company)</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryUrl>https://github.com/KeyffMS/SightAdapt</RepositoryUrl>
<Version>0.5.0.30</Version>
<Version>0.5.0.31</Version>
<AssemblyVersion>0.5.0.0</AssemblyVersion>
<FileVersion>0.5.0.30</FileVersion>
<InformationalVersion>0.5.0.30</InformationalVersion>
<FileVersion>0.5.0.31</FileVersion>
<InformationalVersion>0.5.0.31</InformationalVersion>
<PlatformTarget>x64</PlatformTarget>
<Platforms>x64</Platforms>
<SupportedOSPlatformVersion>10.0.19041.0</SupportedOSPlatformVersion>
Expand Down
69 changes: 68 additions & 1 deletion tests/SightAdapt.Tests/RuntimeCoordinatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,43 @@ public void ForegroundChangeAutomaticallyActivatesConfiguredTarget()
context.State.Current.VisualProfileId);
}

[TestMethod]
public void ProfileToggleWithSettingsEventActivatesOverlayOnce()
{
using var context = new RuntimeTestContext();
context.AddDisabledAssignment();
context.WireSettingsChanged();

context.Coordinator.ToggleActiveApplicationProfile();

Assert.AreEqual(1, context.Overlay.ActivationCount);
Assert.IsTrue(context.Overlay.IsActive);
Assert.IsTrue(
context.Settings.Current.AutomaticMode);
Assert.AreEqual(
ApplicationRunState.AutomaticActive,
context.State.Current.Kind);
}

[TestMethod]
public void EnablingAutomaticModeWithSettingsEventActivatesOverlayOnce()
{
using var context = new RuntimeTestContext();
context.AddEnabledAssignment();
context.DisableAutomaticMode();
context.WireSettingsChanged();

context.Coordinator.SetAutomaticMode(enabled: true);

Assert.AreEqual(1, context.Overlay.ActivationCount);
Assert.IsTrue(context.Overlay.IsActive);
Assert.IsTrue(
context.Settings.Current.AutomaticMode);
Assert.AreEqual(
ApplicationRunState.AutomaticActive,
context.State.Current.Kind);
}

[TestMethod]
public void EmergencyDisablesOverlayBeforePersistingAutomaticMode()
{
Expand Down Expand Up @@ -141,6 +178,36 @@ public void AddEnabledAssignment()
Assert.IsTrue(result.Succeeded);
}

public void AddDisabledAssignment()
{
var result = Settings.Commit(settings =>
{
var assignment =
ApplicationProfileManagementService.AddOrEnable(
settings,
_identity).Profile;
ApplicationProfileManagementService.SetEnabled(
settings,
assignment,
enabled: false);
AutomaticModeManagementService.Disable(settings);
});
Assert.IsTrue(result.Succeeded);
}

public void DisableAutomaticMode()
{
var result = Settings.Commit(settings =>
AutomaticModeManagementService.Disable(settings));
Assert.IsTrue(result.Succeeded);
}

public void WireSettingsChanged()
{
Settings.Changed += (_, _) =>
Coordinator.HandleSettingsChanged();
}

public void Dispose()
{
if (Directory.Exists(_directory))
Expand Down Expand Up @@ -180,4 +247,4 @@ public void Disable()
TargetWindow = nint.Zero;
}
}
}
}
Loading