paneru 0.5.2

A sliding, tiling window manager for MacOS.
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
use std::sync::mpsc::Receiver;
use std::time::Duration;

use bevy::MinimalPlugins;
use bevy::app::App as BevyApp;
use bevy::app::{First, Last, PostUpdate, PreUpdate, Startup};
use bevy::ecs::hierarchy::ChildOf;
use bevy::ecs::lifecycle::RemovedComponents;
use bevy::ecs::query::{Added, Changed, With};
use bevy::ecs::resource::Resource;
use bevy::ecs::schedule::SystemCondition;
use bevy::ecs::schedule::common_conditions::{not, resource_exists};
use bevy::ecs::schedule::{ScheduleLabel as _, SingleThreadedExecutor};
use bevy::ecs::system::{Commands, EntityCommands, Query, Res, SystemId};
use bevy::prelude::Event as BevyEvent;
use bevy::tasks::Task;
use bevy::time::Timer;
use bevy::time::common_conditions::on_timer;
use bevy::time::{Time, Virtual};
use bevy::{
    app::Update,
    ecs::{component::Component, entity::Entity, schedule::IntoScheduleConfigs},
};
use derive_more::{Deref, DerefMut};
use tracing::{Level, error, instrument, warn};

use crate::commands::register_commands;
use crate::config::snippet::SnippetDialect;
use crate::config::{CONFIGURATION_FILE, Config, InitialWindowWidth, WindowParams};
use crate::ecs::layout::LayoutStrip;
use crate::ecs::state::PaneruState;
use crate::errors::Result;
use crate::events::{Event, EventSender, InputEvent};
#[cfg(feature = "lua")]
use crate::lua;
use crate::manager::{
    Application, Origin, ProcessApi, Size, Window, WindowManager, WindowManagerApi, WindowManagerOS,
};
use crate::menubar::MenuBarManager;
use crate::overlay::{FlashMessageManager, OverlayManager};
use crate::platform::{Modifiers, PlatformCallbacks, WinID, WorkspaceId};

pub mod display;
pub mod focus;
pub mod layout;
#[cfg(feature = "lua")]
pub mod layout_ops;
pub mod mouse;
pub mod params;
pub(crate) mod restore;
pub mod script_state;
pub mod scroll;
pub mod state;
pub(crate) mod systems;
mod triggers;
pub mod workspace;

// Shared by the Lua reload system so a `paneru.setup{...}` reload applies the
// same menubar/passthrough side effects as a TOML reload.
#[cfg(feature = "lua")]
pub(crate) use triggers::apply_config_side_effects;

/// Registers the Bevy systems for the `WindowManager`.
/// This function adds various systems to the `Update` schedule, including event dispatchers,
/// process/application/window lifecycle management, animation, and periodic watchers.
///
/// # Arguments
///
/// * `app` - The Bevy application to register the systems with.
#[allow(clippy::too_many_lines)]
pub fn register_systems(app: &mut bevy::app::App) {
    const CLOSED_WINDOW_CHECK_FREQ: Duration = Duration::from_secs(1);
    const LOW_POWER_MODE_CHECK: Duration = Duration::from_mins(1);
    const APP_OBSERVABILITY_CHECK_FREQ: Duration = Duration::from_millis(200);

    let not_swiping = |scrolling: Query<&Scrolling, With<ActiveWorkspaceMarker>>| {
        scrolling
            .iter()
            .next()
            .is_none_or(|marker| !marker.is_user_swiping)
    };
    let dimming_enabled = |config: Option<Res<Config>>| {
        config
            .is_some_and(|config| config.has_dim_inactive_color() || config.border_active_window())
    };
    // The overlay must refresh not just when the active strip's layout changes,
    // but also whenever focus moves — including focus *loss* (e.g. switching to
    // an empty virtual workspace), which otherwise leaves a stale outline.
    // Position changes on the focused window also dirty the overlay so that
    // dragging a floating window moves the highlight with it.
    let vw_indicator_dirty =
        |strip_changed: Query<(), (With<ActiveWorkspaceMarker>, Changed<LayoutStrip>)>,
         focus_gained: Query<(), Added<FocusedMarker>>,
         workspace_changed: Query<(), Added<ActiveWorkspaceMarker>>,
         focused_moved: Query<(), (With<FocusedMarker>, Changed<Position>)>| {
            !strip_changed.is_empty()
                || !focus_gained.is_empty()
                || !workspace_changed.is_empty()
                || !focused_moved.is_empty()
        };
    // The menu bar additionally shows how many virtual workspaces exist, so it
    // has to redraw when one is created or reaped, neither of which touches the
    // active strip.
    let strip_count_changed =
        |added: Query<(), Added<LayoutStrip>>, mut removed: RemovedComponents<LayoutStrip>| {
            !added.is_empty() || removed.read().next().is_some()
        };
    let native_tabs_enabled =
        |config: Option<Res<Config>>| config.is_none_or(|config| config.native_tabs_enabled());

    app.add_systems(
        Startup,
        (
            systems::gather_displays,
            systems::gather_initial_processes,
            systems::initialise_workspaces,
        )
            .chain(),
    );
    // Registered with `add_message`, not `init_resource`, so the buffer is
    // double-buffered and dropped after a frame like any other message stream.
    app.add_message::<InputEvent>();
    app.add_systems(
        PreUpdate,
        (
            systems::pump_events,
            // Read creation notifications in the same frame that pumps them.
            // Otherwise an idle pump can wait up to its next timeout before
            // the first resize of a newly visible window.
            systems::window_creation_event.after(systems::pump_events),
            systems::demux_input_events.after(systems::pump_events),
        ),
    );
    app.add_systems(
        Update,
        (
            (
                triggers::apply_window_defaults,
                systems::detect_tabbed_windows.run_if(native_tabs_enabled),
                triggers::apply_window_positions,
            )
                .chain()
                .after(systems::finish_setup),
            (
                systems::add_existing_process,
                systems::add_existing_application,
                systems::finish_setup,
            )
                .chain()
                .run_if(resource_exists::<Initializing>),
            systems::add_launched_process.run_if(on_timer(APP_OBSERVABILITY_CHECK_FREQ)),
            systems::add_launched_application.run_if(on_timer(APP_OBSERVABILITY_CHECK_FREQ)),
            systems::fresh_marker_cleanup,
            systems::timeout_ticker,
            workspace::cleanup_unordered_windows
                .run_if(not(resource_exists::<Initializing>))
                .run_if(on_timer(CLOSED_WINDOW_CHECK_FREQ)),
            systems::regroup_stray_native_tabs
                .run_if(native_tabs_enabled)
                .run_if(not(resource_exists::<Initializing>))
                .run_if(on_timer(CLOSED_WINDOW_CHECK_FREQ)),
            systems::auto_discover_unmanaged_focused_windows,
            systems::retry_front_switch,
            systems::update_low_power_state
                .run_if(resource_exists::<LowPowerMode>)
                .run_if(on_timer(LOW_POWER_MODE_CHECK)),
            (
                systems::window_resized_update_frame,
                systems::window_moved_update_frame,
            )
                .chain()
                .run_if(not_swiping),
            systems::cleanup_on_exit,
            restore::tick_restore_grace,
            state::periodic_state_save.run_if(on_timer(Duration::from_mins(5))),
            state::cleanup_on_exit,
            script_state::periodic_script_state_save.run_if(on_timer(Duration::from_mins(5))),
            script_state::script_state_cleanup_on_exit,
        ),
    );
    app.add_systems(
        PostUpdate,
        (
            (
                systems::animate_entities,
                systems::commit_window_position.run_if(not(resource_exists::<Initializing>)),
                systems::verify_window_position.run_if(not(resource_exists::<Initializing>)),
            )
                .chain(),
            (
                systems::animate_resize_entities,
                systems::commit_window_size.run_if(not(resource_exists::<Initializing>)),
            )
                .chain(),
            (
                systems::update_overlays
                    .after(systems::animate_entities)
                    .after(systems::animate_resize_entities)
                    .run_if(dimming_enabled)
                    .run_if(vw_indicator_dirty),
                systems::update_flash_messages,
            )
                .chain(),
            crate::menubar::update_menu_bar
                .run_if(vw_indicator_dirty.or_eager(strip_count_changed)),
        ),
    );
}

/// Registers all the event triggers for the window manager.
pub fn register_triggers(app: &mut bevy::app::App) {
    app.add_systems(
        Update,
        (
            triggers::front_switched_trigger,
            triggers::window_focused_trigger,
            triggers::mission_control_trigger,
            triggers::application_event_trigger,
            triggers::dispatch_application_messages,
            triggers::window_destroyed_trigger,
            triggers::invalidate_window_title,
            triggers::refresh_configuration_trigger,
            triggers::theme_change_trigger,
            triggers::window_resize_verifier,
        ),
    );
    app.add_observer(triggers::window_unmanaged_trigger)
        .add_observer(triggers::window_managed_trigger)
        .add_observer(triggers::window_minimized_trigger)
        .add_observer(triggers::spawn_window_trigger)
        .add_observer(triggers::send_message_trigger)
        .add_observer(triggers::window_removal_trigger)
        .add_observer(triggers::cleanup_timeout_trigger)
        .add_observer(restore::restore_window_state);
}

/// Marker component for the currently focused window.
#[derive(Component)]
pub struct FocusedMarker;

#[derive(Component)]
pub struct ActiveWorkspaceMarker;

#[derive(Component)]
pub struct SelectedVirtualMarker;

#[derive(Component)]
pub struct FlashMessage(pub String);

/// Marker component for the currently active display.
#[derive(Component)]
pub struct ActiveDisplayMarker;

/// Marker component signifying a freshly created process, application, or window.
#[derive(Component)]
pub struct FreshMarker;

/// Marker component used to gather existing processes and windows during initialization.
#[derive(Component)]
pub struct ExistingMarker;

/// Component representing a request to reposition a window.
#[derive(Component, Debug, Deref, DerefMut)]
pub struct RepositionMarker(pub Origin);

/// Component representing a request to resize a window.
#[derive(Component, Debug, Deref, DerefMut)]
pub struct ResizeMarker(pub Size);

/// Marker component indicating that windows around the marked entity need to be reshuffled.
#[derive(Component)]
pub struct ReshuffleAroundMarker;

/// Marker component requesting that the strip scroll *minimally* to keep the
/// entity's NEW layout position inside the viewport. Unlike
/// [`ReshuffleAroundMarker`], this does not anchor the entity to its old visual
/// position — if the new layout slot is already on-screen, the strip is left
/// alone and the entity is free to slide there. Only when the new slot would
/// fall off the edge does the strip scroll just enough to expose it.
#[derive(Component)]
pub struct EnsureVisibleMarker {
    /// Assign the corrected scroll directly instead of animating toward it.
    /// Needed for the one-tick-delayed correction issued after a virtual
    /// workspace restore (`show_active_workspace`): its own `ensure_visible`
    /// call is skipped on the activation tick by
    /// `ensure_visible_in_strip`'s `is_added(ActiveWorkspaceMarker)` guard,
    /// so by the time it actually runs (the next tick), it has no way to
    /// tell this apart from an ordinary reshuffle-driven correction — which
    /// must keep animating regardless of `virtual_workspace_animations`.
    /// `false` for every other caller, which should keep animating.
    pub snap: bool,
}

/// Marks a [`LayoutStrip`](crate::ecs::layout::LayoutStrip) whose offset was
/// placed deliberately by the user (`Operation::Center`, `Operation::Snap`)
/// rather than derived from a window frame. While it is present and still
/// current, `reshuffle_layout_strip` leaves the offset alone — edge invariant
/// included — for any window that is already fully visible.
///
/// Currency is a value comparison, not change detection: the animator writes
/// `Position` every frame and `layout_sizes_changed` turns that into a
/// `LayoutStrip` change, so a tick would go stale immediately. `signature`
/// holds the ordered column tops with their target widths, which survives
/// scrolling and tab reordering but not an add, remove, reorder or resize.
#[derive(Component, Debug)]
pub struct ManualStripOffset {
    pub signature: Vec<(Entity, i32)>,
}

#[derive(Component, Debug)]
pub struct Scrolling {
    pub velocity: f64,
    pub position: f64,
    /// When true, the user's fingers are on the trackpad.
    pub is_user_swiping: bool,
    /// Last time a physical swipe event was received.
    pub last_event: Duration,
}

#[derive(Component, Clone, Debug, Default, Deref, DerefMut)]
pub struct LayoutPosition(pub Origin);

