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