Skip to content

Commit

Permalink
Finish develop ShellMenu function(in ShellMenuPage)
Browse files Browse the repository at this point in the history
Finish develop ShellMenu function(in ShellMenuPage)
  • Loading branch information
Gaoyifei1011 committed Sep 4, 2024
1 parent a45ad8c commit 3cadaee
Show file tree
Hide file tree
Showing 44 changed files with 784 additions and 825 deletions.
9 changes: 2 additions & 7 deletions WindowsTools/Extensions/DataType/Enums/MenuType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,14 @@
/// </summary>
public enum MenuType
{
/// <summary>
/// 根菜单
/// </summary>
RootMenu = 0,

/// <summary>
/// 一级菜单
/// </summary>
FirstLevelMenu = 1,
FirstLevelMenu = 0,

/// <summary>
/// 二级菜单
/// </summary>
SecondLevelMenu = 2
SecondLevelMenu = 1
}
}
3 changes: 2 additions & 1 deletion WindowsTools/Extensions/DataType/Enums/OperationKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public enum OperationKind
MenuLightThemeIconPathEmpty = 18,
MenuDarkThemeIconPathEmpty = 19,
MenuProgramPathEmpty = 20,
MenuMatchRuleEmpty = 21
MenuMatchRuleEmpty = 21,
ShellMenuNeedToRefreshData = 22
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static string ReadNullTerminatedString(this BinaryReader reader, Encoding
using BinaryReader binaryReader = new(reader.BaseStream, encoding, true);
StringBuilder result = new();
char c;
while ((c = binaryReader.ReadChar()) != '\0')
while ((c = binaryReader.ReadChar()) is not '\0')
{
result.Append(c);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ public HierarchicalSchemaSection(string sectionIdentifier, BinaryReader binaryRe
byte flags = binaryReader.ReadByte();
uint nameOffset = binaryReader.ReadUInt16() | (uint)((flags & 0xF) << 16);
ushort index = binaryReader.ReadUInt16();
bool isScope = (flags & 0x10) != 0;
bool nameInAscii = (flags & 0x20) != 0;
bool isScope = (flags & 0x10) is not 0;
bool nameInAscii = (flags & 0x20) is not 0;
scopeAndItemInfosList.Add(new ScopeAndItemInfo()
{
Parent = parent,
Expand Down Expand Up @@ -170,7 +170,7 @@ public HierarchicalSchemaSection(string sectionIdentifier, BinaryReader binaryRe

binaryReader.BaseStream.Seek(pos, SeekOrigin.Begin);

string name = scopeAndItemInfosList[i].FullPathLength != 0
string name = scopeAndItemInfosList[i].FullPathLength is not 0
? binaryReader.ReadNullTerminatedString(scopeAndItemInfosList[i].NameInAscii ? Encoding.ASCII : Encoding.Unicode)
: string.Empty;

Expand Down Expand Up @@ -212,7 +212,7 @@ public HierarchicalSchemaSection(string sectionIdentifier, BinaryReader binaryRe

ushort parent = scopeAndItemInfosList[scopeAndItemInfosList[i].Parent].Index;

if (parent != 0xFFFF)
if (parent is not 0xFFFF)
{
if (scopeAndItemInfosList[i].IsScope)
{
Expand Down
4 changes: 2 additions & 2 deletions WindowsTools/Extensions/PriExtract/ReferencedFileSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public ReferencedFileSection(string sectionIdentifier, BinaryReader binaryReader

for (int i = 0; i < numFolders; i++)
{
if (folderInfosList[i].ParentFolder != 0xFFFF)
if (folderInfosList[i].ParentFolder is not 0xFFFF)
{
referencedFolders[i].Parent = referencedFolders[folderInfosList[i].ParentFolder];
}
Expand All @@ -126,7 +126,7 @@ public ReferencedFileSection(string sectionIdentifier, BinaryReader binaryReader

ReferencedFileOrFolder parentFolder;

if (fileInfos[i].ParentFolder != 0xFFFF)
if (fileInfos[i].ParentFolder is not 0xFFFF)
parentFolder = referencedFolders[fileInfos[i].ParentFolder];
else
parentFolder = null;
Expand Down
8 changes: 4 additions & 4 deletions WindowsTools/Extensions/PriExtract/ResourceMapSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public ResourceMapSection(string sectionIdentifier, BinaryReader binaryReader, b
byte[] environmentReferencesDataArray = binaryReader.ReadBytes(environmentReferencesLength);
byte[] schemaReferenceDataArray = binaryReader.ReadBytes(hierarchicalSchemaReferenceLength);

if (schemaReferenceDataArray.Length != 0)
if (schemaReferenceDataArray.Length is not 0)
using (BinaryReader r = new(new MemoryStream(schemaReferenceDataArray, false)))
{
ushort majorVersion = r.ReadUInt16();
Expand Down Expand Up @@ -222,7 +222,7 @@ public ResourceMapSection(string sectionIdentifier, BinaryReader binaryReader, b
{
byte type = binaryReader.ReadByte();

if (type == 0x01)
if (type is 0x01)
{
ResourceValueType resourceValueType = resourceValueTypeTableList[binaryReader.ReadByte()];
ushort sourceFileIndex = binaryReader.ReadUInt16();
Expand All @@ -239,7 +239,7 @@ public ResourceMapSection(string sectionIdentifier, BinaryReader binaryReader, b
DataOffset = 0
});
}
else if (type == 0x00)
else if (type is 0x00)
{
ResourceValueType resourceValueType = resourceValueTypeTableList[binaryReader.ReadByte()];
ushort length = binaryReader.ReadUInt16();
Expand Down Expand Up @@ -300,7 +300,7 @@ public ResourceMapSection(string sectionIdentifier, BinaryReader binaryReader, b

if (candidateInfo.Type is 0x01)
{
int? sourceFile = candidateInfo.SourceFileIndex == 0 ? null : candidateInfo.SourceFileIndex - 1;
int? sourceFile = candidateInfo.SourceFileIndex is 0 ? null : candidateInfo.SourceFileIndex - 1;

candidatesList.Add(new Candidate()
{
Expand Down
12 changes: 6 additions & 6 deletions WindowsTools/Extensions/PriExtract/ReverseMapSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,19 @@ public ReverseMapSection(string sectionIdentifier, BinaryReader binaryReader)

for (int i = 0; i < numScopes + numItems; i++)
{
bool nameInAscii = (scopeAndItemInfoList[i].Item3 & 0x20000000) != 0;
bool nameInAscii = (scopeAndItemInfoList[i].Item3 & 0x20000000) is not 0;
long pos = (nameInAscii ? asciiDataOffset : unicodeDataOffset) + (scopeAndItemInfoList[i].Item4 * (nameInAscii ? 1 : 2));
binaryReader.BaseStream.Seek(pos, SeekOrigin.Begin);

string name = string.Empty;

if (scopeAndItemInfoList[i].Item2 != 0)
if (scopeAndItemInfoList[i].Item2 is not 0)
{
name = binaryReader.ReadNullTerminatedString(nameInAscii ? Encoding.ASCII : Encoding.Unicode);
}

ushort index = scopeAndItemInfoList[i].Item5;
bool isScope = (scopeAndItemInfoList[i].Item3 & 0x10000000) != 0;
bool isScope = (scopeAndItemInfoList[i].Item3 & 0x10000000) is not 0;

if (isScope)
{
Expand Down Expand Up @@ -152,11 +152,11 @@ public ReverseMapSection(string sectionIdentifier, BinaryReader binaryReader)
for (int i = 0; i < numScopes + numItems; i++)
{
ushort index = scopeAndItemInfoList[i].Item5;
bool isScope = (scopeAndItemInfoList[i].Item3 & 0x10000000) != 0;
bool isScope = (scopeAndItemInfoList[i].Item3 & 0x10000000) is not 0;
ushort parent = scopeAndItemInfoList[i].Item1;
parent = scopeAndItemInfoList[parent].Item5;

if (parent != 0xFFFF)
if (parent is not 0xFFFF)
{
if (isScope)
{
Expand All @@ -180,7 +180,7 @@ public ReverseMapSection(string sectionIdentifier, BinaryReader binaryReader)
{
Tuple<ushort, ushort, uint, uint, ushort> saiInfo = scopeAndItemInfoList[scopeExInfo[i].Item3 + j];

bool isScope = (saiInfo.Item3 & 0x10000000) != 0;
bool isScope = (saiInfo.Item3 & 0x10000000) is not 0;

childrenArray[j] = isScope ? scopesArray[saiInfo.Item5] : itemsArray[saiInfo.Item5];
}
Expand Down
5 changes: 5 additions & 0 deletions WindowsTools/Extensions/ShellMenu/ShellMenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public sealed class ShellMenuItem
/// </summary>
public string MenuParameter { get; set; }

/// <summary>
/// 是否总是需要提权运行
/// </summary>
public bool IsAlwaysRunAsAdministrator { get; set; }

/// <summary>
/// 是否启用文件夹背景菜单项
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion WindowsTools/Helpers/Controls/TeachingTipHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static async Task ShowAsync(TeachingTip teachingTip, int duration = 2000)
{
foreach (UIElement item in ((MainWindow.Current.Content as MainPage).Content as Grid).Children)
{
if ((item as FrameworkElement).Name == teachingTip.Name)
if ((item as FrameworkElement).Name.Equals(teachingTip.Name))
{
((MainWindow.Current.Content as MainPage).Content as Grid).Children.Remove(item);
break;
Expand Down
2 changes: 1 addition & 1 deletion WindowsTools/Helpers/Root/FeatureAccessHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static FeatureAccessHelper()
{
int length = 0;

if (Kernel32Library.GetCurrentPackageFamilyName(ref length, null) != Kernel32Library.APPMODEL_ERROR_NO_PACKAGE)
if (Kernel32Library.GetCurrentPackageFamilyName(ref length, null) is not (int)Kernel32Library.APPMODEL_ERROR_NO_PACKAGE)
{
StringBuilder packageFamilyNameBuilder = new(length + 1);
Kernel32Library.GetCurrentPackageFamilyName(ref length, packageFamilyNameBuilder);
Expand Down
31 changes: 31 additions & 0 deletions WindowsTools/Helpers/Root/RegistryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using System;
using System.Diagnostics.Tracing;
using System.IO;
using System.Threading;
using WindowsTools.Extensions.Registry;
using WindowsTools.Services.Root;
using WindowsTools.WindowsAPI.PInvoke.Advapi32;

namespace WindowsTools.Helpers.Root
{
Expand Down Expand Up @@ -187,5 +189,34 @@ public static RegistryEnumKeyItem EnumSubKey(string rootKey)

return registryEnumKeyItem;
}

/// <summary>
/// 添加注册表监控
/// </summary>
public static void MonitorRegistryValueChange(string rootKey)
{
ManualResetEvent manualResetEvent = null;
RegisteredWaitHandle registeredWaitHandle = null;

try
{
if (Registry.CurrentUser.OpenSubKey(rootKey, false) is RegistryKey registryKey)
{
manualResetEvent = new(false);
int ret = Advapi32Library.RegNotifyChangeKeyValue(registryKey.Handle.DangerousGetHandle(), true, REG_NOTIFY_FILTER.REG_NOTIFY_CHANGE_LAST_SET | REG_NOTIFY_FILTER.REG_NOTIFY_THREAD_AGNOSTIC, manualResetEvent.SafeWaitHandle.DangerousGetHandle(), true);
registeredWaitHandle = ThreadPool.RegisterWaitForSingleObject(manualResetEvent, (state, timeout) =>
{
registeredWaitHandle?.Unregister(manualResetEvent);
manualResetEvent.Close();
registryKey.Close();
NotifyKeyValueChanged?.Invoke(null, EventArgs.Empty);
}, null, Timeout.Infinite, true);
}
}
catch (Exception e)
{
LogService.WriteLog(EventLevel.Error, string.Format("Monitor Registry rootKey change {0} failed", rootKey), e);
}
}
}
}
2 changes: 1 addition & 1 deletion WindowsTools/Helpers/Root/RuntimeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static RuntimeHelper()
private static void IsInMsixContainer()
{
int length = 0;
IsMSIX = Kernel32Library.GetCurrentPackageFamilyName(ref length, null) != Kernel32Library.APPMODEL_ERROR_NO_PACKAGE;
IsMSIX = Kernel32Library.GetCurrentPackageFamilyName(ref length, null) is not (int)Kernel32Library.APPMODEL_ERROR_NO_PACKAGE;
}

/// <summary>
Expand Down
5 changes: 5 additions & 0 deletions WindowsTools/Models/ShellMenuItemModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ public int MenuIndex
/// </summary>
public string MenuParameter { get; set; }

/// <summary>
/// 是否总是需要提权运行
/// </summary>
public bool IsAlwaysRunAsAdministrator { get; set; }

/// <summary>
/// 是否启用文件夹背景菜单项
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions WindowsTools/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
[assembly: AssemblyCompany("高怡飞")]
[assembly: AssemblyCopyright("Copyright ©2024 高怡飞, All Rights Reserved.")]
[assembly: AssemblyDescription("Windows 工具箱")]
[assembly: AssemblyFileVersion("2.8.825.0")]
[assembly: AssemblyInformationalVersion("2.8.825.0")]
[assembly: AssemblyFileVersion("2.9.904.0")]
[assembly: AssemblyInformationalVersion("2.9.904.0")]
[assembly: AssemblyProduct("Windows 工具箱")]
[assembly: AssemblyTitle("Windows 工具箱")]
[assembly: AssemblyVersion("2.8.825.0")]
[assembly: AssemblyVersion("2.9.904.0")]
8 changes: 5 additions & 3 deletions WindowsTools/Services/Shell/ShellMenuService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ public static ShellMenuItem GetShellMenuItem()
// 获取根菜单项下的所有子项(包括递归后的项)
RegistryEnumKeyItem shellMenuRegistryKeyItem = RegistryHelper.EnumSubKey(shellMenuKey);

return shellMenuRegistryKeyItem.SubRegistryKeyList.Count is 1
? EnumShellMenuItem(shellMenuRegistryKeyItem.SubRegistryKeyList[0])
: null;
return shellMenuRegistryKeyItem.SubRegistryKeyList.Count is 1 ? EnumShellMenuItem(shellMenuRegistryKeyItem.SubRegistryKeyList[0]) : null;
}

/// <summary>
Expand All @@ -74,6 +72,7 @@ public static void SaveShellMenuItem(string menuKey, ShellMenuItem shellMenuItem
RegistryHelper.SaveRegistryKey(menuKey, "DarkThemeIconPath", shellMenuItem.DarkThemeIconPath);
RegistryHelper.SaveRegistryKey(menuKey, "MenuProgramPath", shellMenuItem.MenuProgramPath);
RegistryHelper.SaveRegistryKey(menuKey, "MenuParameter", shellMenuItem.MenuParameter);
RegistryHelper.SaveRegistryKey(menuKey, "IsAlwaysRunAsAdministrator", shellMenuItem.IsAlwaysRunAsAdministrator);
RegistryHelper.SaveRegistryKey(menuKey, "FolderBackground", shellMenuItem.FolderBackground);
RegistryHelper.SaveRegistryKey(menuKey, "FolderDesktop", shellMenuItem.FolderDesktop);
RegistryHelper.SaveRegistryKey(menuKey, "FolderDirectory", shellMenuItem.FolderDirectory);
Expand Down Expand Up @@ -118,6 +117,7 @@ private static ShellMenuItem EnumShellMenuItem(RegistryEnumKeyItem registryEnumK
DarkThemeIconPath = currentMenuItem.DarkThemeIconPath,
MenuProgramPath = currentMenuItem.MenuProgramPath,
MenuParameter = currentMenuItem.MenuParameter,
IsAlwaysRunAsAdministrator = currentMenuItem.IsAlwaysRunAsAdministrator,
FolderBackground = currentMenuItem.FolderBackground,
FolderDesktop = currentMenuItem.FolderDesktop,
FolderDirectory = currentMenuItem.FolderDirectory,
Expand Down Expand Up @@ -266,6 +266,7 @@ private static ShellMenuItem GetShellItemInfo(string menuKey)
string darkThemeIconPath = RegistryHelper.ReadRegistryKey<string>(menuKey, "DarkThemeIconPath");
string menuProgramPath = RegistryHelper.ReadRegistryKey<string>(menuKey, "MenuProgramPath");
string menuParameter = RegistryHelper.ReadRegistryKey<string>(menuKey, "MenuParameter");
bool? isAlwaysRunAsAdministrator = RegistryHelper.ReadRegistryKey<bool?>(menuKey, "IsAlwaysRunAsAdministrator");
bool? folderBackground = RegistryHelper.ReadRegistryKey<bool?>(menuKey, "FolderBackground");
bool? folderDesktop = RegistryHelper.ReadRegistryKey<bool?>(menuKey, "FolderDesktop");
bool? folderDirectory = RegistryHelper.ReadRegistryKey<bool?>(menuKey, "FolderDirectory");
Expand All @@ -287,6 +288,7 @@ private static ShellMenuItem GetShellItemInfo(string menuKey)
shellMenuItem.DarkThemeIconPath = darkThemeIconPath;
shellMenuItem.MenuProgramPath = menuProgramPath;
shellMenuItem.MenuParameter = menuParameter;
shellMenuItem.IsAlwaysRunAsAdministrator = isAlwaysRunAsAdministrator.HasValue && isAlwaysRunAsAdministrator.Value;
shellMenuItem.FolderBackground = folderBackground.HasValue && folderBackground.Value;
shellMenuItem.FolderDesktop = folderDesktop.HasValue && folderDesktop.Value;
shellMenuItem.FolderDirectory = folderDirectory.HasValue && folderDirectory.Value;
Expand Down
9 changes: 9 additions & 0 deletions WindowsTools/Strings/Notification.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions WindowsTools/Strings/Notification.en-us.resx
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,7 @@
<data name="MenuDarkThemeIconPathEmpty" xml:space="preserve">
<value>The menu dark theme icon is empty, please select icon file</value>
</data>
<data name="ShellMenuNeedToRefreshData" xml:space="preserve">
<value>The menu storage content has changed and needs to be refreshed</value>
</data>
</root>
3 changes: 3 additions & 0 deletions WindowsTools/Strings/Notification.resx
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,7 @@
<data name="MenuDarkThemeIconPathEmpty" xml:space="preserve">
<value>The menu dark theme icon is empty, please select icon file</value>
</data>
<data name="ShellMenuNeedToRefreshData" xml:space="preserve">
<value>The menu storage content has changed and needs to be refreshed</value>
</data>
</root>
3 changes: 3 additions & 0 deletions WindowsTools/Strings/Notification.zh-hans.resx
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,7 @@
<data name="MenuDarkThemeIconPathEmpty" xml:space="preserve">
<value>菜单深色主题图标为空,请选择图标文件</value>
</data>
<data name="ShellMenuNeedToRefreshData" xml:space="preserve">
<value>菜单存储内容已改变,需要刷新才能进行操作</value>
</data>
</root>
Loading

0 comments on commit 3cadaee

Please sign in to comment.