mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-22 14:39:42 +01:00
159 lines
5.2 KiB
C#
159 lines
5.2 KiB
C#
|
|
// 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.Globalization;
|
|||
|
|
using System.Text.Json;
|
|||
|
|
using System.Timers;
|
|||
|
|
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
|
|||
|
|
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
|
|||
|
|
|
|||
|
|
namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
|
|||
|
|
{
|
|||
|
|
public class PowerOcrViewModel : Observable, IDisposable
|
|||
|
|
{
|
|||
|
|
private bool disposedValue;
|
|||
|
|
|
|||
|
|
// Delay saving of settings in order to avoid calling save multiple times and hitting file in use exception. If there is no other request to save settings in given interval, we proceed to save it, otherwise we schedule saving it after this interval
|
|||
|
|
private const int SaveSettingsDelayInMs = 500;
|
|||
|
|
|
|||
|
|
private GeneralSettings GeneralSettingsConfig { get; set; }
|
|||
|
|
|
|||
|
|
private readonly ISettingsUtils _settingsUtils;
|
|||
|
|
private readonly object _delayedActionLock = new object();
|
|||
|
|
|
|||
|
|
private readonly PowerOcrSettings _powerOcrSettings;
|
|||
|
|
private Timer _delayedTimer;
|
|||
|
|
|
|||
|
|
private bool _isEnabled;
|
|||
|
|
|
|||
|
|
private Func<string, int> SendConfigMSG { get; }
|
|||
|
|
|
|||
|
|
public PowerOcrViewModel(
|
|||
|
|
ISettingsUtils settingsUtils,
|
|||
|
|
ISettingsRepository<GeneralSettings> settingsRepository,
|
|||
|
|
Func<string, int> ipcMSGCallBackFunc)
|
|||
|
|
{
|
|||
|
|
// To obtain the general settings configurations of PowerToys Settings.
|
|||
|
|
if (settingsRepository == null)
|
|||
|
|
{
|
|||
|
|
throw new ArgumentNullException(nameof(settingsRepository));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
GeneralSettingsConfig = settingsRepository.SettingsConfig;
|
|||
|
|
|
|||
|
|
// To obtain the settings configurations of Fancy zones.
|
|||
|
|
if (settingsRepository == null)
|
|||
|
|
{
|
|||
|
|
throw new ArgumentNullException(nameof(settingsRepository));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
_settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils));
|
|||
|
|
if (_settingsUtils.SettingsExists(PowerOcrSettings.ModuleName))
|
|||
|
|
{
|
|||
|
|
_powerOcrSettings = _settingsUtils.GetSettingsOrDefault<PowerOcrSettings>(PowerOcrSettings.ModuleName);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
_powerOcrSettings = new PowerOcrSettings();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
_isEnabled = GeneralSettingsConfig.Enabled.PowerOCR;
|
|||
|
|
|
|||
|
|
// set the callback functions value to hangle outgoing IPC message.
|
|||
|
|
SendConfigMSG = ipcMSGCallBackFunc;
|
|||
|
|
|
|||
|
|
_delayedTimer = new Timer();
|
|||
|
|
_delayedTimer.Interval = SaveSettingsDelayInMs;
|
|||
|
|
_delayedTimer.Elapsed += DelayedTimer_Tick;
|
|||
|
|
_delayedTimer.AutoReset = false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public bool IsEnabled
|
|||
|
|
{
|
|||
|
|
get => _isEnabled;
|
|||
|
|
set
|
|||
|
|
{
|
|||
|
|
if (_isEnabled != value)
|
|||
|
|
{
|
|||
|
|
_isEnabled = value;
|
|||
|
|
OnPropertyChanged(nameof(IsEnabled));
|
|||
|
|
|
|||
|
|
// Set the status of PowerOCR in the general settings
|
|||
|
|
GeneralSettingsConfig.Enabled.PowerOCR = value;
|
|||
|
|
var outgoing = new OutGoingGeneralSettings(GeneralSettingsConfig);
|
|||
|
|
|
|||
|
|
SendConfigMSG(outgoing.ToString());
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public HotkeySettings ActivationShortcut
|
|||
|
|
{
|
|||
|
|
get => _powerOcrSettings.Properties.ActivationShortcut;
|
|||
|
|
set
|
|||
|
|
{
|
|||
|
|
if (_powerOcrSettings.Properties.ActivationShortcut != value)
|
|||
|
|
{
|
|||
|
|
_powerOcrSettings.Properties.ActivationShortcut = value;
|
|||
|
|
OnPropertyChanged(nameof(ActivationShortcut));
|
|||
|
|
NotifySettingsChanged();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void ScheduleSavingOfSettings()
|
|||
|
|
{
|
|||
|
|
lock (_delayedActionLock)
|
|||
|
|
{
|
|||
|
|
if (_delayedTimer.Enabled)
|
|||
|
|
{
|
|||
|
|
_delayedTimer.Stop();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
_delayedTimer.Start();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void DelayedTimer_Tick(object sender, EventArgs e)
|
|||
|
|
{
|
|||
|
|
lock (_delayedActionLock)
|
|||
|
|
{
|
|||
|
|
_delayedTimer.Stop();
|
|||
|
|
NotifySettingsChanged();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void NotifySettingsChanged()
|
|||
|
|
{
|
|||
|
|
// Using InvariantCulture as this is an IPC message
|
|||
|
|
SendConfigMSG(
|
|||
|
|
string.Format(
|
|||
|
|
CultureInfo.InvariantCulture,
|
|||
|
|
"{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
|
|||
|
|
PowerOcrSettings.ModuleName,
|
|||
|
|
JsonSerializer.Serialize(_powerOcrSettings)));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
protected virtual void Dispose(bool disposing)
|
|||
|
|
{
|
|||
|
|
if (!disposedValue)
|
|||
|
|
{
|
|||
|
|
if (disposing)
|
|||
|
|
{
|
|||
|
|
_delayedTimer.Dispose();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
disposedValue = true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Dispose()
|
|||
|
|
{
|
|||
|
|
Dispose(disposing: true);
|
|||
|
|
GC.SuppressFinalize(this);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|