mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 11:16:51 +02:00
Combine FolderLinks and DirectoryIndicator into FileSystemPlugin
cc @aaroncampf
This commit is contained in:
224
Wox.Plugin.SystemPlugins/FileSystem/FileSystemPlugin.cs
Normal file
224
Wox.Plugin.SystemPlugins/FileSystem/FileSystemPlugin.cs
Normal file
@@ -0,0 +1,224 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.Storage.UserSettings;
|
||||
|
||||
namespace Wox.Plugin.SystemPlugins.FileSystem
|
||||
{
|
||||
public class FileSystemPlugin : BaseSystemPlugin, ISettingProvider
|
||||
{
|
||||
private PluginInitContext context;
|
||||
private static List<string> driverNames = null;
|
||||
private static Dictionary<string, DirectoryInfo[]> parentDirectories = new Dictionary<string, DirectoryInfo[]>();
|
||||
|
||||
protected override List<Result> QueryInternal(Query query)
|
||||
{
|
||||
//TODO: Consider always clearing the cache
|
||||
List<Result> results = new List<Result>();
|
||||
if (string.IsNullOrEmpty(query.RawQuery))
|
||||
{
|
||||
// clear the cache
|
||||
if (parentDirectories.Count > 0)
|
||||
parentDirectories.Clear();
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
InitialDriverList();
|
||||
|
||||
var input = query.RawQuery.ToLower();
|
||||
var inputName = input.Split(new string[] { @"\" }, StringSplitOptions.None).First().ToLower();
|
||||
|
||||
var link = UserSettingStorage.Instance.FolderLinks.FirstOrDefault(x =>
|
||||
x.Nickname.Equals(inputName, StringComparison.OrdinalIgnoreCase));
|
||||
var currentPath = link != null ? link.Path : null;
|
||||
|
||||
foreach (var item in UserSettingStorage.Instance.FolderLinks)
|
||||
{
|
||||
var Name = item.Nickname;
|
||||
|
||||
if (Name.StartsWith(input, StringComparison.OrdinalIgnoreCase) && Name.Length != input.Length)
|
||||
{
|
||||
Result result = new Result
|
||||
{
|
||||
Title = Name,
|
||||
IcoPath = "Images/folder.png",
|
||||
Action = (c) =>
|
||||
{
|
||||
context.ChangeQuery(item.Nickname);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
results.Add(result);
|
||||
}
|
||||
}
|
||||
|
||||
if (currentPath == null)
|
||||
{
|
||||
if (!driverNames.Any(input.StartsWith))
|
||||
return results;
|
||||
|
||||
currentPath = input;
|
||||
}
|
||||
else
|
||||
currentPath += input.Remove(0, inputName.Length);
|
||||
|
||||
if (Directory.Exists(currentPath))
|
||||
{
|
||||
// show all child directory
|
||||
if (input.EndsWith("\\") || input.EndsWith("/"))
|
||||
{
|
||||
var dirInfo = new DirectoryInfo(currentPath);
|
||||
var dirs = dirInfo.GetDirectories();
|
||||
|
||||
var parentDirKey = input.TrimEnd('\\', '/');
|
||||
if (!parentDirectories.ContainsKey(parentDirKey))
|
||||
parentDirectories.Add(parentDirKey, dirs);
|
||||
|
||||
foreach (var dir in dirs)
|
||||
{
|
||||
if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
|
||||
continue;
|
||||
|
||||
var dirPath = dir.FullName;
|
||||
Result result = new Result
|
||||
{
|
||||
Title = dir.Name,
|
||||
IcoPath = "Images/folder.png",
|
||||
Action = (c) =>
|
||||
{
|
||||
context.ChangeQuery(dirPath);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
results.Add(result);
|
||||
}
|
||||
|
||||
if (results.Count == 0)
|
||||
{
|
||||
Result result = new Result
|
||||
{
|
||||
Title = "Open this directory",
|
||||
SubTitle = "No files in this directory",
|
||||
IcoPath = "Images/folder.png",
|
||||
Action = (c) =>
|
||||
{
|
||||
Process.Start(currentPath);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
results.Add(result);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Result result = new Result
|
||||
{
|
||||
Title = "Open this directory",
|
||||
SubTitle = string.Format("path: {0}", currentPath),
|
||||
Score = 50,
|
||||
IcoPath = "Images/folder.png",
|
||||
Action = (c) =>
|
||||
{
|
||||
Process.Start(currentPath);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
results.Add(result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// change to search in current directory
|
||||
var parentDir = Path.GetDirectoryName(input);
|
||||
if (!string.IsNullOrEmpty(parentDir) && results.Count == 0)
|
||||
{
|
||||
parentDir = parentDir.TrimEnd('\\', '/');
|
||||
if (parentDirectories.ContainsKey(parentDir))
|
||||
{
|
||||
|
||||
var dirs = parentDirectories[parentDir];
|
||||
var queryFileName = Path.GetFileName(currentPath).ToLower();
|
||||
var fuzzy = FuzzyMatcher.Create(queryFileName);
|
||||
foreach (var dir in dirs)
|
||||
{
|
||||
if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
|
||||
continue;
|
||||
|
||||
var matchResult = fuzzy.Evaluate(dir.Name);
|
||||
if (!matchResult.Success)
|
||||
continue;
|
||||
|
||||
var dirPath = dir.FullName;
|
||||
Result result = new Result
|
||||
{
|
||||
Title = dir.Name,
|
||||
IcoPath = "Images/folder.png",
|
||||
Score = matchResult.Score,
|
||||
Action = (c) =>
|
||||
{
|
||||
context.ChangeQuery(dirPath);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
results.Add(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
private void InitialDriverList()
|
||||
{
|
||||
if (driverNames == null)
|
||||
{
|
||||
driverNames = new List<string>();
|
||||
var allDrives = DriveInfo.GetDrives();
|
||||
foreach (var driver in allDrives)
|
||||
{
|
||||
driverNames.Add(driver.Name.ToLower().TrimEnd('\\'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void InitInternal(PluginInitContext context)
|
||||
{
|
||||
this.context = context;
|
||||
|
||||
if (UserSettingStorage.Instance.FolderLinks == null)
|
||||
{
|
||||
UserSettingStorage.Instance.FolderLinks = new List<FolderLink>();
|
||||
UserSettingStorage.Instance.Save();
|
||||
}
|
||||
}
|
||||
|
||||
public override string Name
|
||||
{
|
||||
get { return "File System"; }
|
||||
}
|
||||
|
||||
public override string IcoPath
|
||||
{
|
||||
get { return @"Images\folder.png"; }
|
||||
}
|
||||
|
||||
|
||||
public System.Windows.Controls.Control CreateSettingPanel()
|
||||
{
|
||||
return new FileSystemSettings();
|
||||
}
|
||||
|
||||
public override string Description
|
||||
{
|
||||
get { return base.Description; }
|
||||
}
|
||||
}
|
||||
}
|
||||
32
Wox.Plugin.SystemPlugins/FileSystem/FileSystemSettings.xaml
Normal file
32
Wox.Plugin.SystemPlugins/FileSystem/FileSystemSettings.xaml
Normal file
@@ -0,0 +1,32 @@
|
||||
<UserControl x:Class="Wox.Plugin.SystemPlugins.FileSystem.FileSystemSettings"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="300" d:DesignWidth="300">
|
||||
<Grid Margin="10">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="*"/>
|
||||
<RowDefinition Height="50"/>
|
||||
</Grid.RowDefinitions>
|
||||
<ListView x:Name="lbxFolders" Grid.Row="0">
|
||||
<ListView.View>
|
||||
<GridView>
|
||||
<GridViewColumn Header="Folder Path" Width="180">
|
||||
<GridViewColumn.CellTemplate>
|
||||
<DataTemplate>
|
||||
<TextBlock Text="{Binding Path}"/>
|
||||
</DataTemplate>
|
||||
</GridViewColumn.CellTemplate>
|
||||
</GridViewColumn>
|
||||
</GridView>
|
||||
</ListView.View>
|
||||
</ListView>
|
||||
<StackPanel Grid.Row="1" HorizontalAlignment="Right" Orientation="Horizontal">
|
||||
<Button x:Name="btnDelete" Click="btnDelete_Click" Width="100" Margin="10" Content="Delete"/>
|
||||
<Button x:Name="btnEdit" Click="btnEdit_Click" Width="100" Margin="10" Content="Edit"/>
|
||||
<Button x:Name="btnAdd" Click="btnAdd_Click" Width="100" Margin="10" Content="Add"/>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</UserControl>
|
||||
@@ -0,0 +1,73 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using Wox.Infrastructure.Storage.UserSettings;
|
||||
|
||||
namespace Wox.Plugin.SystemPlugins.FileSystem {
|
||||
/// <summary>
|
||||
/// Interaction logic for FileSystemSettings.xaml
|
||||
/// </summary>
|
||||
public partial class FileSystemSettings : UserControl {
|
||||
public FileSystemSettings() {
|
||||
InitializeComponent();
|
||||
lbxFolders.ItemsSource = Wox.Infrastructure.Storage.UserSettings.UserSettingStorage.Instance.FolderLinks;
|
||||
}
|
||||
|
||||
private void btnDelete_Click(object sender, RoutedEventArgs e) {
|
||||
var selectedFolder = lbxFolders.SelectedItem as FolderLink;
|
||||
if (selectedFolder != null)
|
||||
{
|
||||
UserSettingStorage.Instance.FolderLinks.Remove(selectedFolder);
|
||||
lbxFolders.Items.Refresh();
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Please select a folder link!");
|
||||
}
|
||||
}
|
||||
|
||||
private void btnEdit_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var selectedFolder = lbxFolders.SelectedItem as FolderLink;
|
||||
if (selectedFolder != null)
|
||||
{
|
||||
|
||||
var folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
|
||||
folderBrowserDialog.SelectedPath = selectedFolder.Path;
|
||||
if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
|
||||
{
|
||||
var link = UserSettingStorage.Instance.FolderLinks.First(x => x.Path == selectedFolder.Path);
|
||||
link.Path = folderBrowserDialog.SelectedPath;
|
||||
|
||||
UserSettingStorage.Instance.Save();
|
||||
}
|
||||
|
||||
lbxFolders.Items.Refresh();
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Please select a folder link!");
|
||||
}
|
||||
}
|
||||
|
||||
private void btnAdd_Click(object sender, RoutedEventArgs e) {
|
||||
var folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
|
||||
if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
|
||||
var newFolder = new Wox.Infrastructure.Storage.UserSettings.FolderLink() {
|
||||
Path = folderBrowserDialog.SelectedPath
|
||||
};
|
||||
|
||||
if (UserSettingStorage.Instance.FolderLinks == null) {
|
||||
UserSettingStorage.Instance.FolderLinks = new List<FolderLink>();
|
||||
}
|
||||
|
||||
UserSettingStorage.Instance.FolderLinks.Add(newFolder);
|
||||
UserSettingStorage.Instance.Save();
|
||||
}
|
||||
|
||||
lbxFolders.Items.Refresh();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user