knotra 0.34.0

knotra: multi-project VCS dashboard and bulk-operation tool.
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
//! 4. Conflict resolve panel (right-docked sheet) — RFC-037 Stage 2.
//!
//! Migrated off the hand-rolled `container`/`row`/`button(text("✕"))` chrome
//! onto RFC-034's overlay-host primitives (D1/D4). `modal_shell` was never
//! this file's shell — `resolve_panel` built its own — so nothing here
//! called it and nothing here needs to stop calling it.
//!
//! **`view.rs`'s `ActiveModal::Resolve` arm — its
//! `Sheet::new(el).at(SheetEdge::End).with_size(SheetSize::Half)` — is
//! untouched** (R7/D4: conflict resolution stays a sheet, not a dialog) —
//! this file only changes what `resolve_panel` returns *into* that mount
//! point, not how it is mounted.

use iced::{
    Alignment, Element, Length,
    widget::{Space, column, container, row, text},
};

use knotra_ui::widget::{
    BUTTON_HEIGHT, NoticeTone, Tokens, Tone, body_badge_diameter, centered_button_label, list,
    notice,
    overlay::{OverlayWidth, surface},
    reasoned, status_badge, status_symbol, style,
};
use knotra_vcs::{ProjectId, VcsKind};

use crate::{
    message::{ConflictOpsMessage, Message},
    state::{
        AppState,
        focus::{FocusTarget, LayerFocus},
    },
};

/// Stable keys for this panel's non-per-file `FocusTarget`s (RFC-066 D2).
/// Per-file controls are keyed dynamically (`resolve.file.{index}.*`) since
/// their count varies with the conflict.
mod focus_target {
    pub const CLOSE: &str = "resolve.close";
    pub const FOOTER_CLOSE: &str = "resolve.footer_close";
    pub const DETAILS_TOGGLE: &str = "resolve.details_toggle";
    pub const STOP_ATTEMPT: &str = "resolve.stop_attempt";
    pub const CONFIRM_KEEP: &str = "resolve.confirm_keep";
    pub const CONFIRM_STOP: &str = "resolve.confirm_stop";
}

/// RFC-066 D2: this panel's focus order and entry, for whichever phase
/// `project_id`'s conflict is in. Mirrors `resolve_panel`'s own phase
/// structure exactly (`ConfirmStop` first, then `Operating`/`Done`/the
/// file-list fallback), since the order can only be right if it is built
/// from the same facts the view itself branches on.
pub fn layer_focus(state: &AppState, project_id: &ProjectId) -> LayerFocus<Message> {
    use crate::state::conflict_ops::ConflictPhase;

    let ops = &state.conflict_ops;

    if let ConflictPhase::ConfirmStop { project_id: id, .. } = &ops.phase
        && id == project_id
    {
        let keep = FocusTarget::control(focus_target::CONFIRM_KEEP);
        return LayerFocus {
            order: vec![
                (
                    FocusTarget::control(focus_target::CLOSE),
                    Some(Message::ConflictOps(ConflictOpsMessage::StopCancelled)),
                ),
                (
                    keep.clone(),
                    Some(Message::ConflictOps(ConflictOpsMessage::StopCancelled)),
                ),
                (
                    FocusTarget::control(focus_target::CONFIRM_STOP),
                    Some(Message::ConflictOps(
                        ConflictOpsMessage::AbortMergeRequested(project_id.clone()),
                    )),
                ),
            ],
            entry: Some(keep),
        };
    }

    let close_msg = close_message(state, project_id).map(Message::ConflictOps);
    let close_target = FocusTarget::control(focus_target::CLOSE);
    let footer_close_target = FocusTarget::control(focus_target::FOOTER_CLOSE);

    match &ops.phase {
        ConflictPhase::Operating { project_id: id, .. } if id == project_id => LayerFocus {
            order: Vec::new(),
            entry: None,
        },
        ConflictPhase::Done { project_id: id, .. } if id == project_id => {
            let details_target = FocusTarget::control(focus_target::DETAILS_TOGGLE);
            LayerFocus {
                order: vec![
                    (close_target, close_msg.clone()),
                    (details_target, Some(Message::ToggleOpDetails)),
                    (footer_close_target.clone(), close_msg),
                ],
                entry: Some(footer_close_target),
            }
        }
        _ => {
            let detail = match &ops.phase {
                ConflictPhase::Browsing {
                    project_id: id,
                    detail,
                } if id == project_id => Some(detail),
                _ => ops.cached.get(project_id),
            };
            let vcs_kind = conflict_vcs_kind_for_project(state, project_id);
            let editor_configured = state.config.external_editor.is_some();
            let merge_tool_configured = state.config.external_merge_tool.is_some();
            let abort_supported =
                vcs_kind == Some(VcsKind::Git) && project_has_git_merge_state(state, project_id);

            let mut order = vec![(close_target, close_msg.clone())];
            let mut first_file_target = None;

            if let Some(detail) = detail {
                for (i, f) in detail.conflicted_files.iter().enumerate() {
                    let editor_target =
                        FocusTarget::control_dynamic(format!("resolve.file.{i}.open_editor"));
                    let editor_msg = editor_configured.then_some(Message::ConflictOps(
                        ConflictOpsMessage::OpenInEditorRequested(f.path.clone()),
                    ));
                    if first_file_target.is_none() {
                        first_file_target = Some(editor_target.clone());
                    }
                    order.push((editor_target, editor_msg));

                    let tool_target =
                        FocusTarget::control_dynamic(format!("resolve.file.{i}.open_tool"));
                    let tool_msg = merge_tool_configured.then_some(Message::ConflictOps(
                        ConflictOpsMessage::OpenInMergeToolRequested(f.path.clone()),
                    ));
                    order.push((tool_target, tool_msg));

                    // Mirrors `third_slot_for`'s own classification — Git
                    // gets a real "Mark done" control, Jujutsu/no-evidence
                    // get prose, which is not a Tab stop.
                    if vcs_kind == Some(VcsKind::Git) {
                        let mark_target =
                            FocusTarget::control_dynamic(format!("resolve.file.{i}.mark_done"));
                        order.push((
                            mark_target,
                            Some(Message::ConflictOps(
                                ConflictOpsMessage::MarkResolvedRequested {
                                    project_id: project_id.clone(),
                                    file_path: f.path.clone(),
                                },
                            )),
                        ));
                    }
                }
            }

            if abort_supported {
                let stop_reason = stop_control_reason(state);
                order.push((
                    FocusTarget::control(focus_target::STOP_ATTEMPT),
                    stop_reason.is_none().then_some(Message::ConflictOps(
                        ConflictOpsMessage::StopRequested(project_id.clone()),
                    )),
                ));
            }
            order.push((footer_close_target.clone(), close_msg));

            let entry = first_file_target.or(Some(footer_close_target));
            LayerFocus { order, entry }
        }
    }
}

