diff --git a/DeviceCatalog.cs b/DeviceCatalog.cs new file mode 100644 index 0000000..4dccb6c --- /dev/null +++ b/DeviceCatalog.cs @@ -0,0 +1,37 @@ +using Avalonia.Controls; + +internal sealed record DeviceCategory(string Label, string Value, params DeviceCategory[] Children); + +internal static class DeviceCatalog +{ + internal static readonly DeviceCategory[] Categories = + [ + new DeviceCategory("全部", ""), + new DeviceCategory("手机", "", + new DeviceCategory("手机", "phone"), new DeviceCategory("折叠屏", "foldable"), + new DeviceCategory("阔折叠", "widefold"), new DeviceCategory("三折叠", "triplefold")), + new DeviceCategory("平板", "", new DeviceCategory("平板", "tablet")), + new DeviceCategory("2in1", "", new DeviceCategory("2in1", "2in1"), new DeviceCategory("折叠2in1", "2in1 Foldable")), + new DeviceCategory("电视", "", new DeviceCategory("电视", "tv")), + new DeviceCategory("穿戴设备", "", new DeviceCategory("智能表", "wearable"), new DeviceCategory("儿童表", "wearablekid")), + new DeviceCategory("车机", "", new DeviceCategory("车机", "car")) + ]; + + internal static readonly Dictionary TypeNames = Categories + .SelectMany(group => new[] { group }.Concat(group.Children)) + .Where(node => node.Value.Length > 0) + .ToDictionary(node => node.Value, node => node.Label); + + internal static TreeViewItem CreateItem(DeviceCategory node) + { + var item = new TreeViewItem + { + Header = node.Label, + Tag = node, + IsExpanded = node.Children.Length > 0 + }; + foreach (var child in node.Children) + item.Items.Add(CreateItem(child)); + return item; + } +} diff --git a/EmulatorCli.cs b/EmulatorCli.cs index fc5b29b..ee3076d 100644 --- a/EmulatorCli.cs +++ b/EmulatorCli.cs @@ -1,16 +1,24 @@ using System.Diagnostics; using System.Text.Json; -internal sealed record EmulatorInstance(string Name, string Type, string Version, string Running) +internal sealed record EmulatorInstance( + string Name, string Type, string Version, string ApiVersion, + string Resolution, string ScreenSize, string InstancePath, bool IsRunning) { + public string Running => IsRunning ? "运行中" : "已停止"; public override string ToString() => $"{Name} | {Type} | {Version} | {Running}"; } -internal sealed record EmulatorImage(string Type, string Version, string Downloaded) +internal sealed record EmulatorImage(string Type, string Version, string ApiVersion, string Downloaded) { public override string ToString() => $"{Type} | {Version} | {Downloaded}"; } +internal sealed record EmulatorProfile(string Model, string Details) +{ + public override string ToString() => $"{Model} | {Details.ReplaceLineEndings(" ")}"; +} + internal static class EmulatorCli { internal static string DefaultPath() @@ -50,7 +58,7 @@ internal static class EmulatorCli log($"› Emulator {string.Join(" ", args.Select(arg => JsonSerializer.Serialize(arg)))}"); using var process = Process.Start(info) ?? throw new InvalidOperationException("无法启动 Emulator。"); process.StandardInput.Close(); - var capture = args[0] is "-list" or "-imageList"; + var capture = args[0] is "-list" or "-imageList" or "-screenProfileList"; var stdout = ReadAsync(process.StandardOutput, capture ? null : log, capture, cancellation); var stderr = ReadAsync(process.StandardError, log, false, cancellation); try @@ -89,21 +97,82 @@ internal static class EmulatorCli internal static EmulatorInstance[] Instances(string output) => Parse(output, row => new EmulatorInstance( Field(row, "name"), Field(row, "deviceType"), Field(row, "os.osVersion"), - Field(row, "isRunning").ToLowerInvariant() switch - { - "true" => "运行中", - "false" => "已停止", - _ => "未知" - })); + Field(row, "os.apiVersion"), Resolution(row), ScreenSize(row), + Field(row, "instancePath"), Field(row, "isRunning").Equals("true", StringComparison.OrdinalIgnoreCase))); - internal static EmulatorImage[] Images(string output) => Parse(output, row => new EmulatorImage( - Field(row, "deviceType"), Field(row, "osVersion"), - Field(row, "downloaded").ToLowerInvariant() switch + private static string Resolution(JsonElement row) + { + var width = Field(row, "hw.lcd.single.width"); + var height = Field(row, "hw.lcd.single.height"); + return width.Length > 0 && height.Length > 0 ? $"{width}×{height}" : ""; + } + + private static string ScreenSize(JsonElement row) + { + var size = Field(row, "hw.lcd.single.diagonalSize"); + return size.Length > 0 ? $"{size}\"" : ""; + } + + internal static EmulatorImage[] Images(string output) => Parse(output, row => + { + var version = Field(row, "osVersion"); + return new EmulatorImage( + Field(row, "deviceType"), version, ApiVersion(version), + Field(row, "downloaded").ToLowerInvariant() switch + { + "true" => "已下载", + "false" => "未下载", + _ => "未知" + }); + }); + + private static string ApiVersion(string version) + { + var open = version.IndexOf('('); + var close = version.LastIndexOf(')'); + return open >= 0 && close > open ? version[(open + 1)..close] : ""; + } + + internal static EmulatorProfile[] Profiles(string output) + { + var result = new List(); + string? model = null; + var details = new System.Text.StringBuilder(); + void AddCurrent() { - "true" => "已下载", - "false" => "未下载", - _ => "未知" - })); + if (model is not null) + result.Add(new EmulatorProfile(model, details.ToString().Trim())); + } + + foreach (var raw in output.Split('\n')) + { + var line = raw.Trim(); + if (line.Length == 0) + continue; + if (line.StartsWith("- ", StringComparison.Ordinal)) + { + AddCurrent(); + model = line[2..].Trim(); + details.Clear(); + } + else + { + var separator = line.IndexOf(" - ", StringComparison.Ordinal); + if (separator >= 0) + { + AddCurrent(); + model = line[(separator + 3)..].Trim(); + details.Clear(); + } + else if (model is not null) + { + details.AppendLine(line); + } + } + } + AddCurrent(); + return result.ToArray(); + } private static T[] Parse(string output, Func convert) { diff --git a/MainWindow.cs b/MainWindow.cs index a53a96b..7f67abc 100644 --- a/MainWindow.cs +++ b/MainWindow.cs @@ -1,3 +1,4 @@ +using System.Diagnostics; using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Templates; @@ -8,53 +9,55 @@ using Avalonia.Threading; internal sealed class MainWindow : Window { private readonly TextBox executable = new() { Text = EmulatorCli.DefaultPath(), Watermark = "Emulator 可执行文件路径" }; - private readonly TextBox targetName = new() { Watermark = "选择列表中的实例" }; - private readonly TextBox name = new() { Watermark = "例如 My Phone" }; - private readonly TextBox deviceType = new() { Watermark = "例如 phone", Text = "phone" }; - private readonly TextBox version = new() { Watermark = "例如 HarmonyOS 7.0.0(26.0.0)" }; - private readonly CheckBox headless = new() { Content = "无窗口启动" }; + private readonly TreeView categories = new(); + private readonly TreeView imageCategories = new(); private readonly ListBox instances = new() { MinHeight = 120 }; private readonly ListBox images = new() { MinHeight = 120 }; - private readonly ListBox output = new() { MaxHeight = 156, MinHeight = 104 }; - private readonly Button logToggle = new() { Content = "展开命令输出" }; + private readonly ListBox output = new(); private readonly TextBlock status = new() { Text = "就绪", VerticalAlignment = VerticalAlignment.Center, - TextTrimming = TextTrimming.CharacterEllipsis + TextTrimming = TextTrimming.CharacterEllipsis, + Margin = new Thickness(24, 0, 0, 0) }; private readonly Button cancel = new() { Content = "取消命令", IsVisible = false }; private readonly List