Browse Source

Drop image follows language of window

pull/515/head
Joseph Finney 5 months ago
parent
commit
d5033baa0a
  1. 17
      Text-Grab/Controls/LanguagePicker.xaml.cs
  2. 9
      Text-Grab/Utilities/IoUtilities.cs
  3. 2
      Text-Grab/Views/EditTextWindow.xaml
  4. 15
      Text-Grab/Views/EditTextWindow.xaml.cs

17
Text-Grab/Controls/LanguagePicker.xaml.cs

@ -1,4 +1,5 @@
using System.Collections.ObjectModel;
using System;
using System.Collections.ObjectModel;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using Text_Grab.Utilities; using Text_Grab.Utilities;
@ -51,4 +52,18 @@ public partial class LanguagePicker : UserControl
{ {
LanguageChanged?.Invoke(this, new RoutedEventArgs()); LanguageChanged?.Invoke(this, new RoutedEventArgs());
} }
internal void Select(string ietfLanguageTag)
{
int i = 0;
foreach (object? item in MainComboBox.Items)
{
if (item is Language language && language.LanguageTag == ietfLanguageTag)
{
MainComboBox.SelectedIndex = i;
break;
}
i++;
}
}
} }

9
Text-Grab/Utilities/IoUtilities.cs

@ -3,29 +3,28 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Windows.Globalization;
namespace Text_Grab.Utilities; namespace Text_Grab.Utilities;
public class IoUtilities public class IoUtilities
{ {
public static readonly List<string> ImageExtensions = new() { ".png", ".bmp", ".jpg", ".jpeg", ".tiff", ".gif" };
public static readonly List<string> ImageExtensions = [".png", ".bmp", ".jpg", ".jpeg", ".tiff", ".gif"];
public static async Task<(string TextContent, OpenContentKind SourceKindOfContent)> GetContentFromPath(string pathOfFileToOpen, bool isMultipleFiles = false)
public static async Task<(string TextContent, OpenContentKind SourceKindOfContent)> GetContentFromPath(string pathOfFileToOpen, bool isMultipleFiles = false, Language? language = null)
{ {
StringBuilder stringBuilder = new(); StringBuilder stringBuilder = new();
OpenContentKind openContentKind = OpenContentKind.Image; OpenContentKind openContentKind = OpenContentKind.Image;
if (isMultipleFiles) if (isMultipleFiles)
{
stringBuilder.AppendLine(pathOfFileToOpen); stringBuilder.AppendLine(pathOfFileToOpen);
}
if (ImageExtensions.Contains(Path.GetExtension(pathOfFileToOpen).ToLower())) if (ImageExtensions.Contains(Path.GetExtension(pathOfFileToOpen).ToLower()))
{ {
try try
{ {
stringBuilder.Append(await OcrUtilities.OcrAbsoluteFilePathAsync(pathOfFileToOpen));
stringBuilder.Append(await OcrUtilities.OcrAbsoluteFilePathAsync(pathOfFileToOpen, language));
} }
catch (Exception) catch (Exception)
{ {

2
Text-Grab/Views/EditTextWindow.xaml

@ -389,7 +389,7 @@
x:Name="ReadFolderOfImages" x:Name="ReadFolderOfImages"
Click="ReadFolderOfImages_Click" Click="ReadFolderOfImages_Click"
Header="_Extract Text from Images in Folder..." /> Header="_Extract Text from Images in Folder..." />
<MenuItem x:Name="LanguageMenuItem" Header="Set Window Language..." />
<MenuItem x:Name="LanguageMenuItem" Header="OCR in Language..." />
<MenuItem <MenuItem
x:Name="RecursiveFoldersCheck" x:Name="RecursiveFoldersCheck"
Header="Scan all folders recursively" Header="Scan all folders recursively"

15
Text-Grab/Views/EditTextWindow.xaml.cs

@ -183,7 +183,7 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
if (files is null) if (files is null)
return; return;
List<string> imageFiles = files.Where(x => IoUtilities.ImageExtensions.Contains(Path.GetExtension(x).ToLower())).ToList();
List<string> imageFiles = [.. files.Where(x => IoUtilities.ImageExtensions.Contains(Path.GetExtension(x).ToLower()))];
if (imageFiles.Count == 0) if (imageFiles.Count == 0)
{ {
@ -361,7 +361,8 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
{ {
OpenedFilePath = pathOfFileToOpen; OpenedFilePath = pathOfFileToOpen;
(string TextContent, OpenContentKind KindOpened) = await IoUtilities.GetContentFromPath(pathOfFileToOpen, isMultipleFiles);
Language lang = new(selectedCultureInfo.IetfLanguageTag);
(string TextContent, OpenContentKind KindOpened) = await IoUtilities.GetContentFromPath(pathOfFileToOpen, isMultipleFiles, lang);
if (KindOpened == OpenContentKind.TextFile if (KindOpened == OpenContentKind.TextFile
&& !isMultipleFiles && !isMultipleFiles
@ -787,11 +788,10 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
private void ETWindow_Drop(object sender, System.Windows.DragEventArgs e) private void ETWindow_Drop(object sender, System.Windows.DragEventArgs e)
{ {
// Mark the event as handled, so TextBox's native Drop handler is not called.
if (e.Data.GetDataPresent("Text")) if (e.Data.GetDataPresent("Text"))
return; return;
// Mark the event as handled, so TextBox's native Drop handler is not called.
e.Handled = true; e.Handled = true;
Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait; Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
@ -933,7 +933,7 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
int selectionLength = PassedTextControl.SelectionLength; int selectionLength = PassedTextControl.SelectionLength;
if (string.IsNullOrEmpty(splitString.Last())) if (string.IsNullOrEmpty(splitString.Last()))
splitString = splitString.SkipLast(1).ToArray();
splitString = [.. splitString.SkipLast(1)];
StringBuilder sb = new(); StringBuilder sb = new();
foreach (string line in splitString) foreach (string line in splitString)
@ -1055,7 +1055,6 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
if (LanguageMenuItem is null || sender is not MenuItem clickedMenuItem) if (LanguageMenuItem is null || sender is not MenuItem clickedMenuItem)
return; return;
if (clickedMenuItem.Tag is Language winLang) if (clickedMenuItem.Tag is Language winLang)
{ {
CultureInfo cultureInfo = new(winLang.LanguageTag); CultureInfo cultureInfo = new(winLang.LanguageTag);
@ -1082,6 +1081,10 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
} }
} }
foreach (object? child in BottomBarButtons.Children)
if (child is LanguagePicker languagePicker)
languagePicker.Select(selectedCultureInfo.IetfLanguageTag);
foreach (MenuItem menuItem in LanguageMenuItem.Items) foreach (MenuItem menuItem in LanguageMenuItem.Items)
{ {
menuItem.IsChecked = false; menuItem.IsChecked = false;

Loading…
Cancel
Save