/// RFC-067: the message the panel's own Close controls, Escape and the
/// scrim all send for the given project's Resolve panel right now — `None`
/// while `Operating` (R5, unchanged), `StopCancelled` in `ConfirmStop`
/// (cancelling the confirmation means "keep fixing", not "close the
/// panel"), `PanelClosed` otherwise. `pub(crate)`: `app/focus_ops.rs`'s
/// `close_topmost_layer` calls this directly, so Escape and the scrim stay
/// exactly in step with this panel's own Close button and header close
/// without a second, independently-maintained copy of this match.
pub(crate) fn close_message(
    state: &AppState,
    project_id: &ProjectId,
) -> Option<ConflictOpsMessage> {
    match &state.conflict_ops.phase {
        crate::state::conflict_ops::ConflictPhase::Operating { .. } => None,
        crate::state::conflict_ops::ConflictPhase::ConfirmStop { project_id: id, .. }
            if id == project_id =>
        {
            Some(ConflictOpsMessage::StopCancelled)
        }
        _ => Some(ConflictOpsMessage::PanelClosed),
    }
}

pub fn resolve_panel<'a>(state: &'a AppState, project_id: &'a ProjectId) -> Element<'a, Message> {
    let tokens = &state.theme.tokens;
    let name = project_name_for(state, project_id);
    let ops = &state.conflict_ops;
    let vcs_kind = conflict_vcs_kind_for_project(state, project_id);
    let git_actions_supported = vcs_kind == Some(VcsKind::Git);
    let abort_supported = git_actions_supported && project_has_git_merge_state(state, project_id);
    let editor_configured = state.config.external_editor.is_some();
    let merge_tool_configured = state.config.external_merge_tool.is_some();

    // R5: the one invariant this migration must not drop. `close_msg` is
    // `None` while `Operating` — both the header close (via `surface`'s own
    // `on_close`) and the footer Close button below key off this same
    // value, so neither can ever be pressable during a non-cancellable
    // phase. See the review request for how this was checked.
    //
    // RFC-067: `ConfirmStop` is cancellable, but cancelling it must mean
    // "keep fixing", not "close the panel" — so the header close and
    // (through Task 095) Escape and the scrim all send `StopCancelled`
    // here, the same message "Keep fixing" sends. `close_message` is a
    // standalone function, not inlined here, because `focus_ops.rs`'s
    // `close_topmost_layer` needs the identical phase-dependent choice for
    // Escape/the scrim — one function computing it, not two copies that
    // could drift apart.
    let close_msg = close_message(state, project_id).map(Message::ConflictOps);

    // RFC-067: the confirmation replaces the whole panel body, including
    // the file-list instruction line above it — that instruction ("Open
    // each file...") describes the step this phase is not in. Both the
    // footer (Keep fixing / Stop and discard edits) and the content differ
    // from every other phase, so this is handled before the shared
    // content/footer construction below rather than folded into it.
    if let crate::state::conflict_ops::ConflictPhase::ConfirmStop { project_id: id, .. } =
        &ops.phase
        && id == project_id
    {
        let body = column![
            text(state.t("plain.resolve.stop_confirm_title"))
                .size(snora::design::style::text::title_size(tokens)),
            text(state.t("plain.resolve.stop_confirm_body"))
                .size(snora::design::style::text::body_size(tokens))
                .line_height(snora::design::style::text::body_line_height(tokens)),
        ]
        .spacing(14);

        let footer = row![
            Space::new().width(Length::Fill),
            styled_button(
                tokens,
                state.t("plain.resolve.stop_confirm_keep"),
                Some(Message::ConflictOps(ConflictOpsMessage::StopCancelled)),
                crate::app::ring(state, &FocusTarget::control(focus_target::CONFIRM_KEEP)),
                style::ghost,
            ),
            styled_button(
                tokens,
                state.t("plain.resolve.stop_confirm_stop"),
                Some(Message::ConflictOps(
                    ConflictOpsMessage::AbortMergeRequested(project_id.clone(),)
                )),
                crate::app::ring(state, &FocusTarget::control(focus_target::CONFIRM_STOP)),
                style::danger,
            ),
        ]
        .align_y(Alignment::Center)
        .spacing(8);

        return surface(
            tokens,
            OverlayWidth::Large.resolve(state.window_width),
            format!("{} — {}", state.t("plain.resolve.title"), name),
            close_msg,
            crate::app::ring(state, &FocusTarget::control(focus_target::CLOSE)),
            body,
            footer,
        );
    }

    let content: Element<'_, Message> = match &ops.phase {
        crate::state::conflict_ops::ConflictPhase::Loading(id) if id == project_id => {
            text(state.t("plain.resolve.loading"))
                .size(snora::design::style::text::body_size(tokens))
                .line_height(snora::design::style::text::body_line_height(tokens))
                .into()
        }
        crate::state::conflict_ops::ConflictPhase::Operating {
            project_id: id,
            action,
        } if id == project_id => column![
            text(action)
                .size(snora::design::style::text::body_size(tokens))
                .line_height(snora::design::style::text::body_line_height(tokens)),
            // RFC-071 Amendment A3 D14: explains what's happening while an
            // operation runs — a status, read to understand.
            text(state.t("plain.resolve.working_hint"))
                .size(snora::design::style::text::body_size(tokens))
                .line_height(snora::design::style::text::body_line_height(tokens)),
        ]
        .spacing(8)
        .into(),
        crate::state::conflict_ops::ConflictPhase::Done {
            project_id: id,
            success,
            message,
            result,
        } if id == project_id => {
            // `title` was removed because it duplicated `message` (both
            // derived from `*success`), not because tone supplies the
            // distinction — snora withdrew that claim in 0.41.1; `Notice`'s
            // tone is colour-only. The non-colour channel is `message`
            // itself: a distinct catalog string per outcome
            // (`plain.resolve.done`/`plain.resolve.failed`, both locales),
            // which tone only reinforces.
            let tone = if *success {
                NoticeTone::Success
            } else {
                NoticeTone::Danger
            };
            let banner = notice(tokens, tone, None, message.as_str(), None);

            let details_label = if state.show_op_details {
                state.t("plain.hide_details")
            } else {
                state.t("plain.show_details")
            };
            let mut result_col = column![
                banner,
                styled_button(
                    tokens,
                    details_label,
                    Some(Message::ToggleOpDetails),
                    crate::app::ring(state, &FocusTarget::control(focus_target::DETAILS_TOGGLE)),
                    style::ghost,
                ),
            ]
            .spacing(8);

            // RFC-071 Amendment A3 D14: moved to `body` — raw tool
            // diagnostics (command/stdout/stderr/error) are read in order to
            // understand why an operation failed, the text a user opened
            // "Show details" to see. D14's `body_small` bucket is incidental
            // metadata only (timestamps, VCS kind, branch, counts, keyboard
            // hints); sitting behind a disclosure toggle is not that —
            // `text_outside_catalog_guard.rs`'s export-text exception is
            // about i18n routing, not text size, and does not apply here.
            if state.show_op_details
                && let Some(result) = result
            {
                for command in &result.commands_executed {
                    result_col = result_col.push(
                        text(format!("command: {command}"))
                            .size(snora::design::style::text::body_size(tokens))
                            .line_height(snora::design::style::text::body_line_height(tokens)),
                    );
                }
                if !result.stdout.is_empty() {
                    result_col =
                        result_col.extend(labelled_lines(tokens, "stdout: ", &result.stdout));
                }
                if !result.stderr.is_empty() {
                    result_col =
                        result_col.extend(labelled_lines(tokens, "stderr: ", &result.stderr));
                }
                if let Some(error) = &result.error_message {
                    result_col = result_col.push(
                        text(format!("error: {error}"))
                            .size(snora::design::style::text::body_size(tokens))
                            .line_height(snora::design::style::text::body_line_height(tokens)),
                    );
                }
            }

            result_col.into()
        }
        _ => {
            let detail = match &ops.phase {
                crate::state::conflict_ops::ConflictPhase::Browsing {
                    project_id: id,
                    detail,
                } if id == project_id => Some(detail),
                _ => ops.cached.get(project_id),
            };

            if let Some(detail) = detail {
                if detail.conflicted_files.is_empty() {
                    text(state.t("plain.resolve.no_files"))
                        .size(snora::design::style::text::body_size(tokens))
                        .line_height(snora::design::style::text::body_line_height(tokens))
                        .into()
                } else {
                    let file_rows: Vec<Element<'_, Message>> = detail
                        .conflicted_files
                        .iter()
                        .enumerate()
                        .map(|(i, f)| {
                            let editor_target = FocusTarget::control_dynamic(format!(
                                "resolve.file.{i}.open_editor"
                            ));
                            let tool_target =
                                FocusTarget::control_dynamic(format!("resolve.file.{i}.open_tool"));
                            let mark_target =
                                FocusTarget::control_dynamic(format!("resolve.file.{i}.mark_done"));
                            let editor_reason = (!editor_configured)
                                .then_some(state.t("plain.resolve.editor_not_configured"));
                            let open_editor_msg =
                                editor_configured.then_some(Message::ConflictOps(
                                    ConflictOpsMessage::OpenInEditorRequested(f.path.clone()),
                                ));
                            let merge_tool_reason = (!merge_tool_configured)
                                .then_some(state.t("plain.resolve.merge_tool_not_configured"));
                            let open_merge_tool_msg =
                                merge_tool_configured.then_some(Message::ConflictOps(
                                    ConflictOpsMessage::OpenInMergeToolRequested(f.path.clone()),
                                ));

                            // Task 097: a path and up to three buttons
                            // cannot share one line at the panel's
                            // narrowest width (seen at 1250px, the panel
                            // half that) — the path's `Fill` share shrank
                            // towards zero and iced does not clip text, so
                            // the path was drawn over "Open in editor",
                            // and "Mark done" was squeezed enough that its
                            // label (「完了として」 in Japanese) ran outside
                            // the button. So the name now has its own
                            // line, full width, wrapping rather than
                            // sharing space with anything:
                            // RFC-073 handoff Amendment A3 §1: an
                            // unresolved file is `danger`/alert, the same
                            // badge vocabulary every other status uses now,
                            // not a plain-text "!".
                            let line1 = row![
                                status_badge(
                                    tokens,
                                    Tone::Danger,
                                    &status_symbol::alert(),
                                    body_badge_diameter(tokens)
                                ),
                                text(&f.path)
                                    .size(snora::design::style::text::body_size(tokens))
                                    .line_height(snora::design::style::text::body_line_height(
                                        tokens
                                    ))
                                    .width(Length::Fill),
                            ]
                            .align_y(Alignment::Center)
                            .spacing(6);

                            // Line 2: indented 28px — the 22px glyph column
                            // plus line 1's own 6px spacing before the
                            // path — the same indent the prose line below
                            // already used, so both read as belonging to
                            // this file rather than as new list items.
                            // `.wrap()` (iced 0.14's `Row::wrap`): a button
                            // that does not fit moves to the next line
                            // instead of being squeezed, and every wrapped
                            // line inherits this row's own width (the
                            // panel's width minus the 28px indent), so
                            // continuation lines stay indented too.
                            // Amendment A5 §6: each button's own reason
                            // text ("Choose … in Settings first.") must
                            // wrap at its own column's width, not run on
                            // at its own natural single-line length — the
                            // `reasoned()` primitive itself stays
                            // `Length::Shrink` (every one of its nine
                            // other call sites across the app relies on
                            // that, most beside a `Space::new().width(
                            // Length::Fill)` spacer that would otherwise
                            // be squeezed), so each one is wrapped in its
                            // own `container(...).width(Length::Fill)`
                            // here instead — local to this one row.
                            // iced's layout threads the container's
                            // resolved width down as the content's own
                            // bound regardless of the content's `Shrink`
                            // policy, so the reason text wraps to it; two
                            // equal `Fill` columns split the row evenly,
                            // with this row's own `spacing(6)` as the gap
                            // between them.
                            let mut line2_controls = row![
                                // RFC-037 Stage 6: migrated onto the shared
                                // `reasoned` primitive. `style::secondary`
                                // matches the third slot's own weight when
                                // it is a button — both are peripheral row
                                // actions, not this phase's one completing
                                // action.
                                container(reasoned(
                                    tokens,
                                    state.t("plain.resolve.open_editor"),
                                    open_editor_msg,
                                    editor_reason,
                                    crate::app::ring(state, &editor_target),
                                    style::secondary,
                                ))
                                .width(Length::Fill),
                                // Handoff 058: same control, same weight,
                                // same reasoned-disabled shape as the editor
                                // button beside it — an editor and a merge
                                // tool are different jobs, always shown,
                                // each with its own reason when
                                // unconfigured, rather than the row's shape
                                // depending on Settings.
                                container(reasoned(
                                    tokens,
                                    state.t("plain.resolve.open_merge_tool"),
                                    open_merge_tool_msg,
                                    merge_tool_reason,
                                    crate::app::ring(state, &tool_target),
                                    style::secondary,
                                ))
                                .width(Length::Fill),
                            ]
                            .align_y(Alignment::Start)
                            .spacing(6);

                            let mut file_col = column![line1].spacing(4);
                            match third_slot_for(
                                state,
                                tokens,
                                vcs_kind,
                                project_id,
                                &f.path,
                                crate::app::ring(state, &mark_target),
                            ) {
                                ThirdSlot::Button(control) => {
                                    line2_controls = line2_controls.push(control);
                                    file_col = file_col.push(row![
                                        Space::new().width(Length::Fixed(28.0)),
                                        line2_controls.wrap(),
                                    ]);
                                }
                                ThirdSlot::Prose(sentence) => {
                                    file_col = file_col.push(row![
                                        Space::new().width(Length::Fixed(28.0)),
                                        line2_controls.wrap(),
                                    ]);
                                    // RFC-054 D3: the same 28px indent as
                                    // line 2, on its own full-width line
                                    // below it. Wraps rather than truncates
                                    // (§4): the sentence is the thing the
                                    // user needs to read, and this line
                                    // exists to give it room — `text`'s
                                    // default behaviour under a constrained
                                    // `Fill` width already wraps, so no
                                    // extra configuration is needed to get
                                    // that.
                                    // RFC-071 Amendment A3 D14: this
                                    // sentence is the completing action for
                                    // a Jujutsu project — the prose
                                    // equivalent of the "Mark done" button
                                    // Git rows show beside it, so it is
                                    // `body`, read to act, not metadata.
                                    file_col = file_col.push(row![
                                        Space::new().width(Length::Fixed(28.0)),
                                        text(sentence)
                                            .size(snora::design::style::text::body_size(tokens))
                                            .line_height(
                                                snora::design::style::text::body_line_height(
                                                    tokens
                                                )
                                            )
                                            .width(Length::Fill),
                                    ]);
                                }
                            }
                            file_col.into()
                        })
                        .collect();
                    // No inner `scrollable` here (unlike the original) —
                    // `surface`'s own body scrollable covers the whole
                    // `content` slot; nesting a second independently
                    // scrollable region inside it would be the same
                    // accessibility anti-pattern RFC-035 avoided elsewhere.
                    //
                    // RFC-071 D8a: the conflicted files are a list of
                    // items — one border line between adjacent files.
                    list(tokens, file_rows)
                }
            } else {
                text(state.t("plain.resolve.loading"))
                    .size(snora::design::style::text::body_size(tokens))
                    .line_height(snora::design::style::text::body_line_height(tokens))
                    .into()
            }
        }
    };

    // RFC-067 D2: danger-styled (this discards the user's edits) and
    // disabled with the standard busy reason while any operation holds the
    // lock — `smart_pull.rs`'s `start_reason` is the pattern this mirrors.
    // Sends `StopRequested`, not `AbortMergeRequested`: the first click
    // only opens the confirmation (D1); only "Stop and discard edits",
    // below, runs the abort.
    let stop_control: Element<'_, Message> =
        if abort_supported {
            let reason = stop_control_reason(state);
            reasoned(
                tokens,
                state.t("plain.resolve.stop_attempt"),
                reason.is_none().then_some(Message::ConflictOps(
                    ConflictOpsMessage::StopRequested(project_id.clone()),
                )),
                reason,
                crate::app::ring(state, &FocusTarget::control(focus_target::STOP_ATTEMPT)),
                style::danger,
            )
        } else {
            Space::new().width(Length::Fixed(0.0)).into()
        };

    let footer_close_focused =
        crate::app::ring(state, &FocusTarget::control(focus_target::FOOTER_CLOSE));
    let footer = row![
        stop_control,
        Space::new().width(Length::Fill),
        styled_button(
            tokens,
            state.t("action.close"),
            close_msg.clone(),
            footer_close_focused,
            style::ghost
        ),
    ]
    .align_y(Alignment::Center);

    let body = column![
        text(state.t("plain.resolve.instruction"))
            .size(snora::design::style::text::body_size(tokens))
            .line_height(snora::design::style::text::body_line_height(tokens)),
        content,
    ]
    .spacing(14);

    surface(
        tokens,
        // RFC-051 D4: was Small — 400px, chosen when each file row held one
        // control. It now holds up to three (editor, comparison tool,
        // mark-done or the Jujutsu hint), which need roughly 300px between
        // them; see the Handoff 070 review request for the row arithmetic
        // this floor was set against.
        OverlayWidth::Large.resolve(state.window_width),
        format!("{} — {}", state.t("plain.resolve.title"), name),
        close_msg,
        crate::app::ring(state, &FocusTarget::control(focus_target::CLOSE)),
        body,
        footer,
    )
}

