Rework emulator UI and add device creation wizard
This commit is contained in:
1 parent
c2084b6cd1
commit
9cf29c11ab
5 files changed
+1032
-180
No files matched your search
+85
-16
@@ -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<EmulatorProfile>();
|
||||
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<T>(string output, Func<JsonElement, T> convert)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user