Skip to content

Commit f4fa5b7

Browse files
committed
Add web search Url model as a dynamic way to add new search providers
1 parent 23fb0e2 commit f4fa5b7

File tree

3 files changed

+61
-42
lines changed

3 files changed

+61
-42
lines changed

Text-Grab/Models/WebSearchUrlModel.cs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Collections.Generic;
2+
3+
namespace Text_Grab.Models;
4+
5+
public record WebSearchUrlModel
6+
{
7+
public required string Name { get; set; }
8+
public required string Url { get; set; }
9+
10+
11+
public static List<WebSearchUrlModel> GetDefaultWebSearchUrls()
12+
{
13+
return
14+
[
15+
new() { Name = "Google", Url = "https://www.google.com/search?q=" },
16+
new() { Name = "Bing", Url = "https://www.bing.com/search?q=" },
17+
new() { Name = "DuckDuckGo", Url = "https://duckduckgo.com/?q=" },
18+
new() { Name = "Yahoo", Url = "https://search.yahoo.com/search?p=" },
19+
new() { Name = "Yandex", Url = "https://yandex.com/search/?text=" },
20+
new() { Name = "Baidu", Url = "https://www.baidu.com/s?wd=" },
21+
new() { Name = "GitHub Code", Url = "https://github.com/search?type=code&q=" },
22+
new() { Name = "GitHub Repos", Url = "https://github.com/search?type=repositories&q=" },
23+
];
24+
}
25+
}

Text-Grab/Views/EditTextWindow.xaml

+4-30
Original file line numberDiff line numberDiff line change
@@ -129,20 +129,8 @@
129129
Executed="MakeQrCodeExecuted" />
130130
<CommandBinding
131131
CanExecute="IsolateSelectionCmdCanExecute"
132-
Command="{x:Static local:EditTextWindow.GoogleSearchCmd}"
133-
Executed="GoogleSearchExecuted" />
134-
<CommandBinding
135-
CanExecute="IsolateSelectionCmdCanExecute"
136-
Command="{x:Static local:EditTextWindow.BingSearchCmd}"
137-
Executed="BingSearchExecuted" />
138-
<CommandBinding
139-
CanExecute="IsolateSelectionCmdCanExecute"
140-
Command="{x:Static local:EditTextWindow.DuckDuckGoSearchCmd}"
141-
Executed="DuckDuckGoSearchExecuted" />
142-
<CommandBinding
143-
CanExecute="IsolateSelectionCmdCanExecute"
144-
Command="{x:Static local:EditTextWindow.GitHubSearchCmd}"
145-
Executed="GitHubSearchExecuted" />
132+
Command="{x:Static local:EditTextWindow.WebSearchCmd}"
133+
Executed="WebSearchExecuted" />
146134
</Window.CommandBindings>
147135
<Grid Background="{DynamicResource SolidBackgroundFillColorBaseBrush}">
148136
<Grid.RowDefinitions>
@@ -268,22 +256,8 @@
268256
x:Name="FindAndReplaceMenuItem"
269257
Click="FindAndReplaceMenuItem_Click"
270258
Header="Find and Replace" />
271-
<MenuItem
272-
x:Name="GoogleSearchMenuItem"
273-
Command="{x:Static local:EditTextWindow.GoogleSearchCmd}"
274-
Header="_Google Selection..." />
275-
<MenuItem
276-
x:Name="BingSearchMenuItem"
277-
Command="{x:Static local:EditTextWindow.BingSearchCmd}"
278-
Header="_Bing Selection..." />
279-
<MenuItem
280-
x:Name="DuckSearchMenuItem"
281-
Command="{x:Static local:EditTextWindow.DuckDuckGoSearchCmd}"
282-
Header="_Duck Duck Go Selection..." />
283-
<MenuItem
284-
x:Name="GitHubSearchMenuItem"
285-
Command="{x:Static local:EditTextWindow.GitHubSearchCmd}"
286-
Header="Git_Hub Search Selection..." />
259+
<MenuItem x:Name="DefaultWebSearch" Header="Default Web Search" />
260+
<MenuItem x:Name="WebSearchCollection" Header="Search web with..." />
287261
<Separator />
288262
<MenuItem
289263
x:Name="SelectWordMenuItem"

Text-Grab/Views/EditTextWindow.xaml.cs

+32-12
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
5252
public static RoutedCommand ToggleCaseCmd = new();
5353
public static RoutedCommand UnstackCmd = new();
5454
public static RoutedCommand UnstackGroupCmd = new();
55-
public static RoutedCommand GoogleSearchCmd = new();
56-
public static RoutedCommand BingSearchCmd = new();
57-
public static RoutedCommand DuckDuckGoSearchCmd = new();
58-
public static RoutedCommand GitHubSearchCmd = new();
55+
public static RoutedCommand WebSearchCmd = new();
5956
public bool LaunchedFromNotification = false;
6057
private CancellationTokenSource? cancellationTokenForDirOCR;
6158
private string historyId = string.Empty;
@@ -138,10 +135,7 @@ public static Dictionary<string, RoutedCommand> GetRoutedCommands()
138135
{nameof(InsertSelectionOnEveryLineCmd), InsertSelectionOnEveryLineCmd},
139136
{nameof(OcrPasteCommand), OcrPasteCommand},
140137
{nameof(MakeQrCodeCmd), MakeQrCodeCmd},
141-
{nameof(GoogleSearchCmd), GoogleSearchCmd},
142-
{nameof(BingSearchCmd), BingSearchCmd},
143-
{nameof(DuckDuckGoSearchCmd), DuckDuckGoSearchCmd},
144-
{nameof(GitHubSearchCmd), GitHubSearchCmd},
138+
{nameof(WebSearchCmd), WebSearchCmd},
145139
};
146140
}
147141