/// RFC-073 handoff Amendment A3 §3: `content`'s own lines, the first
/// carrying `label` and every later one an indent of the same width — a
/// multi-line `stdout`/`stderr` (Git's "hint: " lines, for one) stays
/// aligned under the first line instead of starting at this panel's left
/// edge, the same bug `history.rs`'s stderr preview had. `label` is
/// `"stdout: "`/`"stderr: "`, both 8 characters, so one indent width serves
/// either. Split out from [`labelled_lines`] as its own pure function so
/// the indent rule is testable without building an `Element`.
fn labelled_line_strings(label: &str, content: &str) -> Vec<String> {
    let indent = " ".repeat(label.chars().count());
    content
        .lines()
        .enumerate()
        .map(|(i, line)| {
            let prefix = if i == 0 { label } else { indent.as_str() };
            format!("{prefix}{line}")
        })
        .collect()
}

/// [`labelled_line_strings`], each line its own `text`.
fn labelled_lines<'a>(
    tokens: &Tokens,
    label: &'static str,
    content: &str,
) -> Vec<Element<'a, Message>> {
    labelled_line_strings(label, content)
        .into_iter()
        .map(|line| {
            text(line)
                .size(snora::design::style::text::body_size(tokens))
                .line_height(snora::design::style::text::body_line_height(tokens))
                .into()
        })
        .collect()
}

/// A button styled with one of `knotra_ui::widget::style`'s semantic
/// functions plus a focus ring — the same shape `workspace_manager.rs`
/// (RFC-034 R9's validating migration) uses for every dialog button.
fn styled_button<'a>(
    tokens: &Tokens,
    label: &'a str,
    on_press: Option<Message>,
    is_focused: bool,
    style_fn: fn(&Tokens, iced::widget::button::Status) -> iced::widget::button::Style,
) -> Element<'a, Message> {
    let t = tokens.clone();
    iced::widget::button(centered_button_label(
        text(label)
            .size(snora::design::style::text::body_size(tokens))
            .line_height(snora::design::style::text::body_line_height(tokens)),
    ))
    .height(BUTTON_HEIGHT)
    .padding([0, 18])
    .on_press_maybe(on_press)
    .style(move |_theme, status| style::with_focus_ring(&t, is_focused, style_fn(&t, status)))
    .into()
}

/// RFC-054 D4: the conflicted-file row's third slot, as a value rather than
/// something implied by which match arm happened to build which widget.
/// Neither this project nor its reviewer can render this panel, so without
/// this enum "prose and buttons are laid out differently" would only be
/// checkable by looking at it — with it, R7's test asserts the
/// classification directly and only the pixels stay unverifiable.
enum ThirdSlot<'a> {
    /// Git: a real completing action, styled like `plain.resolve.open_editor`
    /// and `plain.resolve.open_merge_tool` beside it — stays inline on the
    /// row (D1/R1).
    Button(Element<'a, Message>),
    /// Jujutsu's hint or the no-evidence `None` case — a sentence, not a
    /// control. Owned rather than borrowed: `jj_finish_hint` builds it with
    /// `format!`, so there is no existing `&str` of the right lifetime to
    /// borrow (D4's "shape is yours" — this is the shape that avoids
    /// threading a lifetime through a value that is not always a borrow to
    /// begin with).
    Prose(String),
}

