use std::any::{Any, TypeId};
use std::cell::Cell;
use std::fmt::{self, Debug, Display};
use std::rc::Rc;
use waft_embedder::{Clip, Matrix4, Offset, Size, TextDirection};
use waft_foundation::{App, Handle, ListenableObject, Listener, ValueNotifier};
use waft_painting::{Alignment, AlignmentGeometry};
use waft_rendering::{
AnyRenderBox, AnyRenderObject, BoxConstraints, BoxHitTestResult, ClipRectLayer, Constraints,
ContainerBoxParentData, ContainerParentData, ContainerParentDataMixin,
ContainerRenderObjectData, ContainerRenderObjectMixin, LayerHandle, PaintingContext,
ParentData, PipelineOwner, RenderBox, RenderBoxData, RenderHandle, RenderObject,
RenderObjectData, RenderObjectWithChildData, RenderObjectWithChildMixin,
RenderObjectWithLayoutCallbackData, RenderObjectWithLayoutCallbackMixin, RenderProxyBoxMixin,
StackParentData,
};
use waft_scheduler::{
FrameCallback, SchedulerBinding, SchedulerPhase, Ticker, TickerCallback, TickerProviderObject,
};
use crate::framework::{
AnyElement, BuildContext, BuildScope, BuildScopeCallback, Element, ElementData, GlobalKey,
InheritedWidget, IntoWidget, KeyRef, MultiChildRenderObjectElementBase,
MultiChildRenderObjectElementData, MultiChildRenderObjectWidget, RenderObjectElement,
RenderObjectElementData, RenderObjectElementWidget, RenderObjectWidget,
SingleChildRenderObjectWidget, Slot, State, StateData, StatefulWidget, Widget, WidgetKind,
WidgetRef, downcast_widget,
};
use crate::view::View;
use crate::widgets::basic::{Builder, Directionality, WidgetBuilder};
use crate::widgets::media_query::MediaQuery;
use crate::widgets::ticker_provider::{
TickerMode, TickerProviderStateMixin, TickerProviderStateMixinData,
};
pub type OverlayChildLayoutBuilder =
Rc<dyn Fn(&mut App, BuildContext, OverlayChildLayoutInfo) -> WidgetRef>;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct OverlayChildLayoutInfo {
child_size: Size,
child_paint_transform: Matrix4,
overlay_size: Size,
}
impl OverlayChildLayoutInfo {
pub fn child_size(&self) -> Size {
self.child_size
}
pub fn child_paint_transform(&self) -> Matrix4 {
self.child_paint_transform
}
pub fn overlay_size(&self) -> Size {
self.overlay_size
}
}
pub struct OverlayEntry {
pub builder: WidgetBuilder,
opaque: bool,
maintain_state: bool,
can_size_overlay: bool,
overlay_entry_state_notifier:
Option<Handle<ValueNotifier<Option<Handle<OverlayEntryWidgetState>>>>>,
overlay: Option<Handle<OverlayState>>,
key: GlobalKey,
disposed_by_owner: bool,
}
impl OverlayEntry {
pub fn new(
app: &mut App,
builder: WidgetBuilder,
opaque: bool,
maintain_state: bool,
can_size_overlay: bool,
) -> Handle<OverlayEntry> {
let notifier = app.create(ValueNotifier::new(None));
app.create(OverlayEntry {
builder,
opaque,
maintain_state,
can_size_overlay,
overlay_entry_state_notifier: Some(notifier),
overlay: None,
key: GlobalKey::new(),
disposed_by_owner: false,
})
}
pub fn opaque(self: Handle<Self>, app: &App) -> bool {
app.get(self).opaque
}
pub fn set_opaque(self: Handle<Self>, app: &mut App, value: bool) {
debug_assert!(!app.get(self).disposed_by_owner);
if app.get(self).opaque == value {
return;
}
app.get_mut(self).opaque = value;
if let Some(overlay) = app.get(self).overlay {
overlay.did_change_entry_opacity(app);
}
}
pub fn maintain_state(self: Handle<Self>, app: &App) -> bool {
app.get(self).maintain_state
}
pub fn set_maintain_state(self: Handle<Self>, app: &mut App, value: bool) {
debug_assert!(!app.get(self).disposed_by_owner);
if app.get(self).maintain_state == value {
return;
}
app.get_mut(self).maintain_state = value;
let overlay = app.get(self).overlay.expect("inserted into an Overlay");
overlay.did_change_entry_opacity(app);
}
pub fn can_size_overlay(self: Handle<Self>, app: &App) -> bool {
app.get(self).can_size_overlay
}
pub fn mounted(self: Handle<Self>, app: &App) -> bool {
app.get(self)
.overlay_entry_state_notifier
.is_some_and(|notifier| app.get(notifier).value().is_some())
}
pub fn remove(self: Handle<Self>, app: &mut App) {
let overlay = app
.get(self)
.overlay
.expect("An OverlayEntry should be removed only once.");
debug_assert!(!app.get(self).disposed_by_owner);
app.get_mut(self).overlay = None;
if !overlay.mounted(app) {
return;
}
app.get_mut(overlay).entries.retain(|entry| *entry != self);
if SchedulerBinding::scheduler_phase(app) == SchedulerPhase::PersistentCallbacks {
SchedulerBinding::add_post_frame_callback(
app,
FrameCallback::handle_method(overlay, OverlayState::mark_dirty_after_frame),
View::maybe_id_of(app, overlay.context(app)),
);
} else {
overlay.mark_dirty(app);
}
}
pub fn mark_needs_build(self: Handle<Self>, app: &mut App) {
debug_assert!(!app.get(self).disposed_by_owner);
let key = app.get(self).key.clone();
if let Some(state) = key.current_state::<OverlayEntryWidgetState>(app) {
state.mark_needs_build(app);
}
}
fn did_unmount(self: Handle<Self>, app: &mut App) {
debug_assert!(!self.mounted(app));
if app.get(self).disposed_by_owner {
self.dispose_notifier(app);
}
}
fn dispose_notifier(self: Handle<Self>, app: &mut App) {
if let Some(notifier) = app.get_mut(self).overlay_entry_state_notifier.take() {
app.get_mut(notifier).dispose();
}
}
pub fn dispose(self: Handle<Self>, app: &mut App) {
debug_assert!(!app.get(self).disposed_by_owner);
debug_assert!(
app.get(self).overlay.is_none(),
"An OverlayEntry must first be removed from the Overlay before dispose is called."
);
app.get_mut(self).disposed_by_owner = true;
if !self.mounted(app) {
self.dispose_notifier(app);
}
}
}
impl ListenableObject for OverlayEntry {
fn add_listener(self: Handle<Self>, app: &mut App, listener: Listener) {
debug_assert!(!app.get(self).disposed_by_owner);
if let Some(notifier) = app.get(self).overlay_entry_state_notifier {
notifier.add_listener(app, listener);
}
}
fn remove_listener(self: Handle<Self>, app: &mut App, listener: &Listener) {
if let Some(notifier) = app.get(self).overlay_entry_state_notifier {
notifier.remove_listener(app, listener);
}
}
}
impl Debug for OverlayEntry {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"OverlayEntry(opaque: {}; maintainState: {}){}",
self.opaque,
self.maintain_state,
if self.disposed_by_owner {
"(DISPOSED)"
} else {
""
}
)
}
}
#[derive(Debug)]
struct OverlayEntryWidget {
key: KeyRef,
entry: Handle<OverlayEntry>,
overlay_state: Handle<OverlayState>,
ticker_enabled: bool,
}
impl StatefulWidget for OverlayEntryWidget {
type State = OverlayEntryWidgetState;
fn key(&self) -> Option<&KeyRef> {
Some(&self.key)
}
fn create_state(&self) -> OverlayEntryWidgetState {
OverlayEntryWidgetState {
state: StateData::new(),
theater: None,
sorted_theater_siblings: None,
}
}
}
struct OverlayEntryWidgetState {
state: StateData<OverlayEntryWidget>,
theater: Option<RenderHandle<RenderTheater>>,
sorted_theater_siblings: Option<Vec<Handle<OverlayEntryLocation>>>,
}
impl OverlayEntryWidgetState {
fn theater(self: Handle<Self>, app: &App) -> RenderHandle<RenderTheater> {
app.get(self).theater.expect("mounted under an Overlay")
}
fn add(self: Handle<Self>, app: &mut App, child: Handle<OverlayEntryLocation>) {
debug_assert!(self.mounted(app));
let z_order_index = app.get(child).z_order_index;
let existing = app
.get(self)
.sorted_theater_siblings
.clone()
.unwrap_or_default();
debug_assert!(!existing.contains(&child));
let mut insert_position = existing.len();
while insert_position > 0
&& app.get(existing[insert_position - 1]).z_order_index > z_order_index
{
insert_position -= 1;
}
app.get_mut(self)
.sorted_theater_siblings
.get_or_insert_with(Vec::new)
.insert(insert_position, child);
}
fn remove(self: Handle<Self>, app: &mut App, child: Handle<OverlayEntryLocation>) {
let children = app.get_mut(self).sorted_theater_siblings.as_mut();
debug_assert!(children.is_some());
let Some(children) = children else { return };
let position = children.iter().position(|other| *other == child);
debug_assert!(position.is_some(), "the child was in the collection");
if let Some(position) = position {
children.remove(position);
}
}
fn paint_order_children(
self: Handle<Self>,
app: &App,
) -> Vec<RenderHandle<RenderDeferredLayoutBox>> {
self.create_child_list(app, false)
}
fn create_child_list(
self: Handle<Self>,
app: &App,
reversed: bool,
) -> Vec<RenderHandle<RenderDeferredLayoutBox>> {
let Some(children) = app.get(self).sorted_theater_siblings.as_ref() else {
return Vec::new();
};
let mut boxes: Vec<RenderHandle<RenderDeferredLayoutBox>> = children
.iter()
.filter_map(|candidate| app.get(*candidate).overlay_child_render_box)
.collect();
if reversed {
boxes.reverse();
}
boxes
}
fn mark_needs_build(self: Handle<Self>, app: &mut App) {
self.set_state(app, |_state| {});
}
}
impl State for OverlayEntryWidgetState {
type Widget = OverlayEntryWidget;
crate::state_accessors!();
fn init_state(self: Handle<Self>, app: &mut App) {
let entry = self.widget(app).entry;
let notifier = app
.get(entry)
.overlay_entry_state_notifier
.expect("a live OverlayEntry");
notifier.set_value(app, Some(self));
let context = self.context(app);
let theater = context
.find_ancestor_render_object_of_type::<RenderTheater>(app)
.expect("an _OverlayEntryWidget is built by an Overlay");
app.get_mut(self).theater = Some(theater);
debug_assert!(app.get(self).sorted_theater_siblings.is_none());
}
fn did_update_widget(self: Handle<Self>, app: &mut App, old_widget: &OverlayEntryWidget) {
debug_assert!(old_widget.entry == self.widget(app).entry);
let changed = old_widget.overlay_state != self.widget(app).overlay_state;
if changed {
let context = self.context(app);
let new_theater = context
.find_ancestor_render_object_of_type::<RenderTheater>(app)
.expect("an _OverlayEntryWidget is built by an Overlay");
debug_assert!(app.get(self).theater != Some(new_theater));
app.get_mut(self).theater = Some(new_theater);
}
}
fn dispose(self: Handle<Self>, app: &mut App) {
let entry = self.widget(app).entry;
if let Some(notifier) = app.get(entry).overlay_entry_state_notifier {
notifier.set_value(app, None);
}
entry.did_unmount(app);
app.get_mut(self).sorted_theater_siblings = None;
}
fn build(self: Handle<Self>, app: &mut App, _context: BuildContext) -> WidgetRef {
let widget = self.widget(app);
let (ticker_enabled, entry) = (widget.ticker_enabled, widget.entry);
let theater = self.theater(app);
let builder = Rc::clone(&app.get(entry).builder);
TickerMode::new(
ticker_enabled,
RenderTheaterMarker {
theater,
overlay_entry_widget_state: self,
child: Builder { key: None, builder }.into_widget(),
},
)
.into_widget()
}
}
#[derive(Debug)]
pub struct Overlay {
pub key: Option<KeyRef>,
pub initial_entries: Vec<Handle<OverlayEntry>>,
pub clip_behavior: Clip,
pub always_size_to_content: bool,
}
impl Overlay {
pub fn new() -> Overlay {
Overlay::default()
}
pub fn key(mut self, key: KeyRef) -> Overlay {
self.key = Some(key);
self
}
pub fn initial_entries(
mut self,
entries: impl IntoIterator<Item = Handle<OverlayEntry>>,
) -> Overlay {
self.initial_entries = entries.into_iter().collect();
self
}
pub fn clip_behavior(mut self, clip_behavior: Clip) -> Overlay {
self.clip_behavior = clip_behavior;
self
}
pub fn always_size_to_content(mut self, always_size_to_content: bool) -> Overlay {
self.always_size_to_content = always_size_to_content;
self
}
pub fn wrap<K>(child: impl IntoWidget<K>) -> WrappingOverlay {
WrappingOverlay {
key: None,
clip_behavior: Clip::HardEdge,
always_size_to_content: false,
child: child.into_widget(),
}
}
pub fn of(app: &mut App, context: BuildContext, root_overlay: bool) -> Handle<OverlayState> {
Overlay::maybe_of(app, context, root_overlay).expect(
"No Overlay widget found. Some widgets require an Overlay widget ancestor for \
correct operation. The most common way to add an Overlay to an application is to \
include a WidgetsApp or Navigator widget in the run_app() call.",
)
}
pub fn maybe_of(
app: &mut App,
context: BuildContext,
root_overlay: bool,
) -> Option<Handle<OverlayState>> {
let marker = RenderTheaterMarker::maybe_of(app, context, root_overlay, false)?;
Some(marker.overlay_entry_widget_state.widget(app).overlay_state)
}
}
impl Default for Overlay {
fn default() -> Overlay {
Overlay {
key: None,
initial_entries: Vec::new(),
clip_behavior: Clip::HardEdge,
always_size_to_content: false,
}
}
}
impl StatefulWidget for Overlay {
type State = OverlayState;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_state(&self) -> OverlayState {
OverlayState {
state: StateData::new(),
ticker_provider: TickerProviderStateMixinData::new(),
entries: Vec::new(),
}
}
}
pub struct OverlayState {
state: StateData<Overlay>,
ticker_provider: TickerProviderStateMixinData,
entries: Vec<Handle<OverlayEntry>>,
}
impl OverlayState {
fn insertion_index(
self: Handle<Self>,
app: &App,
below: Option<Handle<OverlayEntry>>,
above: Option<Handle<OverlayEntry>>,
) -> usize {
debug_assert!(above.is_none() || below.is_none());
let entries = &app.get(self).entries;
if let Some(below) = below {
return entries
.iter()
.position(|entry| *entry == below)
.expect("`below` is in this Overlay");
}
if let Some(above) = above {
return entries
.iter()
.position(|entry| *entry == above)
.expect("`above` is in this Overlay")
+ 1;
}
entries.len()
}
fn debug_can_insert_entry(self: Handle<Self>, app: &App, entry: Handle<OverlayEntry>) -> bool {
assert!(
self.mounted(app),
"Attempted to insert an OverlayEntry to an already disposed Overlay."
);
let current_overlay = app.get(entry).overlay;
assert!(
!app.get(self).entries.contains(&entry),
"The specified entry is already present in the target Overlay. Consider calling \
remove on the OverlayEntry before inserting it to a different Overlay."
);
assert!(
current_overlay.is_none(),
"The specified entry is already present in a different Overlay. Consider calling \
remove on the OverlayEntry before inserting it to a different Overlay."
);
true
}
pub fn insert(
self: Handle<Self>,
app: &mut App,
entry: Handle<OverlayEntry>,
below: Option<Handle<OverlayEntry>>,
above: Option<Handle<OverlayEntry>>,
) {
debug_assert!(self.debug_verify_insert_position(app, above, below, None));
debug_assert!(self.debug_can_insert_entry(app, entry));
app.get_mut(entry).overlay = Some(self);
let index = self.insertion_index(app, below, above);
self.set_state(app, |state| state.entries.insert(index, entry));
}
pub fn insert_all(
self: Handle<Self>,
app: &mut App,
entries: Vec<Handle<OverlayEntry>>,
below: Option<Handle<OverlayEntry>>,
above: Option<Handle<OverlayEntry>>,
) {
debug_assert!(self.debug_verify_insert_position(app, above, below, None));
if cfg!(debug_assertions) {
for entry in &entries {
assert!(self.debug_can_insert_entry(app, *entry));
}
}
if entries.is_empty() {
return;
}
for entry in &entries {
debug_assert!(app.get(*entry).overlay.is_none());
app.get_mut(*entry).overlay = Some(self);
}
let index = self.insertion_index(app, below, above);
self.set_state(app, move |state| {
state.entries.splice(index..index, entries);
});
}
fn debug_verify_insert_position(
self: Handle<Self>,
app: &App,
above: Option<Handle<OverlayEntry>>,
below: Option<Handle<OverlayEntry>>,
new_entries: Option<&[Handle<OverlayEntry>]>,
) -> bool {
assert!(
above.is_none() || below.is_none(),
"Only one of `above` and `below` may be specified."
);
let present = |entry: Handle<OverlayEntry>| {
app.get(entry).overlay == Some(self)
&& app.get(self).entries.contains(&entry)
&& new_entries.is_none_or(|entries| entries.contains(&entry))
};
assert!(
above.is_none_or(present),
"The provided entry used for `above` must be present in the Overlay."
);
assert!(
below.is_none_or(present),
"The provided entry used for `below` must be present in the Overlay."
);
true
}
pub fn rearrange(
self: Handle<Self>,
app: &mut App,
new_entries: Vec<Handle<OverlayEntry>>,
below: Option<Handle<OverlayEntry>>,
above: Option<Handle<OverlayEntry>>,
) {
debug_assert!(self.debug_verify_insert_position(app, above, below, Some(&new_entries)));
debug_assert!(
new_entries.iter().all(|entry| {
let overlay = app.get(*entry).overlay;
overlay.is_none() || overlay == Some(self)
}),
"One or more of the specified entries are already present in another Overlay."
);
debug_assert!(
new_entries.iter().all(|entry| {
let entries = &app.get(self).entries;
entries.iter().position(|other| other == entry)
== entries.iter().rposition(|other| other == entry)
}),
"One or more of the specified entries are specified multiple times."
);
if new_entries.is_empty() {
return;
}
if app.get(self).entries == new_entries {
return;
}
let old: Vec<Handle<OverlayEntry>> = app
.get(self)
.entries
.iter()
.copied()
.filter(|entry| !new_entries.contains(entry))
.collect();
for entry in &new_entries {
if app.get(*entry).overlay.is_none() {
app.get_mut(*entry).overlay = Some(self);
}
}
let index = {
let entries = &new_entries;
if let Some(below) = below {
entries
.iter()
.position(|entry| *entry == below)
.expect("`below` is in `new_entries`")
} else if let Some(above) = above {
entries
.iter()
.position(|entry| *entry == above)
.expect("`above` is in `new_entries`")
+ 1
} else {
entries.len()
}
};
self.set_state(app, move |state| {
state.entries.clear();
state.entries.extend(new_entries);
state.entries.splice(index..index, old);
});
}
fn mark_dirty(self: Handle<Self>, app: &mut App) {
if self.mounted(app) {
self.set_state(app, |_state| {});
}
}
fn mark_dirty_after_frame(self: Handle<Self>, app: &mut App, _time_stamp: std::time::Duration) {
self.mark_dirty(app);
}
pub fn debug_is_visible(self: Handle<Self>, app: &App, entry: Handle<OverlayEntry>) -> bool {
let mut result = false;
debug_assert!(app.get(self).entries.contains(&entry));
if cfg!(debug_assertions) {
let entries = app.get(self).entries.clone();
for index in (1..entries.len()).rev() {
let candidate = entries[index];
if candidate == entry {
result = true;
break;
}
if candidate.opaque(app) {
break;
}
}
}
result
}
fn did_change_entry_opacity(self: Handle<Self>, app: &mut App) {
self.set_state(app, |_state| {});
}
}
impl TickerProviderStateMixin for OverlayState {
fn ticker_provider_data(self: Handle<Self>, app: &App) -> &TickerProviderStateMixinData {
&app.get(self).ticker_provider
}
fn ticker_provider_data_mut(
self: Handle<Self>,
app: &mut App,
) -> &mut TickerProviderStateMixinData {
&mut app.get_mut(self).ticker_provider
}
}
impl TickerProviderObject for OverlayState {
fn create_ticker(self: Handle<Self>, app: &mut App, on_tick: TickerCallback) -> Handle<Ticker> {
TickerProviderStateMixin::create_ticker(self, app, on_tick)
}
}
impl State for OverlayState {
type Widget = Overlay;
crate::state_accessors!();
fn init_state(self: Handle<Self>, app: &mut App) {
let initial = self.widget(app).initial_entries.clone();
self.insert_all(app, initial, None, None);
}
fn activate(self: Handle<Self>, app: &mut App) {
TickerProviderStateMixin::activate(self, app);
}
fn dispose(self: Handle<Self>, app: &mut App) {
TickerProviderStateMixin::dispose(self, app);
}
fn build(self: Handle<Self>, app: &mut App, _context: BuildContext) -> WidgetRef {
let mut children: Vec<WidgetRef> = Vec::new();
let mut onstage = true;
let mut onstage_count = 0usize;
for entry in app.get(self).entries.clone().into_iter().rev() {
let key: KeyRef = Rc::new(app.get(entry).key.clone());
if onstage {
onstage_count += 1;
children.push(
OverlayEntryWidget {
key,
overlay_state: self,
entry,
ticker_enabled: true,
}
.into_widget(),
);
if app.get(entry).opaque {
onstage = false;
}
} else if app.get(entry).maintain_state {
children.push(
OverlayEntryWidget {
key,
overlay_state: self,
entry,
ticker_enabled: false,
}
.into_widget(),
);
}
}
let skip_count = children.len() - onstage_count;
children.reverse();
let widget = self.widget(app);
Theater {
skip_count,
clip_behavior: widget.clip_behavior,
always_size_to_content: widget.always_size_to_content,
children,
}
.into_widget()
}
}
#[derive(Debug)]
pub struct WrappingOverlay {
pub key: Option<KeyRef>,
pub clip_behavior: Clip,
pub always_size_to_content: bool,
pub child: WidgetRef,
}
impl WrappingOverlay {
pub fn key(mut self, key: KeyRef) -> WrappingOverlay {
self.key = Some(key);
self
}
pub fn clip_behavior(mut self, clip_behavior: Clip) -> WrappingOverlay {
self.clip_behavior = clip_behavior;
self
}
pub fn always_size_to_content(mut self, always_size_to_content: bool) -> WrappingOverlay {
self.always_size_to_content = always_size_to_content;
self
}
}
impl StatefulWidget for WrappingOverlay {
type State = WrappingOverlayState;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_state(&self) -> WrappingOverlayState {
WrappingOverlayState {
state: StateData::new(),
entry: None,
}
}
}
pub struct WrappingOverlayState {
state: StateData<WrappingOverlay>,
entry: Option<Handle<OverlayEntry>>,
}
impl WrappingOverlayState {
fn entry(self: Handle<Self>, app: &mut App) -> Handle<OverlayEntry> {
if let Some(entry) = app.get(self).entry {
return entry;
}
let entry = OverlayEntry::new(
app,
Rc::new(move |app: &mut App, _context| self.widget(app).child.clone()),
true,
false,
true,
);
app.get_mut(self).entry = Some(entry);
entry
}
}
impl State for WrappingOverlayState {
type Widget = WrappingOverlay;
crate::state_accessors!();
fn did_update_widget(self: Handle<Self>, app: &mut App, _old_widget: &WrappingOverlay) {
let entry = self.entry(app);
entry.mark_needs_build(app);
}
fn dispose(self: Handle<Self>, app: &mut App) {
let entry = self.entry(app);
entry.remove(app);
entry.dispose(app);
}
fn build(self: Handle<Self>, app: &mut App, _context: BuildContext) -> WidgetRef {
let entry = self.entry(app);
let widget = self.widget(app);
Overlay {
key: None,
initial_entries: vec![entry],
clip_behavior: widget.clip_behavior,
always_size_to_content: widget.always_size_to_content,
}
.into_widget()
}
}
#[derive(Debug)]
struct Theater {
skip_count: usize,
clip_behavior: Clip,
always_size_to_content: bool,
children: Vec<WidgetRef>,
}
impl Theater {
fn into_widget(self) -> WidgetRef {
debug_assert!(self.children.len() >= self.skip_count);
Rc::new(self)
}
}
impl Widget for Theater {
fn key(&self) -> Option<&KeyRef> {
None
}
fn create_element(&self, app: &mut App, this: WidgetRef) -> AnyElement {
TheaterElement::create(app, this).as_element()
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn widget_type(&self) -> std::any::TypeId {
std::any::TypeId::of::<Theater>()
}
fn kind(&self) -> WidgetKind {
WidgetKind::Other
}
}
impl RenderObjectWidget for Theater {
type RenderObject = RenderTheater;
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
let text_direction = Directionality::of(app, context);
let render_object = RenderTheater::new(
app,
text_direction,
self.skip_count,
self.clip_behavior,
self.always_size_to_content,
);
render_object.as_object()
}
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: RenderHandle<RenderTheater>,
) {
render_object.set_skip_count(app, self.skip_count);
let text_direction = Directionality::of(app, context);
render_object.set_text_direction(app, text_direction);
render_object.set_clip_behavior(app, self.clip_behavior);
render_object.set_always_size_to_content(app, self.always_size_to_content);
}
}
impl MultiChildRenderObjectWidget for Theater {
fn children(&self) -> &[WidgetRef] {
&self.children
}
}
struct TheaterElement {
element: ElementData,
render_object_element: RenderObjectElementData,
multi_child: MultiChildRenderObjectElementData,
}
impl TheaterElement {
fn create(app: &mut App, widget: WidgetRef) -> Handle<TheaterElement> {
debug_assert!(downcast_widget::<Theater>(&*widget).is_some());
app.create(TheaterElement {
element: ElementData::new(widget),
render_object_element: RenderObjectElementData::default(),
multi_child: MultiChildRenderObjectElementData::default(),
})
}
fn entry_at_slot(self: Handle<Self>, app: &App, slot: Option<&Slot>) -> Handle<OverlayEntry> {
let index = <Self as MultiChildRenderObjectElementBase>::indexed_slot(slot).index;
let widget = Self::widget_of(self.as_element().widget(app));
downcast_widget::<OverlayEntryWidget>(&*widget.children[index])
.expect("a Theater's children are _OverlayEntryWidgets")
.entry
}
}
impl RenderObjectElementWidget for TheaterElement {
type Widget = Theater;
fn widget_of(widget: &WidgetRef) -> &Theater {
downcast_widget::<Theater>(&**widget).expect("a _TheaterElement holds a _Theater")
}
}
impl RenderObjectElement for TheaterElement {
fn render_object_element_data(self: Handle<Self>, app: &App) -> &RenderObjectElementData {
&app.get(self).render_object_element
}
fn render_object_element_data_mut(
self: Handle<Self>,
app: &mut App,
) -> &mut RenderObjectElementData {
&mut app.get_mut(self).render_object_element
}
}
impl MultiChildRenderObjectElementBase for TheaterElement {
fn multi_child_data(self: Handle<Self>, app: &App) -> &MultiChildRenderObjectElementData {
&app.get(self).multi_child
}
fn multi_child_data_mut(
self: Handle<Self>,
app: &mut App,
) -> &mut MultiChildRenderObjectElementData {
&mut app.get_mut(self).multi_child
}
}
impl Element for TheaterElement {
crate::element_accessors!();
const IS_RENDER_OBJECT_ELEMENT: bool = true;
fn render_object(self: Handle<Self>, app: &App) -> Option<AnyRenderObject> {
self.render_object_element_data(app).render_object()
}
fn render_object_attaching_child(self: Handle<Self>, _app: &App) -> Option<AnyElement> {
None
}
fn debug_doing_build(self: Handle<Self>, app: &App) -> bool {
self.render_object_element_data(app).debug_doing_build()
}
fn perform_rebuild(self: Handle<Self>, app: &mut App) {
RenderObjectElement::perform_rebuild(self, app);
}
fn deactivate(self: Handle<Self>, app: &mut App) {
RenderObjectElement::deactivate(self, app);
}
fn unmount(self: Handle<Self>, app: &mut App) {
RenderObjectElement::unmount(self, app);
}
fn update_parent_data(self: Handle<Self>, app: &mut App, parent_data_element: AnyElement) {
RenderObjectElement::update_parent_data(self, app, parent_data_element);
}
fn update_slot(self: Handle<Self>, app: &mut App, new_slot: Option<Slot>) {
RenderObjectElement::update_slot(self, app, new_slot);
}
fn attach_render_object(self: Handle<Self>, app: &mut App, new_slot: Option<Slot>) {
RenderObjectElement::attach_render_object(self, app, new_slot);
}
fn detach_render_object(self: Handle<Self>, app: &mut App) {
RenderObjectElement::detach_render_object(self, app);
}
fn visit_children(self: Handle<Self>, app: &App, visitor: &mut dyn FnMut(AnyElement)) {
MultiChildRenderObjectElementBase::visit_children(self, app, visitor);
}
fn forget_child(self: Handle<Self>, app: &mut App, child: AnyElement) {
MultiChildRenderObjectElementBase::forget_child(self, app, child);
}
fn mount(
self: Handle<Self>,
app: &mut App,
parent: Option<AnyElement>,
new_slot: Option<Slot>,
) {
MultiChildRenderObjectElementBase::mount(self, app, parent, new_slot);
}
fn update(self: Handle<Self>, app: &mut App, new_widget: WidgetRef) {
MultiChildRenderObjectElementBase::update(self, app, new_widget);
}
fn insert_render_object_child(
self: Handle<Self>,
app: &mut App,
child: AnyRenderObject,
slot: Option<Slot>,
) {
MultiChildRenderObjectElementBase::insert_render_object_child(
self,
app,
child,
slot.clone(),
);
let entry = self.entry_at_slot(app, slot.as_ref());
child
.parent_data_of_mut::<TheaterParentData>(app)
.overlay_entry = Some(entry);
}
fn move_render_object_child(
self: Handle<Self>,
app: &mut App,
child: AnyRenderObject,
old_slot: Option<Slot>,
new_slot: Option<Slot>,
) {
MultiChildRenderObjectElementBase::move_render_object_child(
self,
app,
child,
old_slot,
new_slot.clone(),
);
if cfg!(debug_assertions) {
let entry_at_new_slot = self.entry_at_slot(app, new_slot.as_ref());
let parent_data = child.parent_data_of::<TheaterParentData>(app);
assert!(parent_data.overlay_entry == Some(entry_at_new_slot));
}
}
fn remove_render_object_child(
self: Handle<Self>,
app: &mut App,
child: AnyRenderObject,
slot: Option<Slot>,
) {
MultiChildRenderObjectElementBase::remove_render_object_child(self, app, child, slot);
}
}
#[derive(Debug, Default)]
pub struct TheaterParentData {
stack: StackParentData,
overlay_entry: Option<Handle<OverlayEntry>>,
}
impl TheaterParentData {
pub fn new() -> TheaterParentData {
TheaterParentData {
stack: StackParentData::new(),
overlay_entry: None,
}
}
pub fn stack(&self) -> &StackParentData {
&self.stack
}
pub fn stack_mut(&mut self) -> &mut StackParentData {
&mut self.stack
}
fn paint_order_children(&self, app: &App) -> Vec<RenderHandle<RenderDeferredLayoutBox>> {
match self.child_model(app) {
Some(child_model) => child_model.paint_order_children(app),
None => Vec::new(),
}
}
fn child_model(&self, app: &App) -> Option<Handle<OverlayEntryWidgetState>> {
let notifier = app.get(self.overlay_entry?).overlay_entry_state_notifier?;
Some((*app.get(notifier).value()).expect("the entry is mounted"))
}
fn visit_overlay_portal_children_on_overlay_entry(
&self,
app: &App,
visitor: &mut dyn FnMut(AnyRenderObject),
) {
for child in self.paint_order_children(app) {
visitor(child.as_object());
}
}
}
impl ParentData for TheaterParentData {
fn detach(&mut self) {
ContainerParentDataMixin::detach(self);
}
fn provide(&self, id: TypeId) -> Option<&dyn Any> {
if id == TypeId::of::<TheaterParentData>() {
return Some(self);
}
self.stack.provide(id)
}
fn provide_mut(&mut self, id: TypeId) -> Option<&mut dyn Any> {
if id == TypeId::of::<TheaterParentData>() {
return Some(self);
}
self.stack.provide_mut(id)
}
}
impl ContainerParentDataMixin for TheaterParentData {
type ChildType = AnyRenderBox;
fn container_parent_data(&self) -> &ContainerParentData<AnyRenderBox> {
self.stack.container_parent_data()
}
fn container_parent_data_mut(&mut self) -> &mut ContainerParentData<AnyRenderBox> {
self.stack.container_parent_data_mut()
}
}
impl ContainerBoxParentData for TheaterParentData {
fn box_parent_data(&self) -> &waft_rendering::BoxParentData {
ContainerBoxParentData::box_parent_data(&self.stack)
}
fn box_parent_data_mut(&mut self) -> &mut waft_rendering::BoxParentData {
ContainerBoxParentData::box_parent_data_mut(&mut self.stack)
}
}
impl Display for TheaterParentData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(&self.stack, f)
}
}
fn layout_positioned_theater_child(
app: &mut App,
child: AnyRenderBox,
size: Size,
alignment: Alignment,
) {
let child_constraints = {
let parent_data = child.as_object().parent_data_of::<TheaterParentData>(app);
debug_assert!(parent_data.stack.is_positioned());
parent_data.stack.positioned_child_constraints(size)
};
child.layout(app, child_constraints, true);
let child_size = child.size(app);
let parent_data = child.as_object().parent_data_of::<TheaterParentData>(app);
let x = match (parent_data.stack.left, parent_data.stack.right) {
(Some(left), _) => left,
(None, Some(right)) => size.width() - right - child_size.width(),
(None, None) => alignment.along_offset(size - child_size).dx(),
};
let y = match (parent_data.stack.top, parent_data.stack.bottom) {
(Some(top), _) => top,
(None, Some(bottom)) => size.height() - bottom - child_size.height(),
(None, None) => alignment.along_offset(size - child_size).dy(),
};
child
.as_object()
.parent_data_of_mut::<TheaterParentData>(app)
.set_offset(Offset::new(x, y));
}
pub struct TheaterChildren {
theater: Option<RenderHandle<RenderTheater>>,
entry: Option<AnyRenderBox>,
reversed: bool,
remaining: usize,
phase: TheaterChildrenPhase,
candidate: Option<Handle<OverlayEntryLocation>>,
}
enum TheaterChildrenPhase {
Entry,
OpenPortals,
Portals,
NextEntry,
}
impl TheaterChildren {
fn single(child: Option<AnyRenderBox>) -> Self {
Self {
theater: None,
entry: child,
reversed: false,
remaining: 0,
phase: TheaterChildrenPhase::Entry,
candidate: None,
}
}
fn theater(theater: RenderHandle<RenderTheater>, app: &App, reversed: bool) -> Self {
Self {
theater: Some(theater),
entry: if reversed {
theater.last_onstage_child(app)
} else {
theater.first_onstage_child(app)
},
reversed,
remaining: theater.child_count(app) - theater.get(app).skip_count,
phase: TheaterChildrenPhase::Entry,
candidate: None,
}
}
pub fn next(&mut self, app: &App) -> Option<AnyRenderBox> {
let Some(theater) = self.theater else {
return self.entry.take();
};
loop {
let entry = self.entry?;
match self.phase {
TheaterChildrenPhase::Entry => {
self.phase = TheaterChildrenPhase::OpenPortals;
if !self.reversed {
return Some(entry);
}
}
TheaterChildrenPhase::OpenPortals => {
let model = entry
.as_object()
.parent_data_of::<TheaterParentData>(app)
.child_model(app);
self.candidate = model.and_then(|model| {
let list = app.get(model).sorted_theater_siblings.as_ref()?;
if self.reversed {
list.last().copied()
} else {
list.first().copied()
}
});
self.phase = TheaterChildrenPhase::Portals;
}
TheaterChildrenPhase::Portals => {
if let Some(candidate) = self.candidate {
let render_box = app.get(candidate).overlay_child_render_box;
self.candidate = (|| {
let model = app.get(candidate).child_model;
let list = app.get(model).sorted_theater_siblings.as_ref()?;
let index = list.iter().position(|value| *value == candidate)?;
if self.reversed {
index.checked_sub(1).and_then(|i| list.get(i).copied())
} else {
list.get(index + 1).copied()
}
})();
if let Some(render_box) = render_box {
return Some(render_box.as_box());
}
} else {
self.phase = TheaterChildrenPhase::NextEntry;
if self.reversed {
return Some(entry);
}
}
}
TheaterChildrenPhase::NextEntry => {
self.entry = if self.reversed {
self.remaining -= 1;
if self.remaining == 0 {
None
} else {
theater.child_before(app, entry)
}
} else {
theater.child_after(app, entry)
};
self.phase = TheaterChildrenPhase::Entry;
}
}
}
}
#[cfg(test)]
fn collect(mut self, app: &App) -> Vec<AnyRenderBox> {
let mut children = Vec::new();
while let Some(child) = self.next(app) {
children.push(child);
}
children
}
}
pub trait RenderTheaterMixin: RenderBox {
fn theater(self: RenderHandle<Self>, app: &App) -> RenderHandle<RenderTheater>;
fn children_in_paint_order(self: RenderHandle<Self>, app: &App) -> TheaterChildren;
fn children_in_hit_test_order(self: RenderHandle<Self>, app: &App) -> TheaterChildren;
fn setup_parent_data(self: RenderHandle<Self>, app: &mut App, child: AnyRenderObject) {
if !child.parent_data_is::<TheaterParentData>(app) {
child.set_parent_data(app, TheaterParentData::new());
}
}
fn layout_child(
self: RenderHandle<Self>,
app: &mut App,
child: AnyRenderBox,
non_positioned_child_constraints: BoxConstraints,
) {
let alignment = self.theater(app).resolved_alignment(app);
let is_positioned = child
.as_object()
.parent_data_of::<TheaterParentData>(app)
.stack
.is_positioned();
if !is_positioned {
match child.as_object().downcast::<RenderDeferredLayoutBox>(app) {
Some(deferred) => {
let parent = child
.as_object()
.parent(app)
.expect("a child being laid out has a parent");
deferred.do_layout_from(app, parent, non_positioned_child_constraints);
}
None => child.layout(app, non_positioned_child_constraints, true),
}
child
.as_object()
.parent_data_of_mut::<TheaterParentData>(app)
.set_offset(Offset::ZERO);
} else {
debug_assert!(
child
.as_object()
.downcast::<RenderDeferredLayoutBox>(app)
.is_none(),
"all deferred layout boxes must be non-positioned children."
);
let size = self.size(app);
layout_positioned_theater_child(app, child, size, alignment);
}
}
fn hit_test_children(
self: RenderHandle<Self>,
app: &mut App,
result: &mut BoxHitTestResult<'_>,
position: Offset,
) -> bool {
let mut children = self.children_in_hit_test_order(app);
while let Some(child) = children.next(app) {
let offset = child
.as_object()
.parent_data_of::<TheaterParentData>(app)
.offset();
let is_hit = result.add_with_paint_offset(Some(offset), position, |result, local| {
child.hit_test(app, result, local)
});
if is_hit {
return true;
}
}
false
}
fn paint(
self: RenderHandle<Self>,
app: &mut App,
context: &mut PaintingContext,
offset: Offset,
) {
let mut children = self.children_in_paint_order(app);
while let Some(child) = children.next(app) {
let child_offset = child
.as_object()
.parent_data_of::<TheaterParentData>(app)
.offset();
context.paint_child(app, child.as_object(), child_offset + offset);
}
}
}
pub struct RenderTheater {
render_object: RenderObjectData,
render_box: RenderBoxData,
container: ContainerRenderObjectData<AnyRenderBox>,
alignment_cache: Option<Alignment>,
text_direction: TextDirection,
skip_count: usize,
clip_behavior: Clip,
clip_rect_layer: LayerHandle<Handle<ClipRectLayer>>,
always_size_to_content: bool,
laying_out_size_determining_child: bool,
}
impl RenderTheater {
pub fn new(
app: &mut App,
text_direction: TextDirection,
skip_count: usize,
clip_behavior: Clip,
always_size_to_content: bool,
) -> RenderHandle<RenderTheater> {
RenderHandle::new_box(
app,
RenderTheater {
render_object: RenderObjectData::new(),
render_box: RenderBoxData::new(),
container: ContainerRenderObjectData::new(),
alignment_cache: None,
text_direction,
skip_count,
clip_behavior,
clip_rect_layer: LayerHandle::new(),
always_size_to_content,
laying_out_size_determining_child: false,
},
)
}
pub fn resolved_alignment(self: RenderHandle<Self>, app: &mut App) -> Alignment {
if let Some(cached) = self.get(app).alignment_cache {
return cached;
}
let resolved = AlignmentGeometry::TOP_START.resolve(Some(self.get(app).text_direction));
self.get_mut(app).alignment_cache = Some(resolved);
resolved
}
fn mark_need_resolution(self: RenderHandle<Self>, app: &mut App) {
self.get_mut(app).alignment_cache = None;
self.mark_needs_layout(app);
}
pub fn text_direction(self: RenderHandle<Self>, app: &App) -> TextDirection {
self.get(app).text_direction
}
pub fn set_text_direction(self: RenderHandle<Self>, app: &mut App, value: TextDirection) {
if self.get(app).text_direction == value {
return;
}
self.get_mut(app).text_direction = value;
self.mark_need_resolution(app);
}
pub fn skip_count(self: RenderHandle<Self>, app: &App) -> usize {
self.get(app).skip_count
}
pub fn set_skip_count(self: RenderHandle<Self>, app: &mut App, value: usize) {
if self.get(app).skip_count != value {
self.get_mut(app).skip_count = value;
self.mark_needs_layout(app);
}
}
pub fn clip_behavior(self: RenderHandle<Self>, app: &App) -> Clip {
self.get(app).clip_behavior
}
pub fn set_clip_behavior(self: RenderHandle<Self>, app: &mut App, value: Clip) {
if value != self.get(app).clip_behavior {
self.get_mut(app).clip_behavior = value;
self.mark_needs_paint(app);
}
}
pub fn always_size_to_content(self: RenderHandle<Self>, app: &App) -> bool {
self.get(app).always_size_to_content
}
pub fn set_always_size_to_content(self: RenderHandle<Self>, app: &mut App, value: bool) {
if self.get(app).always_size_to_content != value {
self.get_mut(app).always_size_to_content = value;
self.mark_needs_layout(app);
}
}
fn add_deferred_child(
self: RenderHandle<Self>,
app: &mut App,
child: RenderHandle<RenderDeferredLayoutBox>,
) {
self.adopt_child(app, child.as_object());
self.mark_needs_paint(app);
let surrogate = child.get(app).layout_surrogate;
surrogate.as_object().mark_needs_layout(app);
}
fn remove_deferred_child(
self: RenderHandle<Self>,
app: &mut App,
child: RenderHandle<RenderDeferredLayoutBox>,
) {
self.drop_child(app, child.as_object());
self.mark_needs_paint(app);
}
fn first_onstage_child(self: RenderHandle<Self>, app: &App) -> Option<AnyRenderBox> {
let skip_count = self.get(app).skip_count;
if skip_count == self.child_count(app) {
return None;
}
let mut child = self.first_child(app);
for _ in 0..skip_count {
let current = child.expect("skip_count is at most child_count");
child = self.child_after(app, current);
}
child
}
fn last_onstage_child(self: RenderHandle<Self>, app: &App) -> Option<AnyRenderBox> {
if self.get(app).skip_count == self.child_count(app) {
None
} else {
self.last_child(app)
}
}
fn find_size_determining_child(self: RenderHandle<Self>, app: &App) -> AnyRenderBox {
let mut child = self.last_onstage_child(app);
while let Some(current) = child {
let parent_data = current.as_object().parent_data_of::<TheaterParentData>(app);
let can_size = parent_data
.overlay_entry
.is_some_and(|entry| app.get(entry).can_size_overlay);
if can_size && !parent_data.stack.is_positioned() {
return current;
}
child = self.child_before(app, current);
}
assert!(
!self.get(app).always_size_to_content,
"Overlay was asked to size itself to content but does not have a suitable child. \
When `always_size_to_content` is true, the Overlay requires at least one \
non-positioned `OverlayEntry` with `can_size_overlay` set to true to determine its \
size."
);
panic!(
"Overlay was given infinite constraints and cannot be sized by a suitable child. \
The constraints given to the overlay would result in an illegal infinite size. To \
avoid that, the Overlay tried to size itself to one of its children, but no \
suitable non-positioned child that belongs to an OverlayEntry with \
can_size_overlay set to true could be found."
);
}
}
impl ContainerRenderObjectMixin for RenderTheater {
type ChildType = AnyRenderBox;
type ParentDataType = TheaterParentData;
fn container_data(
self: RenderHandle<Self>,
app: &App,
) -> &ContainerRenderObjectData<AnyRenderBox> {
&self.get(app).container
}
fn container_data_mut(
self: RenderHandle<Self>,
app: &mut App,
) -> &mut ContainerRenderObjectData<AnyRenderBox> {
&mut self.get_mut(app).container
}
}
impl RenderTheaterMixin for RenderTheater {
fn theater(self: RenderHandle<Self>, _app: &App) -> RenderHandle<RenderTheater> {
self
}
fn children_in_paint_order(self: RenderHandle<Self>, app: &App) -> TheaterChildren {
TheaterChildren::theater(self, app, false)
}
fn children_in_hit_test_order(self: RenderHandle<Self>, app: &App) -> TheaterChildren {
TheaterChildren::theater(self, app, true)
}
}
impl RenderObject for RenderTheater {
waft_rendering::render_object_accessors!();
fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
let constraints = self.constraints(app);
let mut size_determining_child = None;
if !self.get(app).always_size_to_content && constraints.biggest().is_finite() {
self.set_size(app, constraints.biggest());
} else {
let child = self.find_size_determining_child(app);
size_determining_child = Some(child);
self.get_mut(app).laying_out_size_determining_child = true;
self.layout_child(app, child, constraints);
self.get_mut(app).laying_out_size_determining_child = false;
let size = child.size(app);
self.set_size(app, size);
}
let non_positioned_child_constraints = BoxConstraints::tight(self.size(app));
let mut children = self.children_in_paint_order(app);
while let Some(child) = children.next(app) {
if Some(child) != size_determining_child {
self.layout_child(app, child, non_positioned_child_constraints);
}
}
}
fn visit_children(
self: RenderHandle<Self>,
app: &App,
visitor: &mut dyn FnMut(AnyRenderObject),
) {
let mut child = self.first_child(app);
while let Some(current) = child {
visitor(current.as_object());
current
.as_object()
.parent_data_of::<TheaterParentData>(app)
.visit_overlay_portal_children_on_overlay_entry(app, visitor);
child = self.child_after(app, current);
}
}
fn did_attach(self: RenderHandle<Self>, app: &mut App, owner: Handle<PipelineOwner>) {
ContainerRenderObjectMixin::did_attach(self, app, owner);
let mut child = self.first_child(app);
while let Some(current) = child {
let deferred = current
.as_object()
.parent_data_of::<TheaterParentData>(app)
.paint_order_children(app);
for deferred_child in deferred {
deferred_child.as_object().attach(app, owner);
}
child = self.child_after(app, current);
}
}
fn did_detach(self: RenderHandle<Self>, app: &mut App) {
ContainerRenderObjectMixin::did_detach(self, app);
let mut child = self.first_child(app);
while let Some(current) = child {
let deferred = current
.as_object()
.parent_data_of::<TheaterParentData>(app)
.paint_order_children(app);
for deferred_child in deferred {
deferred_child.as_object().detach(app);
}
child = self.child_after(app, current);
}
}
fn redepth_children(self: RenderHandle<Self>, app: &mut App) {
let mut children = Vec::new();
RenderObject::visit_children(self, app, &mut |child| children.push(child));
for child in children {
self.as_object().redepth_child(app, child);
}
}
fn paint(
self: RenderHandle<Self>,
app: &mut App,
context: &mut PaintingContext,
offset: Offset,
) {
let clip_behavior = self.clip_behavior(app);
if clip_behavior != Clip::None {
let size = self.size(app);
let old = self.get(app).clip_rect_layer.layer();
let layer = context.push_clip_rect(
app,
self.as_object().needs_compositing(app),
offset,
Offset::ZERO & size,
|app, context, offset| RenderTheaterMixin::paint(self, app, context, offset),
clip_behavior,
old,
);
LayerHandle::set_layer(app, |app| &mut self.get_mut(app).clip_rect_layer, layer);
} else {
LayerHandle::set_layer(app, |app| &mut self.get_mut(app).clip_rect_layer, None);
RenderTheaterMixin::paint(self, app, context, offset);
}
}
fn dispose(self: RenderHandle<Self>, app: &mut App) {
LayerHandle::set_layer(app, |app| &mut self.get_mut(app).clip_rect_layer, None);
waft_rendering::RenderObjectBase::dispose(self, app);
}
}
impl RenderBox for RenderTheater {
waft_rendering::render_box_accessors!();
fn setup_parent_data(self: RenderHandle<Self>, app: &mut App, child: AnyRenderObject) {
RenderTheaterMixin::setup_parent_data(self, app, child)
}
fn hit_test_children(
self: RenderHandle<Self>,
app: &mut App,
result: &mut BoxHitTestResult<'_>,
position: Offset,
) -> bool {
RenderTheaterMixin::hit_test_children(self, app, result, position)
}
}
#[derive(Debug)]
struct RenderTheaterMarker {
theater: RenderHandle<RenderTheater>,
overlay_entry_widget_state: Handle<OverlayEntryWidgetState>,
child: WidgetRef,
}
#[derive(Clone, Copy)]
struct TheaterLocation {
theater: RenderHandle<RenderTheater>,
overlay_entry_widget_state: Handle<OverlayEntryWidgetState>,
}
impl RenderTheaterMarker {
fn of(app: &mut App, context: BuildContext, target_root_overlay: bool) -> TheaterLocation {
RenderTheaterMarker::maybe_of(app, context, target_root_overlay, true).expect(
"No Overlay widget found. This widget requires an Overlay widget ancestor. An \
overlay lets widgets float on top of other widget children. To introduce an \
Overlay widget, you can either directly include one, or use a widget that \
contains an Overlay itself, such as a Navigator or WidgetsApp.",
)
}
fn maybe_of(
app: &mut App,
context: BuildContext,
target_root_overlay: bool,
create_dependency: bool,
) -> Option<TheaterLocation> {
let read = |app: &App, element: AnyElement| {
let widget = element.widget(app);
let marker = downcast_widget::<RenderTheaterMarker>(&**widget)
.expect("a _RenderTheaterMarker element holds a _RenderTheaterMarker");
TheaterLocation {
theater: marker.theater,
overlay_entry_widget_state: marker.overlay_entry_widget_state,
}
};
if target_root_overlay {
let nearest =
context.get_element_for_inherited_widget_of_exact_type::<RenderTheaterMarker>(app);
let ancestor = RenderTheaterMarker::root_marker_of(app, nearest)?;
if create_dependency {
context.depend_on_inherited_element(app, ancestor, None);
}
return Some(read(app, ancestor));
}
if create_dependency {
context.depend_on_inherited_widget_of_exact_type::<RenderTheaterMarker>(app)?;
}
let element =
context.get_element_for_inherited_widget_of_exact_type::<RenderTheaterMarker>(app)?;
Some(read(app, element))
}
fn root_marker_of(app: &App, marker_element: Option<AnyElement>) -> Option<AnyElement> {
let marker_element = marker_element?;
let mut ancestor = None;
marker_element.visit_ancestor_elements(app, &mut |element| {
ancestor =
element.get_element_for_inherited_widget_of_exact_type::<RenderTheaterMarker>(app);
false
});
match ancestor {
None => Some(marker_element),
Some(ancestor) => RenderTheaterMarker::root_marker_of(app, Some(ancestor)),
}
}
}
impl InheritedWidget for RenderTheaterMarker {
fn child(&self) -> &WidgetRef {
&self.child
}
fn update_should_notify(&self, old_widget: &RenderTheaterMarker) -> bool {
old_widget.theater != self.theater
|| old_widget.overlay_entry_widget_state != self.overlay_entry_widget_state
}
}
thread_local! {
static WALL_TIME: Cell<i64> = const { Cell::new(i64::MIN) };
}
pub struct OverlayPortalController {
attach_target: Option<Handle<OverlayPortalState>>,
z_order_index: Option<i64>,
debug_label: Option<String>,
}
impl OverlayPortalController {
pub fn new(app: &mut App, debug_label: Option<String>) -> Handle<OverlayPortalController> {
app.create(OverlayPortalController {
attach_target: None,
z_order_index: None,
debug_label,
})
}
fn now(self: Handle<Self>, app: &App) -> i64 {
let now = WALL_TIME.get() + 1;
WALL_TIME.set(now);
debug_assert!(app.get(self).z_order_index.is_none_or(|index| index < now));
debug_assert!(
app.get(self)
.attach_target
.and_then(|target| app.get(target).z_order_index)
.is_none_or(|index| index < now)
);
now
}
pub fn show(self: Handle<Self>, app: &mut App) {
let now = self.now(app);
match app.get(self).attach_target {
Some(state) => state.show(app, now),
None => app.get_mut(self).z_order_index = Some(now),
}
}
pub fn hide(self: Handle<Self>, app: &mut App) {
match app.get(self).attach_target {
Some(state) => state.hide(app),
None => {
debug_assert!(app.get(self).z_order_index.is_some());
app.get_mut(self).z_order_index = None;
}
}
}
pub fn is_showing(self: Handle<Self>, app: &App) -> bool {
match app.get(self).attach_target {
Some(state) => app.get(state).z_order_index.is_some(),
None => app.get(self).z_order_index.is_some(),
}
}
pub fn toggle(self: Handle<Self>, app: &mut App) {
if self.is_showing(app) {
self.hide(app);
} else {
self.show(app);
}
}
}
impl Debug for OverlayPortalController {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "OverlayPortalController")?;
if let Some(label) = &self.debug_label {
write!(f, "({label})")?;
}
if self.attach_target.is_none() {
write!(f, " DETACHED")?;
}
Ok(())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum OverlayChildLocation {
NearestOverlay,
RootOverlay,
}
pub struct OverlayPortal {
pub key: Option<KeyRef>,
pub controller: Handle<OverlayPortalController>,
pub overlay_child_builder: WidgetBuilder,
pub child: Option<WidgetRef>,
pub overlay_location: OverlayChildLocation,
}
impl OverlayPortal {
pub fn new(
controller: Handle<OverlayPortalController>,
overlay_child_builder: impl Fn(&mut App, BuildContext) -> WidgetRef + 'static,
) -> OverlayPortal {
OverlayPortal {
key: None,
controller,
overlay_child_builder: Rc::new(overlay_child_builder),
child: None,
overlay_location: OverlayChildLocation::NearestOverlay,
}
}
#[deprecated(note = "Use OverlayPortal::new with an overlay_location of \
OverlayChildLocation::RootOverlay instead. This feature was deprecated \
after v3.33.0-0.0.pre.")]
pub fn targets_root_overlay(
controller: Handle<OverlayPortalController>,
overlay_child_builder: impl Fn(&mut App, BuildContext) -> WidgetRef + 'static,
) -> OverlayPortal {
OverlayPortal {
key: None,
controller,
overlay_child_builder: Rc::new(overlay_child_builder),
child: None,
overlay_location: OverlayChildLocation::RootOverlay,
}
}
pub fn overlay_child_layout_builder(
controller: Handle<OverlayPortalController>,
overlay_child_builder: impl Fn(&mut App, BuildContext, OverlayChildLayoutInfo) -> WidgetRef
+ 'static,
) -> OverlayPortal {
let builder: OverlayChildLayoutBuilder = Rc::new(overlay_child_builder);
OverlayPortal::new(controller, move |_app, _context| {
OverlayChildLayoutBuilderWidget {
builder: Rc::clone(&builder),
}
.into_widget()
})
}
pub fn key(mut self, key: KeyRef) -> OverlayPortal {
self.key = Some(key);
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> OverlayPortal {
self.child = Some(child.into_widget());
self
}
pub fn overlay_location(mut self, overlay_location: OverlayChildLocation) -> OverlayPortal {
self.overlay_location = overlay_location;
self
}
}
impl Debug for OverlayPortal {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OverlayPortal")
.field("overlayLocation", &self.overlay_location)
.finish_non_exhaustive()
}
}
impl StatefulWidget for OverlayPortal {
type State = OverlayPortalState;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_state(&self) -> OverlayPortalState {
OverlayPortalState {
state: StateData::new(),
z_order_index: None,
child_model_may_have_changed: true,
location_cache: None,
}
}
}
pub struct OverlayPortalState {
state: StateData<OverlayPortal>,
z_order_index: Option<i64>,
child_model_may_have_changed: bool,
location_cache: Option<Handle<OverlayEntryLocation>>,
}
impl OverlayPortalState {
fn is_the_same_location(
app: &App,
location_cache: Handle<OverlayEntryLocation>,
marker: &TheaterLocation,
) -> bool {
let cached = app.get(location_cache);
cached.child_model == marker.overlay_entry_widget_state && cached.theater == marker.theater
}
fn get_location(
self: Handle<Self>,
app: &mut App,
z_order_index: i64,
overlay_location: OverlayChildLocation,
) -> Handle<OverlayEntryLocation> {
let cached_location = app.get(self).location_cache;
let context = self.context(app);
let target_root_overlay = overlay_location == OverlayChildLocation::RootOverlay;
let mut marker = None;
let is_cache_valid = cached_location.is_some_and(|cached| {
!app.get(self).child_model_may_have_changed || {
let found = RenderTheaterMarker::of(app, context, target_root_overlay);
marker = Some(found);
OverlayPortalState::is_the_same_location(app, cached, &found)
}
});
app.get_mut(self).child_model_may_have_changed = false;
if is_cache_valid {
let cached = cached_location.expect("a valid cache holds a location");
debug_assert!(app.get(cached).z_order_index == z_order_index);
debug_assert!(cached.debug_is_location_valid(app));
return cached;
}
if let Some(cached) = cached_location {
cached.debug_mark_location_invalid(app);
}
let marker = match marker {
Some(marker) => marker,
None => RenderTheaterMarker::of(app, context, target_root_overlay),
};
let new_location = OverlayEntryLocation::new(
app,
z_order_index,
marker.overlay_entry_widget_state,
marker.theater,
);
app.get_mut(self).location_cache = Some(new_location);
new_location
}
fn setup_controller(
self: Handle<Self>,
app: &mut App,
controller: Handle<OverlayPortalController>,
) {
debug_assert!(
app.get(controller).attach_target == Some(self)
|| !app
.get(controller)
.attach_target
.is_some_and(|target| target.mounted(app)),
"Failed to attach the controller: it is already attached to another OverlayPortal."
);
let controller_z_order_index = app.get(controller).z_order_index;
let z_order_index = app.get(self).z_order_index;
let takes_controller_index = match (controller_z_order_index, z_order_index) {
(_, None) => true,
(Some(controller_index), Some(index)) => controller_index > index,
(None, Some(_)) => false,
};
if takes_controller_index {
app.get_mut(self).z_order_index = controller_z_order_index;
}
let controller_state = app.get_mut(controller);
controller_state.z_order_index = None;
controller_state.attach_target = Some(self);
}
fn show(self: Handle<Self>, app: &mut App, z_order_index: i64) {
debug_assert!(
SchedulerBinding::scheduler_phase(app) != SchedulerPhase::PersistentCallbacks,
"OverlayPortalController::show should not be called during build."
);
self.set_state(app, |state| state.z_order_index = Some(z_order_index));
if let Some(cached) = app.get(self).location_cache {
cached.debug_mark_location_invalid(app);
}
app.get_mut(self).location_cache = None;
}
fn hide(self: Handle<Self>, app: &mut App) {
debug_assert!(
SchedulerBinding::scheduler_phase(app) != SchedulerPhase::PersistentCallbacks,
"OverlayPortalController::hide should not be called during build."
);
self.set_state(app, |state| state.z_order_index = None);
if let Some(cached) = app.get(self).location_cache {
cached.debug_mark_location_invalid(app);
}
app.get_mut(self).location_cache = None;
}
}
impl State for OverlayPortalState {
type Widget = OverlayPortal;
crate::state_accessors!();
fn init_state(self: Handle<Self>, app: &mut App) {
let controller = self.widget(app).controller;
self.setup_controller(app, controller);
}
fn did_change_dependencies(self: Handle<Self>, app: &mut App) {
app.get_mut(self).child_model_may_have_changed = true;
}
fn did_update_widget(self: Handle<Self>, app: &mut App, old_widget: &OverlayPortal) {
let (old_location, old_controller) = (old_widget.overlay_location, old_widget.controller);
let widget = self.widget(app);
let (new_location, new_controller) = (widget.overlay_location, widget.controller);
let state = app.get_mut(self);
state.child_model_may_have_changed =
state.child_model_may_have_changed || old_location != new_location;
if old_controller != new_controller {
app.get_mut(old_controller).attach_target = None;
self.setup_controller(app, new_controller);
}
}
fn activate(self: Handle<Self>, app: &mut App) {
debug_assert!(app.get(self.widget(app).controller).attach_target == Some(self));
}
fn dispose(self: Handle<Self>, app: &mut App) {
let controller = self.widget(app).controller;
app.get_mut(controller).attach_target = None;
if let Some(cached) = app.get(self).location_cache {
cached.debug_mark_location_invalid(app);
}
app.get_mut(self).location_cache = None;
}
fn build(self: Handle<Self>, app: &mut App, context: BuildContext) -> WidgetRef {
let child = self.widget(app).child.clone();
let Some(z_order_index) = app.get(self).z_order_index else {
return RawOverlayPortal {
overlay_location: None,
overlay_child: None,
child,
}
.into_widget();
};
let widget_location = self.widget(app).overlay_location;
let overlay_location = self.get_location(app, z_order_index, widget_location);
let child_model_context = app.get(overlay_location).child_model.context(app);
let overlay_data = MediaQuery::of(app, child_model_context);
let data = MediaQuery::of(app, context)
.copy_with()
.padding(overlay_data.padding)
.view_insets(overlay_data.view_insets)
.view_padding(overlay_data.view_padding);
let builder = Rc::clone(&self.widget(app).overlay_child_builder);
RawOverlayPortal {
overlay_location: Some(overlay_location),
overlay_child: Some(
DeferredLayout {
child: MediaQuery::new(data, Builder { key: None, builder }).into_widget(),
}
.into_widget(),
),
child,
}
.into_widget()
}
}
struct OverlayEntryLocation {
z_order_index: i64,
child_model: Handle<OverlayEntryWidgetState>,
theater: RenderHandle<RenderTheater>,
overlay_child_render_box: Option<RenderHandle<RenderDeferredLayoutBox>>,
slot_token: Option<Rc<dyn Any>>,
debug_location_invalid: bool,
}
impl OverlayEntryLocation {
fn new(
app: &mut App,
z_order_index: i64,
child_model: Handle<OverlayEntryWidgetState>,
theater: RenderHandle<RenderTheater>,
) -> Handle<OverlayEntryLocation> {
let this = app.create(OverlayEntryLocation {
z_order_index,
child_model,
theater,
overlay_child_render_box: None,
slot_token: None,
debug_location_invalid: false,
});
app.get_mut(this).slot_token = Some(Rc::new(this));
this
}
fn slot(self: Handle<Self>, app: &App) -> Slot {
Slot::Custom(Rc::clone(
app.get(self)
.slot_token
.as_ref()
.expect("the token is created with the location"),
))
}
fn from_slot(slot: &Slot) -> Option<Handle<OverlayEntryLocation>> {
match slot {
Slot::Custom(token) => token
.downcast_ref::<Handle<OverlayEntryLocation>>()
.copied(),
_ => None,
}
}
fn add_to_child_model(
self: Handle<Self>,
app: &mut App,
child: RenderHandle<RenderDeferredLayoutBox>,
) {
debug_assert!(
app.get(self).overlay_child_render_box.is_none(),
"Failed to add the child: this location is already occupied."
);
app.get_mut(self).overlay_child_render_box = Some(child);
let (child_model, theater) = {
let location = app.get(self);
(location.child_model, location.theater)
};
child_model.add(app, self);
theater.mark_needs_paint(app);
}
fn remove_from_child_model(
self: Handle<Self>,
app: &mut App,
child: RenderHandle<RenderDeferredLayoutBox>,
) {
debug_assert!(app.get(self).overlay_child_render_box == Some(child));
app.get_mut(self).overlay_child_render_box = None;
let (child_model, theater) = {
let location = app.get(self);
(location.child_model, location.theater)
};
child_model.remove(app, self);
theater.mark_needs_paint(app);
}
fn add_child(self: Handle<Self>, app: &mut App, child: RenderHandle<RenderDeferredLayoutBox>) {
debug_assert!(self.debug_is_location_valid(app));
self.add_to_child_model(app, child);
let theater = app.get(self).theater;
theater.add_deferred_child(app, child);
debug_assert!(child.parent(app) == Some(theater.as_object()));
}
fn remove_child(
self: Handle<Self>,
app: &mut App,
child: RenderHandle<RenderDeferredLayoutBox>,
) {
self.remove_from_child_model(app, child);
let theater = app.get(self).theater;
theater.remove_deferred_child(app, child);
debug_assert!(child.parent(app).is_none());
}
fn move_child(
self: Handle<Self>,
app: &mut App,
child: RenderHandle<RenderDeferredLayoutBox>,
from_location: Handle<OverlayEntryLocation>,
) {
debug_assert!(from_location != self);
debug_assert!(self.debug_is_location_valid(app));
let from_theater = app.get(from_location).theater;
let from_model = app.get(from_location).child_model;
let theater = app.get(self).theater;
if from_theater != theater {
from_theater.remove_deferred_child(app, child);
theater.add_deferred_child(app, child);
}
if from_model != app.get(self).child_model
|| app.get(from_location).z_order_index != app.get(self).z_order_index
{
from_location.remove_from_child_model(app, child);
self.add_to_child_model(app, child);
}
}
fn reattach_from_layout_surrogate(
self: Handle<Self>,
app: &mut App,
child: RenderHandle<RenderDeferredLayoutBox>,
) {
debug_assert!(
app.get(self).overlay_child_render_box.is_none(),
"failed to reattach: detach_from_layout_surrogate must be called before \
reattach_from_layout_surrogate."
);
let theater = app.get(self).theater;
theater.add_deferred_child(app, child);
app.get_mut(self).overlay_child_render_box = Some(child);
}
fn detach_from_layout_surrogate(
self: Handle<Self>,
app: &mut App,
child: RenderHandle<RenderDeferredLayoutBox>,
) {
let theater = app.get(self).theater;
theater.remove_deferred_child(app, child);
app.get_mut(self).overlay_child_render_box = None;
}
fn debug_is_location_valid(self: Handle<Self>, app: &App) -> bool {
assert!(
!app.get(self).debug_location_invalid,
"this OverlayPortal location is already disposed."
);
true
}
fn debug_mark_location_invalid(self: Handle<Self>, app: &mut App) {
debug_assert!(self.debug_is_location_valid(app));
if cfg!(debug_assertions) {
app.get_mut(self).debug_location_invalid = true;
}
}
}
#[derive(Debug)]
struct RawOverlayPortal {
overlay_child: Option<WidgetRef>,
child: Option<WidgetRef>,
overlay_location: Option<Handle<OverlayEntryLocation>>,
}
impl RawOverlayPortal {
fn into_widget(self) -> WidgetRef {
debug_assert!(self.overlay_child.is_none() || self.overlay_location.is_some());
Rc::new(self)
}
}
impl Widget for RawOverlayPortal {
fn key(&self) -> Option<&KeyRef> {
None
}
fn create_element(&self, app: &mut App, this: WidgetRef) -> AnyElement {
RawOverlayPortalElement::create(app, this).as_element()
}
fn as_any(&self) -> &dyn Any {
self
}
fn widget_type(&self) -> std::any::TypeId {
std::any::TypeId::of::<RawOverlayPortal>()
}
fn kind(&self) -> WidgetKind {
WidgetKind::Other
}
}
impl RenderObjectWidget for RawOverlayPortal {
type RenderObject = RenderLayoutSurrogateProxyBox;
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderLayoutSurrogateProxyBox::new(app, self.overlay_location).as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderLayoutSurrogateProxyBox>,
) {
render_object.get_mut(app).overlay_location = self.overlay_location;
}
}
struct RawOverlayPortalElement {
element: ElementData,
render_object_element: RenderObjectElementData,
overlay_child: Option<AnyElement>,
child: Option<AnyElement>,
}
impl RawOverlayPortalElement {
fn create(app: &mut App, widget: WidgetRef) -> Handle<RawOverlayPortalElement> {
debug_assert!(downcast_widget::<RawOverlayPortal>(&*widget).is_some());
app.create(RawOverlayPortalElement {
element: ElementData::new(widget),
render_object_element: RenderObjectElementData::default(),
overlay_child: None,
child: None,
})
}
fn update_children(self: Handle<Self>, app: &mut App) {
let this = self.as_element();
let (child_widget, overlay_child_widget, overlay_location) = {
let widget = Self::widget_of(this.widget(app));
(
widget.child.clone(),
widget.overlay_child.clone(),
widget.overlay_location,
)
};
let child = app.get(self).child;
let child = this.update_child(app, child, child_widget, None);
app.get_mut(self).child = child;
let slot = overlay_location.map(|location| location.slot(app));
let overlay_child = app.get(self).overlay_child;
let overlay_child = this.update_child(app, overlay_child, overlay_child_widget, slot);
app.get_mut(self).overlay_child = overlay_child;
}
fn surrogate(self: Handle<Self>, app: &App) -> RenderHandle<RenderLayoutSurrogateProxyBox> {
self.typed_render_object(app)
}
}
impl RenderObjectElementWidget for RawOverlayPortalElement {
type Widget = RawOverlayPortal;
fn widget_of(widget: &WidgetRef) -> &RawOverlayPortal {
downcast_widget::<RawOverlayPortal>(&**widget)
.expect("a RawOverlayPortalElement holds a RawOverlayPortal")
}
}
impl RenderObjectElement for RawOverlayPortalElement {
fn render_object_element_data(self: Handle<Self>, app: &App) -> &RenderObjectElementData {
&app.get(self).render_object_element
}
fn render_object_element_data_mut(
self: Handle<Self>,
app: &mut App,
) -> &mut RenderObjectElementData {
&mut app.get_mut(self).render_object_element
}
}
impl Element for RawOverlayPortalElement {
crate::element_accessors!();
const IS_RENDER_OBJECT_ELEMENT: bool = true;
fn render_object(self: Handle<Self>, app: &App) -> Option<AnyRenderObject> {
self.render_object_element_data(app).render_object()
}
fn render_object_attaching_child(self: Handle<Self>, _app: &App) -> Option<AnyElement> {
None
}
fn debug_doing_build(self: Handle<Self>, app: &App) -> bool {
self.render_object_element_data(app).debug_doing_build()
}
fn mount(
self: Handle<Self>,
app: &mut App,
parent: Option<AnyElement>,
new_slot: Option<Slot>,
) {
RenderObjectElement::mount(self, app, parent, new_slot);
self.update_children(app);
}
fn update(self: Handle<Self>, app: &mut App, new_widget: WidgetRef) {
RenderObjectElement::update(self, app, new_widget);
self.update_children(app);
}
fn forget_child(self: Handle<Self>, app: &mut App, child: AnyElement) {
debug_assert!(app.get(self).child == Some(child));
app.get_mut(self).child = None;
}
fn visit_children(self: Handle<Self>, app: &App, visitor: &mut dyn FnMut(AnyElement)) {
let state = app.get(self);
let (child, overlay_child) = (state.child, state.overlay_child);
if let Some(child) = child {
visitor(child);
}
if let Some(overlay_child) = overlay_child {
visitor(overlay_child);
}
}
fn perform_rebuild(self: Handle<Self>, app: &mut App) {
RenderObjectElement::perform_rebuild(self, app);
}
fn deactivate(self: Handle<Self>, app: &mut App) {
RenderObjectElement::deactivate(self, app);
}
fn unmount(self: Handle<Self>, app: &mut App) {
RenderObjectElement::unmount(self, app);
}
fn update_parent_data(self: Handle<Self>, app: &mut App, parent_data_element: AnyElement) {
RenderObjectElement::update_parent_data(self, app, parent_data_element);
}
fn update_slot(self: Handle<Self>, app: &mut App, new_slot: Option<Slot>) {
RenderObjectElement::update_slot(self, app, new_slot);
}
fn attach_render_object(self: Handle<Self>, app: &mut App, new_slot: Option<Slot>) {
RenderObjectElement::attach_render_object(self, app, new_slot);
}
fn detach_render_object(self: Handle<Self>, app: &mut App) {
RenderObjectElement::detach_render_object(self, app);
}
fn insert_render_object_child(
self: Handle<Self>,
app: &mut App,
child: AnyRenderObject,
slot: Option<Slot>,
) {
debug_assert!(child.parent(app).is_none());
let surrogate = self.surrogate(app);
match slot.as_ref().and_then(OverlayEntryLocation::from_slot) {
Some(location) => {
let deferred = child
.downcast::<RenderDeferredLayoutBox>(app)
.expect("an overlay child is a DeferredLayout");
debug_assert!(surrogate.get(app).deferred_layout_child == Some(deferred));
location.add_child(app, deferred);
}
None => surrogate.set_child(app, Some(child.as_box().expect("a box child"))),
}
}
fn move_render_object_child(
self: Handle<Self>,
app: &mut App,
child: AnyRenderObject,
old_slot: Option<Slot>,
new_slot: Option<Slot>,
) {
let old_location = old_slot
.as_ref()
.and_then(OverlayEntryLocation::from_slot)
.expect("an overlay child moves between locations");
let new_location = new_slot
.as_ref()
.and_then(OverlayEntryLocation::from_slot)
.expect("an overlay child moves between locations");
debug_assert!(new_location.debug_is_location_valid(app));
let deferred = child
.downcast::<RenderDeferredLayoutBox>(app)
.expect("an overlay child is a DeferredLayout");
new_location.move_child(app, deferred, old_location);
}
fn remove_render_object_child(
self: Handle<Self>,
app: &mut App,
child: AnyRenderObject,
slot: Option<Slot>,
) {
let surrogate = self.surrogate(app);
let Some(location) = slot.as_ref().and_then(OverlayEntryLocation::from_slot) else {
surrogate.set_child(app, None);
return;
};
let deferred = child
.downcast::<RenderDeferredLayoutBox>(app)
.expect("an overlay child is a DeferredLayout");
debug_assert!(surrogate.get(app).deferred_layout_child == Some(deferred));
location.remove_child(app, deferred);
surrogate.get_mut(app).deferred_layout_child = None;
}
}
#[derive(Debug)]
struct DeferredLayout {
child: WidgetRef,
}
impl DeferredLayout {
fn layout_parent(
app: &App,
context: BuildContext,
) -> RenderHandle<RenderLayoutSurrogateProxyBox> {
context
.find_ancestor_render_object_of_type::<RenderLayoutSurrogateProxyBox>(app)
.expect("a _DeferredLayout is built by an OverlayPortal")
}
}
impl RenderObjectWidget for DeferredLayout {
type RenderObject = RenderDeferredLayoutBox;
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
let parent = DeferredLayout::layout_parent(app, context);
let render_object = RenderDeferredLayoutBox::new(app, parent);
parent.get_mut(app).deferred_layout_child = Some(render_object);
render_object.as_object()
}
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: RenderHandle<RenderDeferredLayoutBox>,
) {
debug_assert!(
render_object.get(app).layout_surrogate == DeferredLayout::layout_parent(app, context)
);
debug_assert!(
DeferredLayout::layout_parent(app, context)
.get(app)
.deferred_layout_child
== Some(render_object)
);
}
}
impl SingleChildRenderObjectWidget for DeferredLayout {
fn child(&self) -> Option<&WidgetRef> {
Some(&self.child)
}
}
struct RenderDeferredLayoutBox {
render_object: RenderObjectData,
render_box: RenderBoxData,
child: RenderObjectWithChildData<AnyRenderBox>,
layout_surrogate: RenderHandle<RenderLayoutSurrogateProxyBox>,
doing_layout_from_tree_walk: bool,
debug_mutations_locked: bool,
}
impl RenderDeferredLayoutBox {
fn new(
app: &mut App,
layout_surrogate: RenderHandle<RenderLayoutSurrogateProxyBox>,
) -> RenderHandle<RenderDeferredLayoutBox> {
RenderHandle::new_box(
app,
RenderDeferredLayoutBox {
render_object: RenderObjectData::new(),
render_box: RenderBoxData::new(),
child: RenderObjectWithChildData::new(),
layout_surrogate,
doing_layout_from_tree_walk: false,
debug_mutations_locked: false,
},
)
}
fn do_layout_from(
self: RenderHandle<Self>,
app: &mut App,
treewalk_parent: AnyRenderObject,
constraints: BoxConstraints,
) {
debug_assert!(!self.get(app).doing_layout_from_tree_walk);
self.get_mut(app).doing_layout_from_tree_walk = true;
self.as_box().layout(app, constraints, false);
debug_assert!(self.get(app).doing_layout_from_tree_walk);
self.get_mut(app).doing_layout_from_tree_walk = false;
debug_assert!(!self.as_object().debug_needs_layout(app));
treewalk_parent.invoke_layout_callback(app, |app| {
RenderBox::mark_needs_layout(self, app);
});
}
}
impl RenderObjectWithChildMixin for RenderDeferredLayoutBox {
type ChildType = AnyRenderBox;
fn child_data(self: RenderHandle<Self>, app: &App) -> &RenderObjectWithChildData<AnyRenderBox> {
&self.get(app).child
}
fn child_data_mut(
self: RenderHandle<Self>,
app: &mut App,
) -> &mut RenderObjectWithChildData<AnyRenderBox> {
&mut self.get_mut(app).child
}
}
impl RenderProxyBoxMixin for RenderDeferredLayoutBox {}
impl RenderTheaterMixin for RenderDeferredLayoutBox {
fn theater(self: RenderHandle<Self>, app: &App) -> RenderHandle<RenderTheater> {
self.parent(app)
.and_then(|parent| parent.downcast::<RenderTheater>(app))
.expect("the parent of a deferred layout box is a RenderTheater")
}
fn children_in_paint_order(self: RenderHandle<Self>, app: &App) -> TheaterChildren {
TheaterChildren::single(self.child(app))
}
fn children_in_hit_test_order(self: RenderHandle<Self>, app: &App) -> TheaterChildren {
RenderTheaterMixin::children_in_paint_order(self, app)
}
}
impl RenderObject for RenderDeferredLayoutBox {
waft_rendering::render_object_accessors!();
fn sized_by_parent(self: RenderHandle<Self>, _app: &App) -> bool {
true
}
fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
debug_assert!(!self.get(app).debug_mutations_locked);
if self.get(app).doing_layout_from_tree_walk {
return;
}
if cfg!(debug_assertions) {
self.get_mut(app).debug_mutations_locked = true;
}
debug_assert!(self.parent(app).is_some());
let Some(child) = self.child(app) else {
return;
};
let constraints = self.constraints(app);
debug_assert!(constraints.is_tight());
self.layout_child(app, child, constraints);
if cfg!(debug_assertions) {
self.get_mut(app).debug_mutations_locked = false;
}
}
fn redepth_children(self: RenderHandle<Self>, app: &mut App) {
let surrogate = self.get(app).layout_surrogate;
if surrogate.as_object().attached(app) {
surrogate.as_object().redepth_child(app, self.as_object());
}
if let Some(child) = self.child(app) {
self.as_object().redepth_child(app, child.as_object());
}
}
fn visit_children(
self: RenderHandle<Self>,
app: &App,
visitor: &mut dyn FnMut(AnyRenderObject),
) {
if let Some(child) = self.child(app) {
visitor(child.as_object());
}
}
fn paint(
self: RenderHandle<Self>,
app: &mut App,
context: &mut PaintingContext,
offset: Offset,
) {
RenderTheaterMixin::paint(self, app, context, offset);
}
}
impl RenderBox for RenderDeferredLayoutBox {
waft_rendering::render_box_accessors!();
fn perform_resize(self: RenderHandle<Self>, app: &mut App) {
let size = self.constraints(app).biggest();
self.set_size(app, size);
}
fn setup_parent_data(self: RenderHandle<Self>, app: &mut App, child: AnyRenderObject) {
RenderTheaterMixin::setup_parent_data(self, app, child);
}
fn hit_test_children(
self: RenderHandle<Self>,
app: &mut App,
result: &mut BoxHitTestResult<'_>,
position: Offset,
) -> bool {
RenderTheaterMixin::hit_test_children(self, app, result, position)
}
fn compute_min_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
RenderProxyBoxMixin::compute_min_intrinsic_width(self, app, height)
}
fn compute_max_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
RenderProxyBoxMixin::compute_max_intrinsic_width(self, app, height)
}
fn compute_min_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
RenderProxyBoxMixin::compute_min_intrinsic_height(self, app, width)
}
fn compute_max_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
RenderProxyBoxMixin::compute_max_intrinsic_height(self, app, width)
}
fn compute_dry_layout(
self: RenderHandle<Self>,
app: &mut App,
constraints: BoxConstraints,
) -> Size {
RenderProxyBoxMixin::compute_dry_layout(self, app, constraints)
}
}
struct RenderLayoutSurrogateProxyBox {
render_object: RenderObjectData,
render_box: RenderBoxData,
child: RenderObjectWithChildData<AnyRenderBox>,
deferred_layout_child: Option<RenderHandle<RenderDeferredLayoutBox>>,
overlay_location: Option<Handle<OverlayEntryLocation>>,
debug_is_first_attach: bool,
did_detach_deferred_child: bool,
}
impl RenderLayoutSurrogateProxyBox {
fn new(
app: &mut App,
overlay_location: Option<Handle<OverlayEntryLocation>>,
) -> RenderHandle<RenderLayoutSurrogateProxyBox> {
RenderHandle::new_box(
app,
RenderLayoutSurrogateProxyBox {
render_object: RenderObjectData::new(),
render_box: RenderBoxData::new(),
child: RenderObjectWithChildData::new(),
deferred_layout_child: None,
overlay_location,
debug_is_first_attach: true,
did_detach_deferred_child: false,
},
)
}
}
impl RenderObjectWithChildMixin for RenderLayoutSurrogateProxyBox {
type ChildType = AnyRenderBox;
fn child_data(self: RenderHandle<Self>, app: &App) -> &RenderObjectWithChildData<AnyRenderBox> {
&self.get(app).child
}
fn child_data_mut(
self: RenderHandle<Self>,
app: &mut App,
) -> &mut RenderObjectWithChildData<AnyRenderBox> {
&mut self.get_mut(app).child
}
}
impl RenderProxyBoxMixin for RenderLayoutSurrogateProxyBox {}
impl RenderObject for RenderLayoutSurrogateProxyBox {
waft_rendering::render_object_accessors!();
fn did_attach(self: RenderHandle<Self>, app: &mut App, owner: Handle<PipelineOwner>) {
if let Some(child) = self.child(app) {
child.as_object().attach(app, owner);
}
if self.get(app).did_detach_deferred_child {
self.get_mut(app).did_detach_deferred_child = false;
let deferred = self
.get(app)
.deferred_layout_child
.expect("a detached deferred child was recorded");
debug_assert!(!self.get(app).debug_is_first_attach);
let location = self
.get(app)
.overlay_location
.expect("a deferred child has a location");
location.reattach_from_layout_surrogate(app, deferred);
}
if cfg!(debug_assertions) {
self.get_mut(app).debug_is_first_attach = false;
}
}
fn did_detach(self: RenderHandle<Self>, app: &mut App) {
if let Some(deferred) = self.get(app).deferred_layout_child
&& deferred.theater(app).as_object().attached(app)
{
let location = self
.get(app)
.overlay_location
.expect("a deferred child has a location");
location.detach_from_layout_surrogate(app, deferred);
self.get_mut(app).did_detach_deferred_child = true;
}
if let Some(child) = self.child(app) {
child.as_object().detach(app);
}
}
fn redepth_children(self: RenderHandle<Self>, app: &mut App) {
if let Some(child) = self.child(app) {
self.as_object().redepth_child(app, child.as_object());
}
if let Some(deferred) = self.get(app).deferred_layout_child
&& deferred.as_object().attached(app)
{
self.as_object().redepth_child(app, deferred.as_object());
}
}
fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
RenderProxyBoxMixin::perform_layout(self, app);
let Some(deferred_child) = self.get(app).deferred_layout_child else {
return;
};
let theater = deferred_child.theater(app);
if !theater.get(app).laying_out_size_determining_child {
let theater_constraints = theater.constraints(app);
let box_size = if theater_constraints.biggest().is_finite() {
theater_constraints.biggest()
} else {
theater.size(app)
};
deferred_child.do_layout_from(app, self.as_object(), BoxConstraints::tight(box_size));
}
}
fn visit_children(
self: RenderHandle<Self>,
app: &App,
visitor: &mut dyn FnMut(AnyRenderObject),
) {
if let Some(child) = self.child(app) {
visitor(child.as_object());
}
}
fn paint(
self: RenderHandle<Self>,
app: &mut App,
context: &mut PaintingContext,
offset: Offset,
) {
RenderProxyBoxMixin::paint(self, app, context, offset);
}
}
impl RenderBox for RenderLayoutSurrogateProxyBox {
waft_rendering::render_box_accessors!();
fn setup_parent_data(self: RenderHandle<Self>, app: &mut App, child: AnyRenderObject) {
RenderProxyBoxMixin::setup_parent_data(self, app, child);
}
fn apply_paint_transform(
self: RenderHandle<Self>,
app: &App,
child: AnyRenderObject,
transform: &mut Matrix4,
) {
RenderProxyBoxMixin::apply_paint_transform(self, app, child, transform);
}
fn hit_test_children(
self: RenderHandle<Self>,
app: &mut App,
result: &mut BoxHitTestResult<'_>,
position: Offset,
) -> bool {
RenderProxyBoxMixin::hit_test_children(self, app, result, position)
}
fn compute_min_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
RenderProxyBoxMixin::compute_min_intrinsic_width(self, app, height)
}
fn compute_max_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
RenderProxyBoxMixin::compute_max_intrinsic_width(self, app, height)
}
fn compute_min_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
RenderProxyBoxMixin::compute_min_intrinsic_height(self, app, width)
}
fn compute_max_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
RenderProxyBoxMixin::compute_max_intrinsic_height(self, app, width)
}
fn compute_dry_layout(
self: RenderHandle<Self>,
app: &mut App,
constraints: BoxConstraints,
) -> Size {
RenderProxyBoxMixin::compute_dry_layout(self, app, constraints)
}
}
struct OverlayChildLayoutBuilderWidget {
builder: OverlayChildLayoutBuilder,
}
impl OverlayChildLayoutBuilderWidget {
fn into_widget(self) -> WidgetRef {
Rc::new(self)
}
fn update_should_rebuild(&self, old_widget: &OverlayChildLayoutBuilderWidget) -> bool {
let _ = old_widget;
true
}
}
impl Debug for OverlayChildLayoutBuilderWidget {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OverlayChildLayoutBuilder")
.finish_non_exhaustive()
}
}
impl Widget for OverlayChildLayoutBuilderWidget {
fn key(&self) -> Option<&KeyRef> {
None
}
fn create_element(&self, app: &mut App, this: WidgetRef) -> AnyElement {
OverlayChildLayoutBuilderElement::create(app, this).as_element()
}
fn as_any(&self) -> &dyn Any {
self
}
fn widget_type(&self) -> std::any::TypeId {
std::any::TypeId::of::<OverlayChildLayoutBuilderWidget>()
}
fn kind(&self) -> WidgetKind {
WidgetKind::Other
}
}
impl RenderObjectWidget for OverlayChildLayoutBuilderWidget {
type RenderObject = RenderOverlayChildLayoutBuilder;
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderOverlayChildLayoutBuilder::new(app).as_object()
}
fn update_render_object(
&self,
_app: &mut App,
_context: BuildContext,
_render_object: RenderHandle<RenderOverlayChildLayoutBuilder>,
) {
}
fn did_unmount_render_object(
&self,
app: &mut App,
render_object: RenderHandle<RenderOverlayChildLayoutBuilder>,
) {
render_object.cancel_frame_callback(app);
}
}
struct OverlayChildLayoutBuilderElement {
element: ElementData,
render_object_element: RenderObjectElementData,
child: Option<AnyElement>,
build_scope: Option<Handle<BuildScope>>,
deferred_callback_scheduled: bool,
previous_layout_info: Option<OverlayChildLayoutInfo>,
needs_build: bool,
}
impl OverlayChildLayoutBuilderElement {
fn create(app: &mut App, widget: WidgetRef) -> Handle<OverlayChildLayoutBuilderElement> {
debug_assert!(downcast_widget::<OverlayChildLayoutBuilderWidget>(&*widget).is_some());
let this = app.create(OverlayChildLayoutBuilderElement {
element: ElementData::new(widget),
render_object_element: RenderObjectElementData::default(),
child: None,
build_scope: None,
deferred_callback_scheduled: false,
previous_layout_info: None,
needs_build: true,
});
let build_scope = BuildScope::new(
app,
Some(Listener::handle_method(this, Self::schedule_rebuild)),
);
app.get_mut(this).build_scope = Some(build_scope);
this
}
fn render_layout_builder(
self: Handle<Self>,
app: &App,
) -> RenderHandle<RenderOverlayChildLayoutBuilder> {
self.typed_render_object(app)
}
fn schedule_rebuild(self: Handle<Self>, app: &mut App) {
if app.get(self).deferred_callback_scheduled {
return;
}
let defer_mark_needs_layout = match SchedulerBinding::scheduler_phase(app) {
SchedulerPhase::Idle | SchedulerPhase::PostFrameCallbacks => true,
SchedulerPhase::TransientCallbacks
| SchedulerPhase::MidFrameMicrotasks
| SchedulerPhase::PersistentCallbacks => false,
};
if !defer_mark_needs_layout {
self.render_layout_builder(app)
.schedule_layout_callback(app);
return;
}
app.get_mut(self).deferred_callback_scheduled = true;
SchedulerBinding::schedule_frame_callback(
app,
FrameCallback::new(move |app, _time_stamp| self.frame_callback(app)),
View::maybe_id_of(app, self.as_element()),
false,
true,
);
}
fn frame_callback(self: Handle<Self>, app: &mut App) {
app.get_mut(self).deferred_callback_scheduled = false;
if self.as_element().mounted(app) {
self.render_layout_builder(app)
.schedule_layout_callback(app);
}
}
fn rebuild_with_constraints(self: Handle<Self>, app: &mut App) {
let render_object = self.render_layout_builder(app);
let layout_info = render_object.layout_info(app);
let this = self.as_element();
let needs_callback =
app.get(self).needs_build || Some(layout_info) != app.get(self).previous_layout_info;
let callback: Option<BuildScopeCallback> = needs_callback.then(|| {
Box::new(move |app: &mut App| {
debug_assert!(layout_info == render_object.layout_info(app));
let builder = Rc::clone(&Self::widget_of(this.widget(app)).builder);
let built = builder(app, this, layout_info);
let child = app.get(self).child;
let child = this.update_child(app, child, Some(built), None);
debug_assert!(child.is_some());
let state = app.get_mut(self);
state.child = child;
state.needs_build = false;
state.previous_layout_info = Some(layout_info);
}) as BuildScopeCallback
});
let owner = this.owner(app).expect("a laying-out element has an owner");
owner.build_scope(app, this, callback);
}
}
impl RenderObjectElementWidget for OverlayChildLayoutBuilderElement {
type Widget = OverlayChildLayoutBuilderWidget;
fn widget_of(widget: &WidgetRef) -> &OverlayChildLayoutBuilderWidget {
downcast_widget::<OverlayChildLayoutBuilderWidget>(&**widget)
.expect("an OverlayChildLayoutBuilderElement holds its widget")
}
}
impl RenderObjectElement for OverlayChildLayoutBuilderElement {
fn render_object_element_data(self: Handle<Self>, app: &App) -> &RenderObjectElementData {
&app.get(self).render_object_element
}
fn render_object_element_data_mut(
self: Handle<Self>,
app: &mut App,
) -> &mut RenderObjectElementData {
&mut app.get_mut(self).render_object_element
}
}
impl Element for OverlayChildLayoutBuilderElement {
crate::element_accessors!();
const IS_RENDER_OBJECT_ELEMENT: bool = true;
fn render_object(self: Handle<Self>, app: &App) -> Option<AnyRenderObject> {
self.render_object_element_data(app).render_object()
}
fn render_object_attaching_child(self: Handle<Self>, _app: &App) -> Option<AnyElement> {
None
}
fn debug_doing_build(self: Handle<Self>, _app: &App) -> bool {
false
}
fn build_scope(self: Handle<Self>, app: &App) -> Handle<BuildScope> {
app.get(self).build_scope.expect("created with the element")
}
fn visit_children(self: Handle<Self>, app: &App, visitor: &mut dyn FnMut(AnyElement)) {
if let Some(child) = app.get(self).child {
visitor(child);
}
}
fn forget_child(self: Handle<Self>, app: &mut App, child: AnyElement) {
debug_assert!(app.get(self).child == Some(child));
app.get_mut(self).child = None;
}
fn mount(
self: Handle<Self>,
app: &mut App,
parent: Option<AnyElement>,
new_slot: Option<Slot>,
) {
RenderObjectElement::mount(self, app, parent, new_slot); let render_object = self.render_layout_builder(app);
render_object.update_callback(
app,
Listener::handle_method(self, Self::rebuild_with_constraints),
);
}
fn update(self: Handle<Self>, app: &mut App, new_widget: WidgetRef) {
let old_widget = self.as_element().widget(app).clone();
debug_assert!(!Rc::ptr_eq(&old_widget, &new_widget));
RenderObjectElement::update(self, app, new_widget.clone());
let render_object = self.render_layout_builder(app);
render_object.update_callback(
app,
Listener::handle_method(self, Self::rebuild_with_constraints),
);
if Self::widget_of(&new_widget).update_should_rebuild(Self::widget_of(&old_widget)) {
app.get_mut(self).needs_build = true;
render_object.schedule_layout_callback(app);
}
}
fn mark_needs_build(self: Handle<Self>, app: &mut App) {
self.render_layout_builder(app)
.schedule_layout_callback(app);
app.get_mut(self).needs_build = true;
}
fn perform_rebuild(self: Handle<Self>, app: &mut App) {
self.render_layout_builder(app)
.schedule_layout_callback(app);
app.get_mut(self).needs_build = true;
RenderObjectElement::perform_rebuild(self, app);
}
fn deactivate(self: Handle<Self>, app: &mut App) {
RenderObjectElement::deactivate(self, app);
}
fn unmount(self: Handle<Self>, app: &mut App) {
let render_object = self.render_layout_builder(app);
render_object.get_mut(app).callback = None;
let build_scope = app.get_mut(self).build_scope.take();
RenderObjectElement::unmount(self, app);
if let Some(build_scope) = build_scope {
app.destroy(build_scope);
}
}
fn update_parent_data(self: Handle<Self>, app: &mut App, parent_data_element: AnyElement) {
RenderObjectElement::update_parent_data(self, app, parent_data_element);
}
fn update_slot(self: Handle<Self>, app: &mut App, new_slot: Option<Slot>) {
RenderObjectElement::update_slot(self, app, new_slot);
}
fn attach_render_object(self: Handle<Self>, app: &mut App, new_slot: Option<Slot>) {
RenderObjectElement::attach_render_object(self, app, new_slot);
}
fn detach_render_object(self: Handle<Self>, app: &mut App) {
RenderObjectElement::detach_render_object(self, app);
}
fn insert_render_object_child(
self: Handle<Self>,
app: &mut App,
child: AnyRenderObject,
slot: Option<Slot>,
) {
debug_assert!(slot.is_none());
let render_object = self.render_layout_builder(app);
render_object.set_child(app, Some(child.as_box().expect("a box child")));
}
fn move_render_object_child(
self: Handle<Self>,
_app: &mut App,
_child: AnyRenderObject,
_old_slot: Option<Slot>,
_new_slot: Option<Slot>,
) {
debug_assert!(false, "a layout builder has one child");
}
fn remove_render_object_child(
self: Handle<Self>,
app: &mut App,
child: AnyRenderObject,
_slot: Option<Slot>,
) {
let render_object = self.render_layout_builder(app);
debug_assert!(render_object.child(app).map(|current| current.as_object()) == Some(child));
render_object.set_child(app, None);
}
}
struct RenderOverlayChildLayoutBuilder {
render_object: RenderObjectData,
render_box: RenderBoxData,
child: RenderObjectWithChildData<AnyRenderBox>,
layout_callback: RenderObjectWithLayoutCallbackData,
callback: Option<Listener>,
layout_info: Option<OverlayChildLayoutInfo>,
callback_id: Option<i64>,
}
impl RenderOverlayChildLayoutBuilder {
fn new(app: &mut App) -> RenderHandle<RenderOverlayChildLayoutBuilder> {
RenderHandle::new_box(
app,
RenderOverlayChildLayoutBuilder {
render_object: RenderObjectData::new(),
render_box: RenderBoxData::new(),
child: RenderObjectWithChildData::new(),
layout_callback: RenderObjectWithLayoutCallbackData::default(),
callback: None,
layout_info: None,
callback_id: None,
},
)
}
fn layout_info(self: RenderHandle<Self>, app: &App) -> OverlayChildLayoutInfo {
self.get(app)
.layout_info
.expect("the layout callback computes the layout information first")
}
fn update_callback(self: RenderHandle<Self>, app: &mut App, value: Listener) {
if self.get(app).callback.as_ref() == Some(&value) {
return;
}
self.get_mut(app).callback = Some(value);
self.schedule_layout_callback(app);
}
fn compute_new_layout_info(self: RenderHandle<Self>, app: &App) -> OverlayChildLayoutInfo {
let theater = self.theater(app);
let parent = self
.parent(app)
.and_then(|parent| parent.downcast::<RenderDeferredLayoutBox>(app))
.expect("the parent of an overlay child layout builder is a deferred layout box");
let layout_surrogate = parent.get(app).layout_surrogate;
debug_assert!(layout_surrogate.has_size(app));
debug_assert!(
layout_surrogate
.child(app)
.is_none_or(|child| child.size(app) == layout_surrogate.size(app))
);
debug_assert!(self.size(app) == theater.size(app));
let overlay_portal_size = layout_surrogate.size(app);
let paint_transform = layout_surrogate
.as_object()
.get_transform_to(app, Some(theater.as_object()));
OverlayChildLayoutInfo {
child_size: overlay_portal_size,
child_paint_transform: paint_transform,
overlay_size: self.size(app),
}
}
fn frame_callback(self: RenderHandle<Self>, app: &mut App) {
self.get_mut(app).callback_id = None;
RenderBox::mark_needs_layout(self, app);
}
fn cancel_frame_callback(self: RenderHandle<Self>, app: &mut App) {
if let Some(callback_id) = self.get_mut(app).callback_id.take() {
SchedulerBinding::cancel_frame_callback_with_id(app, callback_id);
}
}
}
impl RenderObjectWithChildMixin for RenderOverlayChildLayoutBuilder {
type ChildType = AnyRenderBox;
fn child_data(self: RenderHandle<Self>, app: &App) -> &RenderObjectWithChildData<AnyRenderBox> {
&self.get(app).child
}
fn child_data_mut(
self: RenderHandle<Self>,
app: &mut App,
) -> &mut RenderObjectWithChildData<AnyRenderBox> {
&mut self.get_mut(app).child
}
}
impl RenderProxyBoxMixin for RenderOverlayChildLayoutBuilder {}
impl RenderObjectWithLayoutCallbackMixin for RenderOverlayChildLayoutBuilder {
fn layout_callback_data(
self: RenderHandle<Self>,
app: &App,
) -> &RenderObjectWithLayoutCallbackData {
&self.get(app).layout_callback
}
fn layout_callback_data_mut(
self: RenderHandle<Self>,
app: &mut App,
) -> &mut RenderObjectWithLayoutCallbackData {
&mut self.get_mut(app).layout_callback
}
fn layout_callback(self: RenderHandle<Self>, app: &mut App) {
let layout_info = self.compute_new_layout_info(app);
self.get_mut(app).layout_info = Some(layout_info);
let callback = self
.get(app)
.callback
.clone()
.expect("a callback is set before layout");
callback.call(app);
}
}
impl RenderTheaterMixin for RenderOverlayChildLayoutBuilder {
fn theater(self: RenderHandle<Self>, app: &App) -> RenderHandle<RenderTheater> {
self.parent(app)
.and_then(|parent| parent.downcast::<RenderDeferredLayoutBox>(app))
.expect("the parent of an overlay child layout builder is a deferred layout box")
.theater(app)
}
fn children_in_paint_order(self: RenderHandle<Self>, app: &App) -> TheaterChildren {
TheaterChildren::single(self.child(app))
}
fn children_in_hit_test_order(self: RenderHandle<Self>, app: &App) -> TheaterChildren {
RenderTheaterMixin::children_in_paint_order(self, app)
}
}
impl RenderObject for RenderOverlayChildLayoutBuilder {
waft_rendering::render_object_accessors!();
fn sized_by_parent(self: RenderHandle<Self>, _app: &App) -> bool {
true
}
fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
self.run_layout_callback(app);
if let Some(child) = self.child(app) {
let constraints = self.constraints(app);
self.layout_child(app, child, constraints);
}
debug_assert!(self.get(app).callback_id.is_none());
if self.get(app).callback_id.is_none() {
let callback_id = SchedulerBinding::schedule_frame_callback(
app,
FrameCallback::new(move |app, _time_stamp| self.frame_callback(app)),
None,
false,
false,
);
self.get_mut(app).callback_id = Some(callback_id);
}
}
fn visit_children(
self: RenderHandle<Self>,
app: &App,
visitor: &mut dyn FnMut(AnyRenderObject),
) {
if let Some(child) = self.child(app) {
visitor(child.as_object());
}
}
fn paint(
self: RenderHandle<Self>,
app: &mut App,
context: &mut PaintingContext,
offset: Offset,
) {
RenderTheaterMixin::paint(self, app, context, offset);
}
}
impl RenderBox for RenderOverlayChildLayoutBuilder {
waft_rendering::render_box_accessors!();
fn perform_resize(self: RenderHandle<Self>, app: &mut App) {
let size = self.constraints(app).biggest();
self.set_size(app, size);
}
fn setup_parent_data(self: RenderHandle<Self>, app: &mut App, child: AnyRenderObject) {
RenderTheaterMixin::setup_parent_data(self, app, child);
}
fn hit_test_children(
self: RenderHandle<Self>,
app: &mut App,
result: &mut BoxHitTestResult<'_>,
position: Offset,
) -> bool {
RenderTheaterMixin::hit_test_children(self, app, result, position)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_harness::Harness;
use crate::widgets::basic::SizedBox;
use crate::widgets::media_query::MediaQueryData;
use waft_foundation::AppCell;
fn test_entry(app: &mut App) -> Handle<OverlayEntry> {
OverlayEntry::new(
app,
Rc::new(|_app, _context| SizedBox::shrink().into_widget()),
false,
false,
false,
)
}
fn mount_overlay(app: &mut App, entries: Vec<Handle<OverlayEntry>>) -> Harness {
let tree = MediaQuery::new(
MediaQueryData::new(),
Directionality::new(TextDirection::Ltr, Overlay::new().initial_entries(entries)),
)
.into_widget();
let harness = Harness::mount(app, tree);
harness.pump(app);
harness
}
fn theater_of(harness: &Harness, app: &App) -> RenderHandle<RenderTheater> {
harness
.render_root(app)
.child(app)
.expect("the overlay's render object")
.as_object()
.downcast::<RenderTheater>(app)
.expect("a RenderTheater")
}
fn entry_of(app: &App, child: AnyRenderBox) -> Handle<OverlayEntry> {
child
.as_object()
.parent_data_of::<TheaterParentData>(app)
.overlay_entry
.expect("a child built by an entry")
}
fn entries_in_render_order(
app: &App,
theater: RenderHandle<RenderTheater>,
) -> Vec<Handle<OverlayEntry>> {
let mut order = Vec::new();
let mut child = theater.first_child(app);
while let Some(current) = child {
order.push(entry_of(app, current));
child = theater.child_after(app, current);
}
order
}
#[test]
fn overlay_entries_are_inserted_rearranged_and_removed_in_order() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let (first, second) = (test_entry(&mut app), test_entry(&mut app));
let harness = mount_overlay(&mut app, vec![first, second]);
let theater = theater_of(&harness, &app);
assert_eq!(entries_in_render_order(&app, theater), vec![first, second]);
let overlay = app.get(first).overlay.expect("inserted by initial_entries");
let third = test_entry(&mut app);
overlay.insert(&mut app, third, Some(first), None);
harness.pump(&mut app);
assert_eq!(
entries_in_render_order(&app, theater),
vec![third, first, second]
);
let fourth = test_entry(&mut app);
overlay.insert_all(&mut app, vec![fourth], None, None);
harness.pump(&mut app);
assert_eq!(
entries_in_render_order(&app, theater),
vec![third, first, second, fourth]
);
overlay.rearrange(&mut app, vec![second, first], None, None);
harness.pump(&mut app);
assert_eq!(
entries_in_render_order(&app, theater),
vec![second, first, third, fourth]
);
second.remove(&mut app);
second.dispose(&mut app);
harness.pump(&mut app);
assert_eq!(
entries_in_render_order(&app, theater),
vec![first, third, fourth]
);
}
#[test]
fn an_opaque_entry_takes_the_entries_below_it_offstage() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let bottom = OverlayEntry::new(
&mut app,
Rc::new(|_app, _context| SizedBox::shrink().into_widget()),
false,
true,
false,
);
let top = OverlayEntry::new(
&mut app,
Rc::new(|_app, _context| SizedBox::shrink().into_widget()),
true,
false,
false,
);
let harness = mount_overlay(&mut app, vec![bottom, top]);
let theater = theater_of(&harness, &app);
assert_eq!(theater.child_count(&app), 2);
assert_eq!(theater.skip_count(&app), 1);
let painted = RenderTheaterMixin::children_in_paint_order(theater, &app).collect(&app);
assert_eq!(painted.len(), 1);
assert_eq!(entry_of(&app, painted[0]), top);
bottom.set_maintain_state(&mut app, false);
harness.pump(&mut app);
assert_eq!(theater.child_count(&app), 1);
assert_eq!(theater.skip_count(&app), 0);
assert_eq!(entries_in_render_order(&app, theater), vec![top]);
top.set_opaque(&mut app, false);
harness.pump(&mut app);
assert_eq!(theater.skip_count(&app), 0);
assert_eq!(
RenderTheaterMixin::children_in_paint_order(theater, &app)
.collect(&app)
.len(),
2
);
}
#[test]
fn an_overlay_portal_shows_and_hides_its_overlay_child_in_the_overlay() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let controller = OverlayPortalController::new(&mut app, None);
let entry = OverlayEntry::new(
&mut app,
Rc::new(move |_app: &mut App, _context| {
OverlayPortal::new(controller, |_app: &mut App, _context| {
SizedBox::square(Some(40.0)).into_widget()
})
.child(SizedBox::square(Some(10.0)))
.into_widget()
}),
false,
false,
false,
);
let harness = mount_overlay(&mut app, vec![entry]);
let theater = theater_of(&harness, &app);
assert!(!controller.is_showing(&app));
assert_eq!(
RenderTheaterMixin::children_in_paint_order(theater, &app)
.collect(&app)
.len(),
1
);
controller.show(&mut app);
assert!(controller.is_showing(&app));
harness.pump(&mut app);
let painted = RenderTheaterMixin::children_in_paint_order(theater, &app).collect(&app);
assert_eq!(painted.len(), 2);
let deferred = painted[1]
.as_object()
.downcast::<RenderDeferredLayoutBox>(&app)
.expect("the overlay child");
assert_eq!(deferred.parent(&app), Some(theater.as_object()));
assert_eq!(deferred.size(&app), theater.size(&app));
assert_eq!(
deferred.child(&app).expect("built").size(&app),
theater.size(&app)
);
assert_eq!(theater.child_count(&app), 1);
let mut children = theater.children_in_paint_order(&app);
assert_eq!(children.next(&app), Some(painted[0]));
controller.toggle(&mut app);
assert!(!controller.is_showing(&app));
harness.pump(&mut app);
assert_eq!(children.next(&app), None);
assert_eq!(
RenderTheaterMixin::children_in_paint_order(theater, &app)
.collect(&app)
.len(),
1
);
controller.toggle(&mut app);
assert!(controller.is_showing(&app));
harness.pump(&mut app);
assert_eq!(
RenderTheaterMixin::children_in_paint_order(theater, &app)
.collect(&app)
.len(),
2
);
controller.hide(&mut app);
harness.pump(&mut app);
assert_eq!(
RenderTheaterMixin::children_in_paint_order(theater, &app)
.collect(&app)
.len(),
1
);
}
#[test]
fn overlay_layout_builder_resizes_without_requesting_child_dry_layout() {
use crate::widgets::basic::Positioned;
use std::cell::Cell;
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let controller = OverlayPortalController::new(&mut app, None);
let observed = Rc::new(Cell::new(None::<OverlayChildLayoutInfo>));
let output = observed.clone();
let entry = OverlayEntry::new(
&mut app,
Rc::new(move |_, _| {
let output = output.clone();
Positioned::new(
OverlayPortal::overlay_child_layout_builder(controller, move |_, _, info| {
output.set(Some(info));
Positioned::new(SizedBox::expand())
.left(0.0)
.top(0.0)
.width(40.0)
.height(20.0)
.into_widget()
})
.child(SizedBox::expand()),
)
.left(30.0)
.top(40.0)
.width(80.0)
.height(60.0)
.into_widget()
}),
false,
false,
false,
);
let harness = mount_overlay(&mut app, vec![entry]);
let theater = theater_of(&harness, &app);
for _ in 0..2 {
controller.show(&mut app);
harness.pump(&mut app);
let info = observed.get().expect("the overlay built after layout");
assert_eq!(info.child_size(), Size::new(80.0, 60.0));
assert_eq!(info.overlay_size(), theater.size(&app));
assert_eq!(
waft_painting::transform_point(&info.child_paint_transform(), Offset::ZERO),
Offset::new(30.0, 40.0),
);
let painted = RenderTheaterMixin::children_in_paint_order(theater, &app).collect(&app);
let deferred = painted[1]
.as_object()
.downcast::<RenderDeferredLayoutBox>(&app)
.unwrap();
let builder = deferred.child(&app).unwrap();
assert_eq!(deferred.size(&app), theater.size(&app));
assert_eq!(builder.size(&app), theater.size(&app));
let builder = builder
.as_object()
.downcast::<RenderOverlayChildLayoutBuilder>(&app)
.unwrap();
assert_eq!(
builder.child(&app).unwrap().size(&app),
Size::new(40.0, 20.0)
);
controller.hide(&mut app);
harness.pump(&mut app);
}
}
}