RE ADD CONTEXT

This commit is contained in:
2025-12-15 21:50:53 -05:00
parent dc2be7f688
commit 0b8a93c5ce
39 changed files with 925 additions and 713 deletions

View File

@@ -1,15 +1,16 @@
use crate::prelude::*;
use cosmic_text::{Attrs, Family, Metrics};
use std::marker::Sized;
use std::marker::{PhantomData, Sized};
pub struct TextBuilder<O = TextOutput, H: WidgetOption = ()> {
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<O, H: WidgetOption> TextBuilder<O, H> {
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;
@@ -39,37 +40,48 @@ impl<O, H: WidgetOption> TextBuilder<O, H> {
self.attrs.wrap = wrap;
self
}
pub fn editable(self, single_line: bool) -> TextBuilder<TextEditOutput, H> {
pub fn editable(self, single_line: bool) -> TextBuilder<State, TextEditOutput, H> {
TextBuilder {
content: self.content,
attrs: self.attrs,
hint: self.hint,
output: TextEditOutput { single_line },
state: PhantomData,
}
}
}
impl<O> TextBuilder<O> {
pub fn hint<W: WidgetLike<Tag>, Tag>(self, hint: W) -> TextBuilder<O, impl WidgetOption> {
impl<State: 'static, 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 Ui| Some(hint.add(ui).any()),
hint: move |ui: &mut Ui<State>| Some(hint.add(ui).any()),
output: self.output,
state: PhantomData,
}
}
}
pub trait TextBuilderOutput: Sized {
pub trait TextBuilderOutput<State>: Sized {
type Output;
fn run<H: WidgetOption>(ui: &mut Ui, builder: TextBuilder<Self, H>) -> Self::Output;
fn run<H: WidgetOption<State>>(
ui: &mut Ui<State>,
builder: TextBuilder<State, Self, H>,
) -> Self::Output;
}
pub struct TextOutput;
impl TextBuilderOutput for TextOutput {
type Output = Text;
impl<State: 'static> TextBuilderOutput<State> for TextOutput {
type Output = Text<State>;
fn run<H: WidgetOption>(ui: &mut Ui, builder: TextBuilder<Self, H>) -> Self::Output {
fn run<H: WidgetOption<State>>(
ui: &mut Ui<State>,
builder: TextBuilder<State, Self, H>,
) -> Self::Output {
let mut buf = TextBuffer::new_empty(Metrics::new(
builder.attrs.font_size,
builder.attrs.line_height,
@@ -90,10 +102,13 @@ impl TextBuilderOutput for TextOutput {
pub struct TextEditOutput {
single_line: bool,
}
impl TextBuilderOutput for TextEditOutput {
type Output = TextEdit;
impl<State: 'static> TextBuilderOutput<State> for TextEditOutput {
type Output = TextEdit<State>;
fn run<H: WidgetOption>(ui: &mut Ui, builder: TextBuilder<Self, H>) -> Self::Output {
fn run<H: WidgetOption<State>>(
ui: &mut Ui<State>,
builder: TextBuilder<State, Self, H>,
) -> Self::Output {
let buf = TextBuffer::new_empty(Metrics::new(
builder.attrs.font_size,
builder.attrs.line_height,
@@ -110,19 +125,22 @@ impl TextBuilderOutput for TextEditOutput {
}
}
impl<O: TextBuilderOutput, H: WidgetOption> FnOnce<(&mut Ui,)> for TextBuilder<O, H> {
impl<State, O: TextBuilderOutput<State>, H: WidgetOption<State>> FnOnce<(&mut Ui<State>,)>
for TextBuilder<State, O, H>
{
type Output = O::Output;
extern "rust-call" fn call_once(self, args: (&mut Ui,)) -> Self::Output {
extern "rust-call" fn call_once(self, args: (&mut Ui<State>,)) -> Self::Output {
O::run(args.0, self)
}
}
pub fn wtext(content: impl Into<String>) -> TextBuilder {
pub fn wtext<State>(content: impl Into<String>) -> TextBuilder<State> {
TextBuilder {
content: content.into(),
attrs: TextAttrs::default(),
hint: (),
output: TextOutput,
state: PhantomData,
}
}