/// RFC-054 D4/R6/R7: the same exhaustive `Option<VcsKind>` match Handoff 065
/// built for `mark_control`, now returning the row's third-slot *kind*
/// rather than a pre-laid-out `Element` — `resolve_panel` decides where each
/// kind goes (D1-D3), this function only decides which kind a project gets.
/// Still exhaustive, still wildcard-free: a third `VcsKind` is a compile
/// error here exactly as it was before this handoff.
fn third_slot_for<'a>(
    state: &'a AppState,
    tokens: &Tokens,
    vcs_kind: Option<VcsKind>,
    project_id: &ProjectId,
    file_path: &str,
    is_focused: bool,
) -> ThirdSlot<'a> {
    match vcs_kind {
        Some(VcsKind::Git) => {
            // Task 098 Amendment A1: built with `reasoned`, the same
            // helper and style as its neighbours ("Open in editor",
            // "Open in comparison tool") — a bare `iced::widget::button`
            // with a literal `.height(36.0)` and an uncentred label used
            // to make this button shorter than those neighbours, with its
            // label sitting against the top edge (in Japanese,
            // 「完了としてマーク」 touched the border). `button_label_guard`
            // never saw it: the guard only knew the two height constants,
            // not a numeric literal.
            ThirdSlot::Button(reasoned(
                tokens,
                state.t("plain.resolve.mark_done"),
                Some(Message::ConflictOps(
                    ConflictOpsMessage::MarkResolvedRequested {
                        project_id: project_id.clone(),
                        file_path: file_path.to_owned(),
                    },
                )),
                None,
                is_focused,
                style::secondary,
            ))
        }
        // D1 deleted `ProjectConflictDetail.note`, the VCS layer's discarded
        // English sentence for this same slot — the completing action is
        // computed here instead, from `VcsKind` plus this row's own file
        // path, so it is never English-only prose baked into a record
        // (RFC-046 D1's contract).
        Some(VcsKind::Jujutsu) => ThirdSlot::Prose(jj_finish_hint(state, file_path)),
        // Handoff 065: no evidence either way — the pre-RFC-045 message,
        // generic and therefore not asserting anything that could be false,
        // rather than guessing a specific command that might be wrong.
        None => ThirdSlot::Prose(state.t("plain.resolve.unsupported").to_owned()),
    }
}