@@ -244,7 +238,7 @@ public async Task OcrAllImagesInFolder(string folderPath, OcrDirectoryOptions op
244238
stopwatch.Start();
245239
Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
246240

247-
List<AsyncOcrFileResult> ocrFileResults = new();
241+
List<AsyncOcrFileResult> ocrFileResults = [];
248242
foreach (string path in imageFiles)
249243
{
250244
AsyncOcrFileResult ocrFileResult = new(path);
@@ -899,7 +893,7 @@ private void HideBottomBarMenuItem_Click(object sender, RoutedEventArgs e)
899893

900894
private void InsertSelectionOnEveryLine(object? sender = null, ExecutedRoutedEventArgs? e = null)
901895
{
902-
string[] splitString = PassedTextControl.Text.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.None);
896+
string[] splitString = PassedTextControl.Text.Split([Environment.NewLine], StringSplitOptions.None);
903897
string selectionText = PassedTextControl.SelectedText;
904898
int initialSelectionStart = PassedTextControl.SelectionStart;
905899
int selectionPositionInLine = PassedTextControl.SelectionStart;
@@ -995,6 +989,18 @@ private async void GitHubSearchExecuted(object sender, ExecutedRoutedEventArgs e
995989
_ = await Windows.System.Launcher.LaunchUriAsync(new Uri(string.Format($"https://github.com/search?q={searchStringUrlSafe}")));
996990
}
997991

992+
private async void WebSearchExecuted(object sender, ExecutedRoutedEventArgs e)
993+
{
994+
string possibleSearch = PassedTextControl.SelectedText;
995+
string searchStringUrlSafe = WebUtility.UrlEncode(possibleSearch);
996+
997+
if (e.Parameter is not WebSearchUrlModel webSearcher)
998+
return;
999+
1000+
Uri searchUri = new($"{webSearcher.Url}{searchStringUrlSafe}");
1001+
_ = await Windows.System.Launcher.LaunchUriAsync(searchUri);
1002+
}
1003+
9981004
private void keyedCtrlF(object sender, ExecutedRoutedEventArgs e)
9991005
{
10001006
WindowUtilities.LaunchFullScreenGrab(PassedTextControl);
@@ -1152,7 +1158,7 @@ private async void LoadLanguageMenuItems(MenuItem captureMenuItem)
11521158
private void LoadRecentTextHistory()
11531159
{
11541160
List<HistoryInfo> grabsHistories = Singleton<HistoryService>.Instance.GetEditWindows();
1155-
grabsHistories = grabsHistories.OrderByDescending(x => x.CaptureDateTime).ToList();
1161+
grabsHistories = [.. grabsHistories.OrderByDescending(x => x.CaptureDateTime)];
11561162

11571163
OpenRecentMenuItem.Items.Clear();
11581164

@@ -1777,7 +1783,7 @@ private void SetFontFromSettings()
17771783
if (DefaultSettings.IsFontItalic)
17781784
PassedTextControl.FontStyle = FontStyles.Italic;
17791785

1780-
TextDecorationCollection tdc = new();
1786+
TextDecorationCollection tdc = [];
17811787
if (DefaultSettings.IsFontUnderline) tdc.Add(TextDecorations.Underline);
17821788
if (DefaultSettings.IsFontStrikeout) tdc.Add(TextDecorations.Strikethrough);
17831789
PassedTextControl.TextDecorations = tdc;
@@ -1883,6 +1889,20 @@ private void SetupRoutedCommands()
18831889
RoutedCommand duplicateLine = new();
18841890
_ = duplicateLine.InputGestures.Add(new KeyGesture(Key.D, ModifierKeys.Control));
18851891
_ = CommandBindings.Add(new CommandBinding(duplicateLine, DuplicateSelectedLine));
1892+
1893+
List<WebSearchUrlModel> searchers = WebSearchUrlModel.GetDefaultWebSearchUrls();
1894+
1895+
foreach (WebSearchUrlModel searcher in searchers)
1896+
{
1897+
MenuItem searchItem = new()
1898+
{
1899+
Header = $"Search with {searcher.Name}...",
1900+
Command = WebSearchCmd,
1901+
CommandParameter = searcher,
1902+
};
1903+
1904+
WebSearchCollection.Items.Add(searchItem);
1905+
}
18861906
}
18871907

18881908
private void SingleLineCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)

0 commit comments

Comments
 (0)