use crate::event::ConfigCx;
use crate::geom::{Coord, Offset, Rect};
use crate::layout::{AxisInfo, SizeRules};
use crate::theme::{DrawCx, SizeCx};
use crate::util::IdentifyWidget;
use crate::WidgetId;
use kas_macros::autoimpl;
#[allow(unused)] use super::{Events, Widget};
#[allow(unused)] use crate::layout::{self, AlignPair};
#[allow(unused)] use kas_macros as macros;
#[autoimpl(for<T: trait + ?Sized> &'_ mut T, Box<T>)]
pub trait Layout {
fn as_layout(&self) -> &dyn Layout {
unimplemented!() }
fn id_ref(&self) -> &WidgetId {
unimplemented!() }
fn rect(&self) -> Rect {
unimplemented!() }
fn widget_name(&self) -> &'static str {
unimplemented!() }
fn num_children(&self) -> usize {
unimplemented!() }
fn get_child(&self, index: usize) -> Option<&dyn Layout> {
let _ = index;
unimplemented!() }
#[inline]
fn find_child_index(&self, id: &WidgetId) -> Option<usize> {
id.next_key_after(self.id_ref())
}
fn size_rules(&mut self, sizer: SizeCx, axis: AxisInfo) -> SizeRules;
fn set_rect(&mut self, cx: &mut ConfigCx, rect: Rect) {
let _ = (cx, rect);
unimplemented!() }
fn nav_next(&self, reverse: bool, from: Option<usize>) -> Option<usize> {
let _ = (reverse, from);
unimplemented!() }
#[inline]
fn translation(&self) -> Offset {
Offset::ZERO
}
fn find_id(&mut self, coord: Coord) -> Option<WidgetId> {
let _ = coord;
unimplemented!() }
fn draw(&mut self, draw: DrawCx);
}
pub trait LayoutExt: Layout {
#[inline]
fn id(&self) -> WidgetId {
self.id_ref().clone()
}
#[inline]
fn eq_id<T>(&self, rhs: T) -> bool
where
WidgetId: PartialEq<T>,
{
*self.id_ref() == rhs
}
#[inline]
fn identify(&self) -> IdentifyWidget {
IdentifyWidget(self.widget_name(), self.id_ref())
}
#[inline]
fn is_ancestor_of(&self, id: &WidgetId) -> bool {
self.id().is_ancestor_of(id)
}
#[inline]
fn is_strict_ancestor_of(&self, id: &WidgetId) -> bool {
!self.eq_id(id) && self.id().is_ancestor_of(id)
}
fn for_children(&self, mut f: impl FnMut(&dyn Layout)) {
for index in 0..self.num_children() {
if let Some(child) = self.get_child(index) {
f(child);
}
}
}
fn for_children_try<E>(
&self,
mut f: impl FnMut(&dyn Layout) -> Result<(), E>,
) -> Result<(), E> {
let mut result = Ok(());
for index in 0..self.num_children() {
if let Some(child) = self.get_child(index) {
result = f(child);
}
if result.is_err() {
break;
}
}
result
}
fn find_widget(&self, id: &WidgetId) -> Option<&dyn Layout> {
if let Some(child) = self.find_child_index(id).and_then(|i| self.get_child(i)) {
child.find_widget(id)
} else if self.eq_id(id) {
Some(self.as_layout())
} else {
None
}
}
}
impl<W: Layout + ?Sized> LayoutExt for W {}