Skip to main content

atmos/kernel/
window.rs

1// src/window.rs - システムモニター用右側固定ペインの描画管理
2#![allow(dead_code)]
3#![allow(static_mut_refs)]
4
5extern crate alloc;
6use alloc::format;
7use alloc::string::String;
8
9use crate::kernel::draw::{Color, Screen};
10use crate::kernel::timer;
11use crate::kernel::window_mgr;
12
13#[derive(Clone, Copy, PartialEq, Eq)]
14pub enum SortColumn {
15    Name,
16    Cpu,
17    Ram,
18    Info,
19}
20
21pub struct SystemMonitorPane {
22    pub x: i32,
23    pub y: i32,
24    pub width: u32,
25    pub height: u32,
26    pub last_update_ticks: usize,
27    pub sort_col: SortColumn,
28    pub sort_asc: bool,
29    pub selected_idx: Option<usize>,
30    pub focused: bool,
31    pub last_mouse_btn: bool,
32    pub last_click_ms: u64,
33    pub wants_power_menu: bool,
34    pub core_loads: [u32; 4],
35    pub last_core_update_ticks: usize,
36    pub dhcp_status: String,
37}
38
39impl SystemMonitorPane {
40    pub fn new(x: i32, y: i32, w: u32, h: u32) -> Self {
41        Self {
42            x,
43            y,
44            width: w,
45            height: h,
46            last_update_ticks: 0,
47            sort_col: SortColumn::Name,
48            sort_asc: true,
49            selected_idx: None,
50            focused: false,
51            last_mouse_btn: false,
52            last_click_ms: 0,
53            wants_power_menu: false,
54            core_loads: [0; 4],
55            last_core_update_ticks: 0,
56            dhcp_status: String::new(),
57        }
58    }
59
60    /// システムモニターペインのプレミアムな描画処理 (チラつき防止のため右側領域に部分限定描画)
61    pub fn draw(&mut self, screen: &Screen) {
62        let ux0 = self.x.max(0) as u32;
63        let uy0 = self.y.max(0) as u32;
64        let ux1 = (self.x + self.width as i32).max(0) as u32;
65        let uy1 = (self.y + self.height as i32).max(0) as u32;
66        let theme = crate::kernel::config::get_config().theme;
67
68        // 1. 右側ペイン背景のクリア
69        screen.boxfill(ux0, uy0, ux1, uy1, Color(theme.sysmon_bg));
70
71        // ドットの背景装飾 (プレミアム感の向上)
72        let dot_color = Color(theme.sysmon_header_bg);
73        for gy in (uy0..uy1).step_by(30) {
74            for gx in (ux0..ux1).step_by(30) {
75                screen.draw_pixel(gx, gy, dot_color);
76            }
77        }
78
79        // 装飾ヘッダ
80        screen.boxfill(ux0, uy0, ux1, uy0 + 30, Color(theme.sysmon_header_bg));
81        screen.boxfill(ux0, uy0 + 30, ux1, uy0 + 31, Color(theme.sysmon_accent)); // アクセントライン
82        screen.draw_string_ja_size(
83            ux0 + 15,
84            uy0 + 8,
85            "SYSTEM STATS MONITOR",
86            14,
87            Color(theme.sysmon_accent),
88        );
89
90        // Power ボタン (右上) - Fluent UI System Icons (MIT)
91        static ICON_POWER: &[u8] = include_bytes!("../../icons/ic_fluent_power_20_regular.svg");
92        let pwr_x = ux1.saturating_sub(45);
93        let pwr_y = uy0 + 5;
94        screen.boxfill(pwr_x, pwr_y, pwr_x + 35, pwr_y + 20, Color(theme.focus_highlight)); // 電源ボタン背景
95        screen.draw_svg_icon(pwr_x + 8, pwr_y + 1, ICON_POWER, 18, Color(crate::kernel::config::get_config().theme.mouse_cursor));
96
97        // 2. 統計情報の描画
98        let text_color = Color(theme.sysmon_text);
99        let val_color = Color(theme.sysmon_value);
100
101        // 基本情報 (Uptimeは非表示)
102        screen.draw_string_ja_size(ux0 + 15, uy0 + 45, "OS Kernel  :", 14, text_color);
103        let kernel_ver = alloc::format!("AtmOS v{} (SMP)", crate::kernel::fs::BUILD_VERSION);
104        screen.draw_string_ja_size(ux0 + 120, uy0 + 45, &kernel_ver, 14, val_color);
105        screen.draw_string_ja_size(ux0 + 15, uy0 + 63, "CPU Cores  :", 14, text_color);
106        screen.draw_string_ja_size(ux0 + 120, uy0 + 63, "4 Cores (SMP)", 14, val_color);
107
108        let uptime_ticks = timer::get_ticks();
109
110        // 3. ネットワーク情報 (接続時は Backend, MAC, IP のみ、未接続時はその旨を表示)
111        screen.boxfill(ux0 + 10, uy0 + 82, ux1 - 10, uy0 + 83, Color(theme.sysmon_border));
112        screen.draw_string_ja_size(
113            ux0 + 15,
114            uy0 + 95,
115            "--- Network Interface ---",
116            14,
117            Color(theme.sysmon_accent),
118        );
119
120        let net_info = {
121            let _lock = crate::kernel::net::NET_LOCK.lock();
122            let st = crate::kernel::net::status();
123            if st.link_up {
124                let backend = crate::kernel::net::nic_backend_name(st.nic_backend);
125                let mac_str = format!(
126                    "{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
127                    st.mac[0], st.mac[1], st.mac[2], st.mac[3], st.mac[4], st.mac[5]
128                );
129                let ip_str = format!("{}.{}.{}.{}", st.ip[0], st.ip[1], st.ip[2], st.ip[3]);
130                Some((backend, mac_str, ip_str))
131            } else {
132                None
133            }
134        };
135
136        if let Some((backend, mac, ip)) = net_info {
137            let backend_line = format!("Backend : {}", backend);
138            let mac_line = format!("MAC     : {}", mac);
139            let ip_line = format!("IP      : {}", ip);
140            screen.draw_string_ja_size(ux0 + 15, uy0 + 115, &backend_line, 14, Color(theme.sysmon_accent));
141            screen.draw_string_ja_size(ux0 + 15, uy0 + 131, &mac_line, 14, Color(theme.sysmon_accent));
142            screen.draw_string_ja_size(ux0 + 15, uy0 + 147, &ip_line, 14, Color(theme.sysmon_accent));
143        } else {
144            // DHCP ステータスを取得して表示。IP が未取得の場合はここに来る。
145            let dhcp_status = crate::kernel::net::dhcp_status_line();
146            screen.draw_string_ja_size(ux0 + 15, uy0 + 115, &dhcp_status, 14, Color(theme.focus_highlight));
147            screen.draw_string_ja_size(ux0 + 15, uy0 + 131, "MAC     : -", 14, text_color);
148            screen.draw_string_ja_size(ux0 + 15, uy0 + 147, "IP      : -", 14, text_color);
149        }
150
151        // 4. メモリ使用状況
152        let mem_y0 = uy0 + 165;
153        let (used, total) = crate::kernel::allocator::get_heap_stats();
154        let used_mb = used as f32 / (1024.0 * 1024.0);
155        let total_mb = total as f32 / (1024.0 * 1024.0);
156        let usage_pct = if total > 0 {
157            (used as f32 / total as f32) * 100.0
158        } else {
159            0.0
160        };
161
162        screen.boxfill(ux0 + 10, mem_y0, ux1 - 10, mem_y0 + 1, Color(theme.sysmon_border));
163        screen.draw_string_ja_size(
164            ux0 + 15,
165            mem_y0 + 15,
166            "--- Memory Heap ---",
167            14,
168            Color(theme.sysmon_accent),
169        );
170
171        let size_str = format!("{:.2} MB / {:.2} MB", used_mb, total_mb);
172        screen.draw_string_ja_size(ux0 + 15, mem_y0 + 37, "Usage Info :", 14, text_color);
173        screen.draw_string_ja_size(ux0 + 120, mem_y0 + 37, &size_str, 14, val_color);
174
175        // 使用率インジケーター(メーター)の高さを拡張 (上下の潰れを解消)
176        let bar_x0 = ux0 + 15;
177        let bar_y0 = mem_y0 + 55;
178        let bar_x1 = ux1 - 15;
179        let bar_y1 = bar_y0 + 14; // 高さ14pxに変更
180        let bar_w = bar_x1 - bar_x0;
181        let fill_w = (bar_w as f32 * (usage_pct / 100.0)) as u32;
182
183        screen.boxfill(bar_x0, bar_y0, bar_x1, bar_y1, Color(theme.sysmon_header_bg)); // メーター背景
184        let meter_color = if usage_pct > 50.0 {
185            Color(theme.focus_highlight) // 警告
186        } else {
187            Color(theme.sysmon_accent) // 通常
188        };
189        screen.boxfill(bar_x0, bar_y0, bar_x0 + fill_w, bar_y1, meter_color);
190
191        let pct_str = format!("{:.1}% used", usage_pct);
192        screen.draw_string_ja_size(ux0 + 15, mem_y0 + 74, &pct_str, 14, meter_color);
193
194        // 5. 各CPUコアロード表示
195        let cpu_y0 = mem_y0 + 95;
196        screen.boxfill(ux0 + 10, cpu_y0, ux1 - 10, cpu_y0 + 1, Color(theme.sysmon_border));
197        screen.draw_string_ja_size(
198            ux0 + 15,
199            cpu_y0 + 15,
200            "--- Core Activity ---",
201            14,
202            Color(theme.sysmon_accent),
203        );
204
205        if self.last_core_update_ticks == 0
206            || uptime_ticks.saturating_sub(self.last_core_update_ticks) >= 1000
207        {
208            for core in 0..4 {
209                let seed = uptime_ticks.wrapping_add(core * 31);
210                let mut hash = seed ^ (seed >> 15);
211                hash = hash.wrapping_mul(0x85ebca6b);
212                hash ^= hash >> 13;
213                hash = hash.wrapping_mul(0xc2b2ae35);
214                hash ^= hash >> 16;
215                self.core_loads[core] = 10 + (hash % 41) as u32; // 10% - 50%
216            }
217            self.last_core_update_ticks = uptime_ticks;
218        }
219
220        let meter_bg = Color(theme.sysmon_header_bg);
221        let meter_fg = Color(theme.sysmon_accent);
222        let max_bar_w = (ux1 - ux0).saturating_sub(180).min(150); // 画面幅に応じてメーターの最大幅を制限
223        for core in 0..4 {
224            let core_y = cpu_y0 + 35 + (core as u32 * 22); // 行の高さを 22px に詰める
225            let label = format!("Core {}:", core);
226            screen.draw_string_ja_size(ux0 + 15, core_y, &label, 14, text_color);
227
228            let load_val = self.core_loads[core];
229
230            let bar_x0 = ux0 + 75;
231            let bar_y0 = core_y + 2;
232            let bar_x1 = bar_x0 + max_bar_w;
233            let bar_y1 = bar_y0 + 10;
234
235            screen.boxfill(bar_x0, bar_y0, bar_x1, bar_y1, meter_bg);
236            let fill_width = (load_val * max_bar_w) / 50;
237            screen.boxfill(bar_x0, bar_y0, bar_x0 + fill_width, bar_y1, meter_fg);
238
239            let load_str = format!("{}%", load_val * 2);
240            screen.draw_string_ja_size(bar_x1 + 8, core_y, &load_str, 14, val_color);
241        }
242
243        self.last_update_ticks = uptime_ticks;
244
245        // 6. プロセスリスト (タスクマネージャ) 領域 (開始位置を 395 に引き上げ)
246        self.draw_process_list(screen, ux0, uy0 + 395, ux1, uy1);
247    }
248
249    fn draw_process_list(&self, screen: &Screen, ux0: u32, y_start: u32, ux1: u32, uy1: u32) {
250        let theme = crate::kernel::config::get_config().theme;
251        let text_color = Color(theme.sysmon_text);
252        let header_bg = Color(theme.sysmon_header_bg);
253        let list_bg = Color(theme.sysmon_bg);
254        let selected_bg = Color(theme.sysmon_border);
255
256        screen.boxfill(ux0 + 10, y_start, ux1 - 10, y_start + 1, Color(theme.sysmon_border));
257        screen.draw_string_ja_size(
258            ux0 + 15,
259            y_start + 10,
260            "--- Running Processes ---",
261            14,
262            Color(theme.sysmon_accent),
263        );
264
265        let table_y = y_start + 30;
266
267        // ヘッダ描画
268        screen.boxfill(ux0, table_y, ux1, table_y + 20, header_bg);
269
270        // 列の座標(パーセンテージによる動的レイアウト)
271        let pane_w = ux1 - ux0;
272        let col_ws_x = ux0 + 8;
273        let col_state_x = ux0 + 26;
274        let col_name_x = ux0 + 48;
275        let col_info_x = ux0 + (pane_w * 55 / 100); // 55%
276
277        let get_sort_marker = |col| {
278            if self.sort_col == col {
279                if self.sort_asc {
280                    " [^]"
281                } else {
282                    " [v]"
283                }
284            } else {
285                ""
286            }
287        };
288
289        screen.draw_string_ja_size(col_ws_x, table_y + 3, "W", 14, Color(theme.sysmon_value));
290        screen.draw_string_ja_size(col_state_x, table_y + 3, "S", 14, Color(theme.sysmon_value));
291        screen.draw_string_ja_size(
292            col_name_x,
293            table_y + 3,
294            &format!("Name{}", get_sort_marker(SortColumn::Name)),
295            14,
296            Color(theme.sysmon_value),
297        );
298        screen.draw_string_ja_size(
299            col_info_x,
300            table_y + 3,
301            &format!("Info{}", get_sort_marker(SortColumn::Info)),
302            14,
303            Color(theme.sysmon_value),
304        );
305
306        let processes = self.build_process_list();
307        let mut row_y = table_y + 20;
308        let row_h = 18;
309
310        for (i, p) in processes.iter().enumerate() {
311            if row_y + row_h > uy1 {
312                break;
313            }
314
315            let bg = if Some(i) == self.selected_idx {
316                selected_bg
317            } else {
318                list_bg
319            };
320            screen.boxfill(ux0, row_y, ux1, row_y + row_h, bg);
321
322            let fg = if Some(i) == self.selected_idx {
323                Color(crate::kernel::config::get_config().theme.mouse_cursor)
324            } else {
325                text_color
326            };
327
328            // WS列
329            let ws_str = match p.workspace {
330                Some(ws) => alloc::format!("{}", ws),
331                None => alloc::string::String::from("-"),
332            };
333            screen.draw_string_ja_size(col_ws_x, row_y + 2, &ws_str, 14, fg);
334
335            // State列
336            screen.draw_string_ja_size(col_state_x, row_y + 2, p.state_char, 14, fg);
337
338            let name_str = if p.is_child {
339                p.display_name.clone()
340            } else {
341                format!("> {}", p.display_name)
342            };
343            screen.draw_string_ja_size(col_name_x, row_y + 2, &name_str, 14, fg);
344            screen.draw_string_ja_size(col_info_x, row_y + 2, &p.original_name, 14, fg);
345
346            row_y += row_h;
347        }
348
349        // フォーカス枠
350        if self.focused {
351            screen.boxfill(ux0, y_start, ux1, y_start + 1, Color(theme.sysmon_accent));
352            screen.boxfill(ux0, uy1 - 1, ux1, uy1, Color(theme.sysmon_accent));
353            screen.boxfill(ux0, y_start, ux0 + 1, uy1, Color(theme.sysmon_accent));
354            screen.boxfill(ux1 - 1, y_start, ux1, uy1, Color(theme.sysmon_accent));
355        }
356    }
357
358    pub fn build_process_list(&self) -> alloc::vec::Vec<ProcessInfo> {
359        let wm = window_mgr::get_instance();
360
361        struct AppGroup {
362            name: String,
363            cpu: u32,
364            ram: u32,
365            instances: alloc::vec::Vec<(Option<usize>, String, u32, u32)>,
366        }
367
368        let mut groups: alloc::vec::Vec<AppGroup> = alloc::vec::Vec::new();
369
370        let term_cpu = 15;
371        let term_ram = 10;
372        groups.push(AppGroup {
373            name: String::from("Terminal"),
374            cpu: term_cpu,
375            ram: term_ram,
376            instances: alloc::vec![(None, String::from("System Shell"), term_cpu, term_ram)],
377        });
378
379        for (i, win) in wm.windows.iter().enumerate() {
380            let name = win.app.name();
381            let info = win.app.instance_info();
382
383            let cpu = 5 + (i as u32 * 3) % 20;
384            let ram = 20 + (i as u32 * 7) % 50;
385
386            if let Some(g) = groups.iter_mut().find(|g| g.name == name) {
387                g.cpu += cpu;
388                g.ram += ram;
389                g.instances.push((Some(i), info, cpu, ram));
390            } else {
391                groups.push(AppGroup {
392                    name: String::from(name),
393                    cpu,
394                    ram,
395                    instances: alloc::vec![(Some(i), info, cpu, ram)],
396                });
397            }
398        }
399
400        groups.sort_by(|a, b| {
401            let cmp = match self.sort_col {
402                SortColumn::Name => a.name.cmp(&b.name),
403                SortColumn::Cpu => a.cpu.cmp(&b.cpu),
404                SortColumn::Ram => a.ram.cmp(&b.ram),
405                SortColumn::Info => core::cmp::Ordering::Equal,
406            };
407            if self.sort_asc {
408                cmp
409            } else {
410                cmp.reverse()
411            }
412        });
413
414        let mut flat = alloc::vec::Vec::new();
415        for g in groups {
416            if g.instances.len() == 1 {
417                let inst = &g.instances[0];
418                let display_info = if inst.1.is_empty() {
419                    String::from("-")
420                } else {
421                    inst.1.clone()
422                };
423
424                let mut ws = None;
425                let mut state = "N";
426                if let Some(win_idx) = inst.0 {
427                    let win = &wm.windows[win_idx];
428                    ws = Some(win.workspace);
429                    state = if win.is_minimized {
430                        "m"
431                    } else if win.is_fullscreen {
432                        "F"
433                    } else if win.x == 100 && win.y == 0 {
434                        "M"
435                    } else {
436                        "N"
437                    };
438                } else if g.name == "Terminal" {
439                    ws = Some(wm.active_workspace);
440                    state = "N";
441                }
442
443                flat.push(ProcessInfo {
444                    window_idx: inst.0,
445                    display_name: g.name.clone(),
446                    cpu: inst.2,
447                    ram: inst.3,
448                    is_child: false,
449                    original_name: display_info,
450                    workspace: ws,
451                    state_char: state,
452                    tab_idx: None,
453                });
454                if let Some(win_idx) = inst.0 {
455                    push_tab_rows(&mut flat, &wm, win_idx, "  ");
456                }
457            } else {
458                flat.push(ProcessInfo {
459                    window_idx: None,
460                    display_name: g.name.clone(),
461                    cpu: g.cpu,
462                    ram: g.ram,
463                    is_child: false,
464                    original_name: format!("{} instances", g.instances.len()),
465                    workspace: None,
466                    state_char: "N",
467                    tab_idx: None,
468                });
469
470                for (idx, inst) in g.instances.into_iter().enumerate() {
471                    let display = if inst.1.is_empty() {
472                        format!("{}: {}", g.name, idx + 1)
473                    } else {
474                        inst.1.clone()
475                    };
476
477                    let mut ws = None;
478                    let mut state = "N";
479                    if let Some(win_idx) = inst.0 {
480                        let win = &wm.windows[win_idx];
481                        ws = Some(win.workspace);
482                        state = if win.is_minimized {
483                            "m"
484                        } else if win.is_fullscreen {
485                            "F"
486                        } else if win.x == 100 && win.y == 0 {
487                            "M"
488                        } else {
489                            "N"
490                        };
491                    }
492
493                    flat.push(ProcessInfo {
494                        window_idx: inst.0,
495                        display_name: format!("  |- {}", display),
496                        cpu: inst.2,
497                        ram: inst.3,
498                        is_child: true,
499                        original_name: display,
500                        workspace: ws,
501                        state_char: state,
502                        tab_idx: None,
503                    });
504                    if let Some(win_idx) = inst.0 {
505                        push_tab_rows(&mut flat, &wm, win_idx, "      ");
506                    }
507                }
508            }
509        }
510
511        flat
512    }
513
514    pub fn handle_mouse(&mut self, mx: i32, my: i32, btn_left: bool) -> bool {
515        let btn_just_pressed = btn_left && !self.last_mouse_btn;
516        self.last_mouse_btn = btn_left;
517
518        if mx < self.x || mx > self.x + self.width as i32 {
519            if btn_just_pressed {
520                self.focused = false;
521            }
522            return false;
523        }
524
525        if btn_just_pressed {
526            self.focused = true;
527
528            // PWRボタンの判定
529            let pwr_x = self.x + self.width as i32 - 45;
530            let pwr_y = self.y + 5;
531            if mx >= pwr_x && mx <= pwr_x + 35 && my >= pwr_y && my <= pwr_y + 20 {
532                self.wants_power_menu = true;
533                return true;
534            }
535
536            let now = crate::kernel::timer::get_system_time_ms();
537            let is_double_click = (now - self.last_click_ms) < 400;
538            self.last_click_ms = now;
539
540            let uy0 = self.y.max(0) as u32;
541            let y_start = uy0 + 395;
542            let table_y = y_start + 30;
543
544            if my as u32 >= table_y && (my as u32) < table_y + 20 {
545                let rel_x = mx - self.x;
546                let pane_w = self.width as i32;
547                let new_col = if rel_x < (pane_w * 55 / 100) {
548                    SortColumn::Name
549                } else {
550                    SortColumn::Info
551                };
552
553                if self.sort_col == new_col {
554                    self.sort_asc = !self.sort_asc;
555                } else {
556                    self.sort_col = new_col;
557                    self.sort_asc = true;
558                }
559                return true;
560            }
561
562            let list_start_y = table_y + 20;
563            if my as u32 >= list_start_y {
564                let row = ((my as u32 - list_start_y) / 18) as usize;
565                let procs = self.build_process_list();
566                if row < procs.len() {
567                    self.selected_idx = Some(row);
568                    if is_double_click {
569                        self.activate_selected();
570                    }
571                } else {
572                    self.selected_idx = None;
573                }
574            }
575        }
576        true
577    }
578
579    pub fn handle_key(&mut self, keycode: u8, _ascii: Option<char>) -> bool {
580        if !self.focused {
581            return false;
582        }
583        let procs = self.build_process_list();
584        if procs.is_empty() {
585            return false;
586        }
587
588        let handled = match keycode {
589            0x48 => {
590                // Up
591                if let Some(mut idx) = self.selected_idx {
592                    idx = idx.saturating_sub(1);
593                    self.selected_idx = Some(idx);
594                } else {
595                    self.selected_idx = Some(0);
596                }
597                true
598            }
599            0x50 => {
600                // Down
601                if let Some(mut idx) = self.selected_idx {
602                    if idx + 1 < procs.len() {
603                        idx += 1;
604                    }
605                    self.selected_idx = Some(idx);
606                } else {
607                    self.selected_idx = Some(0);
608                }
609                true
610            }
611            0x1C => {
612                // Enter
613                self.activate_selected();
614                true
615            }
616            0x53 => {
617                // Delete: 選んだアプリを強制終了する(TODO R2。アプリが描画や JS の途中で返ってこなくても効く)。
618                self.kill_selected();
619                true
620            }
621            _ => false,
622        };
623
624        if handled {
625            window_mgr::get_instance().dirty = true;
626        }
627        handled
628    }
629
630    /// ウィンドウ `win_idx` の行を選ぶ(無ければ先頭の行)。Break キーで一覧を開いたときに、前面のアプリを選んでおく。
631    pub fn select_window_row(&mut self, win_idx: Option<usize>) {
632        let procs = self.build_process_list();
633        let row = win_idx.and_then(|w| procs.iter().position(|p| p.window_idx == Some(w)));
634        self.selected_idx = match row {
635            Some(r) => Some(r),
636            None if procs.is_empty() => None,
637            None => Some(0),
638        };
639    }
640
641    /// 選んだ行のアプリ(ウィンドウ)を強制終了する。ターミナルなどウィンドウの無い行は何もしない。
642    /// 【2026-11-18 TODO R2 (d)】タブの行なら、そのタブだけを止める(ウィンドウとほかのタブは残す)。
643    fn kill_selected(&mut self) {
644        let Some(idx) = self.selected_idx else { return };
645        let procs = self.build_process_list();
646        let Some(p) = procs.get(idx) else { return };
647        let Some(win_idx) = p.window_idx else { return };
648        if let Some(tab_idx) = p.tab_idx {
649            let _ = window_mgr::get_instance().kill_tab(win_idx, tab_idx);
650            // タブは少し後(アプリのスレッドが JS を打ち切って戻った所)で消えるので、選んだ行はそのまま。
651            return;
652        }
653        let _ = window_mgr::get_instance().kill_window(win_idx);
654        let left = self.build_process_list().len();
655        self.selected_idx = if left == 0 { None } else { Some(idx.min(left - 1)) };
656    }
657
658    fn activate_selected(&mut self) {
659        if let Some(idx) = self.selected_idx {
660            let procs = self.build_process_list();
661            if idx < procs.len() {
662                let p = &procs[idx];
663                if let Some(win_idx) = p.window_idx {
664                    let mut wm = window_mgr::get_instance();
665                    wm.windows[win_idx].is_minimized = false;
666                    // タブの行なら、そのタブを選んでから前面に出す(TODO R2 (d))。
667                    if let Some(t) = p.tab_idx {
668                        wm.windows[win_idx].app.switch_tab(t);
669                    }
670                    wm.activate_window(win_idx);
671                    self.focused = false;
672                } else if p.display_name == "Terminal" || p.display_name == "> Terminal" {
673                    let mut wm = window_mgr::get_instance();
674                    wm.terminal_focused = true;
675                    if let Some(ws) = p.workspace {
676                        wm.active_workspace = ws;
677                    }
678                    self.focused = false;
679                }
680            }
681        }
682    }
683}
684
685pub struct ProcessInfo {
686    pub window_idx: Option<usize>,
687    pub display_name: String,
688    pub cpu: u32,
689    pub ram: u32,
690    pub is_child: bool,
691    pub original_name: String,
692    pub workspace: Option<u8>,
693    pub state_char: &'static str,
694    /// タブの行ならタブの番号(TODO R2 (d)、2026-11-18。Delete でそのタブだけを止める)。
695    pub tab_idx: Option<usize>,
696}
697
698/// タブが 2 つ以上あるウィンドウの下に、タブごとの行を足す(TODO R2 (d)、2026-11-18)。
699/// 動かしているタブには `*`、止める合図を立てて閉じるのを待っているタブには `x` を付ける。
700/// タブの一覧は OS 側の写し(`ProxyApp::tabs`。錠を待たない)を読む。
701fn push_tab_rows(flat: &mut alloc::vec::Vec<ProcessInfo>, wm: &window_mgr::WindowManager, win_idx: usize, indent: &str) {
702    let Some(win) = wm.windows.get(win_idx) else { return };
703    let tabs = win.app.tabs();
704    if tabs.len() <= 1 {
705        return;
706    }
707    let running = win.app.running_tab();
708    let pending = win.app.tab_kill_pending();
709    for (t, title) in tabs.iter().enumerate() {
710        let mark = if pending == Some(t) {
711            "x"
712        } else if t == running {
713            "*"
714        } else {
715            " "
716        };
717        // 名前の列は短く(「* tab 2」)、題名は Info の列に出す(名前の列に題名を入れると Info の列と重なる)。
718        let label = alloc::format!("{}{} tab {}", indent, mark, t + 1);
719        flat.push(ProcessInfo {
720            window_idx: Some(win_idx),
721            display_name: label,
722            cpu: 0,
723            ram: 0,
724            is_child: true,
725            original_name: title.clone(),
726            workspace: Some(win.workspace),
727            state_char: "T",
728            tab_idx: Some(t),
729        });
730    }
731}