Skip to content

Commit

Permalink
Add web search Url model as a dynamic way to add new search providers
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJoeFin committed Dec 20, 2024
1 parent 23fb0e2 commit f4fa5b7
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 42 deletions.
25 changes: 25 additions & 0 deletions Text-Grab/Models/WebSearchUrlModel.cs
Original file line number Diff line number Diff line change
@@ -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 changes: 4 additions & 30 deletions Text-Grab/Views/EditTextWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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"
Expand Down
44 changes: 32 additions & 12 deletions Text-Grab/Views/EditTextWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -138,10 +135,7 @@ public static Dictionary<string, RoutedCommand> GetRoutedCommands()
{nameof(InsertSelectionOnEveryLineCmd), InsertSelectionOnEveryLineCmd},
{nameof(OcrPasteCommand), OcrPasteCommand},
{nameof(MakeQrCodeCmd), MakeQrCodeCmd},
{nameof(GoogleSearchCmd), GoogleSearchCmd},
{nameof(BingSearchCmd), BingSearchCmd},
{nameof(DuckDuckGoSearchCmd), DuckDuckGoSearchCmd},
{nameof(GitHubSearchCmd), GitHubSearchCmd},
{nameof(WebSearchCmd), WebSearchCmd},
};
}

Expand Down Expand Up @@ -244,7 +238,7 @@ public async Task OcrAllImagesInFolder(string folderPath, OcrDirectoryOptions op
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);
Expand Down Expand Up @@ -899,7 +893,7 @@ private void HideBottomBarMenuItem_Click(object sender, RoutedEventArgs e)

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;
Expand Down Expand Up @@ -995,6 +989,18 @@ private async void GitHubSearchExecuted(object sender, ExecutedRoutedEventArgs e
_ = 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);
Expand Down Expand Up @@ -1152,7 +1158,7 @@ private async void LoadLanguageMenuItems(MenuItem captureMenuItem)
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();

Expand Down Expand Up @@ -1777,7 +1783,7 @@ private void SetFontFromSettings()
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;
Expand Down Expand Up @@ -1883,6 +1889,20 @@ private void SetupRoutedCommands()
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)
Expand Down

0 comments on commit f4fa5b7

Please sign in to comment.