
7 changed files with 158 additions and 31 deletions
-
6Text-Grab/App.config
-
84Text-Grab/Models/WebSearchUrlModel.cs
-
24Text-Grab/Properties/Settings.Designer.cs
-
6Text-Grab/Properties/Settings.settings
-
4Text-Grab/Utilities/CustomBottomBarUtilities.cs
-
9Text-Grab/Views/EditTextWindow.xaml
-
56Text-Grab/Views/EditTextWindow.xaml.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<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(); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue