use super::{Context, HitAction, Id};
use crate::components::{
combo_box::ComboBoxState, context_menu::MenuState, menu_bar::MenuBarState,
};
use std::collections::HashMap;
use winit::{event::ElementState, keyboard::KeyCode};
#[derive(Default)]
pub(crate) struct Menus {
pub(crate) combo_boxes: HashMap<Id, ComboBoxState>,
pub(crate) context_menus: HashMap<Id, MenuState>,
pub(crate) menu_bars: HashMap<Id, MenuBarState>,
keys: HashMap<Id, Vec<KeyCode>>,
}
impl Menus {
pub(super) fn retire(&mut self, frame: u64) {
self.combo_boxes
.retain(|_, state| state.last_frame == frame);
self.context_menus
.retain(|_, state| state.last_frame == frame);
self.menu_bars.retain(|_, state| state.last_frame == frame);
}
pub(super) fn clear_keys(&mut self) {
self.keys.clear();
}
}
impl Context {
pub(super) fn menu_key(&mut self, code: KeyCode, state: ElementState) -> bool {
let popup_target = self.popups.top().and_then(|popup| popup.key_target);
let Some(id) = popup_target.or_else(|| self.interaction.focused_as(HitAction::ComboBox))
else {
return false;
};
let navigation = matches!(
code,
KeyCode::ArrowUp
| KeyCode::ArrowDown
| KeyCode::Enter
| KeyCode::Space
| KeyCode::Home
| KeyCode::End
| KeyCode::PageUp
| KeyCode::PageDown
) || (matches!(code, KeyCode::ArrowLeft | KeyCode::ArrowRight)
&& popup_target.is_some());
if !navigation {
return false;
}
let editing = self.interaction.focused_as(HitAction::TextEdit).is_some();
if editing
&& !matches!(
code,
KeyCode::ArrowUp
| KeyCode::ArrowDown
| KeyCode::Enter
| KeyCode::PageUp
| KeyCode::PageDown
| KeyCode::ArrowLeft
| KeyCode::ArrowRight
)
{
return false;
}
let pressed = state == ElementState::Pressed;
self.input.record_key(code, pressed);
if pressed {
self.menus.keys.entry(id).or_default().push(code);
}
true
}
pub(crate) fn take_menu_keys(&mut self, id: Id) -> Vec<KeyCode> {
self.menus.keys.remove(&id).unwrap_or_default()
}
}