Skip to content

Commit 57e4f58

Browse files
committed
添加任务管理器
1 parent af5b5c5 commit 57e4f58

File tree

8 files changed

+171
-11
lines changed

8 files changed

+171
-11
lines changed

App.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public partial class App : Application
1717
{
1818
public static List<MainWindow> mainWindows = [];
1919
public static Settings settings;
20-
public static WebView2 webView2 = new();
20+
public static WebView2 WebView2 = new();
2121
public static ObservableCollection<WebViewHistory> Histories = [];
2222
public static ObservableCollection<DownloadObject> DownloadList = [];
2323
public static WordSearchEngine searchEngine;
@@ -31,7 +31,7 @@ public App()
3131

3232
public async void EnsureWebView2Async()
3333
{
34-
await webView2.EnsureCoreWebView2Async();
34+
await WebView2.EnsureCoreWebView2Async();
3535
}
3636

3737
public static MainWindow CreateNewWindow()

Edge.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<PackageReference Include="CommunityToolkit.WinUI.Controls.SettingsControls" Version="8.2.250129-preview2" />
3232
<PackageReference Include="CommunityToolkit.WinUI.Controls.Sizers" Version="8.2.250129-preview2" />
3333
<PackageReference Include="CommunityToolkit.WinUI.Converters" Version="8.2.250129-preview2" />
34+
<PackageReference Include="CommunityToolkit.WinUI.UI.Controls.DataGrid" Version="7.1.2" />
3435
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.3116-prerelease" />
3536
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.183" />
3637
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.7.250208002-preview1" />

MainWindow.xaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@
168168

169169
<MenuFlyoutSeparator />
170170

171+
<MenuFlyoutItem
172+
Click="OpenBrowserTaskManager"
173+
Text="浏览器任务管理器">
174+
<MenuFlyoutItem.KeyboardAccelerators>
175+
<KeyboardAccelerator Key="Escape" Modifiers="Shift" />
176+
</MenuFlyoutItem.KeyboardAccelerators>
177+
</MenuFlyoutItem>
178+
179+
<MenuFlyoutSeparator />
180+
171181
<MenuFlyoutItem
172182
Click="OnCloseClicked"
173183
Text="关闭">

MainWindow.xaml.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,5 +322,11 @@ private void TabViewPointerPressed(object sender, Microsoft.UI.Xaml.Input.Pointe
322322
}
323323
}
324324
}
325+
326+
private void OpenBrowserTaskManager(object sender, RoutedEventArgs e)
327+
{
328+
TaskManager taskManager = new();
329+
taskManager.Activate();
330+
}
325331
}
326332
}

Pages/TaskManager.xaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Window
3+
x:Class="Edge.TaskManager"
4+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
6+
xmlns:local="using:Edge"
7+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
8+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9+
xmlns:controls="using:CommunityToolkit.WinUI.UI.Controls"
10+
mc:Ignorable="d"
11+
Title="TaskManager">
12+
13+
<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
14+
<Grid.RowDefinitions>
15+
<RowDefinition Height="Auto" />
16+
<RowDefinition Height="*" />
17+
</Grid.RowDefinitions>
18+
<TitleBar
19+
x:Name="TitleBar"
20+
Title="TaskManager"
21+
Height="48">
22+
<TitleBar.IconSource>
23+
<ImageIconSource ImageSource="ms-appx:///Assets/icon.ico" />
24+
</TitleBar.IconSource>
25+
</TitleBar>
26+
27+
<controls:DataGrid
28+
ItemsSource="{x:Bind infos}"
29+
AutoGenerateColumns="False"
30+
IsReadOnly="True"
31+
ColumnWidth="*"
32+
CanUserSortColumns="True"
33+
Sorting="DataGrid_Sorting"
34+
Grid.Row="1">
35+
<controls:DataGrid.Columns>
36+
<controls:DataGridTextColumn Header="任务" Binding="{Binding Task}" Tag="Task" />
37+
<controls:DataGridTextColumn Header="内存" Binding="{Binding MemoryKB}" Tag="Memory" />
38+
<controls:DataGridTextColumn Header="CPU" Binding="{Binding ProcessId}" Tag="ProcessId" />
39+
<controls:DataGridTextColumn Header="网络" Binding="{Binding ProcessId}" Tag="ProcessId" />
40+
<controls:DataGridTextColumn Header="进程 ID" Binding="{Binding ProcessId}" Tag="ProcessId" />
41+
</controls:DataGrid.Columns>
42+
</controls:DataGrid>
43+
</Grid>
44+
</Window>

Pages/TaskManager.xaml.cs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using CommunityToolkit.WinUI.UI.Controls;
2+
using Microsoft.UI.Xaml;
3+
using Microsoft.Web.WebView2.Core;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Collections.ObjectModel;
7+
using System.Diagnostics;
8+
using System.Linq;
9+
10+
11+
namespace Edge
12+
{
13+
public class ProcessInfo
14+
{
15+
public string Task { get; set; }
16+
public long Memory { get; set; }
17+
public string MemoryKB { get; set; }
18+
public int ProcessId { get; set; }
19+
}
20+
21+
public sealed partial class TaskManager : Window
22+
{
23+
public ObservableCollection<ProcessInfo> infos = [];
24+
25+
public TaskManager()
26+
{
27+
this.InitializeComponent();
28+
ExtendsContentIntoTitleBar = true;
29+
AppWindow.SetIcon("./Assets/icon.ico");
30+
SetTitleBar(TitleBar);
31+
AppWindow.TitleBar.PreferredHeightOption = Microsoft.UI.Windowing.TitleBarHeightOption.Tall;
32+
33+
this.SetBackdrop();
34+
this.SetThemeColor();
35+
36+
InitialData();
37+
}
38+
39+
private async void InitialData()
40+
{
41+
IReadOnlyList<CoreWebView2ProcessExtendedInfo> eInfos = await App.WebView2.CoreWebView2.Environment.GetProcessExtendedInfosAsync();
42+
foreach (CoreWebView2ProcessExtendedInfo item in eInfos)
43+
{
44+
ProcessInfo info = new()
45+
{
46+
Task = item.ProcessInfo.Kind switch
47+
{
48+
CoreWebView2ProcessKind.Browser => "浏览器",
49+
CoreWebView2ProcessKind.Renderer => item.AssociatedFrameInfos[0].Source,
50+
CoreWebView2ProcessKind.Utility => "实用工具",
51+
CoreWebView2ProcessKind.Gpu => "GPU 进程",
52+
_ => "未知"
53+
},
54+
ProcessId = item.ProcessInfo.ProcessId
55+
};
56+
57+
Process process = Process.GetProcessById(item.ProcessInfo.ProcessId);
58+
info.Memory = process.PrivateMemorySize64 / 1024;
59+
info.MemoryKB = $"{info.Memory} KB";
60+
infos.Add(info);
61+
}
62+
}
63+
64+
private void DataGrid_Sorting(object sender, DataGridColumnEventArgs e)
65+
{
66+
DataGrid dataGrid = sender as DataGrid;
67+
foreach (var column in dataGrid.Columns)
68+
{
69+
if (column != e.Column)
70+
{
71+
column.SortDirection = null;
72+
}
73+
}
74+
75+
if (e.Column.SortDirection == DataGridSortDirection.Ascending)
76+
{
77+
e.Column.SortDirection = DataGridSortDirection.Descending;
78+
dataGrid.ItemsSource = (string)e.Column.Tag switch
79+
{
80+
"Task" => infos.OrderByDescending(x => x.Task),
81+
"Memory" => infos.OrderByDescending(x => x.Memory),
82+
"ProcessId" => infos.OrderByDescending(x => x.ProcessId),
83+
_ => infos
84+
};
85+
}
86+
else
87+
{
88+
e.Column.SortDirection = DataGridSortDirection.Ascending;
89+
dataGrid.ItemsSource = (string)e.Column.Tag switch
90+
{
91+
"Task" => infos.OrderBy(x => x.Task),
92+
"Memory" => infos.OrderBy(x => x.Memory),
93+
"ProcessId" => infos.OrderBy(x => x.ProcessId),
94+
_ => infos
95+
};
96+
}
97+
}
98+
}
99+
}

Settings/DownloadItem.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public DownloadItem()
1010
{
1111
this.InitializeComponent();
1212

13-
DownloadFolderCard.Description = App.webView2.CoreWebView2.Profile.DefaultDownloadFolderPath;
13+
DownloadFolderCard.Description = App.WebView2.CoreWebView2.Profile.DefaultDownloadFolderPath;
1414

1515
setDownloadBehavior.IsOn = App.settings.AskDownloadBehavior;
1616
setDownloadFlyout.IsOn = App.settings.ShowFlyoutWhenStartDownloading;
@@ -41,7 +41,7 @@ private async void ChangeDownloadFolder(object sender, Microsoft.UI.Xaml.RoutedE
4141

4242
if (folder != null)
4343
{
44-
DownloadFolderCard.Description = App.webView2.CoreWebView2.Profile.DefaultDownloadFolderPath = folder.Name;
44+
DownloadFolderCard.Description = App.WebView2.CoreWebView2.Profile.DefaultDownloadFolderPath = folder.Name;
4545
}
4646
}
4747
}

Settings/PrivacyItem.xaml.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public PrivacyItem()
4646
this.InitializeComponent();
4747
trackView.ItemsSource = TrackKindList;
4848

49-
var level = App.webView2.CoreWebView2.Profile.PreferredTrackingPreventionLevel;
49+
var level = App.WebView2.CoreWebView2.Profile.PreferredTrackingPreventionLevel;
5050
bool isTrackOn = level != CoreWebView2TrackingPreventionLevel.None;
5151
if (isTrackOn)
5252
{
@@ -58,7 +58,7 @@ public PrivacyItem()
5858
}
5959
trackSwitch.IsOn = isTrackOn;
6060

61-
msSmartScreen.IsOn = App.webView2.CoreWebView2.Settings.IsReputationCheckingRequired;
61+
msSmartScreen.IsOn = App.WebView2.CoreWebView2.Settings.IsReputationCheckingRequired;
6262
}
6363

6464
private async void ClearBrowsingData(object sender, RoutedEventArgs e)
@@ -67,7 +67,7 @@ private async void ClearBrowsingData(object sender, RoutedEventArgs e)
6767
{
6868
if (item.IsChecked)
6969
{
70-
await App.webView2.CoreWebView2.Profile.ClearBrowsingDataAsync(item.Kind);
70+
await App.WebView2.CoreWebView2.Profile.ClearBrowsingDataAsync(item.Kind);
7171
}
7272
}
7373
ClearBrowsingDataButton.Description = "已清理选择的项目";
@@ -77,25 +77,25 @@ private void TrackLevelToggled(object sender, RoutedEventArgs e)
7777
{
7878
if ((sender as ToggleSwitch).IsOn)
7979
{
80-
App.webView2.CoreWebView2.Profile.PreferredTrackingPreventionLevel = trackLevelList[trackView.SelectedIndex + 1];
80+
App.WebView2.CoreWebView2.Profile.PreferredTrackingPreventionLevel = trackLevelList[trackView.SelectedIndex + 1];
8181
}
8282
else
8383
{
84-
App.webView2.CoreWebView2.Profile.PreferredTrackingPreventionLevel = CoreWebView2TrackingPreventionLevel.None;
84+
App.WebView2.CoreWebView2.Profile.PreferredTrackingPreventionLevel = CoreWebView2TrackingPreventionLevel.None;
8585
}
8686
}
8787

8888
private void TrackChanged(object sender, SelectionChangedEventArgs e)
8989
{
9090
if (trackSwitch.IsOn)
9191
{
92-
App.webView2.CoreWebView2.Profile.PreferredTrackingPreventionLevel = trackLevelList[trackView.SelectedIndex + 1];
92+
App.WebView2.CoreWebView2.Profile.PreferredTrackingPreventionLevel = trackLevelList[trackView.SelectedIndex + 1];
9393
}
9494
}
9595

9696
private void SmartScreenChanged(object sender, RoutedEventArgs e)
9797
{
98-
App.webView2.CoreWebView2.Settings.IsReputationCheckingRequired = (sender as ToggleSwitch).IsOn;
98+
App.WebView2.CoreWebView2.Settings.IsReputationCheckingRequired = (sender as ToggleSwitch).IsOn;
9999
}
100100
}
101101
}

0 commit comments

Comments
 (0)