actl-uia 0.1.8

Windows UIA backend: the ONLY crate allowed to touch COM/unsafe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
use super::{model, overlay, paint};
use actl_core::activity::{ActionCursor, ActionPhase};
use actl_core::state::{SignalPaths, unix_ms};
use actl_uia::display_support::{CAPTURE_HIDE, CAPTURE_RESTORE};
use std::sync::{
    Mutex,
    atomic::{AtomicBool, Ordering},
};
use windows::Win32::Foundation::*;
use windows::Win32::Graphics::Gdi::*;
use windows::Win32::System::LibraryLoader::GetModuleHandleW;
use windows::Win32::UI::Input::KeyboardAndMouse::*;
use windows::Win32::UI::Shell::*;
use windows::Win32::UI::WindowsAndMessaging::*;
use windows::core::{PCWSTR, w};

static PRESSED: Mutex<Option<model::Control>> = Mutex::new(None);
static VIEW: Mutex<Option<model::View>> = Mutex::new(None);
static REQUEST_MESSAGE: Mutex<String> = Mutex::new(String::new());
static MENU_FLOW: Mutex<Option<(String, bool)>> = Mutex::new(None);
static CURSOR: Mutex<Option<ActionCursor>> = Mutex::new(None);
static PREPARING: Mutex<String> = Mutex::new(String::new());
static CAPTURES: Mutex<Vec<u32>> = Mutex::new(Vec::new());
static WAS_VISIBLE: AtomicBool = AtomicBool::new(false);
static HOTKEY: AtomicBool = AtomicBool::new(false);
static STOP_HOVERED: AtomicBool = AtomicBool::new(false);
static DRAG_STOP: AtomicBool = AtomicBool::new(false);
static FRAME_READY: AtomicBool = AtomicBool::new(false);
static EXPANDED: AtomicBool = AtomicBool::new(false);
/// 悬浮槽位(paint::HOVER_*)、进入时间与当前打开卡片的归属槽位。
static HOVER_SLOT: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0);
static HOVER_SINCE: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
static HOVER_CARD: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0);

/// 光标当前落在哪个悬浮槽位(100ms 轮询,无 TrackMouseEvent 依赖)。
unsafe fn hover_slot(hwnd: HWND) -> u8 {
    let mut pointer = POINT::default();
    let mut bounds = RECT::default();
    if !GetCursorPos(&mut pointer).is_ok()
        || !GetClientRect(hwnd, &mut bounds).is_ok()
        || !ScreenToClient(hwnd, &mut pointer).as_bool()
        || pointer.x < 0
        || pointer.y < 0
        || pointer.x >= bounds.right
        || pointer.y >= bounds.bottom
    {
        return paint::HOVER_NONE;
    }
    let dpi = windows::Win32::UI::HiDpi::GetDpiForWindow(hwnd);
    let px = |v: i32| paint::pixels(v, dpi);
    // 右缘锚定簇随宽度移动;第二行(步骤链区)整行按空白区处理。
    let bounds_w = bounds.right - bounds.left;
    let expanded = bounds_w > px(paint::WIDTH + 40);
    if expanded && pointer.y >= px(paint::HEIGHT) {
        return paint::HOVER_BODY;
    }
    match pointer.x {
        x if x >= bounds_w - px(30) => paint::HOVER_BUTTON,
        x if !expanded && x >= px(8) && x < px(24) => paint::HOVER_DOT,
        // 非拖拽手柄的空白区承载信息卡;手柄区不打扰拖拽(悬浮无卡,点击切换展开)。
        x if x >= px(181) => paint::HOVER_BODY,
        _ => paint::HOVER_NONE,
    }
}

/// 两行展开/收回:高度两行 + 宽度 1.5 倍,右缘锚定(向左扩展);不激活不抢焦点;
/// 再次点击切换,不自动收回。
unsafe fn set_expanded(hwnd: HWND, on: bool) {
    EXPANDED.store(on, Ordering::Relaxed);
    let dpi = windows::Win32::UI::HiDpi::GetDpiForWindow(hwnd);
    let h = paint::pixels(if on { paint::HEIGHT_EXT } else { paint::HEIGHT }, dpi);
    let w = paint::pixels(if on { paint::WIDTH_EXT } else { paint::WIDTH }, dpi);
    let mut rc = RECT::default();
    let _ = GetWindowRect(hwnd, &mut rc);
    let _ = SetWindowPos(
        hwnd,
        None,
        rc.right - w,
        rc.top,
        w,
        h,
        SWP_NOZORDER | SWP_NOACTIVATE,
    );
    let _ = InvalidateRect(Some(hwnd), None, false);
}
static TRAY_ICON: Mutex<Option<(usize, i32, u32)>> = Mutex::new(None);
static EPOCH: std::sync::OnceLock<String> = std::sync::OnceLock::new();

// Normal replacement is distinct from user close. Older displays do not acknowledge
// this message; never fall back to WM_CLOSE or clear a pre-existing stop request.
const REPLACE_CLOSE: u32 = WM_APP + 45;

fn allow_close(_paths: &SignalPaths, message: u32, input_busy: Option<bool>) -> bool {
    message == WM_CLOSE || (message == REPLACE_CLOSE && input_busy == Some(false))
}

pub(super) fn request_emergency_stop(source: &str) -> Result<(), String> {
    super::handoff::revoke();
    let lease = SignalPaths::default().display_lease();
    SignalPaths::default()
        .request_stop_from(
            source,
            EPOCH
                .get()
                .map(String::as_str)
                .or_else(|| lease.as_ref().map(|l| l.epoch.as_str())),
        )
        .map_err(|e| format!("INTERNAL:{}", e.message))
}

pub(super) fn clear_emergency_stop() -> Result<(), String> {
    super::handoff::revoke();
    actl_uia::display_gate::recover_execution()
        .map(|_| ())
        .map_err(|e| e.message)
}

fn publish_lease(paths: &SignalPaths, state: &str) {
    let lease = actl_core::display::DisplayLease {
        protocol: actl_core::display::DISPLAY_PROTOCOL,
        build: env!("CARGO_PKG_VERSION").into(),
        pid: std::process::id(),
        epoch: EPOCH
            .get_or_init(actl_core::snapshot::new_snapshot_id)
            .clone(),
        state: state.into(),
        ts_ms: unix_ms(),
    };
    let _ = paths.write_display("display.json", &lease);
}

