RE ADD CONTEXT

This commit is contained in:
iris committed 2025-12-15 21:50:53 -05:00
1 parent dc2be7f688
commit 0b8a93c5ce
39 files changed
+925 -713

No files matched your search

+36 -18
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,
}
}
+18 -18
View File
@@ -7,16 +7,16 @@ use winit::{
keyboard::{Key, NamedKey},
};
pub struct TextEdit {
view: TextView,
pub struct TextEdit<State> {
view: TextView<State>,
selection: TextSelection,
history: Vec<(String, TextSelection)>,
double_hit: Option<Cursor>,
pub single_line: bool,
}
impl TextEdit {
pub fn new(view: TextView, single_line: bool) -> Self {
impl<State: 'static> TextEdit<State> {
pub fn new(view: TextView<State>, single_line: bool) -> Self {
Self {
view,
selection: Default::default(),
@@ -43,8 +43,8 @@ impl TextEdit {
}
}
impl Widget for TextEdit {
fn draw(&mut self, painter: &mut Painter) {
impl<State: 'static> Widget<State> for TextEdit<State> {
fn draw(&mut self, painter: &mut Painter<State>) {
let base = painter.layer;
painter.child_layer();
self.view.draw(painter);
@@ -86,11 +86,11 @@ impl Widget for TextEdit {
}
}
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
fn desired_width(&mut self, ctx: &mut SizeCtx<State>) -> Len {
self.view.desired_width(ctx)
}
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
fn desired_height(&mut self, ctx: &mut SizeCtx<State>) -> Len {
self.view.desired_height(ctx)
}
}
@@ -174,12 +174,12 @@ fn cursor_pos(cursor: Cursor, buf: &TextBuffer) -> Option<Vec2> {
prev
}
pub struct TextEditCtx<'a> {
pub text: &'a mut TextEdit,
pub struct TextEditCtx<'a, State> {
pub text: &'a mut TextEdit<State>,
pub font_system: &'a mut FontSystem,
}
impl<'a> TextEditCtx<'a> {
impl<'a, State: 'static> TextEditCtx<'a, State> {
pub fn take(&mut self) -> String {
let text = self
.text
@@ -596,26 +596,26 @@ impl TextInputResult {
}
}
impl Deref for TextEdit {
type Target = TextView;
impl<State> Deref for TextEdit<State> {
type Target = TextView<State>;
fn deref(&self) -> &Self::Target {
&self.view
}
}
impl DerefMut for TextEdit {
impl<State> DerefMut for TextEdit<State> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.view
}
}
pub trait TextEditable {
fn edit<'a>(&self, ui: &'a mut Ui) -> TextEditCtx<'a>;
pub trait TextEditable<State> {
fn edit<'a>(&self, ui: &'a mut Ui<State>) -> TextEditCtx<'a, State>;
}
impl<I: IdLike<Widget = TextEdit>> TextEditable for I {
fn edit<'a>(&self, ui: &'a mut Ui) -> TextEditCtx<'a> {
impl<State: 'static, I: IdLike<State, Widget = TextEdit<State>>> TextEditable<State> for I {
fn edit<'a>(&self, ui: &'a mut Ui<State>) -> TextEditCtx<'a, State> {
TextEditCtx {
text: ui.data.widgets.get_mut(self).unwrap(),
font_system: &mut ui.data.text.font_system,
+20 -20
View File
@@ -11,22 +11,22 @@ use std::ops::{Deref, DerefMut};
pub const SHAPING: Shaping = Shaping::Advanced;
pub struct Text {
pub struct Text<State> {
pub content: MutDetect<String>,
view: TextView,
view: TextView<State>,
}
pub struct TextView {
pub struct TextView<State> {
pub attrs: MutDetect<TextAttrs>,
pub buf: MutDetect<TextBuffer>,
// cache
tex: Option<RenderedText>,
width: Option<f32>,
pub hint: Option<WidgetHandle>,
pub hint: Option<WidgetHandle<State>>,
}
impl TextView {
pub fn new(buf: TextBuffer, attrs: TextAttrs, hint: Option<WidgetHandle>) -> Self {
impl<State: 'static> TextView<State> {
pub fn new(buf: TextBuffer, attrs: TextAttrs, hint: Option<WidgetHandle<State>>) -> Self {
Self {
attrs: attrs.into(),
buf: buf.into(),
@@ -54,7 +54,7 @@ impl TextView {
region
}
fn render(&mut self, ctx: &mut SizeCtx) -> RenderedText {
fn render(&mut self, ctx: &mut SizeCtx<State>) -> RenderedText {
let width = if self.attrs.wrap {
Some(ctx.px_size().x)
} else {
@@ -80,7 +80,7 @@ impl TextView {
pub fn tex(&self) -> Option<&RenderedText> {
self.tex.as_ref()
}
pub fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
pub fn desired_width(&mut self, ctx: &mut SizeCtx<State>) -> Len {
if let Some(hint) = &self.hint
&& let [line] = &self.buf.lines[..]
&& line.text().is_empty()
@@ -90,7 +90,7 @@ impl TextView {
Len::abs(self.render(ctx).size.x)
}
}
pub fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
pub fn desired_height(&mut self, ctx: &mut SizeCtx<State>) -> Len {
if let Some(hint) = &self.hint
&& let [line] = &self.buf.lines[..]
&& line.text().is_empty()
@@ -100,7 +100,7 @@ impl TextView {
Len::abs(self.render(ctx).size.y)
}
}
pub fn draw(&mut self, painter: &mut Painter) -> UiRegion {
pub fn draw(&mut self, painter: &mut Painter<State>) -> UiRegion {
let tex = self.render(&mut painter.size_ctx());
let region = self.tex_region(&tex);
if let Some(hint) = &self.hint
@@ -124,7 +124,7 @@ impl TextView {
}
}
impl Text {
impl<State: 'static> Text<State> {
pub fn new(content: impl Into<String>) -> Self {
let attrs = TextAttrs::default();
let buf = TextBuffer::new_empty(Metrics::new(attrs.font_size, attrs.line_height));
@@ -133,7 +133,7 @@ impl Text {
view: TextView::new(buf, attrs, None),
}
}
fn update_buf(&mut self, ctx: &mut SizeCtx) {
fn update_buf(&mut self, ctx: &mut SizeCtx<State>) {
if self.content.changed {
self.content.changed = false;
self.view.buf.set_text(
@@ -147,18 +147,18 @@ impl Text {
}
}
impl Widget for Text {
fn draw(&mut self, painter: &mut Painter) {
impl<State: 'static> Widget<State> for Text<State> {
fn draw(&mut self, painter: &mut Painter<State>) {
self.update_buf(&mut painter.size_ctx());
self.view.draw(painter);
}
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
fn desired_width(&mut self, ctx: &mut SizeCtx<State>) -> Len {
self.update_buf(ctx);
self.view.desired_width(ctx)
}
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
fn desired_height(&mut self, ctx: &mut SizeCtx<State>) -> Len {
self.update_buf(ctx);
self.view.desired_height(ctx)
}
@@ -174,7 +174,7 @@ pub fn edit_line(line: &mut BufferLine, text: String) {
line.set_text(text, line.ending(), line.attrs_list().clone());
}
impl Deref for Text {
impl<State> Deref for Text<State> {
type Target = TextAttrs;
fn deref(&self) -> &Self::Target {
@@ -182,13 +182,13 @@ impl Deref for Text {
}
}
impl DerefMut for Text {
impl<State> DerefMut for Text<State> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.view
}
}
impl Deref for TextView {
impl<State> Deref for TextView<State> {
type Target = TextAttrs;
fn deref(&self) -> &Self::Target {
@@ -196,7 +196,7 @@ impl Deref for TextView {
}
}
impl DerefMut for TextView {
impl<State> DerefMut for TextView<State> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.attrs
}