jackdaw 0.3.1

A 3D level editor built with Bevy
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
use std::collections::HashMap;

use bevy::prelude::*;
use jackdaw_commands::keybinds::{EditorAction, Keybind, KeybindRegistry};
use jackdaw_feathers::{
    button::{ButtonClickEvent, ButtonProps, ButtonVariant, button},
    dialog::{
        CloseDialogEvent, DialogActionEvent, DialogChildrenSlot, EditorDialog, OpenDialogEvent,
    },
    text_edit::{self, TextEditProps, TextEditValue},
    tokens,
};

pub struct KeybindSettingsPlugin;

impl Plugin for KeybindSettingsPlugin {
    fn build(&self, app: &mut App) {
        app.init_resource::<KeybindRecordingState>()
            .init_resource::<KeyFilterState>()
            .add_observer(open_keybind_settings)
            .add_observer(on_keybind_settings_save)
            .add_observer(on_rebind_click)
            .add_observer(on_reset_click)
            .add_observer(on_reset_all_click)
            .add_observer(on_key_filter_click)
            .add_systems(
                Update,
                (
                    populate_keybind_dialog,
                    capture_keybind_recording,
                    capture_key_filter,
                    apply_keybind_filter,
                    cleanup_on_dialog_close,
                )
                    .run_if(in_state(crate::AppState::Editor)),
            );
    }
}

#[derive(Event)]
pub struct OpenKeybindSettingsEvent;

/// Working copy of keybind changes, applied on Save.
#[derive(Resource)]
struct PendingKeybindChanges(HashMap<EditorAction, Vec<Keybind>>);

/// Tracks which action/binding is being re-recorded.
#[derive(Resource, Default)]
struct KeybindRecordingState {
    target: Option<(EditorAction, usize)>,
    /// When set, a conflict was detected and we're waiting for the user to confirm.
    conflict: Option<PendingConflict>,
}

struct PendingConflict {
    new_bind: Keybind,
    /// The action that already has this binding.
    conflicting_action: EditorAction,
}

/// Inserted when dialog is open, removed on close/cancel.
#[derive(Resource)]
struct KeybindSettingsOpen;

/// Marker for the text filter input.
#[derive(Component)]
struct KeybindFilterInput;

/// Marker on the key-capture filter button.
#[derive(Component)]
struct KeyFilterButton;

/// Resource tracking the captured key filter.
#[derive(Resource, Default)]
struct KeyFilterState {
    /// When true, next non-modifier keypress sets the filter key.
    capturing: bool,
    /// The currently active key filter, if any.
    active_key: Option<KeyCode>,
}

/// Marker on each keybind row, storing its action for filtering.
#[derive(Component)]
struct KeybindRowAction(EditorAction);

/// Marker on category headers, storing category name for filtering.
#[derive(Component)]
struct KeybindCategoryHeader(String);

/// The text element showing the binding string for an action.
#[derive(Component)]
struct KeybindDisplayText(EditorAction);

/// Rebind button: (action, binding index).
#[derive(Component)]
struct KeybindRebindButton(EditorAction, usize);

/// Per-row reset to default button.
#[derive(Component)]
struct KeybindResetButton(EditorAction);

/// Reset All button marker.
#[derive(Component)]
struct KeybindResetAllButton;

/// Flag to prevent double-populating.
#[derive(Component)]
struct KeybindDialogPopulated;

fn open_keybind_settings(
    _event: On<OpenKeybindSettingsEvent>,
    mut commands: Commands,
    registry: Res<KeybindRegistry>,
    existing: Option<Res<KeybindSettingsOpen>>,
) {
    if existing.is_some() {
        return;
    }

    commands.insert_resource(PendingKeybindChanges(registry.bindings.clone()));
    commands.insert_resource(KeybindSettingsOpen);

    let mut dialog_event = OpenDialogEvent::new("Keybinds", "Save")
        .with_max_width(px(700))
        .with_close_on_click_outside(false)
        .without_content_padding();
    dialog_event.close_on_esc = false;
    commands.trigger(dialog_event);
}

fn format_bindings(bindings: &[Keybind]) -> String {
    if bindings.is_empty() {
        return "Unbound".to_string();
    }
    bindings
        .iter()
        .map(|b| b.to_string())
        .collect::<Vec<_>>()
        .join(" / ")
}

fn populate_keybind_dialog(
    mut commands: Commands,
    pending: Option<Res<PendingKeybindChanges>>,
    slots: Query<Entity, (With<DialogChildrenSlot>, Added<DialogChildrenSlot>)>,
    populated: Query<(), With<KeybindDialogPopulated>>,
) {
    let Some(pending) = pending else { return };

    for slot_entity in &slots {
        if !populated.is_empty() {
            continue;
        }

        commands.entity(slot_entity).insert(KeybindDialogPopulated);

        // Top-level column: filter bar + scrollable list
        let wrapper = commands
            .spawn(Node {
                flex_direction: FlexDirection::Column,
                width: percent(100),
                ..Default::default()
            })
            .id();
        commands.entity(slot_entity).add_child(wrapper);

        // Filter bar: text input + key capture button
        let filter_row = commands
            .spawn(Node {
                flex_direction: FlexDirection::Row,
                align_items: AlignItems::Center,
                column_gap: px(tokens::SPACING_MD),
                padding: UiRect::all(px(tokens::SPACING_LG)),
                width: percent(100),
                ..Default::default()
            })
            .id();
        let mut filter_props = TextEditProps::default()
            .with_placeholder("Search actions...")
            .allow_empty();
        filter_props.grow = true;
        // Wrap text_edit in a flex-grow container to avoid duplicate Node
        let filter_input_wrapper = commands
            .spawn((
                KeybindFilterInput,
                Node {
                    flex_grow: 1.0,
                    ..Default::default()
                },
                children![text_edit::text_edit(filter_props)],
            ))
            .id();
        // Key capture filter button
        let key_filter_btn = commands
            .spawn((
                KeyFilterButton,
                button(ButtonProps::new("Key: Any").with_variant(ButtonVariant::Default)),
            ))
            .id();
        // Reset All button (top bar)
        let reset_all_btn = commands
            .spawn((
                KeybindResetAllButton,
                button(
                    ButtonProps::new("Reset All to Defaults").with_variant(ButtonVariant::Default),
                ),
            ))
            .id();
        commands.entity(filter_row).add_children(&[
            filter_input_wrapper,
            key_filter_btn,
            reset_all_btn,
        ]);
        commands.entity(wrapper).add_child(filter_row);

        // Scrollable list
        let scroll = commands
            .spawn(Node {
                flex_direction: FlexDirection::Column,
                max_height: px(460.0),
                overflow: Overflow::scroll_y(),
                width: percent(100),
                ..Default::default()
            })
            .id();
        commands.entity(wrapper).add_child(scroll);

        // Group actions by category
        let mut current_category = "";
        for &action in EditorAction::all() {
            let category = action.category();
            if category != current_category {
                current_category = category;
                let header = commands
                    .spawn((
                        KeybindCategoryHeader(category.to_string()),
                        Node {
                            padding: UiRect {
                                top: px(tokens::SPACING_LG),
                                bottom: px(tokens::SPACING_SM),
                                left: px(tokens::SPACING_LG),
                                right: px(tokens::SPACING_LG),
                            },
                            border: UiRect::bottom(px(2.0)),
                            margin: UiRect::top(px(tokens::SPACING_SM)),
                            ..Default::default()
                        },
                        BorderColor::all(tokens::BORDER_STRONG),
                        children![(
                            Text::new(category),
                            TextFont {
                                font_size: tokens::FONT_LG,
                                weight: FontWeight::BOLD,
                                ..Default::default()
                            },
                            TextColor(tokens::TEXT_PRIMARY),
                        )],
                    ))
                    .id();
                commands.entity(scroll).add_child(header);
            }

            let bindings = pending.0.get(&action).cloned().unwrap_or_default();
            let binding_text = format_bindings(&bindings);

            // Row
            let row = commands
                .spawn((
                    KeybindRowAction(action),
                    Node {
                        flex_direction: FlexDirection::Row,
                        justify_content: JustifyContent::SpaceBetween,
                        align_items: AlignItems::Center,
                        width: percent(100),
                        padding: UiRect::axes(px(tokens::SPACING_LG), px(tokens::SPACING_SM)),
                        border: UiRect::bottom(px(1.0)),
                        ..Default::default()
                    },
                    BorderColor::all(tokens::BORDER_COLOR),
                ))
                .id();

            // Action name
            let name_label = commands
                .spawn((
                    Text::new(action.to_string()),
                    TextFont {
                        font_size: tokens::FONT_MD,
                        ..Default::default()
                    },
                    TextColor(tokens::TEXT_PRIMARY),
                    Node {
                        width: px(200.0),
                        flex_shrink: 0.0,
                        ..Default::default()
                    },
                ))
                .id();

            // Right side
            let right = commands
                .spawn(Node {
                    flex_direction: FlexDirection::Row,
                    align_items: AlignItems::Center,
                    column_gap: px(tokens::SPACING_MD),
                    flex_grow: 1.0,
                    justify_content: JustifyContent::End,
                    ..Default::default()
                })
                .id();

            let text_color = if bindings.is_empty() {
                tokens::TEXT_SECONDARY
            } else {
                tokens::TEXT_PRIMARY
            };
            let binding_label = commands
                .spawn((
                    KeybindDisplayText(action),
                    Text::new(binding_text),
                    TextFont {
                        font_size: tokens::FONT_MD,
                        ..Default::default()
                    },
                    TextColor(text_color),
                    Node {
                        min_width: px(100.0),
                        ..Default::default()
                    },
                ))
                .id();

            let rebind_btn = commands
                .spawn((
                    KeybindRebindButton(action, 0),
                    button(ButtonProps::new("Rebind").with_variant(ButtonVariant::Default)),
                ))
                .id();

            let reset_btn = commands
                .spawn((
                    KeybindResetButton(action),
                    button(ButtonProps::new("Reset").with_variant(ButtonVariant::Ghost)),
                ))
                .id();

            commands
                .entity(right)
                .add_children(&[binding_label, rebind_btn, reset_btn]);
            commands.entity(row).add_children(&[name_label, right]);
            commands.entity(scroll).add_child(row);
        }
    }
}

fn on_key_filter_click(
    event: On<ButtonClickEvent>,
    key_filter_buttons: Query<&ChildOf, With<KeyFilterButton>>,
    parents: Query<&ChildOf>,
    dialogs: Query<(), With<EditorDialog>>,
    mut key_filter: ResMut<KeyFilterState>,
    mut registry: ResMut<KeybindRegistry>,
    recording: Res<KeybindRecordingState>,
    children_query: Query<&Children>,
    mut texts: Query<&mut Text>,
) {
    let Ok(_) = key_filter_buttons.get(event.entity) else {
        return;
    };
    if !is_in_dialog(event.entity, &parents, &dialogs) {
        return;
    }
    if recording.target.is_some() {
        return;
    }

    if key_filter.active_key.is_some() {
        key_filter.active_key = None;
        key_filter.capturing = false;
        set_button_text(event.entity, "Key: Any", &children_query, &mut texts);
    } else if key_filter.capturing {
        key_filter.capturing = false;
        registry.recording = false;
        set_button_text(event.entity, "Key: Any", &children_query, &mut texts);
    } else {
        key_filter.capturing = true;
        registry.recording = true;
        set_button_text(event.entity, "Press a key...", &children_query, &mut texts);
    }
}

/// Set the text content of a button's first Text child.
fn set_button_text(
    button_entity: Entity,
    label: &str,
    children_query: &Query<&Children>,
    texts: &mut Query<&mut Text>,
) {
    let Ok(children) = children_query.get(button_entity) else {
        return;
    };
    for child in children.iter() {
        if let Ok(mut text) = texts.get_mut(child) {
            text.0 = label.to_string();
            return;
        }
    }
}

fn capture_key_filter(
    keyboard: Res<ButtonInput<KeyCode>>,
    mouse: Res<ButtonInput<MouseButton>>,
    mut key_filter: ResMut<KeyFilterState>,
    mut registry: ResMut<KeybindRegistry>,
    recording: Res<KeybindRecordingState>,
    key_filter_btns: Query<Entity, With<KeyFilterButton>>,
    children_query: Query<&Children>,
    mut texts: Query<&mut Text>,
) {
    if !key_filter.capturing {
        return;
    }
    if recording.target.is_some() {
        return;
    }

    // Right-click or Escape cancels
    if mouse.just_pressed(MouseButton::Right) || keyboard.just_pressed(KeyCode::Escape) {
        key_filter.capturing = false;
        registry.recording = false;
        for btn in &key_filter_btns {
            set_button_text(btn, "Key: Any", &children_query, &mut texts);
        }
        return;
    }

    let modifier_keys = [
        KeyCode::ControlLeft,
        KeyCode::ControlRight,
        KeyCode::ShiftLeft,
        KeyCode::ShiftRight,
        KeyCode::AltLeft,
        KeyCode::AltRight,
    ];

    for key in keyboard.get_just_pressed() {
        if modifier_keys.contains(key) {
            continue;
        }

        key_filter.capturing = false;
        key_filter.active_key = Some(*key);
        registry.recording = false;

        let label = format!(
            "Key: {} (click to clear)",
            jackdaw_commands::keybinds::key_display_name(*key)
        );
        for btn in &key_filter_btns {
            set_button_text(btn, &label, &children_query, &mut texts);
        }
        return;
    }
}

/// Show/hide rows and category headers based on both text and key filters.
fn apply_keybind_filter(
    filter_wrappers: Query<&Children, With<KeybindFilterInput>>,
    text_values: Query<&TextEditValue, Changed<TextEditValue>>,
    all_text_values: Query<&TextEditValue>,
    key_filter: Res<KeyFilterState>,
    pending: Option<Res<PendingKeybindChanges>>,
    mut rows: Query<(&KeybindRowAction, &mut Node)>,
    mut headers: Query<(&KeybindCategoryHeader, &mut Node), Without<KeybindRowAction>>,
) {
    // Find the text value from the filter wrapper's child
    let filter_text = filter_wrappers.iter().find_map(|children| {
        children
            .iter()
            .find_map(|child| all_text_values.get(child).ok())
    });
    let Some(filter_value) = filter_text else {
        return;
    };

    // Only re-filter when text changes or key filter changes
    let text_changed = filter_wrappers
        .iter()
        .any(|children| children.iter().any(|child| text_values.get(child).is_ok()));
    if !text_changed && !key_filter.is_changed() {
        return;
    }
    let Some(pending) = pending else { return };

    let text_query = filter_value.0.trim().to_lowercase();
    let key_filter_active = key_filter.active_key;

    let mut visible_categories: std::collections::HashSet<&str> = std::collections::HashSet::new();

    for (row, mut node) in &mut rows {
        let action = row.0;
        let action_name = action.to_string().to_lowercase();
        let bindings = pending.0.get(&action).cloned().unwrap_or_default();
        let binding_str = format_bindings(&bindings).to_lowercase();

        // Text filter: matches action name, binding string, or category
        let text_match = text_query.is_empty()
            || action_name.contains(&text_query)
            || binding_str.contains(&text_query)
            || action.category().to_lowercase().contains(&text_query);

        // Key filter: matches if any binding uses this key
        let key_match = match key_filter_active {
            Some(key) => bindings.iter().any(|b| b.key == key),
            None => true,
        };

        let visible = text_match && key_match;

        node.display = if visible {
            visible_categories.insert(action.category());
            Display::Flex
        } else {
            Display::None
        };
    }

    for (header, mut node) in &mut headers {
        let has_filters = !text_query.is_empty() || key_filter_active.is_some();
        node.display = if !has_filters || visible_categories.contains(header.0.as_str()) {
            Display::Flex
        } else {
            Display::None
        };
    }
}

fn on_rebind_click(
    event: On<ButtonClickEvent>,
    rebind_buttons: Query<(&KeybindRebindButton, &ChildOf)>,
    parents: Query<&ChildOf>,
    dialogs: Query<(), With<EditorDialog>>,
    mut recording_state: ResMut<KeybindRecordingState>,
    mut registry: ResMut<KeybindRegistry>,
    mut texts: Query<(&KeybindDisplayText, &mut Text, &mut TextColor)>,
) {
    let Ok((btn, _)) = rebind_buttons.get(event.entity) else {
        return;
    };

    if !is_in_dialog(event.entity, &parents, &dialogs) {
        return;
    }

    let action = btn.0;
    let index = btn.1;

    recording_state.target = Some((action, index));
    registry.recording = true;

    for (display, mut text, mut color) in &mut texts {
        if display.0 == action {
            text.0 = "Press a key...".to_string();
            color.0 = tokens::TEXT_ACCENT;
        }
    }
}

/// Find which other action (if any) already uses this exact keybind in the pending changes.
fn find_conflict(
    pending: &HashMap<EditorAction, Vec<Keybind>>,
    new_bind: &Keybind,
    exclude_action: EditorAction,
) -> Option<EditorAction> {
    for (&other_action, bindings) in pending {
        if other_action == exclude_action {
            continue;
        }
        if bindings.iter().any(|b| b == new_bind) {
            return Some(other_action);
        }
    }
    None
}

/// Apply a keybind to the target action and remove it from any conflicting action.
fn apply_rebind(
    pending: &mut HashMap<EditorAction, Vec<Keybind>>,
    action: EditorAction,
    index: usize,
    new_bind: Keybind,
    conflict: Option<EditorAction>,
    texts: &mut Query<(&KeybindDisplayText, &mut Text, &mut TextColor)>,
) {
    // Remove the conflicting binding from the other action
    if let Some(conflicting_action) = conflict {
        if let Some(bindings) = pending.get_mut(&conflicting_action) {
            bindings.retain(|b| b != &new_bind);
            let text_str = format_bindings(bindings);
            let text_color = if bindings.is_empty() {
                tokens::TEXT_SECONDARY
            } else {
                tokens::TEXT_PRIMARY
            };
            for (display, mut text, mut color) in texts.iter_mut() {
                if display.0 == conflicting_action {
                    text.0 = text_str.clone();
                    color.0 = text_color;
                }
            }
        }
    }

    // Apply the new binding to the target action
    let bindings = pending.entry(action).or_default();
    if index < bindings.len() {
        bindings[index] = new_bind;
    } else {
        *bindings = vec![new_bind];
    }

    let text_str = format_bindings(bindings);
    for (display, mut text, mut color) in texts.iter_mut() {
        if display.0 == action {
            text.0 = text_str.clone();
            color.0 = tokens::TEXT_PRIMARY;
        }
    }
}

fn capture_keybind_recording(
    keyboard: Res<ButtonInput<KeyCode>>,
    mouse: Res<ButtonInput<MouseButton>>,
    mut recording_state: ResMut<KeybindRecordingState>,
    mut registry: ResMut<KeybindRegistry>,
    mut pending: Option<ResMut<PendingKeybindChanges>>,
    mut texts: Query<(&KeybindDisplayText, &mut Text, &mut TextColor)>,
    mut commands: Commands,
    dialog_exists: Query<(), With<EditorDialog>>,
    settings_open: Option<Res<KeybindSettingsOpen>>,
) {
    let Some((action, index)) = recording_state.target else {
        // If not recording, handle ESC to close dialog
        if settings_open.is_some()
            && !dialog_exists.is_empty()
            && keyboard.just_pressed(KeyCode::Escape)
        {
            commands.trigger(CloseDialogEvent);
        }
        return;
    };

    // Right-click cancels recording (including conflict confirmation)
    if mouse.just_pressed(MouseButton::Right) {
        recording_state.target = None;
        recording_state.conflict = None;
        registry.recording = false;

        if let Some(ref pending) = pending {
            let bindings = pending.0.get(&action).cloned().unwrap_or_default();
            let text_str = format_bindings(&bindings);
            let text_color = if bindings.is_empty() {
                tokens::TEXT_SECONDARY
            } else {
                tokens::TEXT_PRIMARY
            };
            for (display, mut text, mut color) in &mut texts {
                if display.0 == action {
                    text.0 = text_str.clone();
                    color.0 = text_color;
                }
            }
        }
        return;
    }

    let modifier_keys = [
        KeyCode::ControlLeft,
        KeyCode::ControlRight,
        KeyCode::ShiftLeft,
        KeyCode::ShiftRight,
        KeyCode::AltLeft,
        KeyCode::AltRight,
    ];

    for key in keyboard.get_just_pressed() {
        if modifier_keys.contains(key) {
            continue;
        }

        let ctrl = keyboard.any_pressed([KeyCode::ControlLeft, KeyCode::ControlRight]);
        let shift = keyboard.any_pressed([KeyCode::ShiftLeft, KeyCode::ShiftRight]);
        let alt = keyboard.any_pressed([KeyCode::AltLeft, KeyCode::AltRight]);

        let new_bind = Keybind {
            key: *key,
            ctrl,
            shift,
            alt,
        };

        // If we're in conflict confirmation mode, check if user pressed the same key
        if let Some(ref conflict) = recording_state.conflict {
            if new_bind == conflict.new_bind {
                // User confirmed. Apply the rebind and clear the conflict.
                let conflicting_action = conflict.conflicting_action;
                if let Some(ref mut pending) = pending {
                    apply_rebind(
                        &mut pending.0,
                        action,
                        index,
                        new_bind,
                        Some(conflicting_action),
                        &mut texts,
                    );
                }
                recording_state.target = None;
                recording_state.conflict = None;
                registry.recording = false;
                return;
            }
            // Different key pressed, treat as a fresh recording (fall through below).
            recording_state.conflict = None;
        }

        if let Some(ref mut pending) = pending {
            // Check for conflicts with other actions
            if let Some(conflicting_action) = find_conflict(&pending.0, &new_bind, action) {
                // Show warning and wait for confirmation
                let bind_str = new_bind.to_string();
                recording_state.conflict = Some(PendingConflict {
                    new_bind,
                    conflicting_action,
                });

                let warning = format!(
                    "{bind_str} is bound to {conflicting_action}. Press again to override."
                );
                for (display, mut text, mut color) in &mut texts {
                    if display.0 == action {
                        text.0 = warning.clone();
                        color.0 = Color::srgb(1.0, 0.75, 0.2);
                    }
                }
                return;
            }

            // No conflict, apply directly.
            apply_rebind(&mut pending.0, action, index, new_bind, None, &mut texts);
        }

        recording_state.target = None;
        recording_state.conflict = None;
        registry.recording = false;
        return;
    }
}