#[derive(Component, Clone, Debug, Deref, DerefMut)]
pub struct Position(pub Origin);

#[derive(Component, Clone, Debug, Deref, DerefMut)]
pub struct Bounds(pub Size);

#[derive(Component, Clone, Debug, Deref, DerefMut)]
pub struct WidthRatio(pub f64);

/// Marks a window entity that is currently on a native macOS fullscreen space.
/// The window has been removed from its tiled position in the strip.
/// `order` gives the sequence in which windows went fullscreen (0, 1, 2, …)
/// so they can be navigated left-to-right in that order after the tiled strip.
#[derive(Clone, Component, Debug)]
pub struct NativeFullscreenMarker {
    pub layout_strip: Entity,
    pub workspace_id: WorkspaceId,
    pub index: usize,
}

#[derive(Component)]
pub struct FullWidthMarker {
    pub width_ratio: f64,
}

/// Enum component indicating the unmanaged state of a window.
#[derive(Component, Debug)]
pub enum Unmanaged {
    /// The window is floating and not part of the tiling layout.
    Floating,
    /// The window is minimized.
    Minimized,
    /// The window is hidden.
    Hidden,
}

#[derive(Clone, Component, Copy, Debug)]
pub struct PreviousManagedStrip {
    pub workspace_id: WorkspaceId,
    pub virtual_index: u32,
    pub index: usize,
}

/// Wrapper component for a `ProcessApi` trait object, enabling dynamic dispatch for process-related operations within Bevy.
#[derive(Component, Deref, DerefMut)]
pub struct BProcess(pub Box<dyn ProcessApi>);

/// Component to manage a timeout, often used for delaying actions or retries.
#[derive(Component)]
pub struct Timeout {
    /// The Bevy timer instance.
    pub timer: Timer,
    /// An optional system to execute on timeout.
    pub system_id: Option<SystemId>,
    /// Optional custom expiry action. When `None`, the owning entity is
    /// despawned on timeout; when `Some`, the callback runs on the entity
    /// (e.g. removing temporary marker components without despawning the entity).
    pub on_expire: Option<fn(&mut EntityCommands)>,
}

impl Timeout {
    /// Creates a new `Timeout` with a specified duration and an optional message.
    /// The timer is set to run once.
    ///
    /// # Arguments
    ///
    /// * `duration` - The `Duration` for the timeout.
    /// * `message` - An `Option<String>` containing a message to associate with the timeout.
    ///
    /// # Returns
    ///
    /// A new `Timeout` instance.
    pub fn new(duration: Duration, message: Option<String>, commands: &mut Commands) -> Self {
        let timer = Timer::new(duration, bevy::time::TimerMode::Once);
        let system_id = message.map(|message| {
            commands.register_system(move || {
                tracing::debug!("{message}");
            })
        });
        Self {
            timer,
            system_id,
            on_expire: None,
        }
    }

    /// Creates a timeout attached to an existing entity that removes `Timeout`
    /// and `C` when the timer expires instead of despawning the entity.
    #[must_use]
    pub fn for_component<C: Component>(duration: Duration) -> Self {
        let timer = Timer::new(duration, bevy::time::TimerMode::Once);
        Self {
            timer,
            system_id: None,
            on_expire: Some(|entity_commands| {
                entity_commands.try_remove::<(Self, C)>();
            }),
        }
    }

    /// Creates an action timeout, which oneshots a provided system id.
    pub fn callback(duration: Duration, system_id: SystemId, commands: &mut Commands) {
        let timer = Timer::new(duration, bevy::time::TimerMode::Once);
        commands.spawn(Self {
            timer,
            system_id: Some(system_id),
            on_expire: None,
        });
    }
}

/// Component used as a retry mechanism for stray focus events that arrive before the target window is fully created.
#[derive(Component)]
pub struct StrayFocusEvent(pub WinID);

/// Component used as a retry mechanism when `focused_window_id()` fails during
/// an `ApplicationFrontSwitched` event (e.g. transient `kAXErrorCannotComplete`).
#[derive(Component)]
pub struct RetryFrontSwitch(pub Entity);

/// Marker component attached to a window after a keyboard focus request to
/// verify whether the application accepted or rejected focus once the settle
/// [`Timeout`] completes.
#[derive(Component)]
pub struct VerifyFocus;

