Skip to content

Commit

Permalink
Default web search working on ETW
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJoeFin committed Dec 20, 2024
1 parent f4fa5b7 commit f859c11
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 31 deletions.
6 changes: 6 additions & 0 deletions Text-Grab/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@
<setting name="LookupSearchHistory" serializeAs="String">
<value>True</value>
</setting>
<setting name="DefaultWebSearch" serializeAs="String">
<value />
</setting>
<setting name="WebSearchItemsJson" serializeAs="String">
<value />
</setting>
</Text_Grab.Properties.Settings>
</userSettings>
</configuration>
84 changes: 78 additions & 6 deletions Text-Grab/Models/WebSearchUrlModel.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,97 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using Text_Grab.Utilities;

namespace Text_Grab.Models;

public record WebSearchUrlModel
{
public required string Name { get; set; }
public required string Url { get; set; }
public string Name { get; set; } = string.Empty;
public string Url { get; set; } = string.Empty;

private WebSearchUrlModel? defaultSearcher;

public static List<WebSearchUrlModel> GetDefaultWebSearchUrls()
public WebSearchUrlModel DefaultSearcher
{
get
{
defaultSearcher ??= GetDefaultSearcher();
return defaultSearcher;
}
set
{
defaultSearcher = value;
SaveDefaultSearcher(defaultSearcher);
}
}


private List<WebSearchUrlModel> webSearchers = [];

public List<WebSearchUrlModel> WebSearchers
{
get
{
if (webSearchers.Count == 0)
webSearchers = GetWebSearchUrls();

return webSearchers;
}
set
{
webSearchers = value;
SaveWebSearchUrls(webSearchers);
}
}

private WebSearchUrlModel GetDefaultSearcher()
{
string searcherName = AppUtilities.TextGrabSettings.DefaultWebSearch;
if (string.IsNullOrWhiteSpace(searcherName))
return WebSearchers[0];

WebSearchUrlModel? searcher = WebSearchers
.FirstOrDefault(searcher => searcher.Name == searcherName);

return searcher ?? WebSearchers[0];
}

private void SaveDefaultSearcher(WebSearchUrlModel webSearchUrl)
{
AppUtilities.TextGrabSettings.DefaultWebSearch = webSearchUrl.Name;
AppUtilities.TextGrabSettings.Save();
}

private 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 = "Brave", Url = "https://search.brave.com/search?q=" },
new() { Name = "GitHub Code", Url = "https://github.com/search?type=code&q=" },
new() { Name = "GitHub Repos", Url = "https://github.com/search?type=repositories&q=" },
];
}

public static List<WebSearchUrlModel> GetWebSearchUrls()
{
string json = AppUtilities.TextGrabSettings.WebSearchItemsJson;
if (string.IsNullOrWhiteSpace(json))
return GetDefaultWebSearchUrls();
List<WebSearchUrlModel>? webSearchUrls = JsonSerializer.Deserialize<List<WebSearchUrlModel>>(json);
if (webSearchUrls is null || webSearchUrls.Count == 0)
return GetDefaultWebSearchUrls();

return webSearchUrls;
}

public static void SaveWebSearchUrls(List<WebSearchUrlModel> webSearchUrls)
{
string json = JsonSerializer.Serialize(webSearchUrls);
AppUtilities.TextGrabSettings.WebSearchItemsJson = json;
AppUtilities.TextGrabSettings.Save();
}
}
24 changes: 24 additions & 0 deletions Text-Grab/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Text-Grab/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,11 @@
<Setting Name="LookupSearchHistory" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="DefaultWebSearch" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="WebSearchItemsJson" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>
4 changes: 2 additions & 2 deletions Text-Grab/Utilities/CustomBottomBarUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public static List<ButtonInfo> GetCustomBottomBarItemsSetting()
if (customBottomBarItems is null || customBottomBarItems.Count == 0)
return ButtonInfo.DefaultButtonList;

// check to see if the first element is using the default symbol, Diamond24, which is unused by any button
// check to see if the first element is using the default symbol of Diamond24
// which is unused by any button
if (customBottomBarItems.First().SymbolIcon == SymbolRegular.Diamond24)
{
// Migrate to the new SymbolRegular instead of the old symbols.
Expand All @@ -37,7 +38,6 @@ public static List<ButtonInfo> GetCustomBottomBarItemsSetting()
buttonInfo.SymbolIcon = buttonDictionary[buttonInfo.ButtonText];
}


return customBottomBarItems;
}

Expand Down
9 changes: 8 additions & 1 deletion Text-Grab/Views/EditTextWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@
CanExecute="IsolateSelectionCmdCanExecute"
Command="{x:Static local:EditTextWindow.WebSearchCmd}"
Executed="WebSearchExecuted" />
<CommandBinding
CanExecute="IsolateSelectionCmdCanExecute"
Command="{x:Static local:EditTextWindow.DefaultWebSearchCmd}"
Executed="DefaultWebSearchExecuted" />
</Window.CommandBindings>
<Grid Background="{DynamicResource SolidBackgroundFillColorBaseBrush}">
<Grid.RowDefinitions>
Expand Down Expand Up @@ -256,7 +260,10 @@
x:Name="FindAndReplaceMenuItem"
Click="FindAndReplaceMenuItem_Click"
Header="Find and Replace" />
<MenuItem x:Name="DefaultWebSearch" Header="Default Web Search" />
<MenuItem
x:Name="DefaultWebSearch"
Command="{x:Static local:EditTextWindow.DefaultWebSearchCmd}"
Header="Default Web Search" />
<MenuItem x:Name="WebSearchCollection" Header="Search web with..." />
<Separator />
<MenuItem
Expand Down
56 changes: 34 additions & 22 deletions Text-Grab/Views/EditTextWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
public static RoutedCommand UnstackCmd = new();
public static RoutedCommand UnstackGroupCmd = new();
public static RoutedCommand WebSearchCmd = new();
public static RoutedCommand DefaultWebSearchCmd = new();
public bool LaunchedFromNotification = false;
private CancellationTokenSource? cancellationTokenForDirOCR;
private string historyId = string.Empty;
private readonly string historyId = string.Empty;
private int numberOfContextMenuItems;
private string? OpenedFilePath;

Expand Down Expand Up @@ -112,7 +113,7 @@ public EditTextWindow(HistoryInfo historyInfo)
public CurrentCase CaseStatusOfToggle { get; set; } = CurrentCase.Unknown;

public bool WrapText { get; set; } = false;
private bool _IsAccessingClipboard { get; set; } = false;
private bool IsAccessingClipboard { get; set; } = false;

#endregion Properties

Expand Down Expand Up @@ -172,7 +173,7 @@ public async Task OcrAllImagesInFolder(string folderPath, OcrDirectoryOptions op
{
files = Directory.GetFiles(folderPath, "*.*", searchOption);
}
catch (System.Exception ex)
catch (Exception ex)
{
PassedTextControl.AppendText($"Failed to read directory: {ex.Message}{Environment.NewLine}");
}
Expand Down Expand Up @@ -545,7 +546,7 @@ private void CanLaunchUriExecute(object sender, CanExecuteRoutedEventArgs e)

private void CanOcrPasteExecute(object sender, CanExecuteRoutedEventArgs e)
{
_IsAccessingClipboard = true;
IsAccessingClipboard = true;
DataPackageView? dataPackageView = null;

try
Expand All @@ -559,7 +560,7 @@ private void CanOcrPasteExecute(object sender, CanExecuteRoutedEventArgs e)
}
finally
{
_IsAccessingClipboard = false;
IsAccessingClipboard = false;
}

if (dataPackageView is null)
Expand Down Expand Up @@ -614,17 +615,17 @@ private void CheckRightToLeftLanguage()

private async void Clipboard_ContentChanged(object? sender, object e)
{
if (ClipboardWatcherMenuItem.IsChecked is false || _IsAccessingClipboard)
if (ClipboardWatcherMenuItem.IsChecked is false || IsAccessingClipboard)
return;

_IsAccessingClipboard = true;
IsAccessingClipboard = true;
DataPackageView? dataPackageView = null;

try
{
dataPackageView = Windows.ApplicationModel.DataTransfer.Clipboard.GetContent();
}
catch (System.Exception ex)
catch (Exception ex)
{
Debug.WriteLine($"error with Windows.ApplicationModel.DataTransfer.Clipboard.GetContent(). Exception Message: {ex.Message}");
}
Expand All @@ -638,13 +639,13 @@ private async void Clipboard_ContentChanged(object? sender, object e)
text += Environment.NewLine;
System.Windows.Application.Current.Dispatcher.Invoke(new Action(() => { AddCopiedTextToTextBox(text); }));
}
catch (System.Exception ex)
catch (Exception ex)
{
Debug.WriteLine($"error with dataPackageView.GetTextAsync(). Exception Message: {ex.Message}");
}
};

_IsAccessingClipboard = false;
IsAccessingClipboard = false;
}

private void CloseMenuItem_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -1001,12 +1002,23 @@ private async void WebSearchExecuted(object sender, ExecutedRoutedEventArgs e)
_ = await Windows.System.Launcher.LaunchUriAsync(searchUri);
}

