Skip to content

Commit

Permalink
Merge pull request #498 from TheJoeFin/post-grab-actions
Browse files Browse the repository at this point in the history
Post grab actions
  • Loading branch information
TheJoeFin authored Dec 3, 2024
2 parents d5840e9 + 43f2767 commit 1012e3c
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0-pre.42">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
5 changes: 5 additions & 0 deletions Text-Grab-Package/Text-Grab-Package.wapproj
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,10 @@
<SkipGetTargetFrameworkProperties>True</SkipGetTargetFrameworkProperties>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="System.Private.Uri" Version="4.3.2" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
</ItemGroup>
<Import Project="$(WapProjPath)\Microsoft.DesktopBridge.targets" />
</Project>
2 changes: 1 addition & 1 deletion Text-Grab/Text-Grab.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<PackageReference Include="Dapplo.Windows.User32" Version="1.0.28" />
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.3" />
<PackageReference Include="System.Drawing.Common" Version="8.0.10" />
<PackageReference Include="System.Drawing.Common" Version="9.0.0" />
<PackageReference Include="WPF-UI" Version="4.0.0-rc.2" />
<PackageReference Include="WPF-UI.Tray" Version="4.0.0-rc.2" />
<PackageReference Include="ZXing.Net" Version="0.16.9" />
Expand Down
5 changes: 4 additions & 1 deletion Text-Grab/Utilities/CustomBottomBarUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,16 @@ public static List<CollapsibleButton> GetBottomBarButtons(EditTextWindow editTex
List<MethodInfo> methods = GetMethods(editTextWindow);
Dictionary<string, RoutedCommand> routedCommands = EditTextWindow.GetRoutedCommands();

int index = 1;

foreach (ButtonInfo buttonItem in GetCustomBottomBarItemsSetting())
{
CollapsibleButton button = new()
{
ButtonText = buttonItem.ButtonText,
IsSymbol = buttonItem.IsSymbol,
CustomButton = buttonItem,
ToolTip = buttonItem.ButtonText,
ToolTip = $"{buttonItem.ButtonText} (ctrl + {index})",
ButtonSymbol = buttonItem.SymbolIcon
};

Expand All @@ -97,6 +99,7 @@ public static List<CollapsibleButton> GetBottomBarButtons(EditTextWindow editTex
button.Command = routedCommand;

bottomBarButtons.Add(button);
index++;
}

return bottomBarButtons;
Expand Down
9 changes: 9 additions & 0 deletions Text-Grab/Utilities/StringMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ public static (int, int) CursorWordBoundaries(this string input, int cursorPosit
if (cursorPosition < 0)
cursorPosition = 0;

try
{
char check = input[cursorPosition];
}
catch (IndexOutOfRangeException)
{
return (cursorPosition, 0);
}

// Check if the cursor is at a space
if (char.IsWhiteSpace(input[cursorPosition]))
cursorPosition = FindNearestLetterIndex(input, cursorPosition);
Expand Down
66 changes: 66 additions & 0 deletions Text-Grab/Views/FullscreenGrab.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,72 @@
</ToggleButton>
</StackPanel>
</Border>
<wpfui:DropDownButton
x:Name="NextStepDropDownButton"
Height="34"
Margin="2,0"
IsDefault="True"
ToolTip="Actions to perform after grabbing text...">
<wpfui:DropDownButton.Icon>
<wpfui:SymbolIcon Symbol="FlashFlow24" />
</wpfui:DropDownButton.Icon>
<wpfui:DropDownButton.Flyout>
<ContextMenu
MouseEnter="RegionClickCanvas_MouseEnter"
MouseLeave="RegionClickCanvas_MouseLeave"
PreviewKeyDown="FullscreenGrab_KeyDown">
<MenuItem
Name="GuidFixMenuItem"
Click="PostActionMenuItem_Click"
Header="Fix GUIDs"
InputGestureText="CTRL + 1"
IsCheckable="True"
StaysOpenOnClick="False" />
<MenuItem
Name="TrimEachLineMenuItem"
Click="PostActionMenuItem_Click"
Header="Trim each line"
InputGestureText="CTRL + 2"
IsCheckable="True"
StaysOpenOnClick="False" />
<MenuItem
Name="RemoveDuplicatesMenuItem"
Click="PostActionMenuItem_Click"
Header="Remove duplicate lines"
InputGestureText="CTRL + 3"
IsCheckable="True"
StaysOpenOnClick="False" />
<MenuItem
Name="BingSearchPostCapture"
Click="PostActionMenuItem_Click"
Header="Bing Search"
InputGestureText="CTRL + 4"
IsCheckable="True"
StaysOpenOnClick="False" />
<MenuItem
Name="GoogleSearchPostCapture"
Click="PostActionMenuItem_Click"
Header="Google Search"
InputGestureText="CTRL + 5"
IsCheckable="True"
StaysOpenOnClick="False" />
<MenuItem
Name="DuckSearchPostCapture"
Click="PostActionMenuItem_Click"
Header="Duck Duck Go Search"
InputGestureText="CTRL + 6"
IsCheckable="True"
StaysOpenOnClick="False" />
<MenuItem
Name="InsertPostCapture"
Click="PostActionMenuItem_Click"
Header="Try to insert text"
InputGestureText="CTRL + 7"
IsCheckable="True"
StaysOpenOnClick="False" />
</ContextMenu>
</wpfui:DropDownButton.Flyout>
</wpfui:DropDownButton>
<ToggleButton
x:Name="SendToEditTextToggleButton"
Width="34"
Expand Down
113 changes: 97 additions & 16 deletions Text-Grab/Views/FullscreenGrab.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Net;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
Expand Down Expand Up @@ -182,8 +183,23 @@ internal void KeyPressed(Key key, bool? isActive = null)
case Key.D8:
case Key.D9:
int numberPressed = (int)key - 34; // D1 casts to 35, D2 to 36, etc.
int numberOfLanguages = LanguagesComboBox.Items.Count;

if (KeyboardExtensions.IsCtrlDown())
{
if (NextStepDropDownButton.Flyout is not ContextMenu flyoutMenu
|| !flyoutMenu.HasItems
|| numberPressed - 1 >= flyoutMenu.Items.Count
|| flyoutMenu.Items[numberPressed - 1] is not MenuItem selectedItem)
{
return;
}

selectedItem.IsChecked = !selectedItem.IsChecked;
CheckIfAnyPostActionsSelcted();
return;
}

int numberOfLanguages = LanguagesComboBox.Items.Count;
if (numberPressed <= numberOfLanguages
&& numberPressed - 1 >= 0
&& numberPressed - 1 != LanguagesComboBox.SelectedIndex
Expand All @@ -195,6 +211,25 @@ internal void KeyPressed(Key key, bool? isActive = null)
}
}

private void CheckIfAnyPostActionsSelcted()
{
if (NextStepDropDownButton.Flyout is not ContextMenu flyoutMenu || !flyoutMenu.HasItems)
return;

foreach (object anyItem in flyoutMenu.Items)
{
if (anyItem is MenuItem item && item.IsChecked)
{
if (FindResource("DarkTeal") is SolidColorBrush tealButtonStyle)
NextStepDropDownButton.Background = tealButtonStyle;
return;
}
}

if (FindResource("ControlFillColorDefaultBrush") is SolidColorBrush SymbolButtonStyle)
NextStepDropDownButton.Background = SymbolButtonStyle;
}

private static bool CheckIfCheckingOrUnchecking(object? sender)
{
bool isActive = false;
Expand Down Expand Up @@ -637,26 +672,67 @@ private async void RegionClickCanvas_MouseUp(object sender, MouseButtonEventArgs
};
}

if (!string.IsNullOrWhiteSpace(TextFromOCR))
if (string.IsNullOrWhiteSpace(TextFromOCR))
{
if (SendToEditTextToggleButton.IsChecked is true && destinationTextBox is null)
{
EditTextWindow etw = WindowUtilities.OpenOrActivateWindow<EditTextWindow>();
destinationTextBox = etw.PassedTextControl;
}
BackgroundBrush.Opacity = .2;
TopButtonsStackPanel.Visibility = Visibility.Visible;
return;
}

if (GuidFixMenuItem.IsChecked is true)
TextFromOCR = TextFromOCR.CorrectCommonGuidErrors();

OutputUtilities.HandleTextFromOcr(
TextFromOCR,
isSingleLine,
isTable,
destinationTextBox);
WindowUtilities.CloseAllFullscreenGrabs();
if (TrimEachLineMenuItem.IsChecked is true)
{
string workingString = TextFromOCR;
string[] stringSplit = workingString.Split(Environment.NewLine);

string finalString = "";
foreach (string line in stringSplit)
if (!string.IsNullOrWhiteSpace(line))
finalString += line.Trim() + Environment.NewLine;

TextFromOCR = finalString;
}
else

if (RemoveDuplicatesMenuItem.IsChecked is true)
TextFromOCR = TextFromOCR.RemoveDuplicateLines();

if (BingSearchPostCapture.IsChecked is true)
{
BackgroundBrush.Opacity = .2;
TopButtonsStackPanel.Visibility = Visibility.Visible;
string searchStringUrlSafe = WebUtility.UrlEncode(TextFromOCR.MakeStringSingleLine());
_ = await Windows.System.Launcher.LaunchUriAsync(new Uri(string.Format($"https://www.bing.com/search?q={searchStringUrlSafe}")));
}

if (GoogleSearchPostCapture.IsChecked is true)
{
string searchStringUrlSafe = WebUtility.UrlEncode(TextFromOCR.MakeStringSingleLine());
_ = await Windows.System.Launcher.LaunchUriAsync(new Uri(string.Format($"https://www.google.com/search?q={searchStringUrlSafe}")));
}

if (DuckSearchPostCapture.IsChecked is true)
{
string searchStringUrlSafe = WebUtility.UrlEncode(TextFromOCR.MakeStringSingleLine());
_ = await Windows.System.Launcher.LaunchUriAsync(new Uri(string.Format($"https://duckduckgo.com/?va=d&t=he&q={searchStringUrlSafe}&ia=web")));
}

if (SendToEditTextToggleButton.IsChecked is true
&& destinationTextBox is null
&& BingSearchPostCapture.IsChecked is false)
{
EditTextWindow etw = WindowUtilities.OpenOrActivateWindow<EditTextWindow>();
destinationTextBox = etw.PassedTextControl;
}

OutputUtilities.HandleTextFromOcr(
TextFromOCR,
isSingleLine,
isTable,
destinationTextBox);
WindowUtilities.CloseAllFullscreenGrabs();

if (InsertPostCapture.IsChecked is true && !DefaultSettings.TryInsert)
await WindowUtilities.TryInsertString(TextFromOCR);
}

private void SendToEditTextToggleButton_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -805,5 +881,10 @@ private void TableToggleButton_Click(object? sender = null, RoutedEventArgs? e =
WindowUtilities.FullscreenKeyDown(Key.T, isActive);
SelectSingleToggleButton(sender);
}

private void PostActionMenuItem_Click(object sender, RoutedEventArgs e)
{
CheckIfAnyPostActionsSelcted();
}
#endregion Methods
}

0 comments on commit 1012e3c

Please sign in to comment.