Further Refactor & add CustomBrowsers UI

This commit is contained in:
Kevin Zhang 2021-09-22 18:09:30 -05:00
parent 7ff9b688c9
commit d51579967b
16 changed files with 218 additions and 41 deletions

View file

@ -1,4 +1,5 @@
using System;
using Flow.Launcher.Plugin.BrowserBookmark.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

View file

@ -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<Bookmark> LoadBookmarksFromFile(string path, string source)
{
if (!File.Exists(path))
return new();
var bookmarks = new List<Bookmark>();
using var jsonDocument = JsonDocument.Parse(File.ReadAllText(path));
if (!jsonDocument.RootElement.TryGetProperty("roots", out var rootElement))

View file

@ -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<Bookmark> LoadAllBookmarks()
{
var allbookmarks = new List<Bookmark>();
var chromeBookmarks = new ChromeBookmarkLoader();
var mozBookmarks = new FirefoxBookmarkLoader();
var edgeBookmarks = new EdgeBookmarkLoader();
var allBookmarks = new List<Bookmark>();
//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();
}
}
}
}

View file

@ -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<Bookmark> GetBookmarks() => BrowserDataPath != null ? LoadBookmarks(BrowserDataPath, BrowserName) : LoadBookmarksFromFile(BookmarkFilePath, BrowserName);
}
}

View file

@ -1,3 +1,4 @@
using Flow.Launcher.Plugin.BrowserBookmark.Models;
using System;
using System.Collections.Generic;
using System.IO;

View file

@ -1,3 +1,4 @@
using Flow.Launcher.Plugin.BrowserBookmark.Models;
using System;
using System.Collections.Generic;
using System.Data.SQLite;

View file

@ -69,4 +69,8 @@
<PackageReference Include="UnidecodeSharp" Version="1.0.0" />
</ItemGroup>
<ItemGroup>
<Compile Remove="Bookmark.cs" />
</ItemGroup>
</Project>

View file

@ -1,4 +1,5 @@
using System.Collections.Generic;
using Flow.Launcher.Plugin.BrowserBookmark.Models;
using System.Collections.Generic;
namespace Flow.Launcher.Plugin.BrowserBookmark
{

View file

@ -26,7 +26,7 @@ namespace Flow.Launcher.Plugin.BrowserBookmark
_settings = context.API.LoadSettingJsonStorage<Settings>();
cachedBookmarks = Bookmarks.LoadAllBookmarks();
cachedBookmarks = BookmarkLoader.LoadAllBookmarks();
}
public List<Result> 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()

View file

@ -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<CustomBrowser> CustomBrowsers { get; set; }= new();
}
}

View file

@ -0,0 +1,8 @@
namespace Flow.Launcher.Plugin.BrowserBookmark.Models
{
public class CustomBrowser : BaseModel
{
public string Name { get; set; }
public string Path { get; set; }
}
}

View file

@ -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<CustomBrowser> CustomBrowsers { get; set; } = new()
{
new CustomBrowser
{
Name="awefawef",
Path="awefawef"
},
new CustomBrowser
{
Name = "awefawefawefawef",
Path = "aweawefawfawef"
}
};
}
}

View file

@ -0,0 +1,36 @@
<Window x:Class="Flow.Launcher.Plugin.BrowserBookmark.Views.CustomBrowserSettingWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Flow.Launcher.Plugin.BrowserBookmark.Models"
mc:Ignorable="d"
Title="CustomBrowserSetting" Height="300" Width="400"
>
<Window.DataContext>
<local:CustomBrowser/>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="6*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="Browser Name" FontSize="15"
HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock Grid.Row="1" Grid.Column="0" Text="Browser Path" FontSize="15"
HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Name}"
HorizontalAlignment="Center" VerticalAlignment="Center" Width="100" Height="30"/>
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Path}"
HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" Height="30"/>
<StackPanel HorizontalAlignment="Right" Grid.Row="2" Orientation="Horizontal" Grid.Column="1" Height="60">
<Button Content="Confirm" Margin="15" Click="ConfirmEditCustomBrowser"/>
<Button Content="Cancel" Margin="15"/>
</StackPanel>
</Grid>
</Window>

View file

@ -0,0 +1,45 @@
using Flow.Launcher.Plugin.BrowserBookmark.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Flow.Launcher.Plugin.BrowserBookmark.Views
{
/// <summary>
/// Interaction logic for CustomBrowserSetting.xaml
/// </summary>
public partial class CustomBrowserSettingWindow : Window
{
private CustomBrowser currentCustomBrowser;
public CustomBrowserSettingWindow(CustomBrowser browser)
{
InitializeComponent();
currentCustomBrowser = browser;
DataContext = new CustomBrowser
{
Name = browser.Name,
Path = browser.Path
};
}
private void ConfirmEditCustomBrowser(object sender, RoutedEventArgs e)
{
if (DataContext is CustomBrowser editedBrowser)
{
currentCustomBrowser.Name = editedBrowser.Name;
currentCustomBrowser.Path = editedBrowser.Path;
}
Close();
}
}
}

View file

