diff --git a/Text-Grab/App.config b/Text-Grab/App.config index 646b884d..d6361c43 100644 --- a/Text-Grab/App.config +++ b/Text-Grab/App.config @@ -148,6 +148,12 @@ True + + + + + + \ No newline at end of file diff --git a/Text-Grab/Models/WebSearchUrlModel.cs b/Text-Grab/Models/WebSearchUrlModel.cs index f39bb7d6..ef59bbbc 100644 --- a/Text-Grab/Models/WebSearchUrlModel.cs +++ b/Text-Grab/Models/WebSearchUrlModel.cs @@ -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 GetDefaultWebSearchUrls() + public WebSearchUrlModel DefaultSearcher + { + get + { + defaultSearcher ??= GetDefaultSearcher(); + return defaultSearcher; + } + set + { + defaultSearcher = value; + SaveDefaultSearcher(defaultSearcher); + } + } + + + private List webSearchers = []; + + public List 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 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 GetWebSearchUrls() + { + string json = AppUtilities.TextGrabSettings.WebSearchItemsJson; + if (string.IsNullOrWhiteSpace(json)) + return GetDefaultWebSearchUrls(); + List? webSearchUrls = JsonSerializer.Deserialize>(json); + if (webSearchUrls is null || webSearchUrls.Count == 0) + return GetDefaultWebSearchUrls(); + + return webSearchUrls; + } + + public static void SaveWebSearchUrls(List webSearchUrls) + { + string json = JsonSerializer.Serialize(webSearchUrls); + AppUtilities.TextGrabSettings.WebSearchItemsJson = json; + AppUtilities.TextGrabSettings.Save(); + } } diff --git a/Text-Grab/Properties/Settings.Designer.cs b/Text-Grab/Properties/Settings.Designer.cs index 2e79afb9..d5a18153 100644 --- a/Text-Grab/Properties/Settings.Designer.cs +++ b/Text-Grab/Properties/Settings.Designer.cs @@ -586,5 +586,29 @@ public bool LookupSearchHistory { 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; + } + } } } diff --git a/Text-Grab/Properties/Settings.settings b/Text-Grab/Properties/Settings.settings index 99ca662d..a9a2887f 100644 --- a/Text-Grab/Properties/Settings.settings +++ b/Text-Grab/Properties/Settings.settings @@ -143,5 +143,11 @@ True + + + + + + \ No newline at end of file diff --git a/Text-Grab/Utilities/CustomBottomBarUtilities.cs b/Text-Grab/Utilities/CustomBottomBarUtilities.cs index 59814293..840993f5 100644 --- a/Text-Grab/Utilities/CustomBottomBarUtilities.cs +++ b/Text-Grab/Utilities/CustomBottomBarUtilities.cs @@ -27,7 +27,8 @@ public static List 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. @@ -37,7 +38,6 @@ public static List GetCustomBottomBarItemsSetting() buttonInfo.SymbolIcon = buttonDictionary[buttonInfo.ButtonText]; } - return customBottomBarItems; } diff --git a/Text-Grab/Views/EditTextWindow.xaml b/Text-Grab/Views/EditTextWindow.xaml index 0e4f1a29..f741192a 100644 --- a/Text-Grab/Views/EditTextWindow.xaml +++ b/Text-Grab/Views/EditTextWindow.xaml @@ -131,6 +131,10 @@ CanExecute="IsolateSelectionCmdCanExecute" Command="{x:Static local:EditTextWindow.WebSearchCmd}" Executed="WebSearchExecuted" /> + @@ -256,7 +260,10 @@ x:Name="FindAndReplaceMenuItem" Click="FindAndReplaceMenuItem_Click" Header="Find and Replace" /> - + { 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) @@ -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.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(); } @@ -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; } @@ -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}"); } @@ -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}"); } @@ -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; @@ -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)); @@ -1890,7 +1902,7 @@ private void SetupRoutedCommands() _ = duplicateLine.InputGestures.Add(new KeyGesture(Key.D, ModifierKeys.Control)); _ = CommandBindings.Add(new CommandBinding(duplicateLine, DuplicateSelectedLine)); - List searchers = WebSearchUrlModel.GetDefaultWebSearchUrls(); + List searchers = Singleton.Instance.WebSearchers; foreach (WebSearchUrlModel searcher in searchers) {