#[derive(Component)]
pub struct BruteforceWindows(Task<Vec<Window>>);

#[derive(Component, Debug)]
pub enum DockPosition {
    Bottom(i32),
    Left(i32),
    Right(i32),
    Hidden,
}

#[derive(Component)]
pub struct VerifyWindowPosition {
    remaining: u8,
}

impl Default for VerifyWindowPosition {
    fn default() -> Self {
        Self { remaining: 3 }
    }
}

impl VerifyWindowPosition {
    pub fn tick(&mut self) -> bool {
        self.remaining = self.remaining.saturating_sub(1);
        self.remaining == 0
    }
}

#[derive(Deref, DerefMut, Resource)]
pub struct LowPowerMode(pub bool);

#[derive(Resource)]
pub struct SystemTheme {
    pub is_dark: bool,
}

/// Resource to control whether window reshuffling should be skipped.
#[derive(Resource)]
pub struct SkipReshuffle(pub bool);

/// Component marking a deferred reshuffle while the mouse button is held down.
/// Spawned with a `Timeout` so it auto-despawns if the mouse-up event is lost.
#[derive(Component)]
pub struct MouseHeldMarker(pub Entity);

/// Resource indicating whether Mission Control is currently active.
#[derive(Resource)]
pub struct MissionControlActive(pub bool);

/// Resource holding the `WinID` of a window that should gain focus when focus-follows-mouse is enabled.
#[derive(Resource)]
pub struct FocusFollowsMouse(pub Option<WinID>);

#[derive(Resource)]
pub struct Initializing;

/// Bevy event trigger for spawning new windows.
#[derive(BevyEvent)]
pub struct SpawnWindowTrigger(pub Vec<Window>);

#[derive(BevyEvent)]
pub struct ReadDisplayProperties(pub Entity);

#[derive(BevyEvent)]
pub struct SendMessageTrigger(pub Event);

#[derive(BevyEvent)]
pub struct RestoreWindowState;

#[derive(BevyEvent)]
pub struct RaiseWindow {
    pub entity: Entity,
    pub with_strip: bool,
}

pub trait SpawnCommandsExt {
    fn reposition_entity(&mut self, entity: Entity, origin: Origin);

    fn resize_entity(&mut self, entity: Entity, size: Size);

    fn reshuffle_around(&mut self, entity: Entity);

    fn ensure_visible(&mut self, entity: Entity);

    /// Like [`SpawnCommandsExt::ensure_visible`], but `snap` controls whether
    /// `ensure_visible_in_strip`'s correction is animated or assigned
    /// directly. Only `show_active_workspace` needs this — everyone else
    /// wants the correction to keep animating.
    fn ensure_visible_snap(&mut self, entity: Entity, snap: bool);

    fn focus_entity(&mut self, entity: Entity, raise: bool);

    fn flash_message(&mut self, message: String, duration: f32);

    // Spawns a layout strip in a single place, to properly insert all components.
    fn spawn_layout_strip(
        &mut self,
        layout_strip: LayoutStrip,
        origin: Origin,
        display_entity: Entity,
        active: bool,
    ) -> EntityCommands<'_>;
}

impl SpawnCommandsExt for Commands<'_, '_> {
    #[instrument(level = Level::TRACE, skip(self))]
    fn reposition_entity(&mut self, entity: Entity, origin: Origin) {
        if let Ok(mut entity_commands) = self.get_entity(entity) {
            entity_commands.try_insert(RepositionMarker(origin));
        }
    }

    #[instrument(level = Level::TRACE, skip(self))]
    fn resize_entity(&mut self, entity: Entity, size: Size) {
        if size.x <= 0 || size.y <= 0 {
            return;
        }
        if let Ok(mut entity_commands) = self.get_entity(entity) {
            entity_commands.try_insert(ResizeMarker(size));
        }
    }

    #[instrument(level = Level::TRACE, skip(self))]
    fn reshuffle_around(&mut self, entity: Entity) {
        if let Ok(mut entity_commands) = self.get_entity(entity) {
            entity_commands.try_insert(ReshuffleAroundMarker);
        }
    }

