use crate::AccessLabel;
use kas::{layout, prelude::*};
impl_scope! {
#[autoimpl(Deref, DerefMut using self.inner)]
#[derive(Clone, Default)]
#[widget {
Data = W::Data;
layout = list! 'row (self.dir, [self.inner, non_navigable!(self.label)]);
}]
pub struct WithLabel<W: Widget, D: Directional = Direction> {
core: widget_core!(),
dir: D,
#[widget]
inner: W,
#[widget(&())]
label: AccessLabel,
}
impl Self {
pub fn new<T: Into<AccessString>>(inner: W, label: T) -> Self where D: Default {
Self::new_dir(inner, D::default(), label)
}
}
impl<W: Widget> WithLabel<W, kas::dir::Left> {
pub fn left<T: Into<AccessString>>(inner: W, label: T) -> Self {
Self::new(inner, label)
}
}
impl<W: Widget> WithLabel<W, kas::dir::Right> {
pub fn right<T: Into<AccessString>>(inner: W, label: T) -> Self {
Self::new(inner, label)
}
}
impl Self {
#[inline]
pub fn new_dir<T: Into<AccessString>>(inner: W, direction: D, label: T) -> Self {
WithLabel {
core: Default::default(),
dir: direction,
inner,
label: AccessLabel::new(label.into()),
}
}
#[inline]
pub fn direction(&self) -> Direction {
self.dir.as_direction()
}
#[inline]
pub fn take_inner(self) -> W {
self.inner
}
#[inline]
pub fn layout_storage(&mut self) -> &mut impl layout::RowStorage {
&mut self.core.row
}
#[inline]
pub fn wrap(&self) -> bool {
self.label.wrap()
}
#[inline]
pub fn set_wrap(&mut self, wrap: bool) {
self.label.set_wrap(wrap);
}
#[inline]
pub fn with_wrap(mut self, wrap: bool) -> Self {
self.label.set_wrap(wrap);
self
}
pub fn set_text<T: Into<AccessString>>(&mut self, text: T) -> Action {
self.label.set_text(text.into())
}
}
impl Layout for Self {
fn find_id(&mut self, coord: Coord) -> Option<WidgetId> {
self.rect().contains(coord).then(|| self.inner.id())
}
}
}