pub(super) fn stop_timestamp(paths: &SignalPaths) -> Option<u64> {
    paths.stop_timestamp()
}

// Filesystem snapshots arrive on a worker; the window thread only takes the latest value.
fn call_snapshot() -> Vec<actl_core::state::SessionState> {
    type States = Vec<actl_core::state::SessionState>;
    static CACHE: std::sync::OnceLock<std::sync::Arc<Mutex<States>>> = std::sync::OnceLock::new();
    let cache = CACHE.get_or_init(|| {
        let cache = std::sync::Arc::new(Mutex::new(vec![]));
        let output = cache.clone();
        if std::thread::Builder::new()
            .name("signal-history-retention".into())
            .spawn(|| {
                loop {
                    if let Err(error) = actl_core::history::maintain(&SignalPaths::default().dir) {
                        eprintln!("INTERNAL: display history retention failed: {error}");
                    }
                    std::thread::sleep(actl_core::history::RETENTION_INTERVAL);
                }
            })
            .is_err()
        {
            eprintln!("INTERNAL: cannot start display history retention");
        }
        if std::thread::Builder::new()
            .name("signal-state-reader".into())
            .spawn(move || {
                loop {
                    let paths = SignalPaths::default();
                    let mut states = paths.read_calls();
                    super::flow_display::retain(&paths, &mut states);
                    if let Ok(mut latest) = output.lock() {
                        *latest = states;
                    }
                    std::thread::sleep(std::time::Duration::from_millis(100));
                }
            })
            .is_err()
        {
            eprintln!("INTERNAL: cannot start display state reader");
        }
        cache
    });
    cache
        .lock()
        .map(|states| states.clone())
        .unwrap_or_default()
}
unsafe fn tick(hwnd: HWND) {
    let slot = hover_slot(hwnd);
    let slot_entered = HOVER_SLOT.swap(slot, Ordering::Relaxed) != slot;
    if slot_entered {
        HOVER_SINCE.store(unix_ms(), Ordering::Relaxed);
    }
    let hovered = slot == paint::HOVER_BUTTON;
    if slot_entered || STOP_HOVERED.swap(hovered, Ordering::Relaxed) != hovered {
        let _ = InvalidateRect(Some(hwnd), None, false);
    }
    let paths = SignalPaths::default();
    let now = unix_ms();
    let states = call_snapshot();
    let stop = stop_timestamp(&paths);
    let mut v = model::view(&states, now, stop, actl_uia::display_support::input_busy());
    v.details.push_str(&paths.stop_details());
    // 常驻预授权的可见性补偿:青色描边(paint 层)+ 窗口标题文字版;正文不再加前缀。
    super::paint::set_preauth(paths.preauthorized());
    // 展开态第二行步骤链:当前绑定流程的步骤列表;无流程时清空(回落最近调用摘要)。
    // 真机验收发现的孤儿化:最近调用的流程完成后 visible-flow 指向 completed,
    // 更早的暂停流程从显示与探测中消失——绑定回落到 discover(唯一未完结流程)。
    let bound_flow = model::selected(&states, now)
        .and_then(|s| {
            s.workflow
                .as_ref()
                .map(|f| f.run_id.clone())
                .filter(|id| actl_core::history::valid_id(id))
        })
        .or_else(|| super::flow_ui::discover(&paths));
    let bound_state = bound_flow
        .as_deref()
        .and_then(|id| super::flow_ui::read(&paths, id));
    let plan = bound_flow
        .as_deref()
        .and_then(|id| super::ui_text::plan(&paths, id));
    let progress = bound_flow.as_deref().map(|id| {
        super::flow_ui::read(&paths, id)
            .map(|s| super::ui_text::strip_progress(&s, plan.as_ref()))
            .unwrap_or_else(|| "步骤状态未确认".into())
    });
    super::paint::set_progress(progress.unwrap_or_default());
    // 批次4:修复检测探测(5s 静默跑阻塞步骤的 before/verify)——点亮"什么时候
    // 可以点继续";auto_resume 仅在流程显式声明、Retriable 且预算内时派发,
    // 且急停/损坏代次在场绝不派发。
    if let Some((id, verdict)) = super::probe::tick(
        &paths,
        bound_flow.as_deref(),
        bound_state.as_ref().and_then(|s| s["status"].as_str()),
        bound_state.as_ref().and_then(|s| s["reason"].as_str()),
        bound_state
            .as_ref()
            .and_then(|s| s["updated_ms"].as_u64())
            .unwrap_or(0),
        plan.as_ref(),
    ) {
        let declared = plan
            .as_ref()
            .is_some_and(|p| p["auto_resume"] == "action_failed");
        match verdict {
            super::probe::Verdict::Retriable => {
                // 先核验派发前置(真实代次/无急停),再扣预算,最后派发——
                // 真机验收发现:预算不得在派发失败时消耗,generation 必须传真实值
                // (曾把标记串当 generation 传给子进程,flow-resume 代次校验即败)。
                let generation = paths.execution_generation().ok();
                if declared
                    && generation.is_some()
                    && !paths.stop_requested()
                    && super::probe::consume_auto_resume(&paths, &id, true, verdict)
                    && super::flow_ui::request(&id, true, generation.as_deref().unwrap()).is_ok()
                {
                    super::probe::record(&paths, &id, "auto_resume", &serde_json::json!({}));
                } else {
                    v.label = "障碍已清除 · 可重试".into();
                    v.details.push_str(
                        "\n探测显示失败步骤的前置条件已满足;点「已修复,重试继续」将重发该步动作",
                    );
                }
            }
            super::probe::Verdict::AlreadyDone => {
                v.label = "结果已存在 · 继续前请核对".into();
                v.details.push_str(
                    "\n探测显示该步后置条件已满足(可能是你手动完成了这步);重发动作可能重复生效,请先核对",
                );
            }
            super::probe::Verdict::Pending => {}
        }
    }
    if matches!(
        v.mode,
        model::Mode::Running | model::Mode::Background | model::Mode::Desktop
    ) && let Some(flow) = model::selected(&states, now).and_then(|s| s.workflow.as_ref())
        && actl_core::history::valid_id(&flow.run_id)
        && let Ok(meta) = std::fs::metadata(
            paths
                .dir
                .join("flows")
                .join(&flow.run_id)
                .join("pause-requested"),
        )
    {
        // 批次2①③:带阻塞步骤与已等待时长,超阈值(~5s)附急停教育(不自动升级,
        // 保留用户意图);标签不带耗时,防托盘图标每秒重建。
        let elapsed = meta
            .modified()
            .map_or(0, |t| t.elapsed().map_or(0, |d| d.as_secs()));
        let (mode, label, details) = model::pause_pending_view(
            flow.step_id.as_deref(),
            elapsed,
            HOTKEY.load(Ordering::Relaxed),
        );
        v.mode = mode;
        v.label = label;
        v.details.push_str(&format!("\n{details}"));
    }
    if stop.is_none() {
        DRAG_STOP.store(false, Ordering::Relaxed);
    }
    if DRAG_STOP.load(Ordering::Relaxed) {
        v.label = "移动状态条 · 已请求暂停".into();
        v.details.push_str("\n移动后已请求暂停;继续时请重新确认");
    }
    let request_message = super::flow_ui::message();
    if !request_message.is_empty() {
        v.details.push_str(&format!("\n{request_message}"));
        let is_new = REQUEST_MESSAGE
            .lock()
            .map(|mut old| {
                let changed = *old != request_message;
                *old = request_message.clone();
                changed
            })
            .unwrap_or(false);
        if is_new && request_message.contains(':') {
            super::popup::show(
                hwnd,
                vec!["流程操作未完成".into(), request_message.clone()],
                false,
            );
        }
    }
    // 标题/图标只在四要素变化时更新;展开行内容(second/elapsed)变化只触发重绘,
    // 避免运行中每秒重建托盘图标。
    let (changed, content) = if let Ok(mut old) = VIEW.lock() {
        let changed = old.as_ref().is_none_or(|o| {
            o.mode != v.mode
                || o.label != v.label
                || o.effects != v.effects
                || o.control != v.control
        });
        let content = old
            .as_ref()
            .is_none_or(|o| o.second != v.second || o.elapsed != v.elapsed);
        *old = Some(v.clone());
        (changed, content)
    } else {
        (false, false)
    };
    if changed || content {
        let _ = InvalidateRect(Some(hwnd), None, false);
    }
    // 动画态(呼吸/脉冲)与展开态需要每 tick 连续重绘(走下方 DwmFlush 配对路径);
    // 静止态保持静止(docs/18 立场)。
    let animated = matches!(
        v.mode,
        model::Mode::Running
            | model::Mode::Background
            | model::Mode::Preparing
            | model::Mode::Desktop
            | model::Mode::Paused
            | model::Mode::Waiting
            | model::Mode::Partial
            | model::Mode::Blocked
            | model::Mode::StopRequested
    ) || EXPANDED.load(Ordering::Relaxed);
    if changed {
        let title = paint::wide(&format!(
            "actl · {}{}",
            if paths.preauthorized() {
                "预授权·"
            } else {
                ""
            },
            v.label
        ));
        let _ = SetWindowTextW(hwnd, PCWSTR(title.as_ptr()));
        let mut nid = tray(hwnd);
        nid.uFlags = NIF_TIP | NIF_ICON;
        nid.hIcon = update_icon(hwnd, v.mode);
        let tip = paint::wide(&format!(
            "{}\n{}\n右键:更多",
            v.label,
            if HOTKEY.load(Ordering::Relaxed) {
                "Ctrl+Alt+F12 请求停止"
            } else {
                "右键菜单:急停"
            }
        ));
        let n = tip.len().saturating_sub(1).min(126);
        nid.szTip[..n].copy_from_slice(&tip[..n]);
        let _ = Shell_NotifyIconW(NIM_MODIFY, &nid);
    }
    let suppressed = if let Ok(mut captures) = CAPTURES.lock() {
        let had = !captures.is_empty();
        captures.retain(|pid| process_alive(*pid));
        if had && captures.is_empty() && WAS_VISIBLE.load(Ordering::Relaxed) {
            let _ = ShowWindow(hwnd, SW_SHOWNOACTIVATE);
        }
        !captures.is_empty()
    } else {
        true
    };
    let visible = IsWindowVisible(hwnd).as_bool() && !suppressed;
    let selected = model::selected(&states, now);
    let preparing = selected
        .filter(|s| s.phase == actl_core::state::Phase::Running)
        .and_then(|s| {
            s.action
                .as_ref()
                .filter(|a| a.phase == ActionPhase::Preparing && a.expires_ms > now)
                .map(|a| (s, a))
        });
    if visible
        && (changed
            || content
            || animated
            || slot_entered
            || preparing.is_some()
            || !FRAME_READY.load(Ordering::Relaxed))
    {
        FRAME_READY.store(
            paint::draw(hwnd, &v, slot, EXPANDED.load(Ordering::Relaxed))
                && windows::Win32::Graphics::Dwm::DwmFlush().is_ok(),
            Ordering::Relaxed,
        );
    }
    // 悬浮卡:槽位 dwell ≥500ms 触发;换槽/离开即收;按下按钮或拖拽期间不出卡;
    // 其他弹窗(菜单/详情)占用时让位,且只有悬浮卡归属者能撤自己。
    let card = if slot != 0
        && !slot_entered
        && unix_ms().saturating_sub(HOVER_SINCE.load(Ordering::Relaxed)) >= 500
        && PRESSED.lock().map(|p| p.is_none()).unwrap_or(true)
        && !DRAG_STOP.load(Ordering::Relaxed)
        && (!super::popup::any_open() || super::popup::shown_by_hover())
    {
        hover_card(slot, &v, &paths, &states, now)
    } else {
        None
    };
    if let Some(lines) = card {
        if HOVER_CARD.load(Ordering::Relaxed) != slot {
            super::popup::show_hover(hwnd, lines);
            HOVER_CARD.store(slot, Ordering::Relaxed);
        }
    } else if HOVER_CARD.load(Ordering::Relaxed) != 0 {
        if super::popup::shown_by_hover() && (slot == 0 || slot_entered) {
            super::popup::hide();
        }
        if !super::popup::shown_by_hover() {
            // 弹窗已被菜单/详情接管或自行关闭:放弃归属,不再补卡。
            HOVER_CARD.store(0, Ordering::Relaxed);
        }
    }
    let ready = visible
        && FRAME_READY.load(Ordering::Relaxed)
        && actl_uia::display_support::surface_exposed(hwnd);
    publish_lease(
        &paths,
        if suppressed {
            "capture"
        } else if ready {
            "ready"
        } else {
            "hidden"
        },
    );
    if ready
        && stop.is_none()
        && let Some((s, a)) = preparing
        && s.call_id
            .bytes()
            .all(|c| c.is_ascii_alphanumeric() || c == b'-')
        && !s.call_id.is_empty()
    {
        let ack = actl_core::display::DisplayAck {
            epoch: EPOCH
                .get_or_init(actl_core::snapshot::new_snapshot_id)
                .clone(),
            call_id: s.call_id.clone(),
            action_id: a.id,
            ts_ms: unix_ms(),
        };
        let _ = paths.write_display(&format!("ack-{}.json", s.call_id), &ack);
    }
    if visible {
        let _ = std::fs::write(paths.dir.join("display-live"), now.to_string());
    } else {
        let _ = std::fs::remove_file(paths.dir.join("display-live"));
    }
    let events = CURSOR
        .lock()
        .ok()
        .map(|mut c| {
            c.get_or_insert_with(|| ActionCursor::new(now))
                .take(&states, now)
        })
        .unwrap_or_default();
    if suppressed || !visible {
        overlay::hide();
        super::perimeter::hide();
        return;
    }
    super::perimeter::update(hwnd, &v, &states, now, ready);
    let key = preparing
        .map(|(s, a)| format!("{}:{}", s.call_id, a.id))
        .unwrap_or_default();
    let changed = if let Ok(mut old) = PREPARING.lock() {
        let changed = *old != key;
        *old = key;
        changed
    } else {
        false
    };
    if changed {
        overlay::hide();
        if let Some((_, a)) = preparing {
            overlay::show(a);
        }
    }
    // Coalesce bursts to the most recent delivered spatial action; never accumulate a screenful.
    if let Some(a) = events
        .iter()
        .rev()
        .find(|a| a.target.point.is_some() || a.target.bounds.is_some())
    {
        overlay::show(a);
    }
}

