feture 增加关联实时文件夹

This commit is contained in:
BookerLiu
2023-03-21 17:18:18 +08:00
parent 32adce1815
commit 425d1a4ce5
9 changed files with 280 additions and 1 deletions

14
Constant/MenuType.cs Normal file
View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GeekDesk.Constant
{
public enum MenuType
{
NORMAL, //普通菜单
LINK, //关联菜单
}
}

View File

@@ -49,7 +49,7 @@ namespace GeekDesk.Control.UserControls.Config
OpenFileDialog ofd = new OpenFileDialog
{
Multiselect = false, //只允许选中单个文件
Filter = "图像文件(*.png, *.jpg)|*.png;*.jpg;*.gif"
Filter = "图像文件(*.png, *.jpg, *.gif)|*.png;*.jpg;*.gif"
};
if (ofd.ShowDialog() == true)
{

View File

@@ -139,6 +139,7 @@
<hc:Card.ContextMenu>
<ContextMenu Width="200">
<MenuItem Header="新建菜单" Click="CreateMenu"/>
<MenuItem Header="新建关联菜单" Click="CreateLinkMenu"/>
<MenuItem x:Name="AlterPW1" Header="修改密码" Click="AlterPassword"/>
</ContextMenu>
</hc:Card.ContextMenu>
@@ -158,6 +159,7 @@
<ListBox.Resources>
<ContextMenu x:Key="MenuDialog" Width="200">
<MenuItem Header="新建菜单" Click="CreateMenu"/>
<MenuItem Header="新建关联菜单" Click="CreateLinkMenu"/>
<MenuItem Header="重命名" Click="RenameMenu" Tag="{Binding}"/>
<MenuItem Header="加密此列表" Click="EncryptMenu" Tag="{Binding}"/>
<MenuItem x:Name="AlterPW2" Header="修改密码" Click="AlterPassword"/>

View File

@@ -4,14 +4,19 @@ using GeekDesk.Control.Other;
using GeekDesk.Control.Windows;
using GeekDesk.Util;
using GeekDesk.ViewModel;
using Microsoft.Win32;
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using WindowsAPICodePack.Dialogs;
namespace GeekDesk.Control.UserControls.PannelCard
{
@@ -200,6 +205,87 @@ namespace GeekDesk.Control.UserControls.PannelCard
Lbi_Selected(obj, null);
}
/// <summary>
/// 创建实时文件菜单
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CreateLinkMenu(object sender, RoutedEventArgs e)
{
try
{
CommonOpenFileDialog dialog = new CommonOpenFileDialog
{
IsFolderPicker = true,
Title = "选择关联文件夹"
};
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
string menuId = System.Guid.NewGuid().ToString();
new Thread(() =>
{
this.Dispatcher.BeginInvoke(new Action(() =>
{
string path = dialog.FileName;
MenuInfo menuInfo = new MenuInfo
{
MenuName = Path.GetFileNameWithoutExtension(path),
MenuId = menuId,
MenuType = MenuType.LINK,
LinkPath = path,
IsEncrypt = false,
};
appData.MenuList.Add(menuInfo);
MenuListBox.SelectedIndex = appData.MenuList.Count - 1;
appData.AppConfig.SelectedMenuIndex = MenuListBox.SelectedIndex;
appData.AppConfig.SelectedMenuIcons = menuInfo.IconList;
//首次触发不了Selected事件
object obj = MenuListBox.ItemContainerGenerator.ContainerFromIndex(MenuListBox.SelectedIndex);
SetListBoxItemEvent((ListBoxItem)obj);
Lbi_Selected(obj, null);
HandyControl.Controls.Growl.Success("菜单关联成功, 后台加载列表!", "MainWindowGrowl");
FileWatcher.LinkMenuWatcher(menuInfo);
}));
}).Start();
new Thread(() =>
{
Thread.Sleep(1000);
this.Dispatcher.BeginInvoke(new Action(() =>
{
MenuInfo info = null;
foreach (MenuInfo menuInfo in appData.MenuList)
{
if (menuInfo.MenuId.Equals(menuId))
{
info = menuInfo;
}
}
DirectoryInfo dirInfo = new DirectoryInfo(info.LinkPath);
FileSystemInfo[] fileInfos = dirInfo.GetFileSystemInfos();
foreach (FileSystemInfo fileInfo in fileInfos)
{
IconInfo iconInfo = CommonCode.GetIconInfoByPath_NoWrite(fileInfo.FullName);
info.IconList.Add(iconInfo);
}
}));
}).Start();
}
}
catch (Exception ex)
{
LogUtil.WriteErrorLog(ex, "新建关联菜单失败!");
HandyControl.Controls.Growl.WarningGlobal("新建关联菜单失败!");
}
}
/// <summary>
/// 重命名菜单 将textbox 设置为可见
@@ -635,5 +721,7 @@ namespace GeekDesk.Control.UserControls.PannelCard
}
}
}
}

View File

@@ -147,6 +147,9 @@
<Reference Include="System.Xaml">
<RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference>
<Reference Include="WindowsAPICodePack.Shell.CommonFileDialogs, Version=1.1.5.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\WindowsAPICodePack.Shell.CommonFileDialogs.1.1.5\lib\net452\WindowsAPICodePack.Shell.CommonFileDialogs.dll</HintPath>
</Reference>
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
@@ -167,6 +170,7 @@
<Compile Include="Constant\IconType.cs" />
<Compile Include="Constant\CommonEnum.cs" />
<Compile Include="Constant\IconStartType.cs" />
<Compile Include="Constant\MenuType.cs" />
<Compile Include="Constant\PasswordType.cs" />
<Compile Include="Constant\RunTimeStatus.cs" />
<Compile Include="Constant\SearchType.cs" />
@@ -287,6 +291,7 @@
<Compile Include="Util\ColorUtil.cs" />
<Compile Include="Util\DefaultIcons.cs" />
<Compile Include="Util\DragAdorner.cs" />
<Compile Include="Util\FileWatcher.cs" />
<Compile Include="Util\GlobalHotKey.cs" />
<Compile Include="Util\CommonCode.cs" />
<Compile Include="Util\FileIcon.cs" />

