reovim-module-window-ops 0.14.4

Window operations module for reovim
Documentation
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
//! Window operation command handlers.
//!
//! These commands implement window management operations (split, close, focus)
//! by delegating to the `CompositorApi` trait methods on `SessionRuntime`.
//!
//! # Architecture
//!
//! Following the kernel's "mechanism vs policy" principle:
//! - **Mechanism** (drivers): `CompositorApi` trait provides operations
//! - **Policy** (this module): Commands decide when/how to use those operations
//!
//! The compositor handles the actual layout logic; commands just invoke it.

use {
    reovim_driver_command::{
        ArgKind, ArgSpec, Command, CommandContext, CommandHandler, CommandResult,
    },
    reovim_driver_layout::{NavigateDirection, SplitDirection},
    reovim_driver_session::{CompositorApi, SessionRuntime},
    reovim_kernel::api::v1::CommandId,
};

use crate::ids;

// =============================================================================
// Focus Navigation Commands
// =============================================================================

/// Move focus to the window on the left.
#[derive(Debug, Clone, Copy, Default)]
pub struct FocusLeft;

impl Command for FocusLeft {
    fn id(&self) -> CommandId {
        ids::FOCUS_LEFT
    }

    fn description(&self) -> &'static str {
        "Move focus to left window"
    }

    fn args(&self) -> Vec<ArgSpec> {
        Vec::new()
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
impl CommandHandler for FocusLeft {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
        match runtime.navigate(NavigateDirection::Left) {
            Ok(window) => match runtime.focus(window) {
                Ok(()) => CommandResult::Success,
                Err(e) => CommandResult::Error(e.to_string()),
            },
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

/// Move focus to the window below.
#[derive(Debug, Clone, Copy, Default)]
pub struct FocusDown;

impl Command for FocusDown {
    fn id(&self) -> CommandId {
        ids::FOCUS_DOWN
    }

    fn description(&self) -> &'static str {
        "Move focus to window below"
    }

    fn args(&self) -> Vec<ArgSpec> {
        Vec::new()
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
impl CommandHandler for FocusDown {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
        match runtime.navigate(NavigateDirection::Down) {
            Ok(window) => match runtime.focus(window) {
                Ok(()) => CommandResult::Success,
                Err(e) => CommandResult::Error(e.to_string()),
            },
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

/// Move focus to the window above.
#[derive(Debug, Clone, Copy, Default)]
pub struct FocusUp;

impl Command for FocusUp {
    fn id(&self) -> CommandId {
        ids::FOCUS_UP
    }

    fn description(&self) -> &'static str {
        "Move focus to window above"
    }

    fn args(&self) -> Vec<ArgSpec> {
        Vec::new()
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
impl CommandHandler for FocusUp {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
        match runtime.navigate(NavigateDirection::Up) {
            Ok(window) => match runtime.focus(window) {
                Ok(()) => CommandResult::Success,
                Err(e) => CommandResult::Error(e.to_string()),
            },
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

/// Move focus to the window on the right.
#[derive(Debug, Clone, Copy, Default)]
pub struct FocusRight;

impl Command for FocusRight {
    fn id(&self) -> CommandId {
        ids::FOCUS_RIGHT
    }

    fn description(&self) -> &'static str {
        "Move focus to right window"
    }

    fn args(&self) -> Vec<ArgSpec> {
        Vec::new()
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
impl CommandHandler for FocusRight {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
        match runtime.navigate(NavigateDirection::Right) {
            Ok(window) => match runtime.focus(window) {
                Ok(()) => CommandResult::Success,
                Err(e) => CommandResult::Error(e.to_string()),
            },
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

// =============================================================================
// Focus Cycling Commands
// =============================================================================

/// Cycle focus to the next window.
#[derive(Debug, Clone, Copy, Default)]
pub struct FocusNext;

impl Command for FocusNext {
    fn id(&self) -> CommandId {
        ids::FOCUS_NEXT
    }

    fn description(&self) -> &'static str {
        "Cycle focus to next window"
    }

    fn args(&self) -> Vec<ArgSpec> {
        Vec::new()
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
impl CommandHandler for FocusNext {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
        match runtime.cycle(true) {
            Ok(window) => match runtime.focus(window) {
                Ok(()) => CommandResult::Success,
                Err(e) => CommandResult::Error(e.to_string()),
            },
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

/// Cycle focus to the previous window.
#[derive(Debug, Clone, Copy, Default)]
pub struct FocusPrev;

impl Command for FocusPrev {
    fn id(&self) -> CommandId {
        ids::FOCUS_PREV
    }

    fn description(&self) -> &'static str {
        "Cycle focus to previous window"
    }

    fn args(&self) -> Vec<ArgSpec> {
        Vec::new()
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
impl CommandHandler for FocusPrev {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
        match runtime.cycle(false) {
            Ok(window) => match runtime.focus(window) {
                Ok(()) => CommandResult::Success,
                Err(e) => CommandResult::Error(e.to_string()),
            },
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

// =============================================================================
// Window Splitting Commands
// =============================================================================

/// Split window horizontally (creates window below).
#[derive(Debug, Clone, Copy, Default)]
pub struct SplitHorizontal;

impl Command for SplitHorizontal {
    fn id(&self) -> CommandId {
        ids::SPLIT_HORIZONTAL
    }

    fn description(&self) -> &'static str {
        "Split window horizontally"
    }

    fn args(&self) -> Vec<ArgSpec> {
        Vec::new()
    }
}

impl CommandHandler for SplitHorizontal {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
        match runtime.split(SplitDirection::Horizontal) {
            Ok(_new_window) => CommandResult::Success,
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

/// Split window vertically (creates window to the right).
#[derive(Debug, Clone, Copy, Default)]
pub struct SplitVertical;

impl Command for SplitVertical {
    fn id(&self) -> CommandId {
        ids::SPLIT_VERTICAL
    }

    fn description(&self) -> &'static str {
        "Split window vertically"
    }

    fn args(&self) -> Vec<ArgSpec> {
        Vec::new()
    }
}

impl CommandHandler for SplitVertical {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
        match runtime.split(SplitDirection::Vertical) {
            Ok(_new_window) => CommandResult::Success,
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

// =============================================================================
// Window Closing Commands
// =============================================================================

/// Close the current window.
#[derive(Debug, Clone, Copy, Default)]
pub struct CloseWindow;

impl Command for CloseWindow {
    fn id(&self) -> CommandId {
        ids::CLOSE_WINDOW
    }

    fn description(&self) -> &'static str {
        "Close current window"
    }

    fn args(&self) -> Vec<ArgSpec> {
        Vec::new()
    }
}

impl CommandHandler for CloseWindow {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
        match runtime.close_current_window() {
            Ok(_neighbor) => CommandResult::Success,
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

/// Close all windows except the current one.
#[derive(Debug, Clone, Copy, Default)]
pub struct CloseOthers;

impl Command for CloseOthers {
    fn id(&self) -> CommandId {
        ids::CLOSE_OTHERS
    }

    fn description(&self) -> &'static str {
        "Close all other windows"
    }

    fn args(&self) -> Vec<ArgSpec> {
        Vec::new()
    }
}

impl CommandHandler for CloseOthers {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
        match runtime.close_others() {
            Ok(()) => CommandResult::Success,
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

/// Create new window with empty buffer.
#[derive(Debug, Clone, Copy, Default)]
pub struct SplitNew;

impl Command for SplitNew {
    fn id(&self) -> CommandId {
        ids::SPLIT_NEW
    }

    fn description(&self) -> &'static str {
        "Create new window with empty buffer"
    }

    fn args(&self) -> Vec<ArgSpec> {
        Vec::new()
    }
}

impl CommandHandler for SplitNew {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
        // Split vertically first to create the new window
        match runtime.split(SplitDirection::Vertical) {
            Ok(_new_window) => {
                // The new window will share the same buffer as the original.
                // To make it truly empty, we'd need BufferApi.create_buffer() +
                // set_window_buffer(), but for MVP just split is sufficient.
                CommandResult::Success
            }
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

// =============================================================================
// Window Resizing Commands
// =============================================================================

/// Default resize delta (in cells).
const RESIZE_DELTA: i16 = 1;

/// Increase window height.
#[derive(Debug, Clone, Copy, Default)]
pub struct ResizeHeightIncrease;

impl Command for ResizeHeightIncrease {
    fn id(&self) -> CommandId {
        ids::RESIZE_HEIGHT_INCREASE
    }

    fn description(&self) -> &'static str {
        "Increase window height"
    }

    fn args(&self) -> Vec<ArgSpec> {
        Vec::new()
    }
}

impl CommandHandler for ResizeHeightIncrease {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
        // Resize down direction expands height
        match runtime.resize(NavigateDirection::Down, RESIZE_DELTA) {
            Ok(()) => CommandResult::Success,
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

/// Decrease window height.
#[derive(Debug, Clone, Copy, Default)]
pub struct ResizeHeightDecrease;

impl Command for ResizeHeightDecrease {
    fn id(&self) -> CommandId {
        ids::RESIZE_HEIGHT_DECREASE
    }

    fn description(&self) -> &'static str {
        "Decrease window height"
    }

    fn args(&self) -> Vec<ArgSpec> {
        Vec::new()
    }
}

impl CommandHandler for ResizeHeightDecrease {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
        // Resize down direction with negative delta shrinks height
        match runtime.resize(NavigateDirection::Down, -RESIZE_DELTA) {
            Ok(()) => CommandResult::Success,
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

/// Increase window width.
#[derive(Debug, Clone, Copy, Default)]
pub struct ResizeWidthIncrease;

impl Command for ResizeWidthIncrease {
    fn id(&self) -> CommandId {
        ids::RESIZE_WIDTH_INCREASE
    }

    fn description(&self) -> &'static str {
        "Increase window width"
    }

    fn args(&self) -> Vec<ArgSpec> {
        Vec::new()
    }
}

impl CommandHandler for ResizeWidthIncrease {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
        // Resize right direction expands width
        match runtime.resize(NavigateDirection::Right, RESIZE_DELTA) {
            Ok(()) => CommandResult::Success,
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

/// Decrease window width.
#[derive(Debug, Clone, Copy, Default)]
pub struct ResizeWidthDecrease;

impl Command for ResizeWidthDecrease {
    fn id(&self) -> CommandId {
        ids::RESIZE_WIDTH_DECREASE
    }

    fn description(&self) -> &'static str {
        "Decrease window width"
    }

    fn args(&self) -> Vec<ArgSpec> {
        Vec::new()
    }
}

impl CommandHandler for ResizeWidthDecrease {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
        // Resize right direction with negative delta shrinks width
        match runtime.resize(NavigateDirection::Right, -RESIZE_DELTA) {
            Ok(()) => CommandResult::Success,
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

/// Equalize all window sizes.
#[derive(Debug, Clone, Copy, Default)]
pub struct ResizeEqual;

impl Command for ResizeEqual {
    fn id(&self) -> CommandId {
        ids::RESIZE_EQUAL
    }

    fn description(&self) -> &'static str {
        "Equalize all window sizes"
    }

    fn args(&self) -> Vec<ArgSpec> {
        Vec::new()
    }
}

impl CommandHandler for ResizeEqual {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
        match runtime.equalize() {
            Ok(()) => CommandResult::Success,
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

// =============================================================================
// Float Zone Commands
// =============================================================================

/// Toggle window between tiled and floating zones.
#[derive(Debug, Clone, Copy, Default)]
pub struct ToggleFloat;

impl Command for ToggleFloat {
    fn id(&self) -> CommandId {
        ids::TOGGLE_FLOAT
    }

    fn description(&self) -> &'static str {
        "Toggle window between tiled and floating"
    }

    fn args(&self) -> Vec<ArgSpec> {
        Vec::new()
    }
}

impl CommandHandler for ToggleFloat {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
        match runtime.toggle_float() {
            Ok(()) => CommandResult::Success,
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

/// Raise floating window to front.
#[derive(Debug, Clone, Copy, Default)]
pub struct RaiseFloat;

impl Command for RaiseFloat {
    fn id(&self) -> CommandId {
        ids::RAISE_FLOAT
    }

    fn description(&self) -> &'static str {
        "Raise floating window to front"
    }

    fn args(&self) -> Vec<ArgSpec> {
        Vec::new()
    }
}

impl CommandHandler for RaiseFloat {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
        match runtime.raise_float() {
            Ok(()) => CommandResult::Success,
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

/// Lower floating window to back.
#[derive(Debug, Clone, Copy, Default)]
pub struct LowerFloat;

impl Command for LowerFloat {
    fn id(&self) -> CommandId {
        ids::LOWER_FLOAT
    }

    fn description(&self) -> &'static str {
        "Lower floating window to back"
    }

    fn args(&self) -> Vec<ArgSpec> {
        Vec::new()
    }
}

impl CommandHandler for LowerFloat {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
        match runtime.lower_float() {
            Ok(()) => CommandResult::Success,
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

// =============================================================================
// Layer Opacity Commands (#400)
// =============================================================================

/// Set layer opacity to a specific percentage (0-100).
#[derive(Debug, Clone, Copy, Default)]
pub struct LayerOpacitySet;

impl Command for LayerOpacitySet {
    fn id(&self) -> CommandId {
        ids::LAYER_OPACITY_SET
    }

    fn description(&self) -> &'static str {
        "Set layer opacity (0-100)"
    }

    fn args(&self) -> Vec<ArgSpec> {
        vec![ArgSpec::required(
            "percentage",
            ArgKind::Count,
            "Opacity percentage (0-100)",
        )]
    }

    fn names(&self) -> &[&'static str] {
        &["LayerOpacity"]
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
impl CommandHandler for LayerOpacitySet {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, ctx: &CommandContext) -> CommandResult {
        let pct = ctx.count().unwrap_or(100);
        #[allow(clippy::cast_precision_loss)]
        let opacity = (pct as f32 / 100.0).clamp(0.0, 1.0);
        match runtime.set_active_layer_opacity(opacity) {
            Ok(()) => CommandResult::Success,
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

/// Increase layer opacity by 10%.
#[derive(Debug, Clone, Copy, Default)]
pub struct LayerOpacityIncrease;

impl Command for LayerOpacityIncrease {
    fn id(&self) -> CommandId {
        ids::LAYER_OPACITY_INCREASE
    }

    fn description(&self) -> &'static str {
        "Increase layer opacity by 10%"
    }

    fn names(&self) -> &[&'static str] {
        &["LayerOpacityUp"]
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
impl CommandHandler for LayerOpacityIncrease {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
        match runtime.adjust_active_layer_opacity(0.1) {
            Ok(_) => CommandResult::Success,
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

/// Decrease layer opacity by 10%.
#[derive(Debug, Clone, Copy, Default)]
pub struct LayerOpacityDecrease;

impl Command for LayerOpacityDecrease {
    fn id(&self) -> CommandId {
        ids::LAYER_OPACITY_DECREASE
    }

    fn description(&self) -> &'static str {
        "Decrease layer opacity by 10%"
    }

    fn names(&self) -> &[&'static str] {
        &["LayerOpacityDown"]
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
impl CommandHandler for LayerOpacityDecrease {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
        match runtime.adjust_active_layer_opacity(-0.1) {
            Ok(_) => CommandResult::Success,
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

// =============================================================================
// Tab Page Commands (#401)
// =============================================================================

/// Create a new tab page.
#[derive(Debug, Clone, Copy, Default)]
pub struct TabNew;

impl Command for TabNew {
    fn id(&self) -> CommandId {
        ids::TAB_NEW
    }

    fn description(&self) -> &'static str {
        "Create a new tab page"
    }

    fn names(&self) -> &[&'static str] {
        &["tabnew"]
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
impl CommandHandler for TabNew {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
        match runtime.tab_new() {
            Ok(_) => CommandResult::Success,
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

/// Close the current tab page.
#[derive(Debug, Clone, Copy, Default)]
pub struct TabClose;

impl Command for TabClose {
    fn id(&self) -> CommandId {
        ids::TAB_CLOSE
    }

    fn description(&self) -> &'static str {
        "Close the current tab page"
    }

    fn names(&self) -> &[&'static str] {
        &["tabclose", "tabc"]
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
impl CommandHandler for TabClose {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
        match runtime.tab_close() {
            Ok(()) => CommandResult::Success,
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

/// Switch to the next tab page.
#[derive(Debug, Clone, Copy, Default)]
pub struct TabNext;

impl Command for TabNext {
    fn id(&self) -> CommandId {
        ids::TAB_NEXT
    }

    fn description(&self) -> &'static str {
        "Switch to the next tab page"
    }

    fn names(&self) -> &[&'static str] {
        &["tabnext", "tabn"]
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
impl CommandHandler for TabNext {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
        match runtime.tab_next() {
            Ok(_) => CommandResult::Success,
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

/// Switch to the previous tab page.
#[derive(Debug, Clone, Copy, Default)]
pub struct TabPrev;

impl Command for TabPrev {
    fn id(&self) -> CommandId {
        ids::TAB_PREV
    }

    fn description(&self) -> &'static str {
        "Switch to the previous tab page"
    }

    fn names(&self) -> &[&'static str] {
        &["tabprev", "tabp"]
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
impl CommandHandler for TabPrev {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
        match runtime.tab_prev() {
            Ok(_) => CommandResult::Success,
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

/// Switch to a tab page by index.
#[derive(Debug, Clone, Copy, Default)]
pub struct TabGoto;

impl Command for TabGoto {
    fn id(&self) -> CommandId {
        ids::TAB_GOTO
    }

    fn description(&self) -> &'static str {
        "Switch to tab page by index"
    }

    fn args(&self) -> Vec<ArgSpec> {
        vec![ArgSpec::required(
            "index",
            ArgKind::Count,
            "Tab index (1-based)",
        )]
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
impl CommandHandler for TabGoto {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, ctx: &CommandContext) -> CommandResult {
        let index = ctx.count().unwrap_or(1);
        // Convert from 1-based (user) to 0-based (internal)
        match runtime.tab_goto(index.saturating_sub(1)) {
            Ok(_) => CommandResult::Success,
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

/// Move the current window to a new tab page.
///
/// Creates a new tab page and switches to it. In vim, `<C-w>T` moves the
/// current window to a new tab; here the new tab starts empty and the
/// compositor will assign the default window on next layout.
#[derive(Debug, Clone, Copy, Default)]
pub struct MoveToNewTab;

impl Command for MoveToNewTab {
    fn id(&self) -> CommandId {
        ids::MOVE_TO_NEW_TAB
    }

    fn description(&self) -> &'static str {
        "Move current window to new tab"
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
impl CommandHandler for MoveToNewTab {
    fn execute(&self, runtime: &mut SessionRuntime<'_>, _ctx: &CommandContext) -> CommandResult {
        match runtime.tab_new() {
            Ok(_) => CommandResult::Success,
            Err(e) => CommandResult::Error(e.to_string()),
        }
    }
}

// =============================================================================
// Command Collection
// =============================================================================

/// Returns all window operation command handlers.
///
/// Used by `WindowOps::command_handlers()` to register commands.
#[must_use]
pub fn all_commands() -> Vec<Box<dyn CommandHandler>> {
    vec![
        // Focus navigation
        Box::new(FocusLeft),
        Box::new(FocusDown),
        Box::new(FocusUp),
        Box::new(FocusRight),
        // Focus cycling
        Box::new(FocusNext),
        Box::new(FocusPrev),
        // Splitting
        Box::new(SplitHorizontal),
        Box::new(SplitVertical),
        Box::new(SplitNew),
        // Closing
        Box::new(CloseWindow),
        Box::new(CloseOthers),
        // Resizing
        Box::new(ResizeHeightIncrease),
        Box::new(ResizeHeightDecrease),
        Box::new(ResizeWidthIncrease),
        Box::new(ResizeWidthDecrease),
        Box::new(ResizeEqual),
        // Float zone
        Box::new(ToggleFloat),
        Box::new(RaiseFloat),
        Box::new(LowerFloat),
        // Layer opacity (#400)
        Box::new(LayerOpacitySet),
        Box::new(LayerOpacityIncrease),
        Box::new(LayerOpacityDecrease),
        // Tab pages (#401)
        Box::new(TabNew),
        Box::new(TabClose),
        Box::new(TabNext),
        Box::new(TabPrev),
        Box::new(TabGoto),
        Box::new(MoveToNewTab),
    ]
}

#[cfg(test)]
#[path = "command_tests.rs"]
mod tests;