use crate::AccessLabel;
use kas::prelude::*;
#[impl_self]
mod WithLabel {
#[autoimpl(Deref, DerefMut using self.inner)]
#[autoimpl(Viewport using self.inner where W: trait)]
#[widget]
#[layout(list![self.inner, self.label].with_direction(self.dir))]
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 label_direction(&self) -> Direction {
self.dir.as_direction()
}
#[inline]
pub fn take_inner(self) -> W {
self.inner
}
#[inline]
pub fn label_wrap(&self) -> bool {
self.label.wrap()
}
#[inline]
pub fn set_label_wrap(&mut self, wrap: bool) {
self.label.set_wrap(wrap);
}
#[inline]
pub fn with_label_wrap(mut self, wrap: bool) -> Self {
self.label.set_wrap(wrap);
self
}
pub fn set_label_text<T: Into<AccessString>>(&mut self, cx: &mut ConfigCx, text: T) {
self.label.set_text(cx, text.into());
}
}
impl Tile for Self {
fn role_child_properties(&self, cx: &mut dyn RoleCx, index: usize) {
if index == widget_index!(self.inner) {
cx.set_label(self.label.id());
}
}
fn nav_next(&self, _: bool, from: Option<usize>) -> Option<usize> {
from.xor(Some(widget_index!(self.inner)))
}
}
impl Events for Self {
type Data = W::Data;
fn probe(&self, _: Coord) -> Id {
self.inner.id()
}
fn post_configure(&mut self, _: &mut ConfigCx) {
self.label.set_target(self.inner.id());
}
}
}
#[impl_self]
mod WithHiddenLabel {
#[autoimpl(Deref, DerefMut using self.inner)]
#[autoimpl(Clone where W: trait)]
#[autoimpl(Viewport using self.inner where W: trait)]
#[derive_widget]
pub struct WithHiddenLabel<W: Widget> {
#[widget]
inner: W,
label: String,
}
impl Self {
#[inline]
pub fn new<T: ToString>(inner: W, label: T) -> Self {
WithHiddenLabel {
inner,
label: label.to_string(),
}
}
#[inline]
pub fn take_inner(self) -> W {
self.inner
}
pub fn set_label<T: ToString>(&mut self, text: T) {
self.label = text.to_string();
}
}
impl Tile for Self {
fn tooltip(&self) -> Option<&str> {
Some(&self.label)
}
fn role(&self, cx: &mut dyn RoleCx) -> Role<'_> {
cx.set_label(&self.label);
self.inner.role(cx)
}
}
}