Add per-VM action buttons in VM list

This commit is contained in:
xiaji
2026-04-17 07:38:34 +08:00
parent eb751fdb0b
commit c8e8db3412

View File

@@ -316,39 +316,28 @@ impl eframe::App for App {
}
});
// 虚拟机列表(只读
// 虚拟机列表(每行带操作按钮
ui.label("虚拟机列表:");
egui::ScrollArea::vertical().max_height(120.0).stick_to_bottom(true).show(ui, |ui| {
let vm_count = st.vms.len();
ui.label(format!("{} 台虚拟机 (点击选择)", vm_count));
let vms_list = st.vms.clone();
let client_ref = st.client.clone();
let node_ref = st.node.clone();
egui::ScrollArea::vertical().max_height(180.0).stick_to_bottom(true).show(ui, |ui| {
let vm_count = vms_list.len();
ui.label(format!("{} 台虚拟机", vm_count));
ui.separator();
for (idx, (id, name)) in st.vms.iter().enumerate() {
let is_selected = st.vm_id == *id;
let label = if is_selected {
format!("{} ({}) - 已选择", name, id)
} else {
format!("{} ({})", name, id)
};
if is_selected {
ui.colored_label(egui::Color32::LIGHT_BLUE, label);
} else {
ui.label(label);
}
}
});
// 选择VM后显示操作按钮
if !st.vms.is_empty() {
for (_, (id, name)) in vms_list.iter().enumerate() {
ui.horizontal(|ui| {
ui.label("选中 VM 操作:");
let client = st.client.clone();
let node = st.node.clone();
let vm_id = st.vm_id;
ui.label(format!("{} ({})", name, id));
ui.add_space(10.0);
let client = client_ref.clone();
let node = node_ref.clone();
let vm_id = *id;
let state_clone = state.clone();
if ui.button(" 启动").clicked() {
st.add_log(&format!("启动 VM {}...", vm_id));
if ui.button("").clicked() {
state_clone.write().unwrap().add_log(&format!("启动 VM {}...", vm_id));
ctx.request_repaint();
thread::spawn(move || {
let rt = tokio::runtime::Runtime::new().unwrap();
@@ -364,12 +353,12 @@ impl eframe::App for App {
});
}
let client2 = st.client.clone();
let node2 = st.node.clone();
let vm_id2 = st.vm_id;
let client2 = client_ref.clone();
let node2 = node_ref.clone();
let vm_id2 = *id;
let state2 = state.clone();
if ui.button(" 停止").clicked() {
st.add_log(&format!("停止 VM {}...", vm_id2));
if ui.button("").clicked() {
state2.write().unwrap().add_log(&format!("停止 VM {}...", vm_id2));
ctx.request_repaint();
thread::spawn(move || {
let rt = tokio::runtime::Runtime::new().unwrap();
@@ -385,12 +374,12 @@ impl eframe::App for App {
});
}
let client3 = st.client.clone();
let node3 = st.node.clone();
let vm_id3 = st.vm_id;
let client3 = client_ref.clone();
let node3 = node_ref.clone();
let vm_id3 = *id;
let state3 = state.clone();
if ui.button(" 重启").clicked() {
st.add_log(&format!("重启 VM {}...", vm_id3));
if ui.button("").clicked() {
state3.write().unwrap().add_log(&format!("重启 VM {}...", vm_id3));
ctx.request_repaint();
thread::spawn(move || {
let rt = tokio::runtime::Runtime::new().unwrap();
@@ -415,6 +404,7 @@ impl eframe::App for App {
}
});
});
});
ui.add_space(12.0);