use super::ContextMenuItem;
use crate::{
components::{Response, Ui},
context::popup::Wrapped,
AccessAction, AccessActionKind as Kind, AccessNode, Id, Vec2,
};
#[derive(Clone, Copy)]
pub(super) enum Target {
Find,
Wrapped { node: usize, asked: bool },
Silent,
}
fn show(request: &AccessAction) -> bool {
matches!(request, AccessAction::ShowContextMenu)
}
pub(super) fn before(ui: &Ui<'_>) -> Wrapped {
ui.context.a11y_wrap(show)
}
pub(super) fn wrapped(ui: &mut Ui<'_>, before: Wrapped) -> Target {
match ui.context.a11y_wrapped(before) {
Some((node, requests)) => Target::Wrapped {
node,
asked: !requests.is_empty(),
},
None => Target::Silent,
}
}
pub(super) fn asked(
ui: &mut Ui<'_>,
target: Target,
response: Response,
anchor: Id,
usable: bool,
) -> Option<Vec2> {
if !ui.context.a11y_on() || !usable || !ui.is_enabled() {
return None;
}
let (node, asked) = match target {
Target::Silent => return None,
Target::Wrapped { node, asked } => (ui.context.a11y_node_mut(node), asked),
Target::Find => {
let requests = ui.context.a11y_requests(show);
let asked = requests.iter().any(|(node, _)| *node == response.id);
(ui.context.a11y_node_of(response.id), asked)
}
};
node?.action(Kind::ShowContextMenu);
if !asked {
return None;
}
let shown = ui
.context
.interaction
.previous_hits
.iter()
.find(|hit| hit.id == anchor);
let visible = shown
.map(|hit| hit.rect.intersect(hit.clip))
.filter(|rect| !rect.is_empty())
.unwrap_or(response.rect);
Some(visible.center())
}
pub(super) fn row(node: &mut AccessNode, id: Id, item: &ContextMenuItem) {
if item.left_text.is_empty() {
node.label(item.text.as_str());
} else {
node.label(format!("{} {}", item.left_text, item.text));
}
if !item.right_text.is_empty() {
node.keyboard_shortcut(item.right_text.as_str());
}
node.disabled(!item.enabled).clicks(id);
if item.checked {
node.toggled(true);
}
if item.submenu {
node.has_popup()
.expanded(false)
.action(Kind::Expand)
.action(Kind::Collapse);
}
}