unsafe fn process_alive(pid: u32) -> bool {
    use windows::Win32::System::Threading::*;
    match OpenProcess(PROCESS_SYNCHRONIZE, false, pid) {
        Ok(h) => {
            let alive = WaitForSingleObject(h, 0) != WAIT_OBJECT_0;
            let _ = CloseHandle(h);
            alive
        }
        Err(e) => e.code() != windows::core::HRESULT::from_win32(87),
    }
}

unsafe fn tray(hwnd: HWND) -> NOTIFYICONDATAW {
    NOTIFYICONDATAW {
        cbSize: std::mem::size_of::<NOTIFYICONDATAW>() as u32,
        hWnd: hwnd,
        uID: 1,
        ..Default::default()
    }
}

unsafe fn update_icon(hwnd: HWND, mode: model::Mode) -> HICON {
    let dpi = windows::Win32::UI::HiDpi::GetDpiForWindow(hwnd);
    let size = windows::Win32::UI::HiDpi::GetSystemMetricsForDpi(SM_CXSMICON, dpi).max(16);
    let color = match mode {
        model::Mode::Failed | model::Mode::Blocked => 0xE59188,
        model::Mode::StopRequested
        | model::Mode::Preparing
        | model::Mode::Partial
        | model::Mode::Desktop
        | model::Mode::Paused
        | model::Mode::Waiting => 0xD4B879,
        model::Mode::Done => 0x8AB698,
        model::Mode::Running | model::Mode::Background => 0x93B4CA,
        _ => 0xB4BAC0,
    };
    let Ok(mut old) = TRAY_ICON.lock() else {
        return HICON::default();
    };
    if let Some((icon, s, c)) = *old
        && s == size
        && c == color
    {
        return HICON(icon as *mut _);
    }
    let Ok(icon) = super::icon::create(size, color) else {
        return old.map(|v| HICON(v.0 as *mut _)).unwrap_or_default();
    };
    if let Some((handle, _, _)) = old.replace((icon.0 as usize, size, color)) {
        let _ = DestroyIcon(HICON(handle as *mut _));
    }
    icon
}