    #[instrument(level = Level::TRACE, skip(self))]
    fn ensure_visible(&mut self, entity: Entity) {
        self.ensure_visible_snap(entity, false);
    }

    #[instrument(level = Level::TRACE, skip(self))]
    fn ensure_visible_snap(&mut self, entity: Entity, snap: bool) {
        if let Ok(mut entity_commands) = self.get_entity(entity) {
            entity_commands.try_insert(EnsureVisibleMarker { snap });
        }
    }

    #[instrument(level = Level::TRACE, skip(self))]
    fn focus_entity(&mut self, entity: Entity, raise: bool) {
        if let Ok(mut entity_commands) = self.get_entity(entity) {
            entity_commands.try_insert(FocusedMarker);
            self.trigger(focus::FocusWindow { entity, raise });
        }
    }

    #[instrument(level = Level::TRACE, skip(self))]
    fn flash_message(&mut self, message: String, duration: f32) {
        let timeout = Timeout::new(Duration::from_secs_f32(duration), None, self);
        self.spawn((timeout, FlashMessage(message)));
    }

    #[instrument(level = Level::TRACE, skip(self))]
    fn spawn_layout_strip(
        &mut self,
        layout_strip: LayoutStrip,
        origin: Origin,
        display_entity: Entity,
        active: bool,
    ) -> EntityCommands<'_> {
        let mut spawned = self.spawn((layout_strip, Position(origin), ChildOf(display_entity)));
        if active {
            spawned.insert(ActiveWorkspaceMarker);
        } else {
            spawned.insert(SelectedVirtualMarker);
        }
        spawned
    }
}

/// Rebuilds the config watcher around `changed`, then re-registers every other
/// config file. Editors that save atomically (write-new-then-rename) break the
/// original watch, and since the TOML and Lua script share one watcher,
/// rebuilding it for just the changed file would otherwise silently stop the
/// other one from hot-reloading.
pub(crate) fn rewatch_configs(
    window_manager: &WindowManager,
    changed: &std::path::Path,
) -> Option<Box<dyn notify::Watcher>> {
    let mut watcher = window_manager
        .setup_config_watcher(changed)
        .inspect_err(|err| error!("watching the config '{}': {err}", changed.display()))
        .ok()?;

    let others = [
        CONFIGURATION_FILE.clone(),
        #[cfg(feature = "lua")]
        crate::config::discover_lua_file(),
    ];
    for other in others.into_iter().flatten() {
        if other == changed {
            continue;
        }
        if let Err(err) = watcher.watch(&other, notify::RecursiveMode::NonRecursive) {
            warn!("re-watching config '{}': {err}", other.display());
        }
    }
    Some(watcher)
}

/// Runs the Bevy schedule loop with each tick wrapped in an Objective-C
/// `NSAutoreleasePool`.
///
/// `MinimalPlugins`'s default `ScheduleRunnerPlugin` runs `app.update()` in a
/// bare Rust loop with no pool. While `pump_cocoa_event_loop` wraps
/// `nextEventMatchingMask` in its own pool, every Bevy system in `Startup`,
/// `PreUpdate` (after the pump), `Update`, and `PostUpdate` (`OverlayManager`,
/// `FlashMessageManager`, `MenuBarManager`, `NSScreen`, `NSWorkspace`,
/// `CoreAnimation` `CATransaction`s) runs outside that pool and would otherwise
/// push autoreleased objects into the root pool for the lifetime of the daemon.
fn autorelease_runner(mut app: BevyApp) -> bevy::app::AppExit {
    if app.plugins_state() != bevy::app::PluginsState::Cleaned {
        while app.plugins_state() == bevy::app::PluginsState::Adding {
            bevy::tasks::tick_global_task_pools_on_main_thread();
        }
        objc2::rc::autoreleasepool(|_| {
            app.finish();
            app.cleanup();
        });
    }

    loop {
        objc2::rc::autoreleasepool(|_| {
            app.update();
        });
        if let Some(exit) = app.should_exit() {
            return exit;
        }
    }
}

