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
@@ -0,0 +1,475 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Templates;
|
||||
using Avalonia.Layout;
|
||||
using Avalonia.Media;
|
||||
|
||||
internal sealed class NewEmulatorWindow : Window
|
||||
{
|
||||
private readonly string executable;
|
||||
private readonly Action<string> log;
|
||||
private readonly Action onCreated;
|
||||
private readonly TreeView deviceTypes = new();
|
||||
private readonly ListBox profiles = new() { MinHeight = 220 };
|
||||
private readonly ListBox images = new() { MinHeight = 220 };
|
||||
private readonly TextBox name = new() { Watermark = "例如 My Phone" };
|
||||
private readonly TextBox memory = new() { Text = "4", Watermark = "GB(2~32)" };
|
||||
private readonly TextBox storage = new() { Text = "8", Watermark = "GB(2~1023)" };
|
||||
private readonly CheckBox hotBoot = new() { Content = "启用快速启动(快照)", IsChecked = true };
|
||||
private readonly TextBlock status = new() { Text = "选择系统镜像", TextTrimming = TextTrimming.CharacterEllipsis };
|
||||
private readonly ContentControl stepHost = new();
|
||||
private readonly Control[] stepPages = new Control[3];
|
||||
private readonly TextBlock selectionSummary = new() { TextWrapping = TextWrapping.Wrap };
|
||||
private readonly Button back = new() { Content = "上一步", IsVisible = false };
|
||||
private readonly Button next = new() { Content = "下一步", IsEnabled = false };
|
||||
private readonly Button finish = new() { Content = "完成", IsVisible = false };
|
||||
private string deviceType = "";
|
||||
private EmulatorImage[] allImages = [];
|
||||
private EmulatorProfile? selectedProfile;
|
||||
private EmulatorImage? selectedImage;
|
||||
private int step;
|
||||
private CancellationTokenSource? active;
|
||||
|
||||
internal NewEmulatorWindow(string executable, Action<string> log, Action onCreated)
|
||||
{
|
||||
this.executable = executable;
|
||||
this.log = log;
|
||||
this.onCreated = onCreated;
|
||||
Title = "新建模拟器";
|
||||
Width = 900;
|
||||
Height = 640;
|
||||
MinWidth = 700;
|
||||
MinHeight = 500;
|
||||
WindowStartupLocation = WindowStartupLocation.CenterOwner;
|
||||
|
||||
profiles.ItemTemplate = new FuncDataTemplate<EmulatorProfile>((item, _) =>
|
||||
item is null ? null : ProfileRow(item));
|
||||
images.ItemTemplate = new FuncDataTemplate<EmulatorImage>((item, _) =>
|
||||
item is null ? null : ImageRow(item));
|
||||
deviceTypes.ItemsSource = DeviceCatalog.Categories
|
||||
.Where(node => node.Value.Length > 0 || node.Children.Length > 0)
|
||||
.Select(DeviceCatalog.CreateItem).ToArray();
|
||||
deviceTypes.SelectionChanged += (_, _) => OnDeviceTypeChanged();
|
||||
profiles.SelectionChanged += (_, _) =>
|
||||
{
|
||||
selectedProfile = profiles.SelectedItem as EmulatorProfile;
|
||||
UpdateButtons();
|
||||
};
|
||||
images.SelectionChanged += (_, _) =>
|
||||
{
|
||||
selectedImage = images.SelectedItem as EmulatorImage;
|
||||
UpdateSelectionSummary();
|
||||
UpdateButtons();
|
||||
};
|
||||
name.TextChanged += (_, _) => UpdateButtons();
|
||||
Closing += (_, _) => active?.Cancel();
|
||||
back.Click += (_, _) => GoStep(step - 1);
|
||||
next.Click += (_, _) => GoStep(step + 1);
|
||||
finish.Click += async (_, _) => await CreateAsync();
|
||||
stepPages[0] = SplitPage("选择系统镜像", ImageTable(), new ScrollViewer { Content = deviceTypes }, () => LoadImages());
|
||||
stepPages[1] = SplitPage("选择设备型号", profiles, selectionSummary, () => LoadProfiles());
|
||||
stepPages[2] = ConfigPage();
|
||||
Content = BuildLayout();
|
||||
SelectDeviceType("phone");
|
||||
}
|
||||
|
||||
private Control BuildLayout()
|
||||
{
|
||||
var layout = new Grid
|
||||
{
|
||||
Margin = new Thickness(20),
|
||||
RowDefinitions = new RowDefinitions("Auto,*,Auto,Auto")
|
||||
};
|
||||
layout.Children.Add(new TextBlock
|
||||
{
|
||||
Text = "新建模拟器", FontSize = 22, FontWeight = FontWeight.SemiBold
|
||||
});
|
||||
Grid.SetRow(stepHost, 1);
|
||||
layout.Children.Add(stepHost);
|
||||
Grid.SetRow(status, 2);
|
||||
status.Margin = new Thickness(0, 10, 0, 4);
|
||||
layout.Children.Add(status);
|
||||
var buttons = new StackPanel
|
||||
{
|
||||
Orientation = Orientation.Horizontal,
|
||||
HorizontalAlignment = HorizontalAlignment.Right,
|
||||
Spacing = 8
|
||||
};
|
||||
var cancel = new Button { Content = "取消" };
|
||||
cancel.Click += (_, _) => Close();
|
||||
buttons.Children.Add(cancel);
|
||||
buttons.Children.Add(back);
|
||||
buttons.Children.Add(next);
|
||||
buttons.Children.Add(finish);
|
||||
Grid.SetRow(buttons, 3);
|
||||
layout.Children.Add(buttons);
|
||||
GoStep(0);
|
||||
return layout;
|
||||
}
|
||||
|
||||
private Control SplitPage(string title, Control list, Control sidebar, Func<Task> loader)
|
||||
{
|
||||
var refresh = new Button { Content = "刷新" };
|
||||
refresh.Click += (_, _) => _ = loader();
|
||||
var header = new DockPanel();
|
||||
DockPanel.SetDock(refresh, Dock.Right);
|
||||
header.Children.Add(refresh);
|
||||
header.Children.Add(Caption(title));
|
||||
var body = new Grid { RowDefinitions = new RowDefinitions("Auto,*") };
|
||||
AddRow(body, header, 0);
|
||||
AddRow(body, Card(list), 1);
|
||||
var page = new Grid { ColumnDefinitions = new ColumnDefinitions("180,*") };
|
||||
page.Children.Add(Card(sidebar));
|
||||
var content = Card(body);
|
||||
content.Margin = new Thickness(14, 0, 0, 0);
|
||||
Grid.SetColumn(content, 1);
|
||||
page.Children.Add(content);
|
||||
return page;
|
||||
}
|
||||
|
||||
private Control ConfigPage()
|
||||
{
|
||||
var panel = new StackPanel { Spacing = 12 };
|
||||
panel.Children.Add(Caption("模拟器配置"));
|
||||
panel.Children.Add(Field("实例名称", name));
|
||||
panel.Children.Add(Field("内存(GB)", memory));
|
||||
panel.Children.Add(Field("存储(GB)", storage));
|
||||
panel.Children.Add(hotBoot);
|
||||
var summary = new TextBlock
|
||||
{
|
||||
Text = $"镜像:{selectedImage?.Version ?? "未选择"}\n" +
|
||||
$"型号:{selectedProfile?.Model ?? "未选择"}\n" +
|
||||
$"设备类型:{DeviceCatalog.TypeNames.GetValueOrDefault(selectedImage?.Type ?? deviceType, selectedImage?.Type ?? deviceType)}",
|
||||
TextWrapping = TextWrapping.Wrap,
|
||||
Opacity = 0.8
|
||||
};
|
||||
panel.Children.Add(summary);
|
||||
return new Border { Child = Card(panel), VerticalAlignment = VerticalAlignment.Top };
|
||||
}
|
||||
|
||||
private static Control ProfileRow(EmulatorProfile item) => new TextBlock
|
||||
{
|
||||
Text = $"{item.Model} {item.Details.ReplaceLineEndings(" ")}",
|
||||
FontSize = 13,
|
||||
TextTrimming = TextTrimming.CharacterEllipsis,
|
||||
TextWrapping = TextWrapping.NoWrap
|
||||
};
|
||||
|
||||
private Control ImageTable()
|
||||
{
|
||||
images.ItemTemplate = new FuncDataTemplate<EmulatorImage>((item, _) =>
|
||||
item is null ? null : ImageRow(item));
|
||||
images.Padding = new Thickness(0);
|
||||
var panel = new DockPanel();
|
||||
var header = new Border
|
||||
{
|
||||
Background = new SolidColorBrush(Color.FromArgb(20, 128, 128, 128)),
|
||||
CornerRadius = new CornerRadius(6),
|
||||
Padding = new Thickness(10, 7),
|
||||
Child = ImageGrid(
|
||||
[
|
||||
HeaderCell("名称"), HeaderCell("版本(API)"), HeaderCell("操作")
|
||||
])
|
||||
};
|
||||
DockPanel.SetDock(header, Dock.Top);
|
||||
panel.Children.Add(header);
|
||||
panel.Children.Add(images);
|
||||
return panel;
|
||||
}
|
||||
|
||||
private Control ImageRow(EmulatorImage item)
|
||||
{
|
||||
var action = new Button
|
||||
{
|
||||
Content = item.Downloaded == "已下载" ? "删除" : "下载",
|
||||
FontSize = 12,
|
||||
Padding = new Thickness(8, 3),
|
||||
HorizontalAlignment = HorizontalAlignment.Left
|
||||
};
|
||||
action.Click += async (_, _) => await ToggleImageAsync(item, item.Downloaded != "已下载");
|
||||
var row = ImageGrid([ImageCell(item.Version), ImageCell(item.ApiVersion), action]);
|
||||
row.Margin = new Thickness(0, 2);
|
||||
return row;
|
||||
}
|
||||
|
||||
private static Grid ImageGrid(Control[] cells)
|
||||
{
|
||||
var grid = new Grid { ColumnDefinitions = new ColumnDefinitions("2*,1*,76") };
|
||||
for (var index = 0; index < cells.Length; index++)
|
||||
{
|
||||
Grid.SetColumn(cells[index], index);
|
||||
grid.Children.Add(cells[index]);
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
|
||||
private static TextBlock HeaderCell(string text) => new()
|
||||
{
|
||||
Text = text, FontSize = 12, FontWeight = FontWeight.SemiBold,
|
||||
VerticalAlignment = VerticalAlignment.Center,
|
||||
TextTrimming = TextTrimming.CharacterEllipsis
|
||||
};
|
||||
|
||||
private static TextBlock ImageCell(string text) => new()
|
||||
{
|
||||
Text = text, FontSize = 13, VerticalAlignment = VerticalAlignment.Center,
|
||||
TextTrimming = TextTrimming.CharacterEllipsis
|
||||
};
|
||||
|
||||
private void SelectDeviceType(string value)
|
||||
{
|
||||
foreach (var item in deviceTypes.Items.Cast<TreeViewItem>())
|
||||
{
|
||||
if (item.Tag is not DeviceCategory group)
|
||||
continue;
|
||||
var match = group.Value == value ? group : group.Children.FirstOrDefault(child => child.Value == value);
|
||||
if (match is null)
|
||||
continue;
|
||||
deviceTypes.SelectedItem = item.Items.OfType<TreeViewItem>()
|
||||
.FirstOrDefault(child => (child.Tag as DeviceCategory)?.Value == value) ?? item;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ToggleImageAsync(EmulatorImage image, bool install)
|
||||
{
|
||||
if (active is not null)
|
||||
return;
|
||||
using var source = new CancellationTokenSource();
|
||||
active = source;
|
||||
status.Text = install ? "正在下载镜像…" : "正在删除镜像…";
|
||||
try
|
||||
{
|
||||
string[] args = install
|
||||
? ["-install", "-deviceType", image.Type, "-osVersion", image.Version]
|
||||
: ["-uninstall", "-deviceType", image.Type, "-osVersion", image.Version, "-force"];
|
||||
await EmulatorCli.RunAsync(executable, args, log, source.Token);
|
||||
active = null;
|
||||
await LoadImages();
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
status.Text = error.Message;
|
||||
log(error.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
active = null;
|
||||
UpdateButtons();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDeviceTypeChanged()
|
||||
{
|
||||
var node = (deviceTypes.SelectedItem as TreeViewItem)?.Tag as DeviceCategory;
|
||||
deviceType = node?.Value ?? "";
|
||||
profiles.ItemsSource = null;
|
||||
selectedProfile = null;
|
||||
selectedImage = null;
|
||||
ApplyImageFilter();
|
||||
UpdateSelectionSummary();
|
||||
UpdateButtons();
|
||||
_ = LoadImages();
|
||||
}
|
||||
|
||||
private void ApplyImageFilter()
|
||||
{
|
||||
var node = (deviceTypes.SelectedItem as TreeViewItem)?.Tag as DeviceCategory;
|
||||
images.ItemsSource = node switch
|
||||
{
|
||||
null => allImages,
|
||||
{ Value.Length: > 0 } => allImages.Where(item => item.Type == node.Value).ToArray(),
|
||||
{ Children.Length: > 0 } => allImages.Where(item =>
|
||||
node.Children.Any(child => child.Value == item.Type)).ToArray(),
|
||||
_ => allImages
|
||||
};
|
||||
}
|
||||
|
||||
private void UpdateSelectionSummary()
|
||||
{
|
||||
var type = selectedImage?.Type ?? deviceType;
|
||||
selectionSummary.Text = $"设备类型:{DeviceCatalog.TypeNames.GetValueOrDefault(type, type)}\n" +
|
||||
$"镜像:{selectedImage?.Version ?? "未选择"}";
|
||||
}
|
||||
|
||||
private async Task LoadProfiles()
|
||||
{
|
||||
var type = selectedImage?.Type ?? deviceType;
|
||||
if (active is not null || type.Length == 0)
|
||||
return;
|
||||
using var source = new CancellationTokenSource();
|
||||
active = source;
|
||||
status.Text = "正在读取设备型号…";
|
||||
try
|
||||
{
|
||||
var raw = await EmulatorCli.RunAsync(executable,
|
||||
["-screenProfileList", "-deviceType", type, "-details"], log, source.Token);
|
||||
var items = EmulatorCli.Profiles(raw);
|
||||
profiles.ItemsSource = items;
|
||||
selectedProfile = null;
|
||||
status.Text = $"已读取 {items.Length} 个设备型号。";
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
status.Text = error.Message;
|
||||
log(error.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
active = null;
|
||||
UpdateButtons();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task LoadImages()
|
||||
{
|
||||
if (active is not null)
|
||||
return;
|
||||
using var source = new CancellationTokenSource();
|
||||
active = source;
|
||||
status.Text = "正在读取系统镜像…";
|
||||
try
|
||||
{
|
||||
var raw = await EmulatorCli.RunAsync(executable, ["-imageList"], log, source.Token);
|
||||
allImages = EmulatorCli.Images(raw);
|
||||
selectedImage = null;
|
||||
ApplyImageFilter();
|
||||
status.Text = $"已读取 {images.Items.Count} 个镜像。";
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
status.Text = error.Message;
|
||||
log(error.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
active = null;
|
||||
UpdateButtons();
|
||||
}
|
||||
}
|
||||
|
||||
private void GoStep(int index)
|
||||
{
|
||||
step = Math.Clamp(index, 0, 2);
|
||||
stepHost.Content = stepPages[step];
|
||||
status.Text = step switch
|
||||
{
|
||||
0 => "选择系统镜像",
|
||||
1 => "选择设备型号",
|
||||
_ => "配置模拟器"
|
||||
};
|
||||
if (step == 1)
|
||||
_ = LoadProfiles();
|
||||
if (step == 2 && string.IsNullOrWhiteSpace(name.Text) && selectedProfile is not null)
|
||||
name.Text = selectedProfile.Model.Replace(" ", "");
|
||||
UpdateButtons();
|
||||
}
|
||||
|
||||
private void UpdateButtons()
|
||||
{
|
||||
back.IsVisible = step > 0;
|
||||
next.IsVisible = step < 2;
|
||||
finish.IsVisible = step == 2;
|
||||
next.IsEnabled = step switch
|
||||
{
|
||||
0 => selectedImage is not null,
|
||||
1 => selectedProfile is not null,
|
||||
_ => false
|
||||
};
|
||||
finish.IsEnabled = step == 2 && selectedProfile is not null && selectedImage is not null
|
||||
&& !string.IsNullOrWhiteSpace(name.Text);
|
||||
}
|
||||
|
||||
private async Task CreateAsync()
|
||||
{
|
||||
if (active is not null)
|
||||
return;
|
||||
var target = name.Text?.Trim();
|
||||
if (string.IsNullOrEmpty(target) || target.StartsWith('-'))
|
||||
{
|
||||
status.Text = "请输入有效的实例名称。";
|
||||
return;
|
||||
}
|
||||
var imageType = selectedImage!.Type;
|
||||
var args = new List<string>
|
||||
{
|
||||
"-create", target, "-deviceType", imageType,
|
||||
"-osVersion", selectedImage.Version,
|
||||
"-screenProfile", selectedProfile!.Model
|
||||
};
|
||||
if (!TryNumber(memory.Text, 2, 32, out var memorySize))
|
||||
{
|
||||
status.Text = "内存需为 2~32 之间的整数。";
|
||||
return;
|
||||
}
|
||||
if (!TryNumber(storage.Text, 2, 1023, out var storageSize))
|
||||
{
|
||||
status.Text = "存储需为 2~1023 之间的整数。";
|
||||
return;
|
||||
}
|
||||
args.AddRange(["-memory", memorySize.ToString(), "-storage", storageSize.ToString()]);
|
||||
if (hotBoot.IsChecked == true)
|
||||
args.AddRange(["-hotBoot", "true"]);
|
||||
|
||||
using var source = new CancellationTokenSource();
|
||||
active = source;
|
||||
status.Text = "正在创建模拟器…";
|
||||
try
|
||||
{
|
||||
if (selectedImage!.Downloaded == "未下载")
|
||||
{
|
||||
status.Text = "正在下载系统镜像…";
|
||||
await EmulatorCli.RunAsync(executable,
|
||||
["-install", "-deviceType", imageType, "-osVersion", selectedImage.Version],
|
||||
log, source.Token);
|
||||
status.Text = "正在创建模拟器…";
|
||||
}
|
||||
await EmulatorCli.RunAsync(executable, [.. args], log, source.Token);
|
||||
onCreated();
|
||||
Close();
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
status.Text = error.Message;
|
||||
log(error.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
active = null;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool TryNumber(string? text, int min, int max, out int value) =>
|
||||
int.TryParse(text, out value) && value >= min && value <= max;
|
||||
|
||||
private static TextBlock Caption(string text) => new()
|
||||
{
|
||||
Text = text, FontSize = 15, FontWeight = FontWeight.SemiBold
|
||||
};
|
||||
|
||||
private static StackPanel Field(string label, Control field)
|
||||
{
|
||||
var panel = new StackPanel { Spacing = 4 };
|
||||
panel.Children.Add(new TextBlock { Text = label, FontSize = 12, Opacity = 0.78 });
|
||||
panel.Children.Add(field);
|
||||
return panel;
|
||||
}
|
||||
|
||||
private static Border Card(Control child) => new()
|
||||
{
|
||||
BorderThickness = new Thickness(1),
|
||||
BorderBrush = new SolidColorBrush(Color.FromArgb(62, 128, 128, 128)),
|
||||
Background = new SolidColorBrush(Color.FromArgb(12, 128, 128, 128)),
|
||||
CornerRadius = new CornerRadius(10),
|
||||
Padding = new Thickness(14),
|
||||
Child = child
|
||||
};
|
||||
|
||||
private static void AddRow(Grid grid, Control control, int row)
|
||||
{
|
||||
if (row > 0)
|
||||
control.Margin = new Thickness(0, 10, 0, 0);
|
||||
Grid.SetRow(control, row);
|
||||
grid.Children.Add(control);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user