Improve Emulator management layout
This commit is contained in:
1 parent
8520c599b4
commit
5e6afd32d4
3 files changed
+234
-67
No files matched your search
+225
-62
@@ -1,5 +1,6 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Templates;
|
||||
using Avalonia.Layout;
|
||||
using Avalonia.Media;
|
||||
using Avalonia.Threading;
|
||||
@@ -7,15 +8,21 @@ using Avalonia.Threading;
|
||||
internal sealed class MainWindow : Window
|
||||
{
|
||||
private readonly TextBox executable = new() { Text = EmulatorCli.DefaultPath(), Watermark = "Emulator 可执行文件路径" };
|
||||
private readonly TextBox name = new() { Watermark = "实例名称", Width = 210 };
|
||||
private readonly TextBox deviceType = new() { Watermark = "设备类型,如 phone", Width = 170, Text = "phone" };
|
||||
private readonly TextBox version = new() { Watermark = "HarmonyOS 镜像版本", Width = 270 };
|
||||
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 ListBox instances = new() { MinHeight = 120 };
|
||||
private readonly ListBox images = new() { MinHeight = 120 };
|
||||
private readonly ListBox output = new();
|
||||
private readonly TextBlock status = new() { Text = "就绪", VerticalAlignment = VerticalAlignment.Center };
|
||||
private readonly Button cancel = new() { Content = "取消命令", IsEnabled = false };
|
||||
private readonly ListBox output = new() { MaxHeight = 156, MinHeight = 104 };
|
||||
private readonly Button logToggle = new() { Content = "展开命令输出" };
|
||||
private readonly TextBlock status = new()
|
||||
{
|
||||
Text = "就绪", VerticalAlignment = VerticalAlignment.Center,
|
||||
TextTrimming = TextTrimming.CharacterEllipsis
|
||||
};
|
||||
private readonly Button cancel = new() { Content = "取消命令", IsVisible = false };
|
||||
private readonly List<Button> actions = [];
|
||||
private readonly HashSet<(string Executable, string Name)> starting = [];
|
||||
private CancellationTokenSource? active;
|
||||
@@ -23,10 +30,10 @@ internal sealed class MainWindow : Window
|
||||
internal MainWindow(bool smoke, Action<int> shutdown)
|
||||
{
|
||||
Title = "HarmonyOS Emulator 管理器";
|
||||
Width = 990;
|
||||
Height = 730;
|
||||
MinWidth = 740;
|
||||
MinHeight = 580;
|
||||
Width = 1060;
|
||||
Height = 690;
|
||||
MinWidth = 780;
|
||||
MinHeight = 570;
|
||||
executable.TextChanged += (_, _) =>
|
||||
{
|
||||
instances.ItemsSource = null;
|
||||
@@ -43,7 +50,7 @@ internal sealed class MainWindow : Window
|
||||
instances.SelectionChanged += (_, _) =>
|
||||
{
|
||||
if (instances.SelectedItem is EmulatorInstance instance)
|
||||
name.Text = instance.Name;
|
||||
targetName.Text = instance.Name;
|
||||
};
|
||||
cancel.Click += (_, _) => active?.Cancel();
|
||||
Closing += (_, args) =>
|
||||
@@ -56,8 +63,15 @@ internal sealed class MainWindow : Window
|
||||
}
|
||||
};
|
||||
|
||||
var instancePage = Page(
|
||||
Row(headless, Command("刷新", RefreshInstances),
|
||||
var instancePage = ManagementPage(
|
||||
"模拟器实例", Row(Command("刷新", RefreshInstances)),
|
||||
Table(instances, "1.1*,0.65*,1.55*,0.65*", ["实例名称", "设备类型", "系统版本", "状态"],
|
||||
(EmulatorInstance item) => [item.Name, item.Type, item.Version, item.Running]),
|
||||
Panel(
|
||||
Caption("实例操作"),
|
||||
Field("目标实例", targetName),
|
||||
headless,
|
||||
Row(
|
||||
Command("启动", _ =>
|
||||
{
|
||||
StartInstance();
|
||||
@@ -65,33 +79,36 @@ internal sealed class MainWindow : Window
|
||||
}),
|
||||
Command("停止", async token =>
|
||||
{
|
||||
await Run(token, "-stop", Required(name, "实例名称"));
|
||||
await Run(token, "-stop", Required(targetName, "目标实例"));
|
||||
await RefreshInstances(token);
|
||||
}),
|
||||
})),
|
||||
Command("删除实例", async token =>
|
||||
{
|
||||
var target = Required(name, "实例名称");
|
||||
var target = Required(targetName, "目标实例");
|
||||
if (await Confirm($"删除实例「{target}」及其数据?"))
|
||||
{
|
||||
await Run(token, "-delete", target, "-force");
|
||||
await RefreshInstances(token);
|
||||
}
|
||||
})),
|
||||
instances);
|
||||
})));
|
||||
|
||||
var imagePage = Page(
|
||||
var imagePage = ManagementPage(
|
||||
"系统镜像",
|
||||
Row(Command("已下载", token => RefreshImages(token, true)),
|
||||
Command("全部镜像", token => RefreshImages(token, false)),
|
||||
Command("全部镜像", token => RefreshImages(token, false))),
|
||||
Table(images, "0.85*,1.65*,0.7*", ["设备类型", "系统版本", "下载状态"],
|
||||
(EmulatorImage item) => [item.Type, item.Version, item.Downloaded]),
|
||||
Panel(
|
||||
Caption("镜像操作"),
|
||||
Field("设备类型", deviceType),
|
||||
Field("系统版本", version),
|
||||
Row(
|
||||
Command("下载镜像", async token =>
|
||||
{
|
||||
ShowLog(true);
|
||||
await Run(token, ImageArgs("-install"));
|
||||
await RefreshImages(token, true);
|
||||
}),
|
||||
Command("创建实例", async token =>
|
||||
{
|
||||
await Run(token, ["-create", Required(name, "实例名称"), .. ImageArgs()]);
|
||||
await RefreshInstances(token);
|
||||
}),
|
||||
Command("删除镜像", async token =>
|
||||
{
|
||||
var args = ImageArgs("-uninstall");
|
||||
@@ -101,40 +118,65 @@ internal sealed class MainWindow : Window
|
||||
await RefreshImages(token, true);
|
||||
}
|
||||
})),
|
||||
images);
|
||||
// The inputs stay visible while switching between instances and images.
|
||||
Caption("创建模拟器"),
|
||||
Field("实例名称", name),
|
||||
Command("创建实例", async token =>
|
||||
{
|
||||
await Run(token, ["-create", Required(name, "实例名称"), .. ImageArgs()]);
|
||||
await RefreshInstances(token);
|
||||
})));
|
||||
output.ItemTemplate = new FuncDataTemplate<string>((line, _) => new TextBlock
|
||||
{
|
||||
Text = line, FontFamily = new FontFamily("monospace"), FontSize = 12,
|
||||
TextWrapping = TextWrapping.Wrap
|
||||
});
|
||||
output.IsVisible = false;
|
||||
logToggle.Click += (_, _) => ShowLog(!output.IsVisible);
|
||||
|
||||
var settings = Panel(
|
||||
Caption("Emulator 工具"),
|
||||
Field("可执行文件路径", executable),
|
||||
Row(OutputCommand("查看版本", "-version"), OutputCommand("命令帮助", "--help"),
|
||||
OutputCommand("查看许可协议", "-license"),
|
||||
Command("接受许可", async token =>
|
||||
{
|
||||
if (await Confirm("确认已阅读并接受 Emulator 显示的许可协议?"))
|
||||
{
|
||||
ShowLog(true);
|
||||
await Run(token, "-license", "accept");
|
||||
}
|
||||
})));
|
||||
var tabs = new TabControl
|
||||
{
|
||||
ItemsSource = new[]
|
||||
{
|
||||
new TabItem { Header = "模拟器实例", Content = instancePage },
|
||||
new TabItem { Header = "系统镜像", Content = imagePage }
|
||||
new TabItem { Header = "系统镜像", Content = imagePage },
|
||||
new TabItem { Header = "工具设置", Content = new Border
|
||||
{
|
||||
Margin = new Thickness(0, 16, 0, 0),
|
||||
VerticalAlignment = VerticalAlignment.Top, Child = Card(settings)
|
||||
} }
|
||||
}
|
||||
};
|
||||
tabs.SelectionChanged += async (_, args) =>
|
||||
{
|
||||
if (!smoke && args.Source == tabs && tabs.SelectedIndex == 1 && images.ItemsSource is null)
|
||||
await Execute(token => RefreshImages(token, true));
|
||||
};
|
||||
var layout = new Grid
|
||||
{
|
||||
Margin = new Thickness(24),
|
||||
RowDefinitions = new RowDefinitions("Auto,Auto,Auto,*,Auto,170,Auto")
|
||||
RowDefinitions = new RowDefinitions("Auto,*,Auto")
|
||||
};
|
||||
Add(layout, new TextBlock { Text = "Emulator 管理", FontSize = 28, FontWeight = FontWeight.SemiBold }, 0);
|
||||
var tools = new DockPanel { LastChildFill = true };
|
||||
var help = Row(Command("版本", token => Run(token, "-version")),
|
||||
Command("帮助", token => Run(token, "--help")),
|
||||
Command("许可协议", token => Run(token, "-license")),
|
||||
Command("接受许可", async token =>
|
||||
{
|
||||
if (await Confirm("确认已阅读并接受 Emulator 显示的许可协议?"))
|
||||
await Run(token, "-license", "accept");
|
||||
}));
|
||||
DockPanel.SetDock(help, Dock.Right);
|
||||
tools.Children.Add(help);
|
||||
tools.Children.Add(executable);
|
||||
Add(layout, tools, 1);
|
||||
Add(layout, Row(name, deviceType, version), 2);
|
||||
Add(layout, tabs, 3);
|
||||
Add(layout, new TextBlock { Text = "命令输出", FontWeight = FontWeight.SemiBold }, 4);
|
||||
Add(layout, output, 5);
|
||||
Add(layout, Row(cancel, status), 6);
|
||||
Add(layout, new TextBlock { Text = "Emulator 管理", FontSize = 26, FontWeight = FontWeight.SemiBold }, 0);
|
||||
Add(layout, tabs, 1);
|
||||
var footer = new DockPanel();
|
||||
var controls = Row(logToggle, cancel);
|
||||
DockPanel.SetDock(controls, Dock.Right);
|
||||
footer.Children.Add(controls);
|
||||
footer.Children.Add(status);
|
||||
Add(layout, Card(Panel(footer, output)), 2);
|
||||
Content = layout;
|
||||
|
||||
if (smoke)
|
||||
@@ -162,12 +204,34 @@ internal sealed class MainWindow : Window
|
||||
}
|
||||
}
|
||||
|
||||
private async Task RefreshInstances(CancellationToken token) =>
|
||||
instances.ItemsSource = EmulatorCli.Instances(await Run(token, "-list", "-details"));
|
||||
private async Task RefreshInstances(CancellationToken token)
|
||||
{
|
||||
var items = await ReadList(token, EmulatorCli.Instances, "-list", "-details");
|
||||
instances.ItemsSource = items;
|
||||
AppendOutput($"已读取 {items.Length} 个模拟器实例。");
|
||||
}
|
||||
|
||||
private async Task RefreshImages(CancellationToken token, bool downloaded) =>
|
||||
images.ItemsSource = EmulatorCli.Images(await Run(token, downloaded
|
||||
? ["-imageList", "-downloaded", "true"] : ["-imageList"]));
|
||||
private async Task RefreshImages(CancellationToken token, bool downloaded)
|
||||
{
|
||||
var items = await ReadList(token, EmulatorCli.Images, downloaded
|
||||
? ["-imageList", "-downloaded", "true"] : ["-imageList"]);
|
||||
images.ItemsSource = items;
|
||||
AppendOutput($"已读取 {items.Length} 个镜像。");
|
||||
}
|
||||
|
||||
private async Task<T[]> ReadList<T>(CancellationToken token, Func<string, T[]> parse, params string[] args)
|
||||
{
|
||||
var raw = await Run(token, args);
|
||||
try
|
||||
{
|
||||
return parse(raw);
|
||||
}
|
||||
catch
|
||||
{
|
||||
AppendOutput(raw);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private string[] ImageArgs(params string[] prefix) =>
|
||||
[.. prefix, "-deviceType", Required(deviceType, "设备类型"), "-osVersion", Required(version, "镜像版本")];
|
||||
@@ -175,7 +239,7 @@ internal sealed class MainWindow : Window
|
||||
private void StartInstance()
|
||||
{
|
||||
var path = Required(executable, "Emulator 路径");
|
||||
var target = Required(name, "实例名称");
|
||||
var target = Required(targetName, "目标实例");
|
||||
if (!starting.Add((path, target)))
|
||||
throw new InvalidOperationException($"「{target}」已有启动进程。");
|
||||
string[] args = headless.IsChecked == true ? ["-start", target, "-noWindow"] : ["-start", target];
|
||||
@@ -207,12 +271,27 @@ internal sealed class MainWindow : Window
|
||||
if (output.ItemCount >= 1500)
|
||||
output.Items.RemoveAt(0);
|
||||
output.Items.Add(line);
|
||||
output.ScrollIntoView(line);
|
||||
if (output.IsVisible)
|
||||
output.ScrollIntoView(output.ItemCount - 1);
|
||||
});
|
||||
|
||||
private void ShowLog(bool visible)
|
||||
{
|
||||
output.IsVisible = visible;
|
||||
logToggle.Content = visible ? "收起命令输出" : "展开命令输出";
|
||||
if (visible && output.ItemCount > 0)
|
||||
output.ScrollIntoView(output.ItemCount - 1);
|
||||
}
|
||||
|
||||
private Button OutputCommand(string label, string argument) => Command(label, token =>
|
||||
{
|
||||
ShowLog(true);
|
||||
return Run(token, argument);
|
||||
});
|
||||
|
||||
private Button Command(string label, Func<CancellationToken, Task> action)
|
||||
{
|
||||
var button = new Button { Content = label };
|
||||
var button = new Button { Content = label, HorizontalAlignment = HorizontalAlignment.Left };
|
||||
button.Click += async (_, _) => await Execute(action);
|
||||
actions.Add(button);
|
||||
return button;
|
||||
@@ -226,7 +305,7 @@ internal sealed class MainWindow : Window
|
||||
using var source = new CancellationTokenSource();
|
||||
active = source;
|
||||
executable.IsEnabled = false;
|
||||
cancel.IsEnabled = true;
|
||||
cancel.IsVisible = true;
|
||||
actions.ForEach(button => button.IsEnabled = false);
|
||||
status.Text = "正在执行…";
|
||||
try
|
||||
@@ -241,12 +320,14 @@ internal sealed class MainWindow : Window
|
||||
catch (Exception error)
|
||||
{
|
||||
status.Text = error.Message;
|
||||
AppendOutput(error.Message);
|
||||
ShowLog(true);
|
||||
}
|
||||
finally
|
||||
{
|
||||
active = null;
|
||||
executable.IsEnabled = true;
|
||||
cancel.IsEnabled = false;
|
||||
cancel.IsVisible = false;
|
||||
actions.ForEach(button => button.IsEnabled = true);
|
||||
}
|
||||
}
|
||||
@@ -290,15 +371,97 @@ internal sealed class MainWindow : Window
|
||||
return panel;
|
||||
}
|
||||
|
||||
private static DockPanel Page(Control toolbar, Control list)
|
||||
private static Control ManagementPage(string title, Control toolbar, Control table, Control controls)
|
||||
{
|
||||
var panel = new DockPanel { Margin = new Thickness(0, 8, 0, 0) };
|
||||
DockPanel.SetDock(toolbar, Dock.Top);
|
||||
panel.Children.Add(toolbar);
|
||||
var header = new DockPanel();
|
||||
DockPanel.SetDock(toolbar, Dock.Right);
|
||||
header.Children.Add(toolbar);
|
||||
header.Children.Add(Caption(title));
|
||||
var left = new Grid { RowDefinitions = new RowDefinitions("Auto,*") };
|
||||
Add(left, header, 0);
|
||||
Add(left, table, 1);
|
||||
var grid = new Grid
|
||||
{
|
||||
Margin = new Thickness(0, 16, 0, 0),
|
||||
ColumnDefinitions = new ColumnDefinitions("*,252")
|
||||
};
|
||||
grid.Children.Add(Card(left));
|
||||
var sidebar = Card(new ScrollViewer { Content = controls });
|
||||
sidebar.Margin = new Thickness(14, 0, 0, 0);
|
||||
Grid.SetColumn(sidebar, 1);
|
||||
grid.Children.Add(sidebar);
|
||||
return grid;
|
||||
}
|
||||
|
||||
private static Control Table<T>(ListBox list, string columns, string[] headings, Func<T, string[]> cells)
|
||||
where T : class
|
||||
{
|
||||
list.ItemTemplate = new FuncDataTemplate<T>((item, _) => TableRow(columns, cells(item!), false));
|
||||
list.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 = TableRow(columns, headings, true)
|
||||
};
|
||||
DockPanel.SetDock(header, Dock.Top);
|
||||
panel.Children.Add(header);
|
||||
panel.Children.Add(list);
|
||||
return panel;
|
||||
}
|
||||
|
||||
private static Grid TableRow(string columns, string[] values, bool header)
|
||||
{
|
||||
var grid = new Grid { ColumnDefinitions = new ColumnDefinitions(columns) };
|
||||
for (var index = 0; index < values.Length; index++)
|
||||
{
|
||||
var cell = new TextBlock
|
||||
{
|
||||
Text = values[index],
|
||||
TextTrimming = TextTrimming.CharacterEllipsis,
|
||||
FontWeight = header || index == 0 ? FontWeight.SemiBold : FontWeight.Normal,
|
||||
FontSize = 13, Margin = new Thickness(0, 0, 8, 0)
|
||||
};
|
||||
ToolTip.SetTip(cell, values[index]);
|
||||
Grid.SetColumn(cell, index);
|
||||
grid.Children.Add(cell);
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
|
||||
private static TextBlock Caption(string text) => new()
|
||||
{
|
||||
Text = text, FontSize = 16, FontWeight = FontWeight.SemiBold,
|
||||
VerticalAlignment = VerticalAlignment.Center
|
||||
};
|
||||
|
||||
private static StackPanel Field(string label, Control field)
|
||||
{
|
||||
var panel = Panel(new TextBlock { Text = label, FontSize = 12, Opacity = 0.78 }, field);
|
||||
panel.Spacing = 5;
|
||||
return panel;
|
||||
}
|
||||
|
||||
private static StackPanel Panel(params Control[] controls)
|
||||
{
|
||||
var panel = new StackPanel { Spacing = 10 };
|
||||
foreach (var control in controls)
|
||||
panel.Children.Add(control);
|
||||
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 Add(Grid grid, Control control, int row)
|
||||
{
|
||||
if (row > 0)
|
||||
|
||||
Reference in new issue
Block a user