From d51579967bad180e9bc3da557674cfe1d40fa0c1 Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Wed, 22 Sep 2021 18:09:30 -0500 Subject: [PATCH] Further Refactor & add CustomBrowsers UI --- .../ChromeBookmarkLoader.cs | 3 +- .../ChromiumBookmarkLoader.cs | 5 +- .../{Bookmarks.cs => BookmarkLoader.cs} | 16 ++++--- .../CustomChromiumBookmarkLoader.cs | 14 ++++++ .../EdgeBookmarkLoader.cs | 1 + .../FirefoxBookmarkLoader.cs | 1 + ...low.Launcher.Plugin.BrowserBookmark.csproj | 4 ++ .../IBookmarkLoader.cs | 3 +- .../Main.cs | 6 +-- .../{ => Models}/Bookmark.cs | 11 ++--- .../Models/CustomBrowser.cs | 8 ++++ .../Models/Settings.cs | 20 +++++++- .../Views/CustomBrowserSetting.xaml | 36 +++++++++++++++ .../Views/CustomBrowserSetting.xaml.cs | 45 ++++++++++++++++++ .../Views/SettingsControl.xaml | 46 +++++++++++++++---- .../Views/SettingsControl.xaml.cs | 40 +++++++++++----- 16 files changed, 218 insertions(+), 41 deletions(-) rename Plugins/Flow.Launcher.Plugin.BrowserBookmark/Commands/{Bookmarks.cs => BookmarkLoader.cs} (71%) create mode 100644 Plugins/Flow.Launcher.Plugin.BrowserBookmark/CustomChromiumBookmarkLoader.cs rename Plugins/Flow.Launcher.Plugin.BrowserBookmark/{ => Models}/Bookmark.cs (73%) create mode 100644 Plugins/Flow.Launcher.Plugin.BrowserBookmark/Models/CustomBrowser.cs create mode 100644 Plugins/Flow.Launcher.Plugin.BrowserBookmark/Views/CustomBrowserSetting.xaml create mode 100644 Plugins/Flow.Launcher.Plugin.BrowserBookmark/Views/CustomBrowserSetting.xaml.cs diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromeBookmarkLoader.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromeBookmarkLoader.cs index 7fdb86f31..d7cfcaf6a 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromeBookmarkLoader.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromeBookmarkLoader.cs @@ -1,4 +1,5 @@ -using System; +using Flow.Launcher.Plugin.BrowserBookmark.Models; +using System; using System.Collections.Generic; using System.IO; using System.Linq; diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromiumBookmarkLoader.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromiumBookmarkLoader.cs index 7be66eea5..a6edf3302 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromiumBookmarkLoader.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromiumBookmarkLoader.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Authentication; +using Flow.Launcher.Plugin.BrowserBookmark.Models; +using Microsoft.AspNetCore.Authentication; using System.Collections.Generic; using System.IO; using System.Text.Json; @@ -28,6 +29,8 @@ namespace Flow.Launcher.Plugin.BrowserBookmark protected List LoadBookmarksFromFile(string path, string source) { + if (!File.Exists(path)) + return new(); var bookmarks = new List(); using var jsonDocument = JsonDocument.Parse(File.ReadAllText(path)); if (!jsonDocument.RootElement.TryGetProperty("roots", out var rootElement)) diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Commands/Bookmarks.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Commands/BookmarkLoader.cs similarity index 71% rename from Plugins/Flow.Launcher.Plugin.BrowserBookmark/Commands/Bookmarks.cs rename to Plugins/Flow.Launcher.Plugin.BrowserBookmark/Commands/BookmarkLoader.cs index c7cf4867d..b4c6235e0 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Commands/Bookmarks.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Commands/BookmarkLoader.cs @@ -1,11 +1,12 @@ using System.Collections.Generic; using System.Linq; using Flow.Launcher.Infrastructure; +using Flow.Launcher.Plugin.BrowserBookmark.Models; using Flow.Launcher.Plugin.SharedModels; namespace Flow.Launcher.Plugin.BrowserBookmark.Commands { - internal static class Bookmarks + internal static class BookmarkLoader { internal static MatchResult MatchProgram(Bookmark bookmark, string queryString) { @@ -18,23 +19,24 @@ namespace Flow.Launcher.Plugin.BrowserBookmark.Commands internal static List LoadAllBookmarks() { - var allbookmarks = new List(); var chromeBookmarks = new ChromeBookmarkLoader(); var mozBookmarks = new FirefoxBookmarkLoader(); var edgeBookmarks = new EdgeBookmarkLoader(); + var allBookmarks = new List(); + //TODO: Let the user select which browser's bookmarks are displayed // Add Firefox bookmarks - mozBookmarks.GetBookmarks().ForEach(x => allbookmarks.Add(x)); + allBookmarks.AddRange(mozBookmarks.GetBookmarks()); // Add Chrome bookmarks - chromeBookmarks.GetBookmarks().ForEach(x => allbookmarks.Add(x)); + allBookmarks.AddRange(chromeBookmarks.GetBookmarks()); // Add Edge (Chromium) bookmarks - edgeBookmarks.GetBookmarks().ForEach(x => allbookmarks.Add(x)); + allBookmarks.AddRange(edgeBookmarks.GetBookmarks()); - return allbookmarks.Distinct().ToList(); + return allBookmarks.Distinct().ToList(); } } -} +} \ No newline at end of file diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/CustomChromiumBookmarkLoader.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/CustomChromiumBookmarkLoader.cs new file mode 100644 index 000000000..91189ace7 --- /dev/null +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/CustomChromiumBookmarkLoader.cs @@ -0,0 +1,14 @@ +using Flow.Launcher.Plugin.BrowserBookmark.Models; +using System.Collections.Generic; + +namespace Flow.Launcher.Plugin.BrowserBookmark +{ + public class CustomChromiumBookmarkLoader : ChromiumBookmarkLoader + { + public string BrowserDataPath { get; init; } + public string BookmarkFilePath { get; init; } + public string BrowserName { get; init; } + + public override List GetBookmarks() => BrowserDataPath != null ? LoadBookmarks(BrowserDataPath, BrowserName) : LoadBookmarksFromFile(BookmarkFilePath, BrowserName); + } +} \ No newline at end of file diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/EdgeBookmarkLoader.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/EdgeBookmarkLoader.cs index d3f109445..d982ee99e 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/EdgeBookmarkLoader.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/EdgeBookmarkLoader.cs @@ -1,3 +1,4 @@ +using Flow.Launcher.Plugin.BrowserBookmark.Models; using System; using System.Collections.Generic; using System.IO; diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarkLoader.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarkLoader.cs index 5d47c80e3..3df034781 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarkLoader.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarkLoader.cs @@ -1,3 +1,4 @@ +using Flow.Launcher.Plugin.BrowserBookmark.Models; using System; using System.Collections.Generic; using System.Data.SQLite; diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Flow.Launcher.Plugin.BrowserBookmark.csproj b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Flow.Launcher.Plugin.BrowserBookmark.csproj index acfc79d7a..46313d38f 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Flow.Launcher.Plugin.BrowserBookmark.csproj +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Flow.Launcher.Plugin.BrowserBookmark.csproj @@ -69,4 +69,8 @@ + + + + \ No newline at end of file diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/IBookmarkLoader.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/IBookmarkLoader.cs index 1dbd8b6bf..2c48cfd55 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/IBookmarkLoader.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/IBookmarkLoader.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using Flow.Launcher.Plugin.BrowserBookmark.Models; +using System.Collections.Generic; namespace Flow.Launcher.Plugin.BrowserBookmark { diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs index a0b443e75..eaf14d5c2 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Main.cs @@ -26,7 +26,7 @@ namespace Flow.Launcher.Plugin.BrowserBookmark _settings = context.API.LoadSettingJsonStorage(); - cachedBookmarks = Bookmarks.LoadAllBookmarks(); + cachedBookmarks = BookmarkLoader.LoadAllBookmarks(); } public List Query(Query query) @@ -45,7 +45,7 @@ namespace Flow.Launcher.Plugin.BrowserBookmark Title = c.Name, SubTitle = c.Url, IcoPath = @"Images\bookmark.png", - Score = Bookmarks.MatchProgram(c, param).Score, + Score = BookmarkLoader.MatchProgram(c, param).Score, Action = _ => { if (_settings.OpenInNewBrowserWindow) @@ -93,7 +93,7 @@ namespace Flow.Launcher.Plugin.BrowserBookmark { cachedBookmarks.Clear(); - cachedBookmarks = Bookmarks.LoadAllBookmarks(); + cachedBookmarks = BookmarkLoader.LoadAllBookmarks(); } public string GetTranslatedPluginTitle() diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Bookmark.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Models/Bookmark.cs similarity index 73% rename from Plugins/Flow.Launcher.Plugin.BrowserBookmark/Bookmark.cs rename to Plugins/Flow.Launcher.Plugin.BrowserBookmark/Models/Bookmark.cs index 185cf1620..c2fa9d977 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Bookmark.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Models/Bookmark.cs @@ -1,11 +1,6 @@ -using BinaryAnalysis.UnidecodeSharp; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Collections.Generic; - -namespace Flow.Launcher.Plugin.BrowserBookmark +namespace Flow.Launcher.Plugin.BrowserBookmark.Models { // Source may be important in the future public record Bookmark(string Name, string Url, string Source = "") @@ -21,5 +16,7 @@ namespace Flow.Launcher.Plugin.BrowserBookmark { return other != null && Name == other.Name && Url == other.Url; } + + public List CustomBrowsers { get; set; }= new(); } } \ No newline at end of file diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Models/CustomBrowser.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Models/CustomBrowser.cs new file mode 100644 index 000000000..1ddeab5df --- /dev/null +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Models/CustomBrowser.cs @@ -0,0 +1,8 @@ +namespace Flow.Launcher.Plugin.BrowserBookmark.Models +{ + public class CustomBrowser : BaseModel + { + public string Name { get; set; } + public string Path { get; set; } + } +} \ No newline at end of file diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Models/Settings.cs b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Models/Settings.cs index dc444fd73..e63b90dbc 100644 --- a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Models/Settings.cs +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Models/Settings.cs @@ -1,9 +1,27 @@ -namespace Flow.Launcher.Plugin.BrowserBookmark.Models +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Text.Json.Serialization; + +namespace Flow.Launcher.Plugin.BrowserBookmark.Models { public class Settings : BaseModel { public bool OpenInNewBrowserWindow { get; set; } = true; public string BrowserPath { get; set; } + + public ObservableCollection CustomBrowsers { get; set; } = new() + { + new CustomBrowser + { + Name="awefawef", + Path="awefawef" + }, + new CustomBrowser + { + Name = "awefawefawefawef", + Path = "aweawefawfawef" + } + }; } } \ No newline at end of file diff --git a/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Views/CustomBrowserSetting.xaml b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Views/CustomBrowserSetting.xaml new file mode 100644 index 000000000..2370789f1 --- /dev/null +++ b/Plugins/Flow.Launcher.Plugin.BrowserBookmark/Views/CustomBrowserSetting.xaml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + +