|
| 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 | +} |
0 commit comments