use super::adapt::MapAny;
use crate::event::ConfigCx;
use crate::geom::Rect;
use crate::layout::AlignHints;
use crate::text::format::FormattableText;
use crate::theme::{SizeCx, Text, TextClass};
use crate::{Events, Layout, Role, RoleCx, Tile};
use kas_macros::impl_self;
use std::fmt::Debug;
#[impl_self]
mod Label {
#[derive(Debug)]
#[widget]
#[layout(self.text)]
pub struct Label<T: FormattableText + 'static = String> {
core: widget_core!(),
text: Text<T>,
}
impl Self {
#[inline]
pub fn new(text: T) -> Self {
Label {
core: Default::default(),
text: Text::new(text, TextClass::Label, true),
}
}
#[inline]
pub fn new_any<A>(text: T) -> MapAny<A, Self> {
MapAny::new(Label::new(text))
}
#[inline]
pub fn class(&self) -> TextClass {
self.text.class()
}
#[inline]
pub fn set_class(&mut self, class: TextClass) {
self.text.set_class(class);
}
#[inline]
pub fn with_class(mut self, class: TextClass) -> Self {
self.text.set_class(class);
self
}
#[inline]
pub fn wrap(&self) -> bool {
self.text.wrap()
}
#[inline]
pub fn set_wrap(&mut self, wrap: bool) {
self.text.set_wrap(wrap);
}
#[inline]
pub fn with_wrap(mut self, wrap: bool) -> Self {
self.text.set_wrap(wrap);
self
}
#[inline]
pub fn text(&self) -> &Text<T> {
&self.text
}
pub fn set_text(&mut self, cx: &mut ConfigCx, text: T) {
self.text.set_text(text);
self.text.reprepare_action(cx);
}
pub fn as_str(&self) -> &str {
self.text.as_str()
}
}
impl Layout for Self {
fn set_rect(&mut self, cx: &mut SizeCx, rect: Rect, hints: AlignHints) {
self.text
.set_rect(cx, rect, hints.combine(AlignHints::VERT_CENTER));
}
}
impl Tile for Self {
fn role(&self, _: &mut dyn RoleCx) -> Role<'_> {
Role::Label(self.text.as_str())
}
}
impl Events for Self {
type Data = ();
fn configure(&mut self, cx: &mut ConfigCx) {
self.text.configure(&mut cx.size_cx());
}
}
impl Label<String> {
pub fn set_string(&mut self, cx: &mut ConfigCx, string: String) {
if self.text.set_string(string) {
self.text.reprepare_action(cx);
}
}
}
}
impl<T: FormattableText + 'static> From<T> for Label<T> {
fn from(text: T) -> Self {
Label::new(text)
}
}
impl<'a> From<&'a str> for Label<String> {
fn from(text: &'a str) -> Self {
Label::new(text.to_string())
}
}