remove state generic from a lot of things

This commit is contained in:
iris committed 2025-12-17 21:37:55 -05:00
1 parent 7e6369029f
commit 30bc55c78e
45 files changed
+740 -856

No files matched your search

+16 -15
View File
@@ -51,7 +51,7 @@ impl<State, O, H: WidgetOption<State>> TextBuilder<State, O, H> {
}
}
impl<State: 'static, O> TextBuilder<State, O> {
impl<State: HasUi, O> TextBuilder<State, O> {
pub fn hint<W: WidgetLike<State, Tag>, Tag>(
self,
hint: W,
@@ -59,7 +59,7 @@ impl<State: 'static, O> TextBuilder<State, O> {
TextBuilder {
content: self.content,
attrs: self.attrs,
hint: move |ui: &mut Ui<State>| Some(hint.add(ui).any()),
hint: move |ui: &mut State| Some(hint.add(ui).any()),
output: self.output,
state: PhantomData,
}
@@ -69,25 +69,25 @@ impl<State: 'static, O> TextBuilder<State, O> {
pub trait TextBuilderOutput<State>: Sized {
type Output;
fn run<H: WidgetOption<State>>(
ui: &mut Ui<State>,
state: &mut State,
builder: TextBuilder<State, Self, H>,
) -> Self::Output;
}
pub struct TextOutput;
impl<State: 'static> TextBuilderOutput<State> for TextOutput {
type Output = Text<State>;
impl<State: HasUi> TextBuilderOutput<State> for TextOutput {
type Output = Text;
fn run<H: WidgetOption<State>>(
ui: &mut Ui<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(ui);
let font_system = &mut ui.text.font_system;
let hint = builder.hint.get(state);
let font_system = &mut state.ui().text.font_system;
buf.set_text(font_system, &builder.content, &Attrs::new(), SHAPING, None);
let mut text = Text {
content: builder.content.into(),
@@ -102,11 +102,12 @@ impl<State: 'static> TextBuilderOutput<State> for TextOutput {
pub struct TextEditOutput {
mode: EditMode,
}
impl<State: 'static> TextBuilderOutput<State> for TextEditOutput {
type Output = TextEdit<State>;
impl<State: HasUi> TextBuilderOutput<State> for TextEditOutput {
type Output = TextEdit;
fn run<H: WidgetOption<State>>(
ui: &mut Ui<State>,
state: &mut State,
builder: TextBuilder<State, Self, H>,
) -> Self::Output {
let buf = TextBuffer::new_empty(Metrics::new(
@@ -114,10 +115,10 @@ impl<State: 'static> TextBuilderOutput<State> for TextEditOutput {
builder.attrs.line_height,
));
let mut text = TextEdit::new(
TextView::new(buf, builder.attrs, builder.hint.get(ui)),
TextView::new(buf, builder.attrs, builder.hint.get(state)),
builder.output.mode,
);
let font_system = &mut ui.text.font_system;
let font_system = &mut state.ui().text.font_system;
text.buf
.set_text(font_system, &builder.content, &Attrs::new(), SHAPING, None);
builder.attrs.apply(font_system, &mut text.buf, None);
@@ -125,12 +126,12 @@ impl<State: 'static> TextBuilderOutput<State> for TextEditOutput {
}
}
impl<State, O: TextBuilderOutput<State>, H: WidgetOption<State>> FnOnce<(&mut Ui<State>,)>
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 Ui<State>,)) -> Self::Output {
extern "rust-call" fn call_once(self, args: (&mut State,)) -> Self::Output {
O::run(args.0, self)
}
}
+18 -18
View File
@@ -7,8 +7,8 @@ use winit::{
keyboard::{Key, NamedKey},
};
pub struct TextEdit<State> {
view: TextView<State>,
pub struct TextEdit {
view: TextView,
selection: TextSelection,
history: Vec<(String, TextSelection)>,
double_hit: Option<Cursor>,
@@ -21,8 +21,8 @@ pub enum EditMode {
MultiLine,
}
impl<State: 'static> TextEdit<State> {
pub fn new(view: TextView<State>, mode: EditMode) -> Self {
impl TextEdit {
pub fn new(view: TextView, mode: EditMode) -> Self {
Self {
view,
selection: Default::default(),
@@ -49,8 +49,8 @@ impl<State: 'static> TextEdit<State> {
}
}
impl<State: 'static> Widget<State> for TextEdit<State> {
fn draw(&mut self, painter: &mut Painter<State>) {
impl Widget for TextEdit {
fn draw(&mut self, painter: &mut Painter) {
let base = painter.layer;
painter.child_layer();
self.view.draw(painter);
@@ -92,11 +92,11 @@ impl<State: 'static> Widget<State> for TextEdit<State> {
}
}
fn desired_width(&mut self, ctx: &mut SizeCtx<State>) -> Len {
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
self.view.desired_width(ctx)
}
fn desired_height(&mut self, ctx: &mut SizeCtx<State>) -> Len {
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
self.view.desired_height(ctx)
}
}
@@ -180,12 +180,12 @@ fn cursor_pos(cursor: Cursor, buf: &TextBuffer) -> Option<Vec2> {
prev
}
pub struct TextEditCtx<'a, State> {
pub text: &'a mut TextEdit<State>,
pub struct TextEditCtx<'a> {
pub text: &'a mut TextEdit,
pub font_system: &'a mut FontSystem,
}
impl<'a, State: 'static> TextEditCtx<'a, State> {
impl<'a> TextEditCtx<'a> {
pub fn take(&mut self) -> String {
let text = self
.text
@@ -602,26 +602,26 @@ impl TextInputResult {
}
}
impl<State> Deref for TextEdit<State> {
type Target = TextView<State>;
impl Deref for TextEdit {
type Target = TextView;
fn deref(&self) -> &Self::Target {
&self.view
}
}
impl<State> DerefMut for TextEdit<State> {
impl DerefMut for TextEdit {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.view
}
}
pub trait TextEditable<State> {
fn edit<'a>(&self, ui: &'a mut Ui<State>) -> TextEditCtx<'a, State>;
pub trait TextEditable {
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> {
impl<I: IdLike<Widget = TextEdit>> TextEditable for I {
fn edit<'a>(&self, ui: &'a mut Ui) -> TextEditCtx<'a> {
TextEditCtx {
text: ui.widgets.get_mut(self).unwrap(),
font_system: &mut ui.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<State> {
pub struct Text {
pub content: MutDetect<String>,
view: TextView<State>,
view: TextView,
}
pub struct TextView<State> {
pub struct TextView {
pub attrs: MutDetect<TextAttrs>,
pub buf: MutDetect<TextBuffer>,
// cache
tex: Option<RenderedText>,
width: Option<f32>,
pub hint: Option<WidgetHandle<State>>,
pub hint: Option<WidgetHandle>,
}
impl<State: 'static> TextView<State> {
pub fn new(buf: TextBuffer, attrs: TextAttrs, hint: Option<WidgetHandle<State>>) -> Self {
impl TextView {
pub fn new(buf: TextBuffer, attrs: TextAttrs, hint: Option<WidgetHandle>) -> Self {
Self {
attrs: attrs.into(),
buf: buf.into(),
@@ -54,7 +54,7 @@ impl<State: 'static> TextView<State> {
region
}
fn render(&mut self, ctx: &mut SizeCtx<State>) -> RenderedText {
fn render(&mut self, ctx: &mut SizeCtx) -> RenderedText {
let width = if self.attrs.wrap {
Some(ctx.px_size().x)
} else {
@@ -80,7 +80,7 @@ impl<State: 'static> TextView<State> {
pub fn tex(&self) -> Option<&RenderedText> {
self.tex.as_ref()
}
pub fn desired_width(&mut self, ctx: &mut SizeCtx<State>) -> Len {
pub fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
if let Some(hint) = &self.hint
&& let [line] = &self.buf.lines[..]
&& line.text().is_empty()
@@ -90,7 +90,7 @@ impl<State: 'static> TextView<State> {
Len::abs(self.render(ctx).size.x)
}
}
pub fn desired_height(&mut self, ctx: &mut SizeCtx<State>) -> Len {
pub fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
if let Some(hint) = &self.hint
&& let [line] = &self.buf.lines[..]
&& line.text().is_empty()
@@ -100,7 +100,7 @@ impl<State: 'static> TextView<State> {
Len::abs(self.render(ctx).size.y)
}
}
pub fn draw(&mut self, painter: &mut Painter<State>) -> UiRegion {
pub fn draw(&mut self, painter: &mut Painter) -> 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<State: 'static> TextView<State> {
}
}
impl<State: 'static> Text<State> {
impl Text {
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<State: 'static> Text<State> {
view: TextView::new(buf, attrs, None),
}
}
fn update_buf(&mut self, ctx: &mut SizeCtx<State>) {
fn update_buf(&mut self, ctx: &mut SizeCtx) {
if self.content.changed {
self.content.changed = false;
self.view.buf.set_text(
@@ -147,18 +147,18 @@ impl<State: 'static> Text<State> {
}
}
impl<State: 'static> Widget<State> for Text<State> {
fn draw(&mut self, painter: &mut Painter<State>) {
impl Widget for Text {
fn draw(&mut self, painter: &mut Painter) {
self.update_buf(&mut painter.size_ctx());
self.view.draw(painter);
}
fn desired_width(&mut self, ctx: &mut SizeCtx<State>) -> Len {
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
self.update_buf(ctx);
self.view.desired_width(ctx)
}
fn desired_height(&mut self, ctx: &mut SizeCtx<State>) -> Len {
fn desired_height(&mut self, ctx: &mut SizeCtx) -> 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<State> Deref for Text<State> {
impl Deref for Text {
type Target = TextAttrs;
fn deref(&self) -> &Self::Target {
@@ -182,13 +182,13 @@ impl<State> Deref for Text<State> {
}
}
impl<State> DerefMut for Text<State> {
impl DerefMut for Text {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.view
}
}
impl<State> Deref for TextView<State> {
impl Deref for TextView {
type Target = TextAttrs;
fn deref(&self) -> &Self::Target {
@@ -196,7 +196,7 @@ impl<State> Deref for TextView<State> {
}
}
impl<State> DerefMut for TextView<State> {
impl DerefMut for TextView {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.attrs
}