Skip to content

Commit

Permalink
Add Context menu item layout
Browse files Browse the repository at this point in the history
Add Context menu item layout
  • Loading branch information
Gaoyifei1011 committed Dec 8, 2024
1 parent 5321ac6 commit b217ef6
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 14 deletions.
4 changes: 3 additions & 1 deletion WindowsTools/Strings/ContextMenuManager.en-us.restext
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ LearnContextMenuManager=Learn right-click menu management
LearnCustomRightClickMenu=Learn about customizing the right-click menu
LoadingContextMenuInformation=Loading right-click menu information, please wait...
OpenPackagePathToolTip=Open the app installation folder
Refresh=Refresh
SearchAppNamePHText=Search app name
SearchEmptyDescription=No matching right-click menu item found
Refresh=Refresh
SelectedToolTip=Selected
UnSelectedToolTip=Unselected
4 changes: 3 additions & 1 deletion WindowsTools/Strings/ContextMenuManager.restext
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ LearnContextMenuManager=Learn right-click menu management
LearnCustomRightClickMenu=Learn about customizing the right-click menu
LoadingContextMenuInformation=Loading right-click menu information, please wait...
OpenPackagePathToolTip=Open the app installation folder
Refresh=Refresh
SearchAppNamePHText=Search app name
SearchEmptyDescription=No matching right-click menu item found
Refresh=Refresh
SelectedToolTip=Selected
UnSelectedToolTip=Unselected
4 changes: 3 additions & 1 deletion WindowsTools/Strings/ContextMenuManager.zh-hans.restext
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ LearnContextMenuManager=了解右键菜单管理
LearnCustomRightClickMenu=了解自定义右键菜单
LoadingContextMenuInformation=正在加载右键菜单信息中,请稍候...
OpenPackagePathToolTip=打开应用安装目录
Refresh=刷新
SearchAppNamePHText=搜索应用名称
SearchEmptyDescription=没有搜索到复合的右键菜单项
Refresh=刷新
SelectedToolTip=已选择
UnSelectedToolTip=未选择
59 changes: 59 additions & 0 deletions WindowsTools/Views/Pages/ContextMenuManager.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
NavigationCacheMode="Enabled"
mc:Ignorable="d">

<Page.Resources>
<XamlUICommand x:Name="CheckCommand" ExecuteRequested="{x:Bind OnCheckExecuteRequested}" />
<XamlUICommand x:Name="OpenPackagePathCommand" ExecuteRequested="{x:Bind OnOpenPackagePathExecuteRequested}" />
</Page.Resources>

<Grid Padding="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
Expand Down Expand Up @@ -292,6 +297,60 @@
</Button>
</Grid>
</muxc:Expander.Header>

<muxc:Expander.Content>
<ListView
ItemsSource="{x:Bind ContextMenuItemCollection, Mode=OneWay}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.HorizontalScrollMode="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollMode="Disabled"
SelectionMode="None">

<ListView.ItemContainerStyle>
<Style BasedOn="{StaticResource DefaultListViewItemStyle}" TargetType="ListViewItem">
<Setter Property="Padding" Value="0" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="Margin" Value="0,1" />
<Setter Property="MinHeight" Value="0 " />
</Style>
</ListView.ItemContainerStyle>

<ListView.ItemTemplate>
<DataTemplate x:DataType="model:ContextMenuItemModel">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<CheckBox
Grid.Column="0"
MinWidth="0"
Margin="0,0,12,0"
Command="{StaticResource CheckCommand}"
CommandParameter="{x:Bind}"
IsChecked="{x:Bind IsEnabled, Mode=TwoWay}">

<ToolTipService.ToolTip>
<ToolTip MaxWidth="9999">
<Grid>
<TextBlock Text="{x:Bind root:ResourceService.ContextMenuManagerResource.GetString('SelectedToolTip')}" Visibility="{x:Bind IsEnabled, Mode=OneWay}" />
<TextBlock Text="{x:Bind root:ResourceService.ContextMenuManagerResource.GetString('UnSelectedToolTip')}" Visibility="{x:Bind converter:ValueConverterHelper.BooleanToVisibilityReverseConvert(IsEnabled), Mode=OneWay}" />
</Grid>
</ToolTip>
</ToolTipService.ToolTip>
</CheckBox>

<TextBlock
Grid.Column="1"
VerticalAlignment="Center"
Text="{x:Bind Clsid.ToString()}" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</muxc:Expander.Content>
</muxc:Expander>
</DataTemplate>
</ListView.ItemTemplate>
Expand Down
89 changes: 79 additions & 10 deletions WindowsTools/Views/Pages/ContextMenuManager.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@
using System.Diagnostics;
using System.Diagnostics.Tracing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using WindowsTools.Models;
using WindowsTools.Services.Root;
using WindowsTools.WindowsAPI.PInvoke.Kernel32;

// 抑制 CA1822 警告
#pragma warning disable CA1822

namespace WindowsTools.Views.Pages
{
Expand Down Expand Up @@ -81,6 +87,33 @@ public ContextMenuManagerPage()
InitializeComponent();
}

/// <summary>
/// 打开应用包路径
/// </summary>
private void OnOpenPackagePathExecuteRequested(XamlUICommand sender, ExecuteRequestedEventArgs args)
{
string path = args.Parameter as string;

Task.Run(() =>
{
try
{
Process.Start(path);
}
catch (Exception e)
{
LogService.WriteLog(EventLevel.Error, "Open package path failed", e);
}
});
}

/// <summary>
/// 点击复选框时使保存按钮处于可选状态
/// </summary>
private void OnCheckExecuteRequested(XamlUICommand sender, ExecuteRequestedEventArgs args)
{
}

#region 第一部分:右键菜单管理页面——挂载的事件

/// <summary>
Expand All @@ -91,7 +124,17 @@ private async void OnLoaded(object sender, RoutedEventArgs args)
if (!isInitialized)
{
isInitialized = true;
await Task.Delay(500);
ContextMenuCollection.Clear();
List<ContextMenuModel> contextMenuList = await Task.Run(async () =>
{
await Task.Delay(500);
List<ContextMenuModel> contextMenuList = GetContextMenuList();
return contextMenuList;
});
foreach (ContextMenuModel contextMenuItem in contextMenuList)
{
ContextMenuCollection.Add(contextMenuItem);
}
IsLoadCompleted = true;
}
}
Expand Down Expand Up @@ -149,7 +192,12 @@ private void OnSerachAppNameTextChanged(object sender, AutoSuggestBoxTextChanged
private async void OnRefreshClicked(object sender, RoutedEventArgs args)
{
IsLoadCompleted = false;
await Task.Delay(500);
ContextMenuCollection.Clear();
List<ContextMenuModel> contextMenuList = await Task.Run(GetContextMenuList);
foreach (ContextMenuModel contextMenuItem in contextMenuList)
{
ContextMenuCollection.Add(contextMenuItem);
}
IsLoadCompleted = true;
}

Expand All @@ -161,6 +209,7 @@ private async void OnRefreshClicked(object sender, RoutedEventArgs args)
private List<ContextMenuModel> GetContextMenuList()
{
List<ContextMenuModel> contextMenuList = [];
List<KeyValuePair<Guid, string>> blockedList = GetBlockedClsidList();

try
{
Expand Down Expand Up @@ -192,11 +241,13 @@ private List<ContextMenuModel> GetContextMenuList()
{
int serverId = Convert.ToInt32(clsidKey.GetValue("ServerId", 0));
int threading = Convert.ToInt32(clsidKey.GetValue("Threading", 0));
bool isBlocked = blockedList.Any(item => item.Key.Equals(clsid));

contextMenuItemList.Add(new ContextMenuItemModel()
{
Clsid = clsid,
DllPath = dllPath,
IsEnabled = !isBlocked,
ThreadingMode = threading switch
{
0 => ApartmentState.STA,
Expand All @@ -207,25 +258,37 @@ private List<ContextMenuModel> GetContextMenuList()
}
}

classKey.Close();
classKey.Dispose();
clsidKey.Close();
clsidKey.Dispose();
}
}

int length = 0;
string packagePath = string.Empty;

if (Kernel32Library.GetPackagePathByFullName(packageFullName, ref length, null) is 122)
{
StringBuilder packagePathBuilder = new(length + 1);
int result = Kernel32Library.GetPackagePathByFullName(packageFullName, ref length, packagePathBuilder);
packagePath = packagePathBuilder.ToString();
}

contextMenuList.Add(new()
{
PackageDisplayName = string.Empty,
PackageFullName = packageFullName,
PackagePath = packagePath,
ContextMenuItemCollection = new(contextMenuItemList)
});
}

classKey.Close();
classKey.Dispose();
classKey.Close();
classKey.Dispose();
}
}
}

packageListKey.Close();
packageListKey.Dispose();
packageListKey.Close();
packageListKey.Dispose();
}
}
catch (Exception e)
{
Expand Down Expand Up @@ -257,6 +320,9 @@ private List<KeyValuePair<Guid, string>> GetBlockedClsidList()
blockClsidList.Add(new KeyValuePair<Guid, string>(clsid, Registry.LocalMachine.ToString()));
}
}

blockKey.Close();
blockKey.Dispose();
}
}
catch (Exception e)
Expand All @@ -279,6 +345,9 @@ private List<KeyValuePair<Guid, string>> GetBlockedClsidList()
blockClsidList.Add(new KeyValuePair<Guid, string>(clsid, Registry.CurrentUser.ToString()));
}
}

blockKey.Close();
blockKey.Dispose();
}
}
catch (Exception e)
Expand Down
16 changes: 15 additions & 1 deletion WindowsTools/WindowsAPI/PInvoke/Kernel32/Kernel32Library.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.InteropServices;
using System;
using System.Runtime.InteropServices;
using System.Text;

// 抑制 CA1401 警告
Expand Down Expand Up @@ -33,6 +34,19 @@ public static class Kernel32Library
[DllImport(Kernel32, CharSet = CharSet.Unicode, EntryPoint = "GetCurrentPackageFamilyName", PreserveSig = true, SetLastError = false)]
public static extern int GetCurrentPackageFamilyName(ref int packageFamilyNameLength, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder packageFamilyName);

/// <summary>
/// 获取指定包的路径。
/// </summary>
/// <param name="packageFullName">包的全名。</param>
/// <param name="pathLength">
/// 指向包含包路径字符串中 WCHAR) 字符数 (的变量的指针,其中包含 null 终止符。
/// 首先,将 NULL 传递给 路径 以获取字符数。 使用此数字为 路径分配内存空间。 然后传递此内存空间的地址以填充 路径。
/// </param>
/// <param name="path">指向接收包路径字符串(包括 null 终止符)的内存空间的指针。</param>
/// <returns>如果函数成功,则返回 ERROR_SUCCESS。 否则,函数将返回错误代码。</returns>
[DllImport(Kernel32, CharSet = CharSet.Unicode, EntryPoint = "GetPackagePathByFullName", PreserveSig = true, SetLastError = false)]
public static extern int GetPackagePathByFullName([MarshalAs(UnmanagedType.LPWStr)] string packageFullName, ref int pathLength, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder path);

/// <summary>
/// 检索系统的电源状态。 状态指示系统是使用交流还是直流电源运行,电池当前是否正在充电,剩余的电池使用时间,以及节电模式是打开还是关闭。
/// </summary>
Expand Down

0 comments on commit b217ef6

Please sign in to comment.