Browse Source

Add web search Url model as a dynamic way to add new search providers

pull/504/head
Joseph Finney 7 months ago
parent
commit
f4fa5b7894
  1. 25
      Text-Grab/Models/WebSearchUrlModel.cs
  2. 34
      Text-Grab/Views/EditTextWindow.xaml
  3. 44
      Text-Grab/Views/EditTextWindow.xaml.cs

25
Text-Grab/Models/WebSearchUrlModel.cs

@ -0,0 +1,25 @@
using System.Collections.Generic;
namespace Text_Grab.Models;
public record WebSearchUrlModel
{
public required string Name { get; set; }
public required string Url { get; set; }
public static List<WebSearchUrlModel> GetDefaultWebSearchUrls()
{
return
[
new() { Name = "Google", Url = "https://www.google.com/search?q=" },
new() { Name = "Bing", Url = "https://www.bing.com/search?q=" },
new() { Name = "DuckDuckGo", Url = "https://duckduckgo.com/?q=" },
new() { Name = "Yahoo", Url = "https://search.yahoo.com/search?p=" },
new() { Name = "Yandex", Url = "https://yandex.com/search/?text=" },
new() { Name = "Baidu", Url = "https://www.baidu.com/s?wd=" },
new() { Name = "GitHub Code", Url = "https://github.com/search?type=code&q=" },
new() { Name = "GitHub Repos", Url = "https://github.com/search?type=repositories&q=" },
];
}
}

34
Text-Grab/Views/EditTextWindow.xaml

@ -129,20 +129,8 @@
Executed="MakeQrCodeExecuted" />
<CommandBinding
CanExecute="IsolateSelectionCmdCanExecute"
Command="{x:Static local:EditTextWindow.GoogleSearchCmd}"
Executed="GoogleSearchExecuted" />
<CommandBinding
CanExecute="IsolateSelectionCmdCanExecute"
Command="{x:Static local:EditTextWindow.BingSearchCmd}"
Executed="BingSearchExecuted" />
<CommandBinding
CanExecute="IsolateSelectionCmdCanExecute"
Command="{x:Static local:EditTextWindow.DuckDuckGoSearchCmd}"
Executed="DuckDuckGoSearchExecuted" />
<CommandBinding
CanExecute="IsolateSelectionCmdCanExecute"
Command="{x:Static local:EditTextWindow.GitHubSearchCmd}"
Executed="GitHubSearchExecuted" />
Command="{x:Static local:EditTextWindow.WebSearchCmd}"
Executed="WebSearchExecuted" />
</Window.CommandBindings>
<Grid Background="{DynamicResource SolidBackgroundFillColorBaseBrush}">
<Grid.RowDefinitions>
@ -268,22 +256,8 @@
x:Name="FindAndReplaceMenuItem"
Click="FindAndReplaceMenuItem_Click"
Header="Find and Replace" />
<MenuItem
x:Name="GoogleSearchMenuItem"
Command="{x:Static local:EditTextWindow.GoogleSearchCmd}"
Header="_Google Selection..." />
<MenuItem
x:Name="BingSearchMenuItem"
Command="{x:Static local:EditTextWindow.BingSearchCmd}"
Header="_Bing Selection..." />
<MenuItem
x:Name="DuckSearchMenuItem"
Command="{x:Static local:EditTextWindow.DuckDuckGoSearchCmd}"
Header="_Duck Duck Go Selection..." />
<MenuItem
x:Name="GitHubSearchMenuItem"
Command="{x:Static local:EditTextWindow.GitHubSearchCmd}"
Header="Git_Hub Search Selection..." />
<MenuItem x:Name="DefaultWebSearch" Header="Default Web Search" />
<MenuItem x:Name="WebSearchCollection" Header="Search web with..." />
<Separator />
<MenuItem
x:Name="SelectWordMenuItem"

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

