-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Peek]Add setting to close after losing focus (#26364)
* [Peek] WindowActivationState checks are added for focus and close after losing focus. * Add setting to activate the behavior --------- Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
- Loading branch information
1 parent
9786d08
commit 0c69e34
Showing
9 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
|
||
namespace Peek.UI | ||
{ | ||
public interface IUserSettings | ||
{ | ||
public bool CloseAfterLosingFocus { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.IO; | ||
using System.IO.Abstractions; | ||
using System.Threading; | ||
using ManagedCommon; | ||
using Microsoft.PowerToys.Settings.UI.Library; | ||
using Microsoft.PowerToys.Settings.UI.Library.Utilities; | ||
|
||
namespace Peek.UI | ||
{ | ||
public class UserSettings : IUserSettings | ||
{ | ||
private const string PeekModuleName = "Peek"; | ||
private const int MaxNumberOfRetry = 5; | ||
|
||
private readonly ISettingsUtils _settingsUtils; | ||
private readonly IFileSystemWatcher _watcher; | ||
private readonly object _loadingSettingsLock = new object(); | ||
|
||
public bool CloseAfterLosingFocus { get; private set; } | ||
|
||
public UserSettings() | ||
{ | ||
_settingsUtils = new SettingsUtils(); | ||
CloseAfterLosingFocus = false; | ||
|
||
LoadSettingsFromJson(); | ||
|
||
_watcher = Helper.GetFileWatcher(PeekModuleName, "settings.json", () => LoadSettingsFromJson()); | ||
} | ||
|
||
private void LoadSettingsFromJson() | ||
{ | ||
lock (_loadingSettingsLock) | ||
{ | ||
var retry = true; | ||
var retryCount = 0; | ||
|
||
while (retry) | ||
{ | ||
try | ||
{ | ||
retryCount++; | ||
|
||
if (!_settingsUtils.SettingsExists(PeekModuleName)) | ||
{ | ||
Logger.LogInfo("Hosts settings.json was missing, creating a new one"); | ||
var defaultSettings = new PeekSettings(); | ||
defaultSettings.Save(_settingsUtils); | ||
} | ||
|
||
var settings = _settingsUtils.GetSettingsOrDefault<PeekSettings>(PeekModuleName); | ||
if (settings != null) | ||
{ | ||
CloseAfterLosingFocus = settings.Properties.CloseAfterLosingFocus.Value; | ||
} | ||
|
||
retry = false; | ||
} | ||
catch (IOException e) | ||
{ | ||
if (retryCount > MaxNumberOfRetry) | ||
{ | ||
retry = false; | ||
Logger.LogError($"Failed to Deserialize PowerToys settings, Retrying {e.Message}", e); | ||
} | ||
else | ||
{ | ||
Thread.Sleep(500); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
retry = false; | ||
Logger.LogError("Failed to read changed settings", ex); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters