Browse Source

Default web search working on ETW

pull/504/head
Joseph Finney 7 months ago
parent
commit
f859c11034
  1. 6
      Text-Grab/App.config
  2. 84
      Text-Grab/Models/WebSearchUrlModel.cs
  3. 24
      Text-Grab/Properties/Settings.Designer.cs
  4. 6
      Text-Grab/Properties/Settings.settings
  5. 4
      Text-Grab/Utilities/CustomBottomBarUtilities.cs
  6. 9
      Text-Grab/Views/EditTextWindow.xaml
  7. 56
      Text-Grab/Views/EditTextWindow.xaml.cs

6
Text-Grab/App.config

@ -148,6 +148,12 @@
<setting name="LookupSearchHistory" serializeAs="String"> <setting name="LookupSearchHistory" serializeAs="String">
<value>True</value> <value>True</value>
</setting> </setting>
<setting name="DefaultWebSearch" serializeAs="String">
<value />
</setting>
<setting name="WebSearchItemsJson" serializeAs="String">
<value />
</setting>
</Text_Grab.Properties.Settings> </Text_Grab.Properties.Settings>
</userSettings> </userSettings>
</configuration> </configuration>

84
Text-Grab/Models/WebSearchUrlModel.cs

@ -1,25 +1,97 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using Text_Grab.Utilities;
namespace Text_Grab.Models; namespace Text_Grab.Models;
public record WebSearchUrlModel 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 return
[ [
new() { Name = "Google", Url = "https://www.google.com/search?q=" }, new() { Name = "Google", Url = "https://www.google.com/search?q=" },
new() { Name = "Bing", Url = "https://www.bing.com/search?q=" }, new() { Name = "Bing", Url = "https://www.bing.com/search?q=" },
new() { Name = "DuckDuckGo", Url = "https://duckduckgo.com/?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 Code", Url = "https://github.com/search?type=code&q=" },
new() { Name = "GitHub Repos", Url = "https://github.com/search?type=repositories&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
Text-Grab/Properties/Settings.Designer.cs

@ -586,5 +586,29 @@ namespace Text_Grab.Properties {
this["LookupSearchHistory"] = value; this["LookupSearchHistory"] = value;
} }
} }
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("")]
public string DefaultWebSearch {
get {
return ((string)(this["DefaultWebSearch"]));
}
set {
this["DefaultWebSearch"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("")]
public string WebSearchItemsJson {
get {
return ((string)(this["WebSearchItemsJson"]));
}
set {
this["WebSearchItemsJson"] = value;
}
}
} }
} }

6
Text-Grab/Properties/Settings.settings

@ -143,5 +143,11 @@
<Setting Name="LookupSearchHistory" Type="System.Boolean" Scope="User"> <Setting Name="LookupSearchHistory" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value> <Value Profile="(Default)">True</Value>
</Setting> </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> </Settings>
</SettingsFile> </SettingsFile>

4
Text-Grab/Utilities/CustomBottomBarUtilities.cs

@ -27,7 +27,8 @@ public class CustomBottomBarUtilities
if (customBottomBarItems is null || customBottomBarItems.Count == 0) if (customBottomBarItems is null || customBottomBarItems.Count == 0)
return ButtonInfo.DefaultButtonList; 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) if (customBottomBarItems.First().SymbolIcon == SymbolRegular.Diamond24)
{ {
// Migrate to the new SymbolRegular instead of the old symbols. // Migrate to the new SymbolRegular instead of the old symbols.
@ -37,7 +38,6 @@ public class CustomBottomBarUtilities
buttonInfo.SymbolIcon = buttonDictionary[buttonInfo.ButtonText]; buttonInfo.SymbolIcon = buttonDictionary[buttonInfo.ButtonText];
} }
return customBottomBarItems; return customBottomBarItems;
} }

9
Text-Grab/Views/EditTextWindow.xaml

@ -131,6 +131,10 @@
CanExecute="IsolateSelectionCmdCanExecute" CanExecute="IsolateSelectionCmdCanExecute"
Command="{x:Static local:EditTextWindow.WebSearchCmd}" Command="{x:Static local:EditTextWindow.WebSearchCmd}"
Executed="WebSearchExecuted" /> Executed="WebSearchExecuted" />
<CommandBinding
CanExecute="IsolateSelectionCmdCanExecute"
Command="{x:Static local:EditTextWindow.DefaultWebSearchCmd}"
Executed="DefaultWebSearchExecuted" />
</Window.CommandBindings> </Window.CommandBindings>
<Grid Background="{DynamicResource SolidBackgroundFillColorBaseBrush}"> <Grid Background="{DynamicResource SolidBackgroundFillColorBaseBrush}">
<Grid.RowDefinitions> <Grid.RowDefinitions>
@ -256,7 +260,10 @@
x:Name="FindAndReplaceMenuItem" x:Name="FindAndReplaceMenuItem"
Click="FindAndReplaceMenuItem_Click" Click="FindAndReplaceMenuItem_Click"
Header="Find and Replace" /> 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..." /> <MenuItem x:Name="WebSearchCollection" Header="Search web with..." />
<Separator /> <Separator />
<MenuItem <MenuItem

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

@ -53,9 +53,10 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
public static RoutedCommand UnstackCmd = new(); public static RoutedCommand UnstackCmd = new();
public static RoutedCommand UnstackGroupCmd = new(); public static RoutedCommand UnstackGroupCmd = new();
public static RoutedCommand WebSearchCmd = new(); public static RoutedCommand WebSearchCmd = new();
public static RoutedCommand DefaultWebSearchCmd = new();
public bool LaunchedFromNotification = false; public bool LaunchedFromNotification = false;
private CancellationTokenSource? cancellationTokenForDirOCR; private CancellationTokenSource? cancellationTokenForDirOCR;
private string historyId = string.Empty;
private readonly string historyId = string.Empty;
private int numberOfContextMenuItems; private int numberOfContextMenuItems;
private string? OpenedFilePath; private string? OpenedFilePath;
@ -112,7 +113,7 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
public CurrentCase CaseStatusOfToggle { get; set; } = CurrentCase.Unknown; public CurrentCase CaseStatusOfToggle { get; set; } = CurrentCase.Unknown;
public bool WrapText { get; set; } = false; public bool WrapText { get; set; } = false;
private bool _IsAccessingClipboard { get; set; } = false;
private bool IsAccessingClipboard { get; set; } = false;
#endregion Properties #endregion Properties
@ -172,7 +173,7 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
{ {
files = Directory.GetFiles(folderPath, "*.*", searchOption); files = Directory.GetFiles(folderPath, "*.*", searchOption);
} }
catch (System.Exception ex)
catch (Exception ex)
{ {
PassedTextControl.AppendText($"Failed to read directory: {ex.Message}{Environment.NewLine}"); PassedTextControl.AppendText($"Failed to read directory: {ex.Message}{Environment.NewLine}");
} }
@ -545,7 +546,7 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
private void CanOcrPasteExecute(object sender, CanExecuteRoutedEventArgs e) private void CanOcrPasteExecute(object sender, CanExecuteRoutedEventArgs e)
{ {
_IsAccessingClipboard = true;
IsAccessingClipboard = true;
DataPackageView? dataPackageView = null; DataPackageView? dataPackageView = null;
try try
@ -559,7 +560,7 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
} }
finally finally
{ {
_IsAccessingClipboard = false;
IsAccessingClipboard = false;
} }
if (dataPackageView is null) if (dataPackageView is null)
@ -614,17 +615,17 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
private async void Clipboard_ContentChanged(object? sender, object e) private async void Clipboard_ContentChanged(object? sender, object e)
{ {
if (ClipboardWatcherMenuItem.IsChecked is false || _IsAccessingClipboard)
if (ClipboardWatcherMenuItem.IsChecked is false || IsAccessingClipboard)
return; return;
_IsAccessingClipboard = true;
IsAccessingClipboard = true;
DataPackageView? dataPackageView = null; DataPackageView? dataPackageView = null;
try try
{ {
dataPackageView = Windows.ApplicationModel.DataTransfer.Clipboard.GetContent(); 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}"); Debug.WriteLine($"error with Windows.ApplicationModel.DataTransfer.Clipboard.GetContent(). Exception Message: {ex.Message}");
} }
@ -638,13 +639,13 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
text += Environment.NewLine; text += Environment.NewLine;
System.Windows.Application.Current.Dispatcher.Invoke(new Action(() => { AddCopiedTextToTextBox(text); })); 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}"); Debug.WriteLine($"error with dataPackageView.GetTextAsync(). Exception Message: {ex.Message}");
} }
}; };
_IsAccessingClipboard = false;
IsAccessingClipboard = false;
} }
private void CloseMenuItem_Click(object sender, RoutedEventArgs e) private void CloseMenuItem_Click(object sender, RoutedEventArgs e)
@ -1001,12 +1002,23 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
_ = await Windows.System.Launcher.LaunchUriAsync(searchUri); _ = 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); WindowUtilities.LaunchFullScreenGrab(PassedTextControl);
} }
private void keyedCtrlG(object sender, ExecutedRoutedEventArgs e)
private void KeyedCtrlG(object sender, ExecutedRoutedEventArgs e)
{ {
CheckForGrabFrameOrLaunch(); CheckForGrabFrameOrLaunch();
} }
@ -1441,21 +1453,21 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
private async void PasteExecuted(object sender, ExecutedRoutedEventArgs? e = null) private async void PasteExecuted(object sender, ExecutedRoutedEventArgs? e = null)
{ {
_IsAccessingClipboard = true;
IsAccessingClipboard = true;
DataPackageView? dataPackageView = null; DataPackageView? dataPackageView = null;
try try
{ {
dataPackageView = Windows.ApplicationModel.DataTransfer.Clipboard.GetContent(); 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}"); Debug.WriteLine($"error with Windows.ApplicationModel.DataTransfer.Clipboard.GetContent(). Exception Message: {ex.Message}");
} }
if (dataPackageView is null) if (dataPackageView is null)
{ {
_IsAccessingClipboard = false;
IsAccessingClipboard = false;
return; return;
} }
@ -1466,7 +1478,7 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
string textFromClipboard = await dataPackageView.GetTextAsync(); string textFromClipboard = await dataPackageView.GetTextAsync();
System.Windows.Application.Current.Dispatcher.Invoke(new Action(() => { AddCopiedTextToTextBox(textFromClipboard); })); 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}"); Debug.WriteLine($"error with dataPackageView.GetTextAsync(). Exception Message: {ex.Message}");
} }
@ -1482,7 +1494,7 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
System.Windows.Application.Current.Dispatcher.Invoke(new Action(() => { AddCopiedTextToTextBox(text); })); 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}"); Debug.WriteLine($"error with dataPackageView.GetBitmapAsync(). Exception Message: {ex.Message}");
} }
@ -1507,13 +1519,13 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
System.Windows.Application.Current.Dispatcher.Invoke(new Action(() => { AddCopiedTextToTextBox(text); })); 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}"); Debug.WriteLine($"error with dataPackageView.GetStorageItemsAsync(). Exception Message: {ex.Message}");
} }
} }
_IsAccessingClipboard = false;
IsAccessingClipboard = false;
if (e is not null) if (e is not null)
e.Handled = true; e.Handled = true;
@ -1816,11 +1828,11 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
{ {
RoutedCommand newFullscreenGrab = new(); RoutedCommand newFullscreenGrab = new();
_ = newFullscreenGrab.InputGestures.Add(new KeyGesture(Key.F, ModifierKeys.Control)); _ = newFullscreenGrab.InputGestures.Add(new KeyGesture(Key.F, ModifierKeys.Control));
_ = CommandBindings.Add(new CommandBinding(newFullscreenGrab, keyedCtrlF));
_ = CommandBindings.Add(new CommandBinding(newFullscreenGrab, KeyedCtrlF));
RoutedCommand newGrabFrame = new(); RoutedCommand newGrabFrame = new();
_ = newGrabFrame.InputGestures.Add(new KeyGesture(Key.G, ModifierKeys.Control)); _ = newGrabFrame.InputGestures.Add(new KeyGesture(Key.G, ModifierKeys.Control));
_ = CommandBindings.Add(new CommandBinding(newGrabFrame, keyedCtrlG));
_ = CommandBindings.Add(new CommandBinding(newGrabFrame, KeyedCtrlG));
RoutedCommand selectLineCommand = new(); RoutedCommand selectLineCommand = new();
_ = selectLineCommand.InputGestures.Add(new KeyGesture(Key.L, ModifierKeys.Control)); _ = selectLineCommand.InputGestures.Add(new KeyGesture(Key.L, ModifierKeys.Control));
@ -1890,7 +1902,7 @@ public partial class EditTextWindow : Wpf.Ui.Controls.FluentWindow
_ = duplicateLine.InputGestures.Add(new KeyGesture(Key.D, ModifierKeys.Control)); _ = duplicateLine.InputGestures.Add(new KeyGesture(Key.D, ModifierKeys.Control));
_ = CommandBindings.Add(new CommandBinding(duplicateLine, DuplicateSelectedLine)); _ = CommandBindings.Add(new CommandBinding(duplicateLine, DuplicateSelectedLine));
List<WebSearchUrlModel> searchers = WebSearchUrlModel.GetDefaultWebSearchUrls();
List<WebSearchUrlModel> searchers = Singleton<WebSearchUrlModel>.Instance.WebSearchers;
foreach (WebSearchUrlModel searcher in searchers) foreach (WebSearchUrlModel searcher in searchers)
{ {

Loading…
Cancel
Save