@ -52,10 +52,7 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
public static RoutedCommand ToggleCaseCmd = new();
public static RoutedCommand UnstackCmd = new();
public static RoutedCommand UnstackGroupCmd = new();
public static RoutedCommand GoogleSearchCmd = new();
public static RoutedCommand BingSearchCmd = new();
public static RoutedCommand DuckDuckGoSearchCmd = new();
public static RoutedCommand GitHubSearchCmd = new();
public static RoutedCommand WebSearchCmd = new();
public bool LaunchedFromNotification = false;
private CancellationTokenSource? cancellationTokenForDirOCR;
private string historyId = string.Empty;
@ -138,10 +135,7 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
{nameof(InsertSelectionOnEveryLineCmd), InsertSelectionOnEveryLineCmd},
{nameof(OcrPasteCommand), OcrPasteCommand},
{nameof(MakeQrCodeCmd), MakeQrCodeCmd},
{nameof(GoogleSearchCmd), GoogleSearchCmd},
{nameof(BingSearchCmd), BingSearchCmd},
{nameof(DuckDuckGoSearchCmd), DuckDuckGoSearchCmd},
{nameof(GitHubSearchCmd), GitHubSearchCmd},
{nameof(WebSearchCmd), WebSearchCmd},
};
}
@ -244,7 +238,7 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
stopwatch.Start();
Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
List<AsyncOcrFileResult> ocrFileResults = new();
List<AsyncOcrFileResult> ocrFileResults = [];
foreach (string path in imageFiles)
{
AsyncOcrFileResult ocrFileResult = new(path);
@ -899,7 +893,7 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
private void InsertSelectionOnEveryLine(object? sender = null, ExecutedRoutedEventArgs? e = null)
{
string[] splitString = PassedTextControl.Text.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.None);
string[] splitString = PassedTextControl.Text.Split([Environment.NewLine], StringSplitOptions.None);
string selectionText = PassedTextControl.SelectedText;
int initialSelectionStart = PassedTextControl.SelectionStart;
int selectionPositionInLine = PassedTextControl.SelectionStart;
@ -995,6 +989,18 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
_ = await Windows.System.Launcher.LaunchUriAsync(new Uri(string.Format($"https://github.com/search?q={searchStringUrlSafe}")));
}
private async void WebSearchExecuted(object sender, ExecutedRoutedEventArgs e)
{
string possibleSearch = PassedTextControl.SelectedText;
string searchStringUrlSafe = WebUtility.UrlEncode(possibleSearch);
if (e.Parameter is not WebSearchUrlModel webSearcher)
return;
Uri searchUri = new($"{webSearcher.Url}{searchStringUrlSafe}");
_ = await Windows.System.Launcher.LaunchUriAsync(searchUri);
}
private void keyedCtrlF(object sender, ExecutedRoutedEventArgs e)
{
WindowUtilities.LaunchFullScreenGrab(PassedTextControl);
@ -1152,7 +1158,7 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
private void LoadRecentTextHistory()
{
List<HistoryInfo> grabsHistories = Singleton<HistoryService>.Instance.GetEditWindows();
grabsHistories = grabsHistories.OrderByDescending(x => x.CaptureDateTime).ToList();
grabsHistories = [.. grabsHistories.OrderByDescending(x => x.CaptureDateTime)];
OpenRecentMenuItem.Items.Clear();
@ -1777,7 +1783,7 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
if (DefaultSettings.IsFontItalic)
PassedTextControl.FontStyle = FontStyles.Italic;
TextDecorationCollection tdc = new();
TextDecorationCollection tdc = [];
if (DefaultSettings.IsFontUnderline) tdc.Add(TextDecorations.Underline);
if (DefaultSettings.IsFontStrikeout) tdc.Add(TextDecorations.Strikethrough);
PassedTextControl.TextDecorations = tdc;
@ -1883,6 +1889,20 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
RoutedCommand duplicateLine = new();
_ = duplicateLine.InputGestures.Add(new KeyGesture(Key.D, ModifierKeys.Control));
_ = CommandBindings.Add(new CommandBinding(duplicateLine, DuplicateSelectedLine));
List<WebSearchUrlModel> searchers = WebSearchUrlModel.GetDefaultWebSearchUrls();
foreach (WebSearchUrlModel searcher in searchers)
{
MenuItem searchItem = new()
{
Header = $"Search with {searcher.Name}...",
Command = WebSearchCmd,
CommandParameter = searcher,
};
WebSearchCollection.Items.Add(searchItem);
}
}
private void SingleLineCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)

Loading…
Cancel
Save