Change default FocusPolicy to Pass (#7161)

# Objective

- While building UI, it makes more sense for most nodes to have a `FocusPolicy` of `Pass`, so that user interaction can correctly bubble
- Only `ButtonBundle` blocks by default

This change means that for someone adding children to a button, it's not needed to change the focus policy of those children to `Pass` for the button to continue to work.

---

## Changelog

- `FocusPolicy` default has changed from `FocusPolicy::Block` to `FocusPolicy::Pass`

## Migration Guide

- `FocusPolicy` default has changed from `FocusPolicy::Block` to `FocusPolicy::Pass`
This commit is contained in:
François 2023-01-12 17:15:20 +00:00
parent 76de9f9407
commit ba3069f008
2 changed files with 23 additions and 23 deletions

View file

@ -63,7 +63,7 @@ pub enum FocusPolicy {
}
impl FocusPolicy {
const DEFAULT: Self = Self::Block;
const DEFAULT: Self = Self::Pass;
}
impl Default for FocusPolicy {
@ -164,9 +164,7 @@ pub fn ui_focus_system(
// Reset their interaction to None to avoid strange stuck state
if let Some(mut interaction) = node.interaction {
// We cannot simply set the interaction to None, as that will trigger change detection repeatedly
if *interaction != Interaction::None {
*interaction = Interaction::None;
}
interaction.set_if_neq(Interaction::None);
}
return None;

View file

@ -96,7 +96,7 @@ pub struct ImageBundle {
}
/// A UI node that is text
#[derive(Bundle, Clone, Debug)]
#[derive(Bundle, Clone, Debug, Default)]
pub struct TextBundle {
/// Describes the size of the node
pub node: Node,
@ -160,25 +160,8 @@ impl TextBundle {
}
}
impl Default for TextBundle {
fn default() -> Self {
TextBundle {
focus_policy: FocusPolicy::Pass,
text: Default::default(),
node: Default::default(),
calculated_size: Default::default(),
style: Default::default(),
transform: Default::default(),
global_transform: Default::default(),
visibility: Default::default(),
computed_visibility: Default::default(),
z_index: Default::default(),
}
}
}
/// A UI node that is a button
#[derive(Bundle, Clone, Debug, Default)]
#[derive(Bundle, Clone, Debug)]
pub struct ButtonBundle {
/// Describes the size of the node
pub node: Node,
@ -213,3 +196,22 @@ pub struct ButtonBundle {
/// Indicates the depth at which the node should appear in the UI
pub z_index: ZIndex,
}
impl Default for ButtonBundle {
fn default() -> Self {
Self {
focus_policy: FocusPolicy::Block,
node: Default::default(),
button: Default::default(),
style: Default::default(),
interaction: Default::default(),
background_color: Default::default(),
image: Default::default(),
transform: Default::default(),
global_transform: Default::default(),
visibility: Default::default(),
computed_visibility: Default::default(),
z_index: Default::default(),
}
}
}