Files
iris/src/widget/text/build.rs
T
irisandClaude Opus 5 f312db60c2 Rename the Sized widget to SetSize
It shadowed the marker trait, so a `?Sized` bound in any crate that imports
the prelude failed to resolve -- a compile error in someone else's code that
nothing here would have caught. Three files inside iris already imported
`std::marker::Sized` to get out from under it; they no longer need to.

`SetSize` rather than `FixedSize` because the size it sets need not be fixed:
`width(rest(2))` and `width(rel(0.5))` build the same widget. It pairs with
the `MaxSize` beside it -- one sets a length, the other caps it.

`tests/prelude_bounds.rs` is a compile-level guard: it fails to build if the
prelude shadows `Sized` again. The pad tab of the tabs example, which is what
uses `sized` and the flexible widths, renders pixel-identical.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 19:34:52 -04:00

133 lines
3.5 KiB
Rust

use crate::prelude::*;
use std::marker::PhantomData;
pub struct TextBuilder<State, O = TextOutput, H: WidgetOption<State> = ()> {
pub content: String,
pub attrs: TextAttrs,
pub hint: H,
pub output: O,
state: PhantomData<State>,
}
impl<State, O, H: WidgetOption<State>> TextBuilder<State, O, H> {
pub fn size(mut self, size: impl UiNum) -> Self {
self.attrs.font_size = size.to_f32();
self.attrs.line_height = self.attrs.font_size * LINE_HEIGHT_MULT;
self
}
pub fn color(mut self, color: UiColor) -> Self {
self.attrs.color = color;
self
}
pub fn family(mut self, family: Family) -> Self {
self.attrs.family = family;
self
}
pub fn line_height(mut self, height: f32) -> Self {
self.attrs.line_height = height;
self
}
pub fn text_align(mut self, align: impl Into<RegionAlign>) -> Self {
self.attrs.align = align.into();
self
}
pub fn center_text(mut self) -> Self {
self.attrs.align = Align::CENTER;
self
}
pub fn wrap(mut self, wrap: bool) -> Self {
self.attrs.wrap = wrap;
self
}
pub fn editable(self, mode: EditMode) -> TextBuilder<State, TextEditOutput, H> {
TextBuilder {
content: self.content,
attrs: self.attrs,
hint: self.hint,
output: TextEditOutput { mode },
state: PhantomData,
}
}
}
impl<Rsc: UiRsc, O> TextBuilder<Rsc, O> {
pub fn hint<W: WidgetLike<Rsc, Tag>, Tag>(
self,
hint: W,
) -> TextBuilder<Rsc, O, impl WidgetOption<Rsc>> {
TextBuilder {
content: self.content,
attrs: self.attrs,
hint: move |rsc: &mut Rsc| Some(hint.add_strong(rsc).any()),
output: self.output,
state: PhantomData,
}
}
}
pub trait TextBuilderOutput<State>: Sized {
type Output;
fn run<H: WidgetOption<State>>(
state: &mut State,
builder: TextBuilder<State, Self, H>,
) -> Self::Output;
}
pub struct TextOutput;
impl<Rsc: UiRsc> TextBuilderOutput<Rsc> for TextOutput {
type Output = Text;
fn run<H: WidgetOption<Rsc>>(
state: &mut Rsc,
builder: TextBuilder<Rsc, Self, H>,
) -> Self::Output {
let buf = TextBuffer::new(&builder.content);
let hint = builder.hint.get(state);
let mut text = Text {
content: builder.content.into(),
view: TextView::new(buf, builder.attrs, hint),
};
text.content.changed = false;
text
}
}
pub struct TextEditOutput {
mode: EditMode,
}
impl<State: UiRsc> TextBuilderOutput<State> for TextEditOutput {
type Output = TextEdit;
fn run<H: WidgetOption<State>>(
state: &mut State,
builder: TextBuilder<State, Self, H>,
) -> Self::Output {
let buf = TextBuffer::new(&builder.content);
TextEdit::new(
TextView::new(buf, builder.attrs, builder.hint.get(state)),
builder.output.mode,
)
}
}
impl<State, O: TextBuilderOutput<State>, H: WidgetOption<State>> FnOnce<(&mut State,)>
for TextBuilder<State, O, H>
{
type Output = O::Output;
extern "rust-call" fn call_once(self, args: (&mut State,)) -> Self::Output {
O::run(args.0, self)
}
}
pub fn wtext<State>(content: impl Into<String>) -> TextBuilder<State> {
TextBuilder {
content: content.into(),
attrs: TextAttrs::default(),
hint: (),
output: TextOutput,
state: PhantomData,
}
}