pub fn setup_bevy_app(sender: EventSender, receiver: Receiver<Event>) -> Result<BevyApp> {
    crate::manager::app::bound_ax_messaging_timeout()?;

    let window_manager: Box<dyn WindowManagerApi> = Box::new(WindowManagerOS::new(sender.clone()));

    // Discover (or create) the Lua init script first: whether it exists decides
    // whether the TOML path runs at all, so it has to be settled before
    // `CONFIGURATION_FILE` is first read.
    #[cfg(feature = "lua")]
    let lua_path = crate::config::ensure_lua_file()
        .inspect_err(|err| warn!("preparing Lua script: {err}"))
        .ok()
        .flatten();

    // With an init.lua there is no TOML at all, so watch whichever config files
    // actually exist. Both feed the same `ConfigRefresh` event.
    let toml_path = CONFIGURATION_FILE.as_deref();
    #[cfg(feature = "lua")]
    let primary = toml_path.or(lua_path.as_deref());
    #[cfg(not(feature = "lua"))]
    let primary = toml_path;
    let primary = primary.ok_or_else(|| {
        crate::errors::Error::InvalidConfig("no configuration file to watch".to_string())
    })?;

    #[cfg_attr(not(feature = "lua"), allow(unused_mut))]
    let mut watcher = window_manager.setup_config_watcher(primary)?;

    #[cfg(feature = "lua")]
    if let Some(path) = &lua_path
        && path.as_path() != primary
        && let Err(err) = watcher.watch(path, notify::RecursiveMode::NonRecursive)
    {
        warn!("watching Lua script '{}': {err}", path.display());
    }

    let mut app = BevyApp::new();

    app.add_plugins(MinimalPlugins)
        // `add_message`, not `init_resource`: the latter never registers the
        // buffer with bevy's `MessageRegistry`, so it's never double-buffered
        // and grows unbounded instead — every event lived for the process's
        // lifetime. Messages now live two frames, which every reader here
        // tolerates: readers gated on `not_swiping` or IPC subscribers would
        // rather drop a missed frame than act on a backlog.
        .add_message::<Event>()
        .insert_resource(Time::<Virtual>::from_max_delta(Duration::from_secs(10)))
        .insert_resource(WindowManager(window_manager))
        .insert_resource(SkipReshuffle(false))
        .insert_resource(SystemTheme {
            is_dark: crate::util::is_dark_mode(),
        })
        .insert_resource(MissionControlActive(false))
        .insert_resource(FocusFollowsMouse(None))
        .insert_resource(Initializing)
        .insert_non_send(watcher)
        .add_plugins(mouse::MouseEventsPlugin)
        .add_plugins(scroll::ScrollEventsPlugin)
        .add_plugins(workspace::WorkspaceEventsPlugin)
        .add_plugins(layout::LayoutEventsPlugin)
        .add_plugins(focus::FocusEventsPlugin)
        .add_plugins(display::DisplayEventsPlugin)
        .add_plugins((register_triggers, register_systems, register_commands));

    // Run every schedule inline rather than fanning systems out across the task
    // pool: the task-pool handoff measured ~45% of main-thread time against
    // ~16% actually spent on accessibility calls, dropping to ~10% once
    // inlined. The expensive systems here all take `&mut Window` and are
    // already mutually exclusive, so the fan-out bought little; genuine
    // parallelism (`par_iter_mut`) still goes through `ComputeTaskPool`
    // directly. `First`/`Last` are included even though unused because an
    // empty schedule still costs a task-pool scope per frame.
    for label in [
        First.intern(),
        PreUpdate.intern(),
        Update.intern(),
        PostUpdate.intern(),
        Last.intern(),
    ] {
        app.edit_schedule(label, |schedule| {
            schedule.set_executor(SingleThreadedExecutor::new());
        });
    }

    app.set_runner(autorelease_runner);

    let menu_events = sender.clone();
    let mut platform_callbacks = PlatformCallbacks::new(sender);
    platform_callbacks.setup_handlers()?;
    let mtm = platform_callbacks.main_thread_marker;
    let overlay_manager = OverlayManager::new(mtm);
    let flash_message_manager = FlashMessageManager::new(mtm);
    let menu_bar_manager = MenuBarManager::new(mtm, menu_events);
    app.insert_non_send(platform_callbacks)
        .insert_non_send(overlay_manager)
        .insert_non_send(flash_message_manager)
        .insert_non_send(menu_bar_manager)
        .insert_non_send(receiver);

    // `CONFIGURATION_FILE` is `None` exactly when an `init.lua` took the TOML
    // file out of play, so copied rules have to be written in Lua instead.
    app.insert_resource(if CONFIGURATION_FILE.is_none() {
        SnippetDialect::Lua
    } else {
        SnippetDialect::Toml
    });

    if let Some(previous_state) =
        PaneruState::load_from_file(&PaneruState::default_state_file_path())
    {
        app.insert_resource(previous_state);
    }

    // Overwrites the empty store `register_commands` put there, which is what
    // the mock harness keeps: only the real app reads the user's file.
    app.insert_resource(script_state::ScriptStateStore::load());

    // Do not insert this in mocks.
    app.insert_resource(LowPowerMode(false));

    // Start the Lua worker and install its hot-reload plugin (kept out of the
    // mock harness). A missing/broken script falls back to an empty runtime so
    // the watcher can still pick up a later fix. `spawn` blocks until the
    // script finishes loading, so its keybinds are published before the event
    // tap can see a keypress.
    #[cfg(feature = "lua")]
    if let Some(path) = lua_path {
        // `paneru.bind` resolves chords on the worker, and the layout-aware
        // keymap behind that goes through Carbon/TIS — must capture it here,
        // on the main thread, before the worker can ask for it.
        crate::config::prime_virtual_keymap();
        // The worker caches the script state store and watches this stamp to
        // know when its copy is stale — including when the writer was a client
        // rather than the script itself.
        let revision = app
            .world()
            .resource::<script_state::ScriptStateStore>()
            .revision_handle();
        let worker = lua::LuaWorker::spawn(lua::LuaSource::Path(path.clone()), revision);
        // A script that called `paneru.setup{...}` is authoritative: insert its
        // config now, before `app.run()`, so it exists ahead of the Startup
        // schedule and wins over the TOML `InitialConfig` (see
        // `gather_initial_processes`). Without `setup`, the TOML config is used.
        if let Some(config) = worker.built_config() {
            app.insert_resource(config);
        }
        app.insert_resource(worker);
        app.insert_resource(lua::LuaScriptPath(path));
        app.add_plugins(lua::LuaPlugin {});
    }

    Ok(app)
}

