148 lines
4.3 KiB
Rust
148 lines
4.3 KiB
Rust
use crate::prelude::*;
|
|
use cosmic_text::{Attrs, Family, Metrics};
|
|
use std::marker::{PhantomData, Sized};
|
|
|
|
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<'static>) -> 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<State: HasUi + StateLike<State>, O> TextBuilder<State, O> {
|
|
pub fn hint<W: WidgetLike<State, Tag>, Tag>(
|
|
self,
|
|
hint: W,
|
|
) -> TextBuilder<State, O, impl WidgetOption<State>> {
|
|
TextBuilder {
|
|
content: self.content,
|
|
attrs: self.attrs,
|
|
hint: move |ui: &mut State| Some(hint.add_strong(ui).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<State: HasUi> TextBuilderOutput<State> for TextOutput {
|
|
type Output = Text;
|
|
|
|
fn run<H: WidgetOption<State>>(
|
|
state: &mut State,
|
|
builder: TextBuilder<State, Self, H>,
|
|
) -> Self::Output {
|
|
let mut buf = TextBuffer::new_empty(Metrics::new(
|
|
builder.attrs.font_size,
|
|
builder.attrs.line_height,
|
|
));
|
|
let hint = builder.hint.get(state);
|
|
let font_system = &mut state.get_mut().text.font_system;
|
|
buf.set_text(font_system, &builder.content, &Attrs::new(), SHAPING, None);
|
|
let mut text = Text {
|
|
content: builder.content.into(),
|
|
view: TextView::new(buf, builder.attrs, hint),
|
|
};
|
|
text.content.changed = false;
|
|
builder.attrs.apply(font_system, &mut text.view.buf, None);
|
|
text
|
|
}
|
|
}
|
|
|
|
pub struct TextEditOutput {
|
|
mode: EditMode,
|
|
}
|
|
|
|
impl<State: HasUi> 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_empty(Metrics::new(
|
|
builder.attrs.font_size,
|
|
builder.attrs.line_height,
|
|
));
|
|
let mut text = TextEdit::new(
|
|
TextView::new(buf, builder.attrs, builder.hint.get(state)),
|
|
builder.output.mode,
|
|
);
|
|
let font_system = &mut state.get_mut().text.font_system;
|
|
text.buf
|
|
.set_text(font_system, &builder.content, &Attrs::new(), SHAPING, None);
|
|
builder.attrs.apply(font_system, &mut text.buf, None);
|
|
text
|
|
}
|
|
}
|
|
|
|
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,
|
|
}
|
|
}
|