private void keyedCtrlF(object sender, ExecutedRoutedEventArgs e)
private async void DefaultWebSearchExecuted(object sender, ExecutedRoutedEventArgs e)
{
string possibleSearch = PassedTextControl.SelectedText;
string searchStringUrlSafe = WebUtility.UrlEncode(possibleSearch);

WebSearchUrlModel searcher = Singleton<WebSearchUrlModel>.Instance.DefaultSearcher;

Uri searchUri = new($"{searcher.Url}{searchStringUrlSafe}");
_ = await Windows.System.Launcher.LaunchUriAsync(searchUri);
}

private void KeyedCtrlF(object sender, ExecutedRoutedEventArgs e)
{
WindowUtilities.LaunchFullScreenGrab(PassedTextControl);
}

private void keyedCtrlG(object sender, ExecutedRoutedEventArgs e)
private void KeyedCtrlG(object sender, ExecutedRoutedEventArgs e)
{
CheckForGrabFrameOrLaunch();
}
Expand Down Expand Up @@ -1441,21 +1453,21 @@ private void PassedTextControl_TextChanged(object sender, TextChangedEventArgs e

private async void PasteExecuted(object sender, ExecutedRoutedEventArgs? e = null)
{
_IsAccessingClipboard = true;
IsAccessingClipboard = true;
DataPackageView? dataPackageView = null;

try
{
dataPackageView = Windows.ApplicationModel.DataTransfer.Clipboard.GetContent();
}
catch (System.Exception ex)
catch (Exception ex)
{
Debug.WriteLine($"error with Windows.ApplicationModel.DataTransfer.Clipboard.GetContent(). Exception Message: {ex.Message}");
}

if (dataPackageView is null)
{
_IsAccessingClipboard = false;
IsAccessingClipboard = false;
return;
}

Expand All @@ -1466,7 +1478,7 @@ private async void PasteExecuted(object sender, ExecutedRoutedEventArgs? e = nul
string textFromClipboard = await dataPackageView.GetTextAsync();
System.Windows.Application.Current.Dispatcher.Invoke(new Action(() => { AddCopiedTextToTextBox(textFromClipboard); }));
}
catch (System.Exception ex)
catch (Exception ex)
{
Debug.WriteLine($"error with dataPackageView.GetTextAsync(). Exception Message: {ex.Message}");
}
Expand All @@ -1482,7 +1494,7 @@ private async void PasteExecuted(object sender, ExecutedRoutedEventArgs? e = nul

System.Windows.Application.Current.Dispatcher.Invoke(new Action(() => { AddCopiedTextToTextBox(text); }));
}
catch (System.Exception ex)
catch (Exception ex)
{
Debug.WriteLine($"error with dataPackageView.GetBitmapAsync(). Exception Message: {ex.Message}");
}
Expand All @@ -1507,13 +1519,13 @@ private async void PasteExecuted(object sender, ExecutedRoutedEventArgs? e = nul
System.Windows.Application.Current.Dispatcher.Invoke(new Action(() => { AddCopiedTextToTextBox(text); }));
}
}
catch (System.Exception ex)
catch (Exception ex)
{
Debug.WriteLine($"error with dataPackageView.GetStorageItemsAsync(). Exception Message: {ex.Message}");
}
}

_IsAccessingClipboard = false;
IsAccessingClipboard = false;

if (e is not null)
e.Handled = true;
Expand Down Expand Up @@ -1816,11 +1828,11 @@ private void SetupRoutedCommands()
{
RoutedCommand newFullscreenGrab = new();
_ = newFullscreenGrab.InputGestures.Add(new KeyGesture(Key.F, ModifierKeys.Control));
_ = CommandBindings.Add(new CommandBinding(newFullscreenGrab, keyedCtrlF));
_ = CommandBindings.Add(new CommandBinding(newFullscreenGrab, KeyedCtrlF));

RoutedCommand newGrabFrame = new();
_ = newGrabFrame.InputGestures.Add(new KeyGesture(Key.G, ModifierKeys.Control));
_ = CommandBindings.Add(new CommandBinding(newGrabFrame, keyedCtrlG));
_ = CommandBindings.Add(new CommandBinding(newGrabFrame, KeyedCtrlG));

RoutedCommand selectLineCommand = new();
_ = selectLineCommand.InputGestures.Add(new KeyGesture(Key.L, ModifierKeys.Control));
Expand Down Expand Up @@ -1890,7 +1902,7 @@ private void SetupRoutedCommands()
_ = duplicateLine.InputGestures.Add(new KeyGesture(Key.D, ModifierKeys.Control));
_ = CommandBindings.Add(new CommandBinding(duplicateLine, DuplicateSelectedLine));

List<WebSearchUrlModel> searchers = WebSearchUrlModel.GetDefaultWebSearchUrls();
List<WebSearchUrlModel> searchers = Singleton<WebSearchUrlModel>.Instance.WebSearchers;

foreach (WebSearchUrlModel searcher in searchers)
{
Expand Down

0 comments on commit f859c11

Please sign in to comment.