unsafe fn details(_hwnd: HWND) {
    use std::os::windows::process::CommandExt;
    if let Ok(exe) = std::env::current_exe() {
        let mut command = std::process::Command::new(exe);
        command.arg("--controls").creation_flags(0x08000000);
        if let Some(id) = super::flow_ui::discover(&SignalPaths::default()) {
            command.args(["--flow", &id]);
        }
        if command.spawn().is_err() {
            super::popup::show(_hwnd, vec!["INTERNAL:无法打开详情".into()], false);
        }
    }
}

unsafe fn menu(hwnd: HWND) {
    let mut lines = vec![
        "急停".into(),
        "修复运行状态".into(),
        "任务详情".into(),
        if IsWindowVisible(hwnd).as_bool() {
            "隐藏".into()
        } else {
            "显示".into()
        },
        "关闭".into(),
    ];
    let paths = SignalPaths::default();
    let action = super::flow_ui::discover(&paths).and_then(|id| {
        let state = super::flow_ui::read(&paths, &id)?;
        match state["status"].as_str()? {
            "running" => Some((id, false)),
            "paused" | "needs_review" => Some((id, true)),
            _ => None,
        }
    });
    lines.push(if paths.preauthorized() {
        "预授权: 开(关)".into()
    } else {
        "预授权: 关(开)".into()
    });
    if let Some((_, resume)) = &action {
        lines.push(if *resume { "继续" } else { "暂停" }.into());
    }
    if let Ok(mut stored) = MENU_FLOW.lock() {
        *stored = action;
    }
    super::popup::show(hwnd, lines, true);
}

