mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 02:36:19 +02:00
[TextExtractor] Extend settings with preferred language (#22901)
* [TextExtractor] Extend settings with preferred language * TextExtractor simplifying code, adding update languages on dropdown opening * fix typo * TextExtractor fixing bug with order of languages
This commit is contained in:
@@ -15,6 +15,8 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
||||
|
||||
public HotkeySettings ActivationShortcut { get; set; }
|
||||
|
||||
public string PreferredLanguage { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
=> JsonSerializer.Serialize(this);
|
||||
}
|
||||
|
||||
@@ -2855,4 +2855,7 @@ Activate by holding the key for the character you want to add an accent to, then
|
||||
<data name="Hosts_AdditionalLinesPosition.Description" xml:space="preserve">
|
||||
<value>Additional content includes the file header and lines that can't parse</value>
|
||||
</data>
|
||||
<data name="TextExtractor_Languages.Header" xml:space="preserve">
|
||||
<value>Preferred language</value>
|
||||
</data>
|
||||
</root>
|
||||
@@ -3,13 +3,18 @@
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using System.Timers;
|
||||
using global::PowerToys.GPOWrapper;
|
||||
using Microsoft.PowerToys.Settings.UI.Library;
|
||||
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
|
||||
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
|
||||
using Windows.Globalization;
|
||||
using Windows.Media.Ocr;
|
||||
|
||||
namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
||||
{
|
||||
@@ -31,6 +36,33 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
||||
private GpoRuleConfigured _enabledGpoRuleConfiguration;
|
||||
private bool _enabledStateIsGPOConfigured;
|
||||
private bool _isEnabled;
|
||||
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)
|
||||
{
|
||||
_powerOcrSettings.Properties.PreferredLanguage = possibleOcrLanguages[_languageIndex].DisplayName;
|
||||
NotifySettingsChanged();
|
||||
}
|
||||
|
||||
OnPropertyChanged(nameof(LanguageIndex));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Func<string, int> SendConfigMSG { get; }
|
||||
|
||||
@@ -130,6 +162,48 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (_powerOcrSettings.Properties.PreferredLanguage?.Equals(language.DisplayName) == true)
|
||||
{
|
||||
preferredLanguageIndex = AvailableLanguages.Count;
|
||||
}
|
||||
|
||||
if (systemCulture.DisplayName.Equals(language.DisplayName) || systemCulture.Parent.DisplayName.Equals(language.DisplayName))
|
||||
{
|
||||
systemLanguageIndex = AvailableLanguages.Count;
|
||||
}
|
||||
|
||||
AvailableLanguages.Add(language.NativeName);
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
private void ScheduleSavingOfSettings()
|
||||
{
|
||||
lock (_delayedActionLock)
|
||||
|
||||
@@ -55,6 +55,18 @@
|
||||
MinWidth="{StaticResource SettingActionControlMinWidth}"
|
||||
HotkeySettings="{x:Bind Path=ViewModel.ActivationShortcut, Mode=TwoWay}" />
|
||||
</labs:SettingsCard>
|
||||
<labs:SettingsCard
|
||||
x:Uid="TextExtractor_Languages">
|
||||
<ComboBox
|
||||
x:Name="TextExtractor_ComboBox"
|
||||
MinWidth="{StaticResource SettingActionControlMinWidth}"
|
||||
ItemsSource="{x:Bind Path=ViewModel.AvailableLanguages, Mode=OneWay}"
|
||||
SelectedIndex="{x:Bind Path=ViewModel.LanguageIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
|
||||
SelectedValue="{Binding SelectedColorRepresentationValue, Mode=TwoWay}"
|
||||
Loaded="TextExtractor_ComboBox_Loaded"
|
||||
DropDownOpened="TextExtractor_ComboBox_DropDownOpened">
|
||||
</ComboBox>
|
||||
</labs:SettingsCard>
|
||||
</controls:SettingsGroup>
|
||||
</StackPanel>
|
||||
</controls:SettingsPageControl.ModuleContent>
|
||||
|
||||
@@ -23,5 +23,21 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
||||
DataContext = ViewModel;
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void TextExtractor_ComboBox_Loaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
|
||||
{
|
||||
/**
|
||||
* UWP hack
|
||||
* because UWP load the bound ItemSource of the ComboBox asynchronous,
|
||||
* so after InitializeComponent() the ItemSource is still empty and can't automatically select a entry.
|
||||
* Selection via SelectedItem and SelectedValue is still not working too
|
||||
*/
|
||||
ViewModel.UpdateLanguages();
|
||||
}
|
||||
|
||||
private void TextExtractor_ComboBox_DropDownOpened(object sender, object e)
|
||||
{
|
||||
ViewModel.UpdateLanguages();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user