/// RFC-045 D2, Handoff 065: `Option<VcsKind>`, not a bare `VcsKind` — there
/// is no value in a two-variant enum that means "I could not tell," and this
/// function sometimes cannot tell. `None` covers exactly the cases the old
/// Git-only bool this replaced had no evidence for: a project in neither
/// `workspace_status` nor `workspace.projects`, and a project present in
/// `workspace.projects` whose path has neither a `.git` nor a `.jj` marker
/// (the state `missing_projects` tracks, `!VcsAdapter::repo_exists`,
/// RFC-046). `Some(Jujutsu)` is returned whenever `.jj` is a directory,
/// including a colocated repo that also has `.git` — a `.jj` marker is
/// still evidence, so this is not a no-evidence case. Every reachable
/// outcome now either matches the pre-RFC-045 boolean exactly (`Some(Git)`
/// where it was `true`, `Some(Jujutsu)`/`None` both where it was `false`,
/// collapsed there because that old bool never distinguished "confirmed
/// Jujutsu" from "unknown") or is the new distinction RFC-045 exists to
/// draw — with no appeal to how rarely the no-evidence path is reached,
/// because `mark_control`'s `None` arm renders the same generic,
/// nothing-asserted message the bool's `false` case always rendered.
fn conflict_vcs_kind_for_project(state: &AppState, project_id: &ProjectId) -> Option<VcsKind> {
    state
        .workspace_status
        .as_ref()
        .and_then(|ws| {
            ws.projects
                .iter()
                .find(|status| &status.project_id == project_id)
        })
        .map(|status| status.identity.vcs_kind)
        .or_else(|| {
            state
                .workspace
                .as_ref()
                .and_then(|ws| ws.projects.iter().find(|project| &project.id == project_id))
                .and_then(|project| {
                    let path = std::path::Path::new(&project.path);
                    if path.join(".jj").is_dir() {
                        Some(VcsKind::Jujutsu)
                    } else if path.join(".git").exists() {
                        Some(VcsKind::Git)
                    } else {
                        None
                    }
                })
        })
}

