use crate::{api::prelude::*, proc_macros::*, themes::theme_orbtk::*};
enum TextAction {
Localize,
}
#[derive(Debug, Clone, Default, AsAny)]
pub struct TextBlockState;
impl TextBlockState {
fn localize(&self, ctx: &mut Context) {
if !*TextBlock::localizable_ref(&ctx.widget()) {
return;
}
let text = TextBlock::text_clone(&ctx.widget());
let localized_text = ctx.localize_text(text);
TextBlock::localized_text_set(&mut ctx.widget(), localized_text);
}
}
impl State for TextBlockState {
fn init(&mut self, _registry: &mut Registry, ctx: &mut Context) {
self.localize(ctx);
}
fn update(&mut self, _registry: &mut Registry, ctx: &mut Context) {
self.localize(ctx);
}
fn messages(
&mut self,
mut messages: MessageReader,
_registry: &mut Registry,
ctx: &mut Context,
) {
for message in messages.read::<TextAction>() {
match message {
TextAction::Localize => self.localize(ctx),
}
}
}
}
widget!(
TextBlock<TextBlockState> {
text: String,
localized_text: String,
water_mark: String,
foreground: Brush,
font_size: f64,
font: String,
offset: f64,
localizable: bool
}
);
impl Template for TextBlock {
fn template(self, id: Entity, _: &mut BuildContext) -> Self {
self.name("TextBlock")
.text("")
.foreground(colors::LINK_WATER_COLOR)
.font_size(orbtk_fonts::FONT_SIZE_12)
.font("Roboto-Regular")
.localizable(true)
.on_changed("text", move |ctx, _| {
ctx.send_message(TextAction::Localize, id)
})
}
fn render_object(&self) -> Box<dyn RenderObject> {
TextRenderObject.into()
}
fn layout(&self) -> Box<dyn Layout> {
FixedSizeLayout::new().into()
}
}