unsafe fn menu_action(hwnd: HWND, cmd: usize) {
    match cmd {
        1 | 2 => {
            let result = if cmd == 1 {
                request_emergency_stop("menu")
            } else {
                clear_emergency_stop()
            };
            if let Err(error) = result {
                super::popup::show(hwnd, vec!["急停状态更新失败".into(), error], false);
            }
        }
        3 => details(hwnd),
        4 => {
            let _ = ShowWindow(
                hwnd,
                if IsWindowVisible(hwnd).as_bool() {
                    SW_HIDE
                } else {
                    SW_SHOWNOACTIVATE
                },
            );
        }
        5 => {
            super::handoff::revoke();
            publish_lease(&SignalPaths::default(), "closed");
            let _ = DestroyWindow(hwnd);
        }
        6 => {
            // 预授权开关(固定第 6 项,流程操作在它之后动态追加):授予动作只发生在
            // 显示端 UI(本菜单),agent/CLI 无写入通道。关闭时立即撤销常驻同意,
            // 回到点击模式;急停在任何模式下都有效。
            let paths = SignalPaths::default();
            let next = !paths.preauthorized();
            match paths.set_preauth(next) {
                Ok(()) => {
                    if !next {
                        super::handoff::revoke();
                    }
                    super::popup::show(
                        hwnd,
                        vec![if next {
                            "预授权写入已开启:桌面写入不再要求开始;让行与急停仍然有效".into()
                        } else {
                            "预授权写入已关闭".into()
                        }],
                        false,
                    );
                }
                Err(error) => super::popup::show(
                    hwnd,
                    vec!["预授权开关写入失败".into(), error.to_string()],
                    false,
                ),
            }
        }
        7 => {
            let action = MENU_FLOW.lock().ok().and_then(|a| a.clone());
            if let Some((id, resume)) = action {
                let result = flow_action(&id, resume);
                if let Err(error) = result {
                    super::popup::show(hwnd, vec!["流程操作未完成".into(), error], false);
                }
            }
        }
        _ => {}
    }
    tick(hwnd);
}

unsafe fn primary(hwnd: HWND, control: model::Control) {
    let result = match control {
        model::Control::Pause { call, flow } => {
            super::handoff::pause_call(&call);
            match flow {
                Some(id) => flow_action(&id, false),
                None => Ok(()),
            }
        }
        model::Control::Continue { flow } => {
            let current = VIEW
                .lock()
                .ok()
                .and_then(|v| v.as_ref().map(|v| v.control.clone()));
            if current != Some(model::Control::Continue { flow: flow.clone() }) {
                super::popup::show(hwnd, vec!["显示任务已变化,请重新查看".into()], false);
                return;
            }
            // Re-read persisted state. A stale button must never revive completed/stopped work.
            let state = super::flow_ui::read(&SignalPaths::default(), &flow);
            if !state
                .as_ref()
                .is_some_and(|s| matches!(s["status"].as_str(), Some("paused" | "needs_review")))
            {
                Err("任务状态已变化,请重新查看".into())
            } else if !state
                .as_ref()
                .is_some_and(|s| super::flow_ui::continue_ready(s, unix_ms()))
            {
                Err("刚刚暂停,请确认任务状态后再点继续".into())
            } else if super::flow_ui::pending(&flow, true) {
                Ok(())
            } else {
                let result = flow_action(&flow, true);
                if result.is_err() {
                    super::handoff::revoke();
                }
                result
            }
        }
        model::Control::Details => {
            details(hwnd);
            Ok(())
        }
    };
    if let Err(error) = result {
        super::popup::show(hwnd, vec!["任务状态未改变".into(), error], false);
    }
}

pub(super) fn flow_action(id: &str, resume: bool) -> Result<(), String> {
    let paths = SignalPaths::default();
    let generation = if resume {
        let state = super::flow_ui::read(&paths, id);
        super::flow_ui::resume_guard(state.as_ref(), unix_ms())?;
        super::flow_start::allow(id)?
    } else {
        paths.execution_generation().map_err(|e| e.message)?
    };
    super::flow_ui::request(id, resume, &generation)
}

unsafe fn place(hwnd: HWND) {
    let dpi = windows::Win32::UI::HiDpi::GetDpiForWindow(hwnd);
    let w = paint::pixels(
        if EXPANDED.load(Ordering::Relaxed) {
            paint::WIDTH_EXT
        } else {
            paint::WIDTH
        },
        dpi,
    );
    let h = paint::pixels(
        if EXPANDED.load(Ordering::Relaxed) {
            paint::HEIGHT_EXT
        } else {
            paint::HEIGHT
        },
        dpi,
    );
    let monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
    let mut info = MONITORINFO {
        cbSize: std::mem::size_of::<MONITORINFO>() as u32,
        ..Default::default()
    };
    if !GetMonitorInfoW(monitor, &mut info).as_bool() {
        return;
    }
    let mut rc = RECT::default();
    let _ = GetWindowRect(hwnd, &mut rc);
    let x = rc.left.clamp(
        info.rcWork.left,
        (info.rcWork.right - w).max(info.rcWork.left),
    );
    let y = rc.top.clamp(
        info.rcWork.top,
        (info.rcWork.bottom - h).max(info.rcWork.top),
    );
    let _ = SetWindowPos(hwnd, None, x, y, w, h, SWP_NOACTIVATE | SWP_NOZORDER);
    // Rounded edges come from the premultiplied alpha bitmap, never a hard region clip.
    let _ = InvalidateRect(Some(hwnd), None, false);
}

/// 悬浮卡内容:问哪答哪——状态点=状态图例,空白区(含展开态第二行)=空闲一站式卡
/// 或图例;按钮/箭头/手柄区不出卡。
fn hover_card(
    slot: u8,
    v: &model::View,
    paths: &SignalPaths,
    states: &[actl_core::state::SessionState],
    now: u64,
) -> Option<Vec<String>> {
    if slot == paint::HOVER_DOT {
        return Some(legend_card(v));
    }
    if slot == paint::HOVER_BODY {
        return Some(
            if matches!(v.mode, model::Mode::Idle | model::Mode::Unknown) {
                idle_card(paths, states, now)
            } else {
                legend_card(v)
            },
        );
    }
    None
}