/// RFC-045 D2/D4: names the completing action with this row's own file
/// path — `jj resolve <path>`, not a generic sentence — in place of the
/// discarded `ProjectConflictDetail.note` (D1). `resolve` and `jj` are not
/// in `FORBIDDEN_EN`; extracted from the match arm that calls it so its
/// wording is a plain string comparison in tests rather than something only
/// checkable by rendering an `Element`.
fn jj_finish_hint(state: &AppState, file_path: &str) -> String {
    format!(
        "{} `jj resolve {file_path}`",
        state.t("plain.resolve.jj_finish_hint")
    )
}

/// RFC-067 R3: the disabled reason for "Stop this fix attempt" while the
/// operation lock is held elsewhere, `None` when it can be pressed —
/// extracted so this is assertable without rendering, the way
/// `third_slot_for` below already is. Mirrors `smart_pull.rs`'s
/// `start_reason`/`select_mode_reason` in `toolbar.rs`: a pure function
/// returning the same localized text the button would show, not a bool, so
/// a test reads the reason it actually gets rather than re-deriving it.
fn stop_control_reason(state: &AppState) -> Option<&str> {
    state
        .operation_interlock
        .is_busy()
        .then_some(state.t("plain.activity.busy"))
}

fn project_has_git_merge_state(state: &AppState, project_id: &ProjectId) -> bool {
    state
        .workspace
        .as_ref()
        .and_then(|ws| ws.projects.iter().find(|project| &project.id == project_id))
        .map(|project| {
            let path = std::path::Path::new(&project.path);
            path.join(".git").join("MERGE_HEAD").exists()
        })
        .unwrap_or(false)
}