@ -5,14 +5,16 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Background="White"
d:DesignHeight="300" d:DesignWidth="500">
d:DesignHeight="300" d:DesignWidth="500"
DataContext="{Binding RelativeSource={RelativeSource Self}, Path=Settings}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="90" />
<RowDefinition />
<RowDefinition Height="50" />
<RowDefinition Height="80"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<StackPanel>
<Grid Grid.Row="0" Margin="40 40 0 0">
<Grid Grid.Row="0" Margin="40 20 0 0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="160" />
<ColumnDefinition Width="140"/>
@ -28,12 +30,40 @@
Click="OnNewTabClick" />
</Grid>
</StackPanel>
<StackPanel VerticalAlignment="Top" Grid.Row="1" Height="106" Margin="41,13,0,0">
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" Grid.Row="1" Height="60" Margin="25,13,0,0">
<Label Content="{DynamicResource flowlauncher_plugin_browserbookmark_settings_setBrowserFromPath}"
Height="28" Margin="0,0,155,0" HorizontalAlignment="Left" Width="290"/>
<TextBox x:Name="browserPathBox" HorizontalAlignment="Left" Height="34" TextWrapping="NoWrap" VerticalAlignment="Top" Width="311" RenderTransformOrigin="0.502,-1.668" TextChanged="OnBrowserPathTextChanged" />
Height="28" Margin="10"/>
<TextBox x:Name="browserPathBox"
HorizontalAlignment="Left"
Height="30"
TextWrapping="NoWrap"
VerticalAlignment="Center"
Text="{Binding BrowserPath}"
Width="240"
Margin="10"/>
<Button x:Name="viewButton" Content="{DynamicResource flowlauncher_plugin_browserbookmark_settings_choose}"
HorizontalAlignment="Left" Margin="340,-35,-1,0" Width="100" Height="34" Click="OnChooseClick" FontSize="14" />
HorizontalAlignment="Left" Margin="10" Width="100" Height="30" Click="OnChooseClick" FontSize="14" />
</StackPanel>
<StackPanel Grid.Row="2" Orientation="Vertical">
<TextBlock Text="CustomBrowsers" Margin="10"/>
<ListView Grid.Row="2" ItemsSource="{Binding CustomBrowsers}"
Margin="10"
BorderBrush="DarkGray"
BorderThickness="1"
Style="{StaticResource {x:Static GridView.GridViewStyleKey}}"
Height="auto"
Name="CustomBrowsers">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Browser Name"/>
<GridViewColumn DisplayMemberBinding="{Binding Path}" Header="Path"/>
</GridView>
</ListView.View>
</ListView>
<StackPanel HorizontalAlignment="Right" Orientation="Horizontal">
<Button Content="Add Custom Browser" Margin="10" Click="NewCustomBrowser"/>
<Button Content="Delete Custom Browser" Margin="10" Click="DeleteCustomBrowser"/>
</StackPanel>
</StackPanel>
</Grid>
</UserControl>

View file

@ -8,27 +8,26 @@ namespace Flow.Launcher.Plugin.BrowserBookmark.Views
/// <summary>
/// Interaction logic for BrowserBookmark.xaml
/// </summary>
public partial class SettingsControl : UserControl
public partial class SettingsControl
{
private readonly Settings _settings;
public Settings Settings { get; }
public SettingsControl(Settings settings)
{
Settings = settings;
InitializeComponent();
_settings = settings;
browserPathBox.Text = _settings.BrowserPath;
NewWindowBrowser.IsChecked = _settings.OpenInNewBrowserWindow;
NewTabInBrowser.IsChecked = !_settings.OpenInNewBrowserWindow;
NewWindowBrowser.IsChecked = Settings.OpenInNewBrowserWindow;
NewTabInBrowser.IsChecked = !Settings.OpenInNewBrowserWindow;
}
private void OnNewBrowserWindowClick(object sender, RoutedEventArgs e)
{
_settings.OpenInNewBrowserWindow = true;
Settings.OpenInNewBrowserWindow = true;
}
private void OnNewTabClick(object sender, RoutedEventArgs e)
{
_settings.OpenInNewBrowserWindow = false;
Settings.OpenInNewBrowserWindow = false;
}
private void OnChooseClick(object sender, RoutedEventArgs e)
@ -39,14 +38,31 @@ namespace Flow.Launcher.Plugin.BrowserBookmark.Views
fileBrowserDialog.CheckPathExists = true;
if (fileBrowserDialog.ShowDialog() == true)
{
browserPathBox.Text = fileBrowserDialog.FileName;
_settings.BrowserPath = fileBrowserDialog.FileName;
Settings.BrowserPath = fileBrowserDialog.FileName;
}
}
private void OnBrowserPathTextChanged(object sender, TextChangedEventArgs e)
private void NewCustomBrowser(object sender, RoutedEventArgs e)
{
_settings.BrowserPath = browserPathBox.Text;
var newBrowser = new CustomBrowser();
var window = new CustomBrowserSettingWindow(newBrowser);
window.ShowDialog();
if (newBrowser is not
{
Name: null,
Path: null
})
{
Settings.CustomBrowsers.Add(newBrowser);
}
}
private void DeleteCustomBrowser(object sender, RoutedEventArgs e)
{
if(CustomBrowsers.SelectedItem is CustomBrowser selectedCustomBrowser)
{
Settings.CustomBrowsers.Remove(selectedCustomBrowser);
}
}
}
}