fn legend_card(v: &model::View) -> Vec<String> {
    let (name, hint) = match v.mode {
        model::Mode::Idle => ("空闲", "无任务无调用"),
        model::Mode::Background => ("后台处理", "只读查询进行中,不动你的键鼠"),
        model::Mode::Running => ("运行中", "自动化正在执行"),
        model::Mode::Preparing => ("准备中", "动作预告已上屏,即将执行"),
        model::Mode::Desktop => ("桌面操作", "正在写入界面,暂勿操作"),
        model::Mode::Waiting => ("等待你", "需要开始/继续确认"),
        model::Mode::Paused => ("已暂停", "继续前先核对上一步"),
        model::Mode::Partial => ("待核对", "结果不确定,先核对再继续"),
        model::Mode::Blocked => ("受阻", "查看详情了解原因"),
        model::Mode::Failed => ("失败", "查看详情了解原因"),
        model::Mode::Done => ("完成", "调用已成功结束"),
        model::Mode::StopRequested => ("停止请求", "后续操作已阻止"),
        model::Mode::Stopped => ("已停止", "本次调用已被停止"),
        model::Mode::Unknown => ("状态未知", "观察超时或状态陈旧"),
    };
    vec![
        format!("状态 · {name}"),
        hint.into(),
        format!("右侧按钮:{}", v.control_hint),
    ]
}

/// 空闲态一站式卡:上次活动/权限模式/急停入口/遗留流程。
fn idle_card(
    paths: &SignalPaths,
    states: &[actl_core::state::SessionState],
    now: u64,
) -> Vec<String> {
    let last = match states.last() {
        Some(s) => format!(
            "上次 {} · {} · {}",
            s.command,
            match s.phase {
                actl_core::state::Phase::Done => "完成",
                actl_core::state::Phase::Error => "失败",
                actl_core::state::Phase::Stopped => "停止",
                actl_core::state::Phase::Running => "进行中",
            },
            relative_time(now.saturating_sub(s.ts_ms))
        ),
        None => "暂无活动记录".into(),
    };
    let mut lines = vec![format!("空闲 · {last}")];
    lines.push(
        if paths.preauthorized() {
            "预授权:开(托盘右键可关闭;让行与急停仍有效)"
        } else {
            "预授权:关(托盘右键可开启)"
        }
        .into(),
    );
    lines.push(
        if HOTKEY.load(Ordering::Relaxed) {
            "急停:Ctrl+Alt+F12 或右键菜单"
        } else {
            "急停:右键菜单 → 急停"
        }
        .into(),
    );
    if let Some(id) = super::flow_ui::discover(paths) {
        lines.push(format!("待处理流程 {id}(任务详情可查看)"));
    }
    lines
}

fn relative_time(ms: u64) -> String {
    let secs = ms / 1000;
    if secs < 60 {
        format!("{secs}秒前")
    } else if secs < 3600 {
        format!("{}分钟前", secs / 60)
    } else {
        format!("{}小时前", secs / 3600)
    }
}

