2022-08-27 07:17:20 +08:00
|
|
|
|
// 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;
|
2022-09-02 23:28:56 +08:00
|
|
|
|
using System.Globalization;
|
2022-08-27 07:17:20 +08:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2022-09-02 23:28:56 +08:00
|
|
|
|
using System.Text.Json;
|
2022-10-26 21:02:31 +08:00
|
|
|
|
using global::PowerToys.GPOWrapper;
|
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Library;
|
2022-08-27 07:17:20 +08:00
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
|
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
|
|
|
|
|
|
2022-10-26 21:02:31 +08:00
|
|
|
|
namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
2022-08-27 07:17:20 +08:00
|
|
|
|
{
|
|
|
|
|
public class MeasureToolViewModel : Observable
|
|
|
|
|
{
|
|
|
|
|
private ISettingsUtils SettingsUtils { get; set; }
|
|
|
|
|
|
|
|
|
|
private GeneralSettings GeneralSettingsConfig { get; set; }
|
|
|
|
|
|
2022-10-26 21:02:31 +08:00
|
|
|
|
private GpoRuleConfigured _enabledGpoRuleConfiguration;
|
|
|
|
|
private bool _enabledStateIsGPOConfigured;
|
|
|
|
|
private bool _isEnabled;
|
|
|
|
|
|
2022-08-27 07:17:20 +08:00
|
|
|
|
private MeasureToolSettings Settings { get; set; }
|
|
|
|
|
|
|
|
|
|
public MeasureToolViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<MeasureToolSettings> measureToolSettingsRepository, Func<string, int> ipcMSGCallBackFunc)
|
|
|
|
|
{
|
|
|
|
|
SettingsUtils = settingsUtils;
|
|
|
|
|
|
|
|
|
|
if (settingsRepository == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(settingsRepository));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GeneralSettingsConfig = settingsRepository.SettingsConfig;
|
|
|
|
|
|
2022-10-26 21:02:31 +08:00
|
|
|
|
_enabledGpoRuleConfiguration = GPOWrapper.GetConfiguredScreenRulerEnabledValue();
|
|
|
|
|
if (_enabledGpoRuleConfiguration == GpoRuleConfigured.Disabled || _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled)
|
|
|
|
|
{
|
|
|
|
|
// Get the enabled state from GPO.
|
|
|
|
|
_enabledStateIsGPOConfigured = true;
|
|
|
|
|
_isEnabled = _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_isEnabled = GeneralSettingsConfig.Enabled.MeasureTool;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-27 07:17:20 +08:00
|
|
|
|
if (measureToolSettingsRepository == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(measureToolSettingsRepository));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Settings = measureToolSettingsRepository.SettingsConfig;
|
|
|
|
|
|
|
|
|
|
SendConfigMSG = ipcMSGCallBackFunc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsEnabled
|
|
|
|
|
{
|
2022-10-26 21:02:31 +08:00
|
|
|
|
get => _isEnabled;
|
2022-08-27 07:17:20 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
2022-10-26 21:02:31 +08:00
|
|
|
|
if (_enabledStateIsGPOConfigured)
|
2022-08-27 07:17:20 +08:00
|
|
|
|
{
|
2022-10-26 21:02:31 +08:00
|
|
|
|
// If it's GPO configured, shouldn't be able to change this state.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_isEnabled != value)
|
|
|
|
|
{
|
|
|
|
|
_isEnabled = value;
|
2022-08-27 07:17:20 +08:00
|
|
|
|
GeneralSettingsConfig.Enabled.MeasureTool = value;
|
|
|
|
|
OnPropertyChanged(nameof(IsEnabled));
|
|
|
|
|
|
|
|
|
|
OutGoingGeneralSettings outgoing = new OutGoingGeneralSettings(GeneralSettingsConfig);
|
|
|
|
|
SendConfigMSG(outgoing.ToString());
|
|
|
|
|
|
|
|
|
|
NotifyPropertyChanged();
|
2022-08-31 05:35:34 +08:00
|
|
|
|
NotifyPropertyChanged(nameof(ShowContinuousCaptureWarning));
|
2022-08-27 07:17:20 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-26 21:02:31 +08:00
|
|
|
|
public bool IsEnabledGpoConfigured
|
|
|
|
|
{
|
|
|
|
|
get => _enabledStateIsGPOConfigured;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-27 07:17:20 +08:00
|
|
|
|
public bool ContinuousCapture
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Settings.Properties.ContinuousCapture;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (Settings.Properties.ContinuousCapture != value)
|
|
|
|
|
{
|
|
|
|
|
Settings.Properties.ContinuousCapture = value;
|
|
|
|
|
NotifyPropertyChanged();
|
2022-08-31 05:35:34 +08:00
|
|
|
|
NotifyPropertyChanged(nameof(ShowContinuousCaptureWarning));
|
2022-08-27 07:17:20 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool DrawFeetOnCross
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Settings.Properties.DrawFeetOnCross;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (Settings.Properties.DrawFeetOnCross != value)
|
|
|
|
|
{
|
|
|
|
|
Settings.Properties.DrawFeetOnCross = value;
|
|
|
|
|
NotifyPropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string CrossColor
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Settings.Properties.MeasureCrossColor.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
value = (value != null) ? SettingsUtilities.ToRGBHex(value) : "#FF4500";
|
|
|
|
|
if (!value.Equals(Settings.Properties.MeasureCrossColor.Value, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
Settings.Properties.MeasureCrossColor.Value = value;
|
|
|
|
|
NotifyPropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool PerColorChannelEdgeDetection
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Settings.Properties.PerColorChannelEdgeDetection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (Settings.Properties.PerColorChannelEdgeDetection != value)
|
|
|
|
|
{
|
|
|
|
|
Settings.Properties.PerColorChannelEdgeDetection = value;
|
|
|
|
|
NotifyPropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-05 20:39:56 +08:00
|
|
|
|
public int UnitsOfMeasure
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Settings.Properties.UnitsOfMeasure.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (Settings.Properties.UnitsOfMeasure.Value != value)
|
|
|
|
|
{
|
|
|
|
|
Settings.Properties.UnitsOfMeasure.Value = value;
|
|
|
|
|
NotifyPropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-27 07:17:20 +08:00
|
|
|
|
public int PixelTolerance
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Settings.Properties.PixelTolerance.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (Settings.Properties.PixelTolerance.Value != value)
|
|
|
|
|
{
|
|
|
|
|
Settings.Properties.PixelTolerance.Value = value;
|
|
|
|
|
NotifyPropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public HotkeySettings ActivationShortcut
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Settings.Properties.ActivationShortcut;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (Settings.Properties.ActivationShortcut != value)
|
|
|
|
|
{
|
|
|
|
|
Settings.Properties.ActivationShortcut = value;
|
2022-10-13 15:46:30 +08:00
|
|
|
|
|
2022-08-27 07:17:20 +08:00
|
|
|
|
NotifyPropertyChanged();
|
2022-10-13 15:46:30 +08:00
|
|
|
|
|
2022-09-02 23:28:56 +08:00
|
|
|
|
SendConfigMSG(
|
2022-10-13 15:46:30 +08:00
|
|
|
|
string.Format(
|
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
|
"{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
|
|
|
|
|
MeasureToolSettings.ModuleName,
|
|
|
|
|
JsonSerializer.Serialize(Settings)));
|
2022-08-27 07:17:20 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
|
|
|
|
|
{
|
|
|
|
|
OnPropertyChanged(propertyName);
|
2022-08-31 05:35:34 +08:00
|
|
|
|
if (propertyName == nameof(ShowContinuousCaptureWarning))
|
|
|
|
|
{
|
|
|
|
|
// Don't trigger a settings update if the changed property is for visual notification.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-27 07:17:20 +08:00
|
|
|
|
SettingsUtils.SaveSettings(Settings.ToJsonString(), MeasureToolSettings.ModuleName);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-31 05:35:34 +08:00
|
|
|
|
public bool ShowContinuousCaptureWarning
|
|
|
|
|
{
|
|
|
|
|
get => IsEnabled && ContinuousCapture;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-27 07:17:20 +08:00
|
|
|
|
private Func<string, int> SendConfigMSG { get; }
|
|
|
|
|
}
|
|
|
|
|
}
|