2022-08-25 05:25:52 -05: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-12-28 19:54:59 +01:00
using System.Collections.Generic ;
using System.Collections.ObjectModel ;
2022-08-25 05:25:52 -05:00
using System.Globalization ;
2022-12-28 19:54:59 +01:00
using System.Linq ;
2022-08-25 05:25:52 -05:00
using System.Text.Json ;
using System.Timers ;
2022-10-26 14:02:31 +01:00
using global : : PowerToys . GPOWrapper ;
2025-01-13 17:13:16 +02:00
using ManagedCommon ;
2022-10-26 14:02:31 +01:00
using Microsoft.PowerToys.Settings.UI.Library ;
2022-08-25 05:25:52 -05:00
using Microsoft.PowerToys.Settings.UI.Library.Helpers ;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces ;
2025-02-25 02:48:54 +08:00
using Microsoft.PowerToys.Settings.UI.SerializationContext ;
2022-12-28 19:54:59 +01:00
using Windows.Globalization ;
using Windows.Media.Ocr ;
2022-08-25 05:25:52 -05:00
2022-10-26 14:02:31 +01:00
namespace Microsoft.PowerToys.Settings.UI.ViewModels
2022-08-25 05:25:52 -05:00
{
2025-02-25 02:48:54 +08:00
public partial class PowerOcrViewModel : Observable , IDisposable
2022-08-25 05:25:52 -05:00
{
private bool disposedValue ;
Updates for check-spelling v0.0.25 (#40386)
## Summary of the Pull Request
- #39572 updated check-spelling but ignored:
> 🐣 Breaking Changes
[Code Scanning action requires a Code Scanning
Ruleset](https://github.com/check-spelling/check-spelling/wiki/Breaking-Change:-Code-Scanning-action-requires-a-Code-Scanning-Ruleset)
If you use SARIF reporting, then instead of the workflow yielding an ❌
when it fails, it will rely on [github-advanced-security
🤖](https://github.com/apps/github-advanced-security) to report the
failure. You will need to adjust your checks for PRs.
This means that check-spelling hasn't been properly doing its job 😦.
I'm sorry, I should have pushed a thing to this repo earlier,...
Anyway, as with most refreshes, this comes with a number of fixes, some
are fixes for typos that snuck in before the 0.0.25 upgrade, some are
for things that snuck in after, some are based on new rules in
spell-check-this, and some are hand written patterns based on running
through this repository a few times.
About the 🐣 **breaking change**: someone needs to create a ruleset for
this repository (see [Code Scanning action requires a Code Scanning
Ruleset: Sample ruleset
](https://github.com/check-spelling/check-spelling/wiki/Breaking-Change:-Code-Scanning-action-requires-a-Code-Scanning-Ruleset#sample-ruleset)).
The alternative to adding a ruleset is to change the condition to not
use sarif for this repository. In general, I think the github
integration from sarif is prettier/more helpful, so I think that it's
the better choice.
You can see an example of it working in:
- https://github.com/check-spelling-sandbox/PowerToys/pull/23
---------
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: Mike Griese <migrie@microsoft.com>
Co-authored-by: Dustin L. Howett <dustin@howett.net>
2025-07-08 18:16:52 -04:00
// 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
2022-08-25 05:25:52 -05:00
private const int SaveSettingsDelayInMs = 500 ;
private GeneralSettings GeneralSettingsConfig { get ; set ; }
private readonly ISettingsUtils _settingsUtils ;
2024-11-13 12:36:45 -05:00
private readonly System . Threading . Lock _delayedActionLock = new System . Threading . Lock ( ) ;
2022-08-25 05:25:52 -05:00
private readonly PowerOcrSettings _powerOcrSettings ;
private Timer _delayedTimer ;
2022-10-26 14:02:31 +01:00
private GpoRuleConfigured _enabledGpoRuleConfiguration ;
private bool _enabledStateIsGPOConfigured ;
2022-08-25 05:25:52 -05:00
private bool _isEnabled ;
2022-12-28 19:54:59 +01:00
private int _languageIndex ;
private List < Language > possibleOcrLanguages ;
public ObservableCollection < string > AvailableLanguages { get ; } = new ObservableCollection < string > ( ) ;
public int LanguageIndex
{
get
{
return _languageIndex ;
}
set
{
if ( value ! = _languageIndex )
{
_languageIndex = value ;
if ( _powerOcrSettings ! = null & & _languageIndex < possibleOcrLanguages . Count & & _languageIndex > = 0 )
{
2025-01-02 18:34:58 +01:00
_powerOcrSettings . Properties . PreferredLanguage = possibleOcrLanguages [ _languageIndex ] . NativeName ;
2022-12-28 19:54:59 +01:00
NotifySettingsChanged ( ) ;
}
OnPropertyChanged ( nameof ( LanguageIndex ) ) ;
}
}
}
2022-08-25 05:25:52 -05:00
private Func < string , int > SendConfigMSG { get ; }
public PowerOcrViewModel (
ISettingsUtils settingsUtils ,
ISettingsRepository < GeneralSettings > settingsRepository ,
2022-10-13 09:46:30 +02:00
ISettingsRepository < PowerOcrSettings > powerOcrsettingsRepository ,
2022-08-25 05:25:52 -05:00
Func < string , int > ipcMSGCallBackFunc )
{
// To obtain the general settings configurations of PowerToys Settings.
2023-11-22 12:46:59 -05:00
ArgumentNullException . ThrowIfNull ( settingsRepository ) ;
2022-08-25 05:25:52 -05:00
GeneralSettingsConfig = settingsRepository . SettingsConfig ;
// To obtain the settings configurations of Fancy zones.
2023-11-22 12:46:59 -05:00
ArgumentNullException . ThrowIfNull ( settingsRepository ) ;
2022-08-25 05:25:52 -05:00
_settingsUtils = settingsUtils ? ? throw new ArgumentNullException ( nameof ( settingsUtils ) ) ;
2022-10-13 09:46:30 +02:00
2023-11-22 12:46:59 -05:00
ArgumentNullException . ThrowIfNull ( powerOcrsettingsRepository ) ;
2022-08-25 05:25:52 -05:00
2022-10-13 09:46:30 +02:00
_powerOcrSettings = powerOcrsettingsRepository . SettingsConfig ;
2023-01-31 00:00:11 +01:00
InitializeEnabledValue ( ) ;
2023-10-24 11:37:22 +02:00
// set the callback functions value to handle outgoing IPC message.
2023-01-31 00:00:11 +01:00
SendConfigMSG = ipcMSGCallBackFunc ;
_delayedTimer = new Timer ( ) ;
_delayedTimer . Interval = SaveSettingsDelayInMs ;
_delayedTimer . Elapsed + = DelayedTimer_Tick ;
_delayedTimer . AutoReset = false ;
}
private void InitializeEnabledValue ( )
{
2022-10-26 14:02:31 +01:00
_enabledGpoRuleConfiguration = GPOWrapper . GetConfiguredTextExtractorEnabledValue ( ) ;
if ( _enabledGpoRuleConfiguration = = GpoRuleConfigured . Disabled | | _enabledGpoRuleConfiguration = = GpoRuleConfigured . Enabled )
{
// Get the enabled state from GPO.
_enabledStateIsGPOConfigured = true ;
_isEnabled = _enabledGpoRuleConfiguration = = GpoRuleConfigured . Enabled ;
}
else
{
2024-04-02 01:09:47 +02:00
_isEnabled = GeneralSettingsConfig . Enabled . PowerOcr ;
2022-10-26 14:02:31 +01:00
}
2022-08-25 05:25:52 -05:00
}
public bool IsEnabled
{
get = > _isEnabled ;
set
{
2022-10-26 14:02:31 +01:00
if ( _enabledStateIsGPOConfigured )
{
// If it's GPO configured, shouldn't be able to change this state.
return ;
}
2022-08-25 05:25:52 -05:00
if ( _isEnabled ! = value )
{
_isEnabled = value ;
OnPropertyChanged ( nameof ( IsEnabled ) ) ;
2024-04-02 01:09:47 +02:00
// Set the status of PowerOcr in the general settings
GeneralSettingsConfig . Enabled . PowerOcr = value ;
2022-08-25 05:25:52 -05:00
var outgoing = new OutGoingGeneralSettings ( GeneralSettingsConfig ) ;
SendConfigMSG ( outgoing . ToString ( ) ) ;
}
}
}
2023-11-20 16:49:24 +01:00
public bool IsWin11OrGreater
{
get = > OSVersionHelper . IsWindows11 ( ) ;
}
2022-10-26 14:02:31 +01:00
public bool IsEnabledGpoConfigured
{
get = > _enabledStateIsGPOConfigured ;
}
2022-08-25 05:25:52 -05:00
public HotkeySettings ActivationShortcut
{
get = > _powerOcrSettings . Properties . ActivationShortcut ;
set
{
if ( _powerOcrSettings . Properties . ActivationShortcut ! = value )
{
2023-06-20 15:42:04 +02:00
_powerOcrSettings . Properties . ActivationShortcut = value ? ? _powerOcrSettings . Properties . DefaultActivationShortcut ;
2022-08-25 05:25:52 -05:00
OnPropertyChanged ( nameof ( ActivationShortcut ) ) ;
2022-10-13 09:46:30 +02:00
_settingsUtils . SaveSettings ( _powerOcrSettings . ToJsonString ( ) , PowerOcrSettings . ModuleName ) ;
2022-08-25 05:25:52 -05:00
NotifySettingsChanged ( ) ;
}
}
}
2022-12-28 19:54:59 +01:00
internal void UpdateLanguages ( )
{
int preferredLanguageIndex = - 1 ;
int systemLanguageIndex = - 1 ;
CultureInfo systemCulture = CultureInfo . CurrentUICulture ;
// get the list of all installed OCR languages. While processing them, search for the previously preferred language and also for the current ui language
possibleOcrLanguages = OcrEngine . AvailableRecognizerLanguages . OrderBy ( x = > x . NativeName ) . ToList ( ) ;
AvailableLanguages . Clear ( ) ;
foreach ( Language language in possibleOcrLanguages )
{
2023-03-16 15:51:31 +01:00
if ( _powerOcrSettings . Properties . PreferredLanguage ? . Equals ( language . DisplayName , StringComparison . Ordinal ) = = true )
2022-12-28 19:54:59 +01:00
{
preferredLanguageIndex = AvailableLanguages . Count ;
}
2023-03-16 15:51:31 +01:00
if ( systemCulture . DisplayName . Equals ( language . DisplayName , StringComparison . Ordinal ) | | systemCulture . Parent . DisplayName . Equals ( language . DisplayName , StringComparison . Ordinal ) )
2022-12-28 19:54:59 +01:00
{
systemLanguageIndex = AvailableLanguages . Count ;
}
2025-01-02 18:34:58 +01:00
AvailableLanguages . Add ( EnsureStartUpper ( language . NativeName ) ) ;
2022-12-28 19:54:59 +01:00
}
// if the previously stored preferred language is not available (has been deleted or this is the first run with language preference)
if ( preferredLanguageIndex = = - 1 )
{
// try to use the current ui language. If it is also not available, set the first language as preferred (to have any selected language)
if ( systemLanguageIndex > = 0 )
{
preferredLanguageIndex = systemLanguageIndex ;
}
else
{
preferredLanguageIndex = 0 ;
}
}
// set the language index -> the preferred language gets selected in the combo box
LanguageIndex = preferredLanguageIndex ;
}
2022-08-25 05:25:52 -05:00
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 ,
2025-02-25 02:48:54 +08:00
JsonSerializer . Serialize ( _powerOcrSettings , SourceGenerationContextContext . Default . PowerOcrSettings ) ) ) ;
2022-08-25 05:25:52 -05:00
}
2023-01-31 00:00:11 +01:00
public void RefreshEnabledState ( )
{
InitializeEnabledValue ( ) ;
OnPropertyChanged ( nameof ( IsEnabled ) ) ;
}
2022-08-25 05:25:52 -05:00
protected virtual void Dispose ( bool disposing )
{
if ( ! disposedValue )
{
if ( disposing )
{
_delayedTimer . Dispose ( ) ;
}
disposedValue = true ;
}
}
public void Dispose ( )
{
Dispose ( disposing : true ) ;
GC . SuppressFinalize ( this ) ;
}
2025-01-02 18:34:58 +01:00
2025-01-19 15:32:46 +01:00
public string SnippingToolInfoBarMargin
{
// Workaround for wrong StackPanel behavior: On hidden controls the margin is still reserved.
get = > IsWin11OrGreater ? "0,0,0,25" : "0,0,0,0" ;
}
2025-01-02 18:34:58 +01:00
private string EnsureStartUpper ( string input )
{
if ( string . IsNullOrEmpty ( input ) )
{
return input ;
}
var inputArray = input . ToCharArray ( ) ;
inputArray [ 0 ] = char . ToUpper ( inputArray [ 0 ] , CultureInfo . CurrentCulture ) ;
return new string ( inputArray ) ;
}
2022-08-25 05:25:52 -05:00
}
}