View File

@@ -226,6 +226,9 @@ namespace GeekDesk
SecondsWindow.ShowWindow();
}
//监听实时文件夹菜单
FileWatcher.StartLinkMenuWatcher(appData);
//更新线程开启 检测更新
UpdateThread.Update();

138
Util/FileWatcher.cs Normal file
View File

@@ -0,0 +1,138 @@
using GeekDesk.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GeekDesk.Util
{
public class FileWatcher
{
public static Dictionary<FileSystemWatcher, MenuInfo> linkMenuMap = new Dictionary<FileSystemWatcher, MenuInfo>();
/// <summary>
/// 实时文件夹监听
/// </summary>
/// <param name="path"></param>
public static void LinkMenuWatcher(MenuInfo menuInfo)
{
try
{
FileSystemWatcher fileSystemWatcher = new FileSystemWatcher
{
Path = menuInfo.LinkPath,
};
linkMenuMap.Add(fileSystemWatcher, menuInfo);
fileSystemWatcher.EnableRaisingEvents = true;
fileSystemWatcher.Changed += LinkIcon_Changed;
fileSystemWatcher.Deleted += LinkIcon_Deleted;
fileSystemWatcher.Created += LinkIcon_Created;
fileSystemWatcher.Renamed += LinkIcon_Renamed;
} catch (Exception e)
{
LogUtil.WriteErrorLog(e, "添加LinkMenu监听异常");
}
}
private static void LinkIcon_Renamed(object sender, RenamedEventArgs e)
{
IconInfo iconInfo = getIconInfoByPath(sender, e.OldFullPath);
iconInfo.Name = e.Name;
iconInfo.Path = e.FullPath;
}
private static void LinkIcon_Changed(object sender, FileSystemEventArgs e)
{
IconInfo iconInfo = getIconInfoByPath(sender, e.FullPath);
if (iconInfo != null)
{
IconInfo newIconInfo = CommonCode.GetIconInfoByPath(e.FullPath);
iconInfo.BitmapImage = newIconInfo.BitmapImage;
}
}
private static void LinkIcon_Deleted(object sender, FileSystemEventArgs e)
{
IconInfo iconInfo = getIconInfoByPath(sender, e.FullPath);
App.Current.Dispatcher.Invoke(() =>
{
linkMenuMap[sender as FileSystemWatcher].IconList.Remove(iconInfo);
});
}
private static void LinkIcon_Created(object sender, FileSystemEventArgs e)
{
IconInfo iconInfo = CommonCode.GetIconInfoByPath(e.FullPath);
App.Current.Dispatcher.Invoke(() =>
{
linkMenuMap[sender as FileSystemWatcher].IconList.Add(iconInfo);
});
}
private static IconInfo getIconInfoByPath(object sender, string path)
{
MenuInfo menuInfo = linkMenuMap[sender as FileSystemWatcher];
foreach (IconInfo iconInfo in menuInfo.IconList)
{
if (iconInfo.Path.Equals(path))
{
return iconInfo;
}
}
return null;
}
/// <summary>
/// 开启所有菜单监听
/// </summary>
/// <param name="appData"></param>
public static void StartLinkMenuWatcher(AppData appData)
{
foreach (MenuInfo menuInfo in appData.MenuList)
{
if (menuInfo.MenuType == Constant.MenuType.LINK)
{
LinkMenuWatcher(menuInfo);
}
}
}
/// <summary>
/// 移除菜单监听
/// </summary>
/// <param name="menuInfo"></param>
public static void RemoveLinkMenuWatcher(MenuInfo menuInfo)
{
try
{
foreach (FileSystemWatcher watcher in linkMenuMap.Keys)
{
if (linkMenuMap[watcher] == menuInfo)
{
//释放资源
watcher.Changed -= LinkIcon_Changed;
watcher.Created -= LinkIcon_Created;
watcher.Deleted -= LinkIcon_Deleted;
watcher.Renamed -= LinkIcon_Renamed;
watcher.EnableRaisingEvents = false;
watcher.Dispose();
linkMenuMap.Remove(watcher);
}
}
}
catch (Exception e)
{
//nothing
}
}
}
}

View File

@@ -21,8 +21,36 @@ namespace GeekDesk.ViewModel
private string geometryColor; //几何图标颜色
private ObservableCollection<IconInfo> iconList = new ObservableCollection<IconInfo>();
private bool isEncrypt; //是否加密
private MenuType menuType; //菜单类型 普通, 关联
private string linkPath; //关联路径
public string LinkPath
{
get
{
return linkPath;
}
set
{
linkPath = value;
OnPropertyChanged("LinkPath");
}
}
public MenuType MenuType
{
get
{
return menuType;
}
set
{
menuType = value;
OnPropertyChanged("MenuType");
}
}
public bool IsEncrypt
{
get

View File

@@ -19,5 +19,6 @@
<package id="System.Reactive" version="5.0.0" targetFramework="net472" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net472" />
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net472" />
<package id="WindowsAPICodePack.Shell.CommonFileDialogs" version="1.1.5" targetFramework="net472" />
<package id="XamlFlair.WPF" version="1.2.13" targetFramework="net472" />
</packages>