unsafe extern "system" fn proc(hwnd: HWND, msg: u32, wp: WPARAM, lp: LPARAM) -> LRESULT {
    match msg {
        super::flow_start::MESSAGE => LRESULT(isize::from(super::flow_start::receive(wp.0 as u32))),
        actl_core::handoff::REQUEST_MESSAGE
        | actl_core::handoff::CHECK_MESSAGE
        | actl_core::handoff::FOREGROUND_MESSAGE => {
            LRESULT(super::handoff::message(msg, wp.0 as u32) as isize)
        }
        actl_core::handoff::RECOVER_MESSAGE => {
            place(hwnd);
            let _ = SetWindowPos(
                hwnd,
                Some(HWND_TOPMOST),
                0,
                0,
                0,
                0,
                SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE,
            );
            let _ = ShowWindow(hwnd, SW_SHOWNOACTIVATE);
            LRESULT(1)
        }
        msg if msg == WM_APP + 44 => {
            menu(hwnd);
            LRESULT(0)
        }
        WM_TIMER => {
            super::handoff::tick();
            tick(hwnd);
            LRESULT(0)
        }
        WM_PAINT => {
            let view = VIEW
                .lock()
                .ok()
                .and_then(|v| v.clone())
                .unwrap_or_else(|| model::view(&[], unix_ms(), None, Some(false)));
            FRAME_READY.store(
                paint::draw(
                    hwnd,
                    &view,
                    HOVER_SLOT.load(Ordering::Relaxed),
                    EXPANDED.load(Ordering::Relaxed),
                ),
                Ordering::Relaxed,
            );
            LRESULT(0)
        }
        WM_MOUSEACTIVATE => LRESULT(MA_NOACTIVATE as isize),
        WM_NCHITTEST => {
            let mut pt = POINT {
                x: (lp.0 as u16 as i16) as i32,
                y: ((lp.0 >> 16) as u16 as i16) as i32,
            };
            let _ = ScreenToClient(hwnd, &mut pt);
            let dpi = windows::Win32::UI::HiDpi::GetDpiForWindow(hwnd);
            // Only the middle text is a drag handle; the stop hit area is never draggable.
            LRESULT(
                if pt.x >= paint::pixels(31, dpi) && pt.x < paint::pixels(181, dpi) {
                    HTCAPTION
                } else {
                    HTCLIENT
                } as isize,
            )
        }
        WM_ENTERSIZEMOVE => {
            DRAG_STOP.store(true, Ordering::Relaxed);
            let control = VIEW
                .lock()
                .ok()
                .and_then(|v| v.as_ref().map(|v| v.control.clone()));
            if let Some(control @ model::Control::Pause { .. }) = control {
                primary(hwnd, control);
            }
            overlay::hide();
            super::perimeter::hide();
            LRESULT(0)
        }
        WM_EXITSIZEMOVE => {
            place(hwnd);
            let mut rc = RECT::default();
            if GetWindowRect(hwnd, &mut rc).is_ok() {
                let _ = std::fs::write(
                    SignalPaths::default().dir.join("signal-pos.txt"),
                    format!("{},{}", rc.left, rc.top),
                );
            }
            LRESULT(0)
        }
        WM_LBUTTONDOWN => {
            let x = (lp.0 as u16 as i16) as i32;
            if x >= paint::pixels(246, windows::Win32::UI::HiDpi::GetDpiForWindow(hwnd))
                && let Ok(mut pressed) = PRESSED.lock()
            {
                *pressed = VIEW
                    .lock()
                    .ok()
                    .and_then(|v| v.as_ref().map(|v| v.control.clone()));
            }
            LRESULT(0)
        }
        WM_LBUTTONUP => {
            let x = (lp.0 as u16 as i16) as i32;
            let dpi = windows::Win32::UI::HiDpi::GetDpiForWindow(hwnd);
            let captured = PRESSED.lock().ok().and_then(|mut p| p.take());
            let mut bounds = RECT::default();
            let _ = GetClientRect(hwnd, &mut bounds);
            let button_x = (bounds.right - bounds.left) - paint::pixels(30, dpi);
            if x >= button_x {
                if let Some(control) = captured {
                    primary(hwnd, control);
                }
                tick(hwnd);
            } else {
                // 点击空白即切换展开/收回(无箭头指示);详情窗口从托盘「任务详情」
                // 或收起态右侧按钮进入。
                set_expanded(hwnd, !EXPANDED.load(Ordering::Relaxed));
            }
            LRESULT(0)
        }
        WM_NCLBUTTONUP => {
            // 拖拽手柄(HTCAPTION)上的"点击未拖拽"同样切换展开;拖拽路径
            // (WM_ENTERSIZEMOVE)不受影响。
            set_expanded(hwnd, !EXPANDED.load(Ordering::Relaxed));
            LRESULT(0)
        }
        WM_RBUTTONUP => {
            menu(hwnd);
            LRESULT(0)
        }
        WM_HOTKEY => {
            if let Err(error) = request_emergency_stop("hotkey") {
                super::popup::show(hwnd, vec!["急停保存失败".into(), error], false);
            }
            tick(hwnd);
            LRESULT(0)
        }
        WM_APP => {
            if (lp.0 as u32 & 0xffff) == WM_RBUTTONUP {
                menu(hwnd);
            } else if (lp.0 as u32 & 0xffff) == WM_LBUTTONUP {
                details(hwnd);
            }
            LRESULT(0)
        }
        super::popup::CHOOSE => {
            menu_action(hwnd, wp.0);
            LRESULT(0)
        }
        CAPTURE_HIDE => {
            super::popup::hide();
            publish_lease(&SignalPaths::default(), "capture");
            if let Ok(mut captures) = CAPTURES.lock() {
                if captures.is_empty() {
                    WAS_VISIBLE.store(IsWindowVisible(hwnd).as_bool(), Ordering::Relaxed);
                }
                captures.push(wp.0 as u32);
            }
            let _ = ShowWindow(hwnd, SW_HIDE);
            overlay::hide();
            super::perimeter::hide();
            LRESULT(1)
        }
        CAPTURE_RESTORE => {
            if let Ok(mut captures) = CAPTURES.lock() {
                if let Some(i) = captures.iter().position(|pid| *pid == wp.0 as u32) {
                    captures.remove(i);
                }
                if captures.is_empty() && WAS_VISIBLE.load(Ordering::Relaxed) {
                    let _ = ShowWindow(hwnd, SW_SHOWNOACTIVATE);
                }
            }
            LRESULT(0)
        }
        WM_DPICHANGED | WM_DISPLAYCHANGE => {
            place(hwnd);
            LRESULT(0)
        }
        WM_CLOSE | REPLACE_CLOSE => {
            if allow_close(
                &SignalPaths::default(),
                msg,
                actl_uia::display_support::input_busy(),
            ) {
                super::handoff::revoke();
                publish_lease(&SignalPaths::default(), "closed");
                let _ = DestroyWindow(hwnd);
                LRESULT(1)
            } else {
                LRESULT(0)
            }
        }

        WM_DESTROY => {
            super::handoff::revoke();
            super::popup::hide();
            overlay::hide();
            super::perimeter::hide();
            let _ = KillTimer(Some(hwnd), 1);
            let _ = UnregisterHotKey(Some(hwnd), 1);
            let _ = Shell_NotifyIconW(NIM_DELETE, &tray(hwnd));
            if let Ok(mut icon) = TRAY_ICON.lock()
                && let Some((h, _, _)) = icon.take()
            {
                let _ = DestroyIcon(HICON(h as *mut _));
            }
            publish_lease(&SignalPaths::default(), "closed");
            let _ = std::fs::remove_file(SignalPaths::default().dir.join("display-live"));
            PostQuitMessage(0);
            LRESULT(0)
        }
        _ => DefWindowProcW(hwnd, msg, wp, lp),
    }
}

