`Sized` 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. It was already biting inside iris: `default/mod.rs`, `widget/ptr.rs` and `widget/text/build.rs` all imported `std::marker::Sized` explicitly to get out from under it, which they no longer need.
`SetSize` rather than `FixedSize` because the size it sets need not be fixed -- `width(rest(2))` (a flex weight) and `width(rel(0.5))` (half the parent) build the same widget, and both are more common than `sized((100, 100))`. It also pairs with the `MaxSize` beside it in that module: one sets a length, the other caps it. The builders are unchanged.
`tests/prelude_bounds.rs` is a compile-level guard -- it fails to build if the prelude shadows `Sized` again, which I checked by reverting `src/` under it:
```
error[E0404]: expected trait, found struct `Sized`
--> tests/prelude_bounds.rs:8:22
|
8 | fn takes_unsized<T: ?Sized>(_: &T) {}
| ^^^^^ not a trait
```
The pad tab of the tabs example -- the one built out of `sized` and the flexible widths -- renders pixel-identical to before the rename.
---------
Co-authored-by: iris <2+iris@noreply.localhost>
Reviewed-on: iris/iris#14
Co-authored-by: AIris <4+iris-ai@noreply.localhost>
133 lines
3.5 KiB
Rust
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,
|
|
}
|
|
}
|