/// Moved from `overlays/mod.rs` (RFC-037 Stage 6, `131` §5) — its only
/// caller is `resolve_panel`, above, in this same file. `mod.rs`'s own
/// Stage 1 doc comment mistakenly justified keeping it there as "used by
/// more than one overlay"; that was corrected in review `131` and the move
/// deferred to this stage so it wouldn't bury a relocation inside an
/// unverifiable migration diff.
fn project_name_for(state: &AppState, id: &ProjectId) -> String {
    state
        .workspace
        .as_ref()
        .and_then(|ws| ws.projects.iter().find(|p| &p.id == id))
        .map(|p| p.name.clone())
        .unwrap_or_else(|| id.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::AppConfig;
    use crate::state::OperationOwner;
    use knotra_vcs::{
        ConflictStatus, RemoteStatus, RepositoryIdentity, WorkingTreeStatus, WorkspaceStatus,
    };

    /// RFC-073 handoff Amendment A3 §3: a two-line stderr — the first line
    /// carries the label, the second gets an indent of the same width, not
    /// the left edge.
    #[test]
    fn labelled_line_strings_indents_every_line_after_the_first() {
        let lines =
            labelled_line_strings("stderr: ", "error: failed to push\nhint: try fetch first");
        assert_eq!(
            lines,
            vec![
                "stderr: error: failed to push".to_owned(),
                "        hint: try fetch first".to_owned(),
            ]
        );
    }

    fn status_for(project_id: ProjectId, vcs_kind: VcsKind) -> knotra_vcs::ProjectStatus {
        knotra_vcs::ProjectStatus {
            project_id,
            identity: RepositoryIdentity {
                path: "/tmp".into(),
                vcs_kind,
            },
            context: None,
            remote: RemoteStatus::default(),
            working_tree: WorkingTreeStatus::default(),
            conflict: ConflictStatus::default(),
            refreshed_at: jiff::Timestamp::now(),
            read_error: None,
        }
    }

    fn state_with_status(vcs_kind: VcsKind) -> (AppState, ProjectId) {
        let mut state = AppState::new(AppConfig::default());
        let project_id = ProjectId::new();
        state.workspace_status = Some(WorkspaceStatus {
            projects: vec![status_for(project_id.clone(), vcs_kind)],
            last_refresh: None,
        });
        (state, project_id)
    }

    /// RFC-045 D2/R6: this is the coverage half — a Jujutsu project's own
    /// recorded `VcsKind` must actually reach `conflict_vcs_kind_for_project`
    /// as `Jujutsu`, not merely happen to pair correctly with whichever arm
    /// a broken lookup fell into (the same coverage-vs-pairing distinction
    /// `062` drew for `label_en`).
    #[test]
    fn conflict_vcs_kind_for_project_reports_jujutsu_for_a_jj_project() {
        let (state, project_id) = state_with_status(VcsKind::Jujutsu);
        assert_eq!(
            conflict_vcs_kind_for_project(&state, &project_id),
            Some(VcsKind::Jujutsu)
        );
    }

    /// The Git-side counterpart — R3 requires Git rows stay unchanged, which
    /// starts with this refactored lookup still resolving Git projects to
    /// `VcsKind::Git` exactly as the bool it replaced did.
    #[test]
    fn conflict_vcs_kind_for_project_reports_git_for_a_git_project() {
        let (state, project_id) = state_with_status(VcsKind::Git);
        assert_eq!(
            conflict_vcs_kind_for_project(&state, &project_id),
            Some(VcsKind::Git)
        );
    }

    /// Handoff 065: a project the workspace knows about, but whose on-disk
    /// path has neither a `.git` nor a `.jj` marker — exactly what
    /// `missing_projects` tracks (RFC-046, `!VcsAdapter::repo_exists`), not
    /// a hypothetical. No `workspace_status` entry either, so this drives
    /// the fallback's on-disk check through a real empty directory rather
    /// than asserting the `None` branch in isolation.
    #[test]
    fn conflict_vcs_kind_for_project_reports_none_with_neither_marker_present() {
        let tmp = tempfile::TempDir::new().expect("tempdir");
        let mut state = AppState::new(AppConfig::default());
        let project = knotra_vcs::Project::new("svc", tmp.path().to_string_lossy());
        let project_id = project.id.clone();
        state.workspace = Some(knotra_vcs::Workspace {
            projects: vec![project],
            ..knotra_vcs::Workspace::new("Test")
        });

        assert_eq!(conflict_vcs_kind_for_project(&state, &project_id), None);
    }

    /// Handoff 065: a project absent from both `workspace_status` and
    /// `workspace.projects` — the other no-evidence case named in `154` §3.
    #[test]
    fn conflict_vcs_kind_for_project_reports_none_when_absent_from_both_sources() {
        let state = AppState::new(AppConfig::default());
        let project_id = ProjectId::new();

        assert_eq!(conflict_vcs_kind_for_project(&state, &project_id), None);
    }

    /// The content half: the jj hint names the completing action and this
    /// row's own file path, not a generic sentence — the thing `note`
    /// (RFC-046 D1, deleted here) used to say and nothing since replaced.
    #[test]
    fn jj_finish_hint_names_the_command_with_the_files_path() {
        let state = AppState::new(AppConfig::default());
        let hint = jj_finish_hint(&state, "src/lib.rs");
        assert_eq!(hint, "Finish with: `jj resolve src/lib.rs`");
    }

    /// RFC-054 D4/R7: the classification the whole RFC exists to make
    /// assertable. Without `ThirdSlot` as a value, "Git gets a button, jj
    /// and `None` get prose" is only checkable by rendering the panel,
    /// which nobody working on this project can do. `matches!` only checks
    /// the variant, not the inner content — the content itself
    /// (`plain.resolve.mark_done`'s label, the jj hint's exact text) is
    /// already covered by other tests; this one is purely about which kind
    /// each `Option<VcsKind>` produces.
    #[test]
    fn third_slot_is_a_button_for_git_and_prose_for_jujutsu_and_none() {
        let state = AppState::new(AppConfig::default());
        let tokens = &state.theme.tokens;
        let project_id = ProjectId::new();

        assert!(matches!(
            third_slot_for(
                &state,
                tokens,
                Some(VcsKind::Git),
                &project_id,
                "src/lib.rs",
                false
            ),
            ThirdSlot::Button(_)
        ));
        assert!(matches!(
            third_slot_for(
                &state,
                tokens,
                Some(VcsKind::Jujutsu),
                &project_id,
                "src/lib.rs",
                false
            ),
            ThirdSlot::Prose(_)
        ));
        assert!(matches!(
            third_slot_for(&state, tokens, None, &project_id, "src/lib.rs", false),
            ThirdSlot::Prose(_)
        ));
    }

    /// RFC-067 R3: idle, nothing holds the interlock — "Stop this fix
    /// attempt" can be pressed.
    #[test]
    fn stop_control_reason_is_none_when_idle() {
        let state = AppState::new(AppConfig::default());
        assert_eq!(stop_control_reason(&state), None);
    }

    /// RFC-067 R3: any operation holding the lock — not necessarily this
    /// panel's own — disables it with the standard busy reason, the same
    /// text `smart_pull.rs`'s own busy gate shows.
    #[test]
    fn stop_control_reason_is_busy_while_interlock_is_held() {
        let mut state = AppState::new(AppConfig::default());
        state
            .operation_interlock
            .try_acquire(OperationOwner::SmartPullExecution)
            .expect("acquire lease");

        assert_eq!(
            stop_control_reason(&state),
            Some(state.t("plain.activity.busy"))
        );
    }
}