pub unsafe fn run() -> windows::core::Result<()> {
    if std::env::args().any(|a| a == "--menu")
        && !std::env::args().any(|a| a == "--replace")
        && let Ok(existing) = FindWindowW(w!("actl_signal_window"), None)
    {
        PostMessageW(Some(existing), WM_APP + 44, WPARAM(0), LPARAM(0))?;
        return Ok(());
    }
    if std::env::args().any(|a| a == "--replace") {
        if actl_uia::display_support::input_busy() != Some(false) {
            return Err(windows::core::Error::new(
                windows::core::HRESULT(0x80004005u32 as i32),
                "input busy; replacement refused",
            ));
        }
        if let Ok(old) = FindWindowW(w!("actl_signal_window"), None) {
            let mut pid = 0;
            GetWindowThreadProcessId(old, Some(&mut pid));
            let process = windows::Win32::System::Threading::OpenProcess(
                windows::Win32::System::Threading::PROCESS_SYNCHRONIZE,
                false,
                pid,
            )?;
            let mut acknowledged = 0;
            let sent = SendMessageTimeoutW(
                old,
                REPLACE_CLOSE,
                WPARAM(0),
                LPARAM(0),
                SMTO_ABORTIFHUNG | SMTO_BLOCK,
                2000,
                Some(&mut acknowledged),
            );
            let waited = if sent.0 != 0 && acknowledged == 1 {
                windows::Win32::System::Threading::WaitForSingleObject(process, 4000)
            } else {
                WAIT_FAILED
            };
            let _ = CloseHandle(process);
            if waited != WAIT_OBJECT_0 {
                return Err(windows::core::Error::new(
                    windows::core::HRESULT(0x80004005u32 as i32),
                    "display replacement not acknowledged or exit timed out; old builds require explicit user close (stop state preserved)",
                ));
            }
        }
    }
    struct Instance(HANDLE);
    impl Drop for Instance {
        fn drop(&mut self) {
            unsafe {
                let _ = CloseHandle(self.0);
            }
        }
    }
    let instance_lock = Instance(windows::Win32::System::Threading::CreateMutexW(
        None,
        false,
        w!("Local\\actl-display-instance"),
    )?);
    if GetLastError() == ERROR_ALREADY_EXISTS {
        return Ok(());
    }
    let _instance_lock = instance_lock;
    let _ = windows::Win32::UI::HiDpi::SetProcessDpiAwarenessContext(
        windows::Win32::UI::HiDpi::DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2,
    );
    if FindWindowW(w!("actl_signal_window"), None).is_ok() {
        return Ok(());
    }
    let instance = GetModuleHandleW(None)?;
    let class = w!("actl_signal_window");
    let wc = WNDCLASSW {
        lpfnWndProc: Some(proc),
        hInstance: instance.into(),
        hCursor: LoadCursorW(None, IDC_ARROW)?,
        lpszClassName: class,
        ..Default::default()
    };
    if RegisterClassW(&wc) == 0 {
        return Err(windows::core::Error::from_thread());
    }
    let mut work = RECT::default();
    let _ = SystemParametersInfoW(
        SPI_GETWORKAREA,
        0,
        Some((&mut work as *mut RECT).cast()),
        SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS(0),
    );
    let position = std::fs::read_to_string(SignalPaths::default().dir.join("signal-pos.txt"))
        .ok()
        .and_then(|s| {
            let (x, y) = s.split_once(',')?;
            Some((x.parse::<i32>().ok()?, y.parse::<i32>().ok()?))
        });
    let (x, y) = position.unwrap_or((work.right - 320, work.top + 16));
    let hwnd = CreateWindowExW(
        WS_EX_TOPMOST | WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE | WS_EX_LAYERED,
        class,
        w!("actl"),
        WS_POPUP,
        x,
        y,
        280,
        34,
        None,
        None,
        Some(instance.into()),
        None,
    )?;
    // actl screenshots use an acknowledged hide; human screen sharing may show the strip.
    place(hwnd);
    if let Ok(mut c) = CURSOR.lock() {
        *c = Some(ActionCursor::new(unix_ms()));
    }
    let mut nid = tray(hwnd);
    nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
    nid.uCallbackMessage = WM_APP;
    nid.hIcon = update_icon(hwnd, model::Mode::Idle);
    let _ = Shell_NotifyIconW(NIM_ADD, &nid);
    HOTKEY.store(
        RegisterHotKey(Some(hwnd), 1, MOD_CONTROL | MOD_ALT, VK_F12.0 as u32).is_ok(),
        Ordering::Relaxed,
    );
    let _ = ShowWindow(hwnd, SW_SHOWNOACTIVATE);
    tick(hwnd);
    let _ = ShowWindow(hwnd, SW_SHOWNOACTIVATE);
    SetTimer(Some(hwnd), 1, 100, None);
    if std::env::args().any(|a| a == "--menu") {
        menu(hwnd);
    }
    let _input_watch = super::handoff::watch()?;
    let mut msg = MSG::default();
    loop {
        let result = GetMessageW(&mut msg, None, 0, 0).0;
        if result == -1 {
            return Err(windows::core::Error::from_thread());
        }
        if result == 0 {
            break;
        }
        if !super::popup::dialog_message(&msg) {
            let _ = TranslateMessage(&msg);
            DispatchMessageW(&msg);
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn revoke_message_does_not_collide_with_handoff_or_close() {
        for message in [
            actl_core::handoff::REQUEST_MESSAGE,
            actl_core::handoff::CHECK_MESSAGE,
            actl_core::handoff::RECOVER_MESSAGE,
            REPLACE_CLOSE,
            CAPTURE_HIDE,
            CAPTURE_RESTORE,
        ] {
            assert_ne!(crate::flow_start::MESSAGE, message);
        }
    }
    #[test]
    fn replacement_must_not_send_user_close() {
        let source = include_str!("shell.rs");
        let replacement = source.split("pub unsafe fn run()").nth(1).unwrap();
        let replacement = replacement.split("struct Instance").next().unwrap();
        assert!(
            !replacement.contains("PostMessageW(Some(old), WM_CLOSE"),
            "replacement must not take the user-close path that sets emergency stop"
        );
    }
    #[test]
    fn ordinary_close_does_not_create_stop_and_preserves_existing_stop() {
        let dir = std::env::temp_dir().join(actl_core::snapshot::new_snapshot_id());
        let paths = SignalPaths::at(dir.clone());
        assert!(allow_close(&paths, REPLACE_CLOSE, Some(false)));
        assert!(!paths.stop_requested());
        assert!(!allow_close(&paths, REPLACE_CLOSE, Some(true)));
        assert!(!allow_close(&paths, REPLACE_CLOSE, None));
        assert!(!paths.stop_requested());
        assert!(allow_close(&paths, WM_CLOSE, Some(true)));
        assert!(
            !paths.stop_requested(),
            "ordinary close must not create sticky emergency stop"
        );
        paths.request_stop().unwrap();
        let original = std::fs::read(paths.stop_file()).unwrap();
        assert!(allow_close(&paths, REPLACE_CLOSE, Some(false)));
        assert_eq!(std::fs::read(paths.stop_file()).unwrap(), original);
        std::fs::remove_dir_all(dir).unwrap();
    }
    #[test]
    fn empty_stop_file_still_means_stop_requested() {
        let dir = std::env::temp_dir().join(format!(
            "actl-stop-{}",
            actl_core::snapshot::new_snapshot_id()
        ));
        let paths = SignalPaths::at(dir.clone());
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(paths.stop_file(), "").unwrap();
        let observed = stop_timestamp(&paths);
        let _ = std::fs::remove_dir_all(dir);
        assert!(observed.is_some());
    }
}