Add target field to EventCtx (#657)

Remove target field from AccessEvent
This commit is contained in:
Olivier FAURE 2024-10-13 03:21:52 +02:00 committed by GitHub
parent 011929321e
commit a26bf11119
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 23 additions and 16 deletions

View File

@ -172,7 +172,7 @@ impl Widget for CalcButton {
fn on_text_event(&mut self, _ctx: &mut EventCtx, _event: &TextEvent) {}
fn on_access_event(&mut self, ctx: &mut EventCtx, event: &AccessEvent) {
if event.target == ctx.widget_id() {
if ctx.target() == ctx.widget_id() {
match event.action {
accesskit::Action::Default => {
ctx.submit_action(Action::Other(Box::new(self.action)));

View File

@ -68,6 +68,7 @@ pub struct EventCtx<'a> {
pub(crate) widget_state: &'a mut WidgetState,
pub(crate) widget_state_children: ArenaMutChildren<'a, WidgetState>,
pub(crate) widget_children: ArenaMutChildren<'a, Box<dyn Widget>>,
pub(crate) target: WidgetId,
pub(crate) allow_pointer_capture: bool,
pub(crate) is_handled: bool,
}
@ -716,6 +717,13 @@ impl EventCtx<'_> {
self.is_handled
}
/// The widget originally targeted by the event.
///
/// This will be different from [`widget_id`](Self::widget_id) during event bubbling.
pub fn target(&self) -> WidgetId {
self.target
}
/// Request keyboard focus.
///
/// Because only one widget can be focused at a time, multiple focus requests

View File

@ -5,8 +5,6 @@
use crate::dpi::{LogicalPosition, PhysicalPosition, PhysicalSize};
use crate::kurbo::Rect;
// TODO - See issue https://github.com/linebender/xilem/issues/367
use crate::WidgetId;
use std::path::PathBuf;
@ -211,8 +209,6 @@ pub enum TextEvent {
#[derive(Debug, Clone)]
pub struct AccessEvent {
// TODO - Split out widget id from AccessEvent
pub target: WidgetId,
pub action: accesskit::Action,
pub data: Option<accesskit::ActionData>,
}

View File

@ -40,6 +40,7 @@ fn run_event_pass<E>(
) -> Handled {
let mut pass_fn = pass_fn;
let original_target = target;
let mut target_widget_id = target;
let mut is_handled = false;
while let Some(widget_id) = target_widget_id {
@ -51,6 +52,7 @@ fn run_event_pass<E>(
widget_state: state_mut.item,
widget_state_children: state_mut.children,
widget_children: widget_mut.children,
target: original_target.unwrap(),
allow_pointer_capture,
is_handled: false,
};
@ -163,26 +165,28 @@ pub(crate) fn root_on_text_event(root: &mut RenderRoot, event: &TextEvent) -> Ha
}
// --- MARK: ACCESS EVENT ---
pub(crate) fn root_on_access_event(root: &mut RenderRoot, event: &AccessEvent) -> Handled {
pub(crate) fn root_on_access_event(
root: &mut RenderRoot,
event: &AccessEvent,
target: WidgetId,
) -> Handled {
let _span = info_span!("access_event").entered();
debug!("Running ON_ACCESS_EVENT pass with {}", event.short_name());
let target = Some(event.target);
let mut handled = run_event_pass(root, target, event, false, |widget, ctx, event| {
let mut handled = run_event_pass(root, Some(target), event, false, |widget, ctx, event| {
widget.on_access_event(ctx, event);
});
// Handle focus events
match event.action {
accesskit::Action::Focus if !handled.is_handled() => {
if root.is_still_interactive(event.target) {
root.state.next_focused_widget = Some(event.target);
if root.is_still_interactive(target) {
root.state.next_focused_widget = Some(target);
handled = Handled::Yes;
}
}
accesskit::Action::Blur if !handled.is_handled() => {
if root.state.next_focused_widget == Some(event.target) {
if root.state.next_focused_widget == Some(target) {
root.state.next_focused_widget = None;
handled = Handled::Yes;
}

View File

@ -416,12 +416,11 @@ impl RenderRoot {
return;
};
let event = AccessEvent {
target: WidgetId(id),
action: event.action,
data: event.data,
};
root_on_access_event(self, &event);
root_on_access_event(self, &event, WidgetId(id));
self.run_rewrite_passes();
self.get_root_widget().debug_validate(false);

View File

@ -100,7 +100,7 @@ impl Widget for Button {
fn on_text_event(&mut self, _ctx: &mut EventCtx, _event: &TextEvent) {}
fn on_access_event(&mut self, ctx: &mut EventCtx, event: &AccessEvent) {
if event.target == ctx.widget_id() {
if ctx.target() == ctx.widget_id() {
match event.action {
accesskit::Action::Default => {
ctx.submit_action(Action::ButtonPressed(PointerButton::Primary));

View File

@ -89,7 +89,7 @@ impl Widget for Checkbox {
fn on_text_event(&mut self, _ctx: &mut EventCtx, _event: &TextEvent) {}
fn on_access_event(&mut self, ctx: &mut EventCtx, event: &AccessEvent) {
if event.target == ctx.widget_id() {
if ctx.target() == ctx.widget_id() {
match event.action {
accesskit::Action::Default => {
self.checked = !self.checked;