struct WindowProperties {
    params: Vec<WindowParams>,
}

impl WindowProperties {
    pub fn new(app: &Application, window: &Window, config: &Config) -> Self {
        let bundle_id = app.bundle_id().unwrap_or_default();
        let title = window.title().unwrap_or_default();
        let params = config.find_window_properties(&title, &bundle_id);
        Self { params }
    }

    pub fn floating(&self) -> bool {
        self.params
            .iter()
            .find_map(|props| props.floating)
            .unwrap_or(false)
    }

    pub fn insertion(&self) -> Option<usize> {
        self.params.iter().find_map(|props| props.index)
    }

    pub fn dont_focus(&self) -> bool {
        self.params
            .iter()
            .find_map(|props| props.dont_focus)
            .unwrap_or(false)
    }

    pub fn border_radius(&self) -> Option<f64> {
        self.params.iter().find_map(|p| p.border_radius)
    }

    pub fn grid_ratios(&self) -> Option<(f64, f64, f64, f64)> {
        self.params.iter().find_map(WindowParams::grid_ratios)
    }

    pub fn passthrough_keys(&self) -> Vec<(u8, Modifiers)> {
        self.params
            .iter()
            .flat_map(|p| p.passthrough_keys().to_vec())
            .collect::<Vec<_>>()
    }

    pub fn width_ratio(&self, focused_ratio: Option<f64>) -> Option<f64> {
        self.params
            .iter()
            .find_map(|props| props.width)
            .map(|width| width.ratio(focused_ratio))
    }

    pub fn fixed_width_ratio(&self) -> Option<f64> {
        match self.params.iter().find_map(|props| props.width) {
            Some(InitialWindowWidth::Ratio(ratio)) => Some(ratio),
            _ => None,
        }
    }

    pub fn vertical_padding(&self) -> i32 {
        self.params
            .iter()
            .find_map(|props| props.vertical_padding)
            .unwrap_or(0)
    }

    pub fn horizontal_padding(&self) -> i32 {
        self.params
            .iter()
            .find_map(|props| props.horizontal_padding)
            .unwrap_or(0)
    }
}