Skip to content

Commit

Permalink
优化文件夹监视器;修复如果当前浏览的文件夹被外部删除就会报错的问题;优化打开zip路径的操作逻辑(末尾无需带'\\');修复“此电脑”右键…
Browse files Browse the repository at this point in the history
…菜单仍存在“新建文件夹”和“新建”的问题(点击无反应);修复“此电脑”右键菜单“显示更多选项”点击直接报错的问题
  • Loading branch information
DearVa committed Aug 27, 2022
1 parent 86e9a10 commit 65ccc6e
Show file tree
Hide file tree
Showing 47 changed files with 1,086 additions and 789 deletions.
47 changes: 32 additions & 15 deletions ExplorerEx/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using ExplorerEx.Database.Interface;
using ExplorerEx.Model;
using ExplorerEx.Database.SqlSugar;
using ExplorerEx.Shell32;
using ExplorerEx.Utils;
using ExplorerEx.View;
using Microsoft.Win32;
using static ExplorerEx.Win32.Win32Interop;

namespace ExplorerEx;
Expand All @@ -24,8 +22,13 @@ public partial class App {
public static Arguments Args { get; private set; } = null!;
public static int ProcessorCount { get; private set; }

private static readonly IBookmarkDbContext BookmarkDbContext = ConfigHelper.Container.Resolve<IBookmarkDbContext>();
private static readonly IFileViewDbContext FileViewDbContext = ConfigHelper.Container.Resolve<IFileViewDbContext>();
/// <summary>
/// 全局实例的注册容器
/// </summary>
public static IWindsorContainer Container { get; private set; } = null!;

public static IBookmarkDbContext BookmarkDbContext { get; private set; } = null!;
public static IFileViewDbContext FileViewDbContext { get; private set; } = null!;

private App() { }

Expand Down Expand Up @@ -65,33 +68,46 @@ public partial class App {
return;
}


Container = new WindsorContainer();
Container.Register(Component.For<IBookmarkDbContext>().Instance(
new BookmarkSugarContext()
// new BookmarkEfContext()
));
BookmarkDbContext = Container.Resolve<IBookmarkDbContext>();
Container.Register(Component.For<IFileViewDbContext>().Instance(
new FileViewSugarContext()
// new FileViewEfContext()
));
FileViewDbContext = Container.Resolve<IFileViewDbContext>();
var loadDataBaseTasks = new[] {
BookmarkDbContext.LoadAsync(),
FileViewDbContext.LoadAsync()
};

isRunning = true;
notifyMmf = new NotifyMemoryMappedFile("ExplorerExIPC", 1024, true);
new Thread(IPCWork) {
IsBackground = true
}.Start();


ProcessorCount = Environment.ProcessorCount;
IconHelper.Initialize();
Shell32Interop.Initialize();
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Settings.Current.LoadSettings();
ChangeTheme(((SolidColorBrush)SystemParameters.WindowGlassBrush).Color, false);
FrameworkElement.StyleProperty.OverrideMetadata(typeof(Separator), new FrameworkPropertyMetadata {
DefaultValue = FindResource("SeparatorBaseStyle")
});

await BookmarkDbContext.LoadAsync();
await FileViewDbContext.LoadAsync();
if (!Args.RunInBackground) {
new MainWindow(null).Show();
}

await Task.WhenAll(loadDataBaseTasks);

notifyIconWindow = new NotifyIconWindow();
//dispatcherTimer = new DispatcherTimer(TimeSpan.FromSeconds(5), DispatcherPriority.Background, LowFrequencyWork, Dispatcher);
//dispatcherTimer.Start();

if (!Args.RunInBackground) {
new MainWindow(null).Show();
}

#if DEBUG
EventManager.RegisterClassHandler(typeof(UIElement), UIElement.PreviewKeyDownEvent, new KeyEventHandler((_, args) => {
if (args.Key == Key.Pause) {
Expand Down Expand Up @@ -190,6 +206,7 @@ public partial class App {
if (isRunning) {
isRunning = false;
BookmarkDbContext.Save();
FileViewDbContext.Save();
notifyIconWindow?.NotifyIcon.Dispose();
mutex!.Dispose();
}
Expand Down
5 changes: 5 additions & 0 deletions ExplorerEx/Assets/Settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
<item header="DoubleClickGoUpperLevel" description="#DoubleClickGoUpperLevelDescription" type="boolean" default="true"/>
</category>

<category header="Customize">
<item header="DontAskWhenRecycle" type="boolean" default="false"/>
<item header="DontAskWhenDelete" type="boolean" default="false"/>
</category>

<category header="Advanced">
<item header="ShowHiddenFilesAndFolders" description="#ShowHiddenFilesAndFoldersDescription" type="boolean" default="false"/>
<item header="ShowProtectedSystemFilesAndFolders" description="#ShowProtectedSystemFilesAndFoldersDescription" type="boolean" default="false"/>
Expand Down
33 changes: 27 additions & 6 deletions ExplorerEx/Command/FileItemCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class FileItemCommand : ICommand {
/// <summary>
/// 当前操作的目录
/// </summary>
public FolderItem Folder { get; set; }
public FolderItem Folder { get; set; } = null!;

public bool CanExecute(object? parameter) => true;

Expand Down Expand Up @@ -96,12 +96,15 @@ public class FileItemCommand : ICommand {
var items = Items;
if (items.Count > 0) {
var data = new DataObject(DataFormats.FileDrop, items.Where(item => item is FileSystemItem or DiskDriveItem).Select(item => item.FullPath).ToArray());
data.SetData("IsCut", str == "Cut");
data.SetData("IsCut", !Folder.IsReadonly && str == "Cut");
Clipboard.SetDataObject(data);
}
break;
}
case "Paste": {
if (Folder.IsReadonly) {
break;
}
if (Clipboard.GetDataObject() is DataObject data) {
if (data.GetData(DataFormats.FileDrop) is string[] filePaths) {
bool isCut;
Expand All @@ -122,6 +125,9 @@ public class FileItemCommand : ICommand {
break;
}
case "Rename": {
if (Folder.IsReadonly) {
break;
}
var items = Items;
switch (items.Count) {
case <= 0:
Expand All @@ -136,8 +142,11 @@ public class FileItemCommand : ICommand {
break;
}
case "Delete": // 删除一个或多个文件,按住shift就是强制删除
if (Folder.IsReadonly) {
break;
}
if ((Keyboard.Modifiers & ModifierKeys.Shift) != ModifierKeys.Shift) { // 没有按Shift
if (!ContentDialog.ShowWithDefault("Recycle", "#AreYouSureToRecycleTheseFiles".L())) {
if (!ContentDialog.ShowWithDefault(Settings.CommonSettings.DontAskWhenRecycle, "#AreYouSureToRecycleTheseFiles".L())) {
return;
}
try {
Expand All @@ -146,7 +155,7 @@ public class FileItemCommand : ICommand {
Logger.Exception(e);
}
} else {
if (!ContentDialog.ShowWithDefault("Delete", "#AreYouSureToDeleteTheseFilesPermanently".L())) {
if (!ContentDialog.ShowWithDefault(Settings.CommonSettings.DontAskWhenDelete, "#AreYouSureToDeleteTheseFilesPermanently".L())) {
return;
}
var failedFiles = new List<string>();
Expand Down Expand Up @@ -192,6 +201,9 @@ public class FileItemCommand : ICommand {
}
break;
case "Unzip":
if (Folder.IsReadonly) {
break;
}
foreach (var item in Items.Where(i => i is FileItem { IsZip: true })) {
ExtractZipWindow.Show(item.FullPath);
}
Expand All @@ -206,9 +218,18 @@ public class FileItemCommand : ICommand {
}
var items = Items;
if (items.Count == 0) {
Shell32Interop.ShowShellContextMenu(Folder.FullPath);
if (Folder is ISpecialFolder specialFolder) {
Shell32Interop.ShowShellContextMenu(specialFolder.Csidl);
} else {
Shell32Interop.ShowShellContextMenu(Folder.FullPath);
}
} else {
Shell32Interop.ShowShellContextMenu(items.Select(i => i.FullPath).ToArray());
if (items[0] is ISpecialFolder) {
Debug.Assert(items.All(i => i is ISpecialFolder));
Shell32Interop.ShowShellContextMenu(items.Select(i => ((ISpecialFolder)i).Csidl).ToArray());
} else {
Shell32Interop.ShowShellContextMenu(items.Select(i => i.FullPath).ToArray());
}
}
break;
}
Expand Down
50 changes: 22 additions & 28 deletions ExplorerEx/Database/EntityFramework/BookmarkEfContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,30 +71,18 @@ public class BookmarkEfContext : DbContext, IBookmarkDbContext {

#region Interfaces

public ISet<BookmarkItem> GetBookmarkItems() {
return BookmarkDbSet.ToHashSet();
public void Add(BookmarkItem bookmark) {
BookmarkDbSet.Add(bookmark);
}

public void Add(BookmarkItem item) {
BookmarkDbSet.Add(item);
}
public Task AddAsync(BookmarkItem item) {
return BookmarkDbSet.AddAsync(item).AsTask();
}

public void Add(BookmarkCategory item) {
BookmarkCategoryDbSet.Add(item);
}
public Task AddAsync(BookmarkCategory item) {
return BookmarkCategoryDbSet.AddAsync(item).AsTask();
public Task AddAsync(BookmarkItem bookmark) {
return BookmarkDbSet.AddAsync(bookmark).AsTask();
}


public ISet<BookmarkItem> GetLocalBookmarkItems() {
return BookmarkDbSet.Local.ToHashSet();
public void Add(BookmarkCategory category) {
BookmarkCategoryDbSet.Add(category);
}
public ISet<BookmarkCategory> GetBookmarkCategories() {
return BookmarkCategoryDbSet.ToHashSet();
public Task AddAsync(BookmarkCategory category) {
return BookmarkCategoryDbSet.AddAsync(category).AsTask();
}

public Task LoadAsync() {
Expand All @@ -109,25 +97,31 @@ public class BookmarkEfContext : DbContext, IBookmarkDbContext {
return base.SaveChangesAsync();
}

public void Remove(BookmarkItem item) {
base.Remove(item);
public void Remove(BookmarkItem bookmark) {
base.Remove(bookmark);
}

public bool Contains(BookmarkItem bookmark) {
return BookmarkDbSet.Contains(bookmark);
}

public bool Any(Func<BookmarkItem, bool> match) {
return BookmarkDbSet.Any(match);
}

public ObservableCollection<BookmarkCategory> GetBindable() {
return BookmarkCategoryDbSet.Local.ToObservableCollection();
}

#region ProbablyModify
public BookmarkCategory? FindFirstOrDefault(Func<BookmarkCategory, bool> match) {
return GetBookmarkCategories().FirstOrDefault(match);
public BookmarkCategory? FirstOrDefault(Func<BookmarkCategory, bool> match) {
return BookmarkCategoryDbSet.FirstOrDefault(match);
}

public BookmarkItem? FindLocalItemFirstOrDefault(Func<BookmarkItem, bool> match) {
return GetLocalBookmarkItems().FirstOrDefault(match);
public BookmarkItem? FirstOrDefault(Func<BookmarkItem, bool> match) {
return BookmarkDbSet.FirstOrDefault(match);
}



#endregion

#endregion
Expand Down
20 changes: 12 additions & 8 deletions ExplorerEx/Database/EntityFramework/FileViewEfContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace ExplorerEx.Database.EntityFramework;

public class FileViewEfContext : DbContext, IFileViewDbContext {
private DbSet<FileView> FolderViewDbSet { get; set; } = null!;
private DbSet<FileView> FileViewDbSet { get; set; } = null!;

private readonly string dbPath;

Expand All @@ -29,22 +29,22 @@ public class FileViewEfContext : DbContext, IFileViewDbContext {
ob.UseSqlite($"Data Source={dbPath}");
}

public ISet<FileView> GetFileViews() {
return FolderViewDbSet.ToHashSet();
public bool Any(Func<FileView, bool> match) {
return FileViewDbSet.Any(match);
}

public void Add(FileView item) {
FolderViewDbSet.Add(item);
FileViewDbSet.Add(item);
}

public Task AddAsync(FileView item) {
return FolderViewDbSet.AddAsync(item).AsTask();
return FileViewDbSet.AddAsync(item).AsTask();
}

public async Task LoadAsync() {
try {
await Database.EnsureCreatedAsync();
await FolderViewDbSet.LoadAsync();
await FileViewDbSet.LoadAsync();
} catch (Exception e) {
MessageBox.Show("无法加载数据库,可能是权限不够或者数据库版本过旧,请删除Data文件夹后再试一次。\n错误为:" + e.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
Logger.Exception(e, false);
Expand All @@ -59,7 +59,11 @@ public class FileViewEfContext : DbContext, IFileViewDbContext {
return base.SaveChangesAsync();
}

public FileView? FindFirstOrDefault(Func<FileView, bool> match) {
return GetFileViews().FirstOrDefault(match);
public FileView? FirstOrDefault(Func<FileView, bool> match) {
return FileViewDbSet.FirstOrDefault(match);
}

public bool Contains(FileView fileView) {
return FileViewDbSet.Contains(fileView);
}
}
23 changes: 9 additions & 14 deletions ExplorerEx/Database/Interface/IBookmarkDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,17 @@
namespace ExplorerEx.Database.Interface;

public interface IBookmarkDbContext : IDatabase {
void Add(BookmarkItem bookmark);
Task AddAsync(BookmarkItem bookmark);
public BookmarkItem? FirstOrDefault(Func<BookmarkItem, bool> match);
void Remove(BookmarkItem bookmark);
bool Contains(BookmarkItem bookmark);
bool Any(Func<BookmarkItem, bool> match);

ISet<BookmarkItem> GetBookmarkItems();

void Add(BookmarkItem item);
Task AddAsync(BookmarkItem item);

ISet<BookmarkItem> GetLocalBookmarkItems();
public BookmarkItem? FindLocalItemFirstOrDefault(Func<BookmarkItem, bool> match);
void Remove(BookmarkItem item);

ISet<BookmarkCategory> GetBookmarkCategories();

BookmarkCategory? FindFirstOrDefault(Func<BookmarkCategory, bool> match);

void Add(BookmarkCategory item);
Task AddAsync(BookmarkCategory item);
BookmarkCategory? FirstOrDefault(Func<BookmarkCategory, bool> match);
void Add(BookmarkCategory category);
Task AddAsync(BookmarkCategory category);

ObservableCollection<BookmarkCategory> GetBindable();
}
18 changes: 9 additions & 9 deletions ExplorerEx/Database/Interface/IFileViewDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ExplorerEx.Model;

namespace ExplorerEx.Database.Interface {
public interface IFileViewDbContext : IDatabase {
ISet<FileView> GetFileViews();
FileView? FindFirstOrDefault(Func<FileView, bool> match);
namespace ExplorerEx.Database.Interface;

void Add(FileView item);
Task AddAsync(FileView item);
}
}
public interface IFileViewDbContext : IDatabase {
FileView? FirstOrDefault(Func<FileView, bool> match);
bool Contains(FileView fileView);
bool Any(Func<FileView, bool> match);

void Add(FileView item);
Task AddAsync(FileView item);
}

0 comments on commit 65ccc6e

Please sign in to comment.