fn on_reset_click(
    event: On<ButtonClickEvent>,
    reset_buttons: Query<(&KeybindResetButton, &ChildOf)>,
    parents: Query<&ChildOf>,
    dialogs: Query<(), With<EditorDialog>>,
    mut pending: Option<ResMut<PendingKeybindChanges>>,
    mut texts: Query<(&KeybindDisplayText, &mut Text, &mut TextColor)>,
) {
    let Ok((btn, _)) = reset_buttons.get(event.entity) else {
        return;
    };

    if !is_in_dialog(event.entity, &parents, &dialogs) {
        return;
    }

    let action = btn.0;
    let defaults = KeybindRegistry::default();
    let default_bindings = defaults.bindings.get(&action).cloned().unwrap_or_default();

    if let Some(ref mut pending) = pending {
        pending.0.insert(action, default_bindings.clone());
    }

    let text_str = format_bindings(&default_bindings);
    let text_color = if default_bindings.is_empty() {
        tokens::TEXT_SECONDARY
    } else {
        tokens::TEXT_PRIMARY
    };
    for (display, mut text, mut color) in &mut texts {
        if display.0 == action {
            text.0 = text_str.clone();
            color.0 = text_color;
        }
    }
}

fn on_reset_all_click(
    event: On<ButtonClickEvent>,
    reset_all_buttons: Query<&ChildOf, With<KeybindResetAllButton>>,
    parents: Query<&ChildOf>,
    dialogs: Query<(), With<EditorDialog>>,
    mut pending: Option<ResMut<PendingKeybindChanges>>,
    mut texts: Query<(&KeybindDisplayText, &mut Text, &mut TextColor)>,
) {
    let Ok(_) = reset_all_buttons.get(event.entity) else {
        return;
    };

    if !is_in_dialog(event.entity, &parents, &dialogs) {
        return;
    }

    let defaults = KeybindRegistry::default();

    if let Some(ref mut pending) = pending {
        pending.0 = defaults.bindings.clone();
    }

    for (display, mut text, mut color) in &mut texts {
        let action = display.0;
        let bindings = defaults.bindings.get(&action).cloned().unwrap_or_default();
        let text_str = format_bindings(&bindings);
        text.0 = text_str;
        color.0 = if bindings.is_empty() {
            tokens::TEXT_SECONDARY
        } else {
            tokens::TEXT_PRIMARY
        };
    }
}

fn on_keybind_settings_save(
    _event: On<DialogActionEvent>,
    mut commands: Commands,
    pending: Option<Res<PendingKeybindChanges>>,
    settings_open: Option<Res<KeybindSettingsOpen>>,
    mut registry: ResMut<KeybindRegistry>,
) {
    if settings_open.is_none() {
        return;
    }

    if let Some(pending) = pending {
        registry.bindings = pending.0.clone();
    }
    registry.recording = false;

    crate::keybinds::save_keybinds(&registry);

    commands.remove_resource::<PendingKeybindChanges>();
    commands.remove_resource::<KeybindSettingsOpen>();
}

fn cleanup_on_dialog_close(
    mut commands: Commands,
    settings_open: Option<Res<KeybindSettingsOpen>>,
    dialogs: Query<(), With<EditorDialog>>,
    mut registry: ResMut<KeybindRegistry>,
    mut recording_state: ResMut<KeybindRecordingState>,
    mut key_filter: ResMut<KeyFilterState>,
) {
    if settings_open.is_none() {
        return;
    }
    if !dialogs.is_empty() {
        return;
    }

    registry.recording = false;
    recording_state.target = None;
    recording_state.conflict = None;
    *key_filter = KeyFilterState::default();
    commands.remove_resource::<PendingKeybindChanges>();
    commands.remove_resource::<KeybindSettingsOpen>();
}

fn is_in_dialog(
    start: Entity,
    parents: &Query<&ChildOf>,
    dialogs: &Query<(), With<EditorDialog>>,
) -> bool {
    let mut current = start;
    loop {
        if dialogs.get(current).is_ok() {
            return true;
        }
        let Ok(child_of) = parents.get(current) else {
            return false;
        };
        current = child_of.parent();
    }
}