Skip to content

Commit

Permalink
修复菜单没有对齐的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
DearVa committed Jul 14, 2022
1 parent f0be4c8 commit f24b0db
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 100 deletions.
4 changes: 2 additions & 2 deletions ExplorerEx/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
<c:Bytes2StringConverter x:Key="Bytes2StringConverter"/>

<Style x:Key="ButtonLikeMenuItem" TargetType="MenuItem">
<Setter Property="Width" Value="40"/>
<Setter Property="Height" Value="35"/>
<Setter Property="Width" Value="38"/>
<Setter Property="Height" Value="34"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="MenuItem">
Expand Down
8 changes: 5 additions & 3 deletions ExplorerEx/Model/FileItem/Bookmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using ExplorerEx.Converter;
using ExplorerEx.Utils;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using static ExplorerEx.Model.FileListViewItem;
using static ExplorerEx.Utils.IconHelper;

namespace ExplorerEx.Model;
Expand Down Expand Up @@ -79,11 +81,11 @@ public class BookmarkItem : FileListViewItem, IFilterable {
category.AddBookmark(this);
}

public override void LoadAttributes() {
public override void LoadAttributes(LoadDetailsOptions options) {
throw new InvalidOperationException();
}

public override void LoadIcon() {
public override void LoadIcon(LoadDetailsOptions options) {
if (FullPath.Length == 3) {
IsFolder = true;
Icon = GetPathThumbnail(FullPath);
Expand Down Expand Up @@ -144,7 +146,7 @@ public class BookmarkDbContext : DbContext {
}
await Task.Run(() => {
foreach (var item in BookmarkDbSet.Local) {
item.LoadIcon();
item.LoadIcon(LoadDetailsOptions.Default);
}
});
} catch (Exception e) {
Expand Down
4 changes: 2 additions & 2 deletions ExplorerEx/Model/FileItem/DiskDriveItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public sealed class DiskDriveItem : FolderItem {
Name = drive.Name;
}

public override void LoadAttributes() {
public override void LoadAttributes(LoadDetailsOptions options) {
Type = DriveUtils.GetTypeDescription(Drive);
if (Drive.IsReady) {
TotalSpace = Drive.TotalSize;
Expand All @@ -56,7 +56,7 @@ public sealed class DiskDriveItem : FolderItem {
UpdateUI(nameof(SpaceOverviewString));
}

public override void LoadIcon() {
public override void LoadIcon(LoadDetailsOptions options) {
Icon = IconHelper.GetPathThumbnail(Drive.Name);
}

Expand Down
26 changes: 17 additions & 9 deletions ExplorerEx/Model/FileItem/FileListViewItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ public abstract class FileListViewItem : SimpleNotifyPropertyChanged {
/// <summary>
/// 加载文件的各项属性
/// </summary>
public abstract void LoadAttributes();
public abstract void LoadAttributes(LoadDetailsOptions options);

/// <summary>
/// LoadIcon用到了shell,那并不是一个可以多线程的方法,所以与其每次都Task.Run,不如提高粗粒度
/// </summary>
public abstract void LoadIcon();
public abstract void LoadIcon(LoadDetailsOptions options);

#region 文件重命名
/// <summary>
Expand Down Expand Up @@ -142,7 +142,7 @@ public abstract class FileListViewItem : SimpleNotifyPropertyChanged {
/// <param name="token"></param>
/// <param name="useLargeIcon"></param>
/// <returns></returns>
public static async Task LoadDetails(IReadOnlyCollection<FileListViewItem> list, CancellationToken token, bool useLargeIcon) {
public static async Task LoadDetails(IReadOnlyCollection<FileListViewItem> list, CancellationToken token, LoadDetailsOptions options) {
try {
if (list.Count > 0) {
await Task.WhenAll(Partitioner.Create(list).GetPartitions(4).Select(partition => Task.Run(() => {
Expand All @@ -152,12 +152,8 @@ public abstract class FileListViewItem : SimpleNotifyPropertyChanged {
using (partition) {
while (partition.MoveNext()) {
var item = partition.Current!;
item.LoadAttributes();
if (item is FileItem fileItem) {
fileItem.UseLargeIcon = useLargeIcon;
}
item.LoadIcon();
item.LoadAttributes(options);
item.LoadIcon(options);
if (token.IsCancellationRequested) {
return Task.FromCanceled(token);
Expand All @@ -174,6 +170,18 @@ public abstract class FileListViewItem : SimpleNotifyPropertyChanged {
Logger.Exception(e);
}
}


/// <summary>
/// 加载详细信息时的设置,例如是否使用大图标
/// </summary>
public class LoadDetailsOptions {
public static LoadDetailsOptions Default { get; } = new() {
UseLargeIcon = false
};

public bool UseLargeIcon { get; set; }
}
}

public class FileItemAttach {
Expand Down
24 changes: 11 additions & 13 deletions ExplorerEx/Model/FileItem/FileSystemItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,15 @@ public abstract class FileSystemItem : FileListViewItem {
return false;
}

public void Refresh() {
/// <summary>
/// 重新加载图标和详细信息
/// </summary>
public void Refresh(LoadDetailsOptions options) {
if (!IsFolder) {
LoadIcon();
LoadIcon(options);
}
UpdateUI(nameof(Icon));
LoadAttributes();
LoadAttributes(options);
}
}

Expand All @@ -95,11 +98,6 @@ public class FileItem : FileSystemItem {

public override string DisplayText => Name;

/// <summary>
/// 是否使用大图标
/// </summary>
public bool UseLargeIcon { get; set; }

protected FileItem() { }

public FileItem(FileInfo fileInfo) {
Expand All @@ -111,7 +109,7 @@ public class FileItem : FileSystemItem {
Icon = UnknownFileDrawingImage;
}

public override void LoadAttributes() {
public override void LoadAttributes(LoadDetailsOptions options) {
if (FileInfo == null) {
return;
}
Expand All @@ -126,8 +124,8 @@ public class FileItem : FileSystemItem {
DateCreated = FileInfo.CreationTime;
}

public override void LoadIcon() {
if (UseLargeIcon) {
public override void LoadIcon(LoadDetailsOptions options) {
if (options.UseLargeIcon) {
Icon = GetPathThumbnail(FullPath);
} else {
Icon = GetSmallIcon(FullPath, false);
Expand Down Expand Up @@ -206,7 +204,7 @@ public class FolderItem : FileSystemItem {

public override string DisplayText => Name;

public override void LoadAttributes() {
public override void LoadAttributes(LoadDetailsOptions options) {
isEmptyFolder = FolderUtils.IsEmptyFolder(FullPath);
Type = isEmptyFolder ? "EmptyFolder".L() : "Folder".L();
IsEmptyFolderDictionary.Add(FullPath, isEmptyFolder);
Expand All @@ -215,7 +213,7 @@ public class FolderItem : FileSystemItem {
DateCreated = directoryInfo.CreationTime;
}

public override void LoadIcon() {
public override void LoadIcon(LoadDetailsOptions options) {
if (isEmptyFolder) {
Icon = EmptyFolderDrawingImage;
} else {
Expand Down
7 changes: 4 additions & 3 deletions ExplorerEx/Model/FileItem/FolderOnlyItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading.Tasks;
using System.Windows;
using ExplorerEx.Utils;
using Microsoft.Extensions.Options;

namespace ExplorerEx.Model;

Expand Down Expand Up @@ -101,11 +102,11 @@ internal sealed class FolderOnlyItem : FileListViewItem {
Application.Current.Dispatcher.Invoke(() => actualChildren = new ObservableCollection<FolderOnlyItem>());
}

public override void LoadAttributes() {
public override void LoadAttributes(LoadDetailsOptions options) {
throw new InvalidOperationException();
}

public override void LoadIcon() {
public override void LoadIcon(LoadDetailsOptions options) {
if (zipPath != null) {
if (relativePath == string.Empty) {
Icon = IconHelper.GetSmallIcon(".zip", true);
Expand Down Expand Up @@ -217,7 +218,7 @@ internal sealed class FolderOnlyItem : FileListViewItem {
if (token.IsCancellationRequested) {
return;
}
item.LoadIcon();
item.LoadIcon(LoadDetailsOptions.Default);
}
}
}, token);
Expand Down
4 changes: 2 additions & 2 deletions ExplorerEx/Model/FileItem/HomeFolderItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ internal sealed class HomeFolderItem : FolderItem, ISpecialFolder {

public override string DisplayText => Name;

public override void LoadAttributes() {
public override void LoadAttributes(LoadDetailsOptions options) {
throw new InvalidOperationException();
}

public override void LoadIcon() { }
public override void LoadIcon(LoadDetailsOptions options) { }

protected override bool InternalRename(string newName) {
throw new InvalidOperationException();
Expand Down
14 changes: 10 additions & 4 deletions ExplorerEx/Model/FileItem/RecycleBinItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,20 @@ public sealed class RecycleBinItem : FileListViewItem, IFilterable {
});
}

public override void LoadAttributes() {
public override void LoadAttributes(LoadDetailsOptions options) {
throw new InvalidOperationException();
}

public override void LoadIcon() {
public override void LoadIcon(LoadDetailsOptions options) {
var shFileInfo = new ShFileInfo();
lock (ShellLock) {
var hr = SHGetFileInfo(pidl, 0, ref shFileInfo, Marshal.SizeOf<ShFileInfo>(), SHGFI.Icon | SHGFI.SmallIcon | SHGFI.Pidl);
var flags = SHGFI.Icon | SHGFI.Pidl;
if (options.UseLargeIcon) {
flags |= SHGFI.LargeIcon;
} else {
flags |= SHGFI.SmallIcon;
}
var hr = SHGetFileInfo(pidl, 0, ref shFileInfo, Marshal.SizeOf<ShFileInfo>(), flags);
if (hr < 0) {
Icon = IconHelper.UnknownFileDrawingImage;
} else {
Expand Down Expand Up @@ -150,7 +156,7 @@ public sealed class RecycleBinItem : FileListViewItem, IFilterable {
break;
}
var item = new RecycleBinItem(pidl);
item.LoadIcon();
item.LoadIcon(LoadDetailsOptions.Default);
dispatcher.Invoke(() => Items.Add(item));
}
Marshal.ReleaseComObject(enumFiles);
Expand Down
8 changes: 4 additions & 4 deletions ExplorerEx/Model/FileItem/ZipItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public class ZipFileItem : FileItem {
extension = Path.GetExtension(zipArchiveEntry.Name);
}

public override void LoadAttributes() {
public override void LoadAttributes(LoadDetailsOptions options) {
Type = FileUtils.GetFileTypeDescription(extension);
}

public override void LoadIcon() {
public override void LoadIcon(LoadDetailsOptions options) {
Icon = IconHelper.GetSmallIcon(extension, true);
}

Expand Down Expand Up @@ -80,9 +80,9 @@ public class ZipFolderItem : FolderItem, IDisposable {
this.relativePath = relativePath;
}

public override void LoadAttributes() { }
public override void LoadAttributes(LoadDetailsOptions options) { }

public override void LoadIcon() {
public override void LoadIcon(LoadDetailsOptions options) {
if (relativePath == string.Empty) {
Icon = IconHelper.GetSmallIcon(".zip", true);
} else if (hasItems) {
Expand Down
4 changes: 1 addition & 3 deletions ExplorerEx/View/Controls/FileListView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@
</c:FileAssocTemplateSelector>

<ContextMenu x:Key="FileItemContextMenu" d:DataContext="{d:DesignInstance ct:FileListView}">
<MenuItem Height="45" Template="{StaticResource FileOperationsTemplate}"/>
<MenuItem Height="40" Template="{StaticResource FileOperationsTemplate}"/>
<Separator Style="{StaticResource SeparatorBaseStyle}"/>

<MenuItem Header="{u:Lang Open}" CommandParameter="Open"
Expand Down Expand Up @@ -507,8 +507,6 @@
<hc:SimplePanel Background="Transparent">
<TextBlock Text="{u:Lang ThisIsAnEmptyFolder}" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="14"
Visibility="{Binding Path=ViewModel.(vm:FileTabViewModel.IsLoading), Mode=OneWay, RelativeSource={RelativeSource AncestorType=ct:FileListView}, Converter={StaticResource Boolean2VisibilityReConverter}}"/>
<TextBlock Text="{u:Lang Loading}" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="14"
Visibility="{Binding Path=ViewModel.(vm:FileTabViewModel.IsLoading), Mode=OneWay, RelativeSource={RelativeSource AncestorType=ct:FileListView}, Converter={StaticResource Boolean2VisibilityConverter}}"/>
</hc:SimplePanel>
</hc:ToggleBlock.UnCheckedContent>
</hc:ToggleBlock>
Expand Down
2 changes: 1 addition & 1 deletion ExplorerEx/View/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ public sealed partial class MainWindow {
var item = new BookmarkItem(bookmarkItem, window.BookmarkName, categoryItem);
await bookmarkDb.BookmarkDbSet.AddAsync(item);
await bookmarkDb.SaveChangesAsync();
item.LoadIcon();
item.LoadIcon(FileListViewItem.LoadDetailsOptions.Default);
}
foreach (var updateItem in MainWindows.SelectMany(mw => mw.SplitGrid).SelectMany(f => f.TabItems).SelectMany(i => i.Items).Where(i => i.FullPath == fullPath)) {
updateItem.UpdateUI(nameof(updateItem.IsBookmarked));
Expand Down
Loading

0 comments on commit f24b0db

Please sign in to comment.