From 99899d49cc93cdec3832f7b5ecad867fdd421e07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Fri, 1 Dec 2023 15:04:08 +0100 Subject: [PATCH 1/4] Clamp `text::measure` to `Buffer::size` --- graphics/src/text.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/graphics/src/text.rs b/graphics/src/text.rs index 7261900e23..fc7694c248 100644 --- a/graphics/src/text.rs +++ b/graphics/src/text.rs @@ -76,7 +76,12 @@ pub fn measure(buffer: &cosmic_text::Buffer) -> Size { (run.line_w.max(width), total_lines + 1) }); - Size::new(width, total_lines as f32 * buffer.metrics().line_height) + let (max_width, max_height) = buffer.size(); + + Size::new( + width.min(max_width), + (total_lines as f32 * buffer.metrics().line_height).min(max_height), + ) } /// Returns the attributes of the given [`Font`]. From 936d480267578d7e80675e78ec1880aaaaab72d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Fri, 1 Dec 2023 16:04:27 +0100 Subject: [PATCH 2/4] Clip text to `viewport` bounds instead of layout bounds --- core/src/renderer/null.rs | 3 ++ core/src/text.rs | 5 +++- core/src/widget/text.rs | 5 +++- graphics/src/primitive.rs | 26 ++++++++++------- graphics/src/renderer.rs | 6 ++++ renderer/src/lib.rs | 13 +++++++-- tiny_skia/src/backend.rs | 17 +++++------ tiny_skia/src/geometry.rs | 15 ++++++---- wgpu/src/geometry.rs | 15 ++++++---- wgpu/src/layer.rs | 7 +++++ wgpu/src/layer/text.rs | 5 ++++ wgpu/src/text.rs | 23 +++++++++------ widget/src/checkbox.rs | 4 ++- widget/src/column.rs | 20 +++++++------ widget/src/combo_box.rs | 3 +- widget/src/container.rs | 32 +++++++++++---------- widget/src/overlay/menu.rs | 1 + widget/src/pick_list.rs | 6 +++- widget/src/radio.rs | 3 +- widget/src/row.rs | 20 +++++++------ widget/src/text_editor.rs | 3 +- widget/src/text_input.rs | 59 ++++++++++++++++++-------------------- widget/src/toggler.rs | 3 +- 23 files changed, 178 insertions(+), 116 deletions(-) diff --git a/core/src/renderer/null.rs b/core/src/renderer/null.rs index da0f32de47..3ce6a4f570 100644 --- a/core/src/renderer/null.rs +++ b/core/src/renderer/null.rs @@ -64,6 +64,7 @@ impl text::Renderer for Null { _paragraph: &Self::Paragraph, _position: Point, _color: Color, + _viewport: Rectangle, ) { } @@ -72,6 +73,7 @@ impl text::Renderer for Null { _editor: &Self::Editor, _position: Point, _color: Color, + _viewport: Rectangle, ) { } @@ -80,6 +82,7 @@ impl text::Renderer for Null { _paragraph: Text<'_, Self::Font>, _position: Point, _color: Color, + _viewport: Rectangle, ) { } } diff --git a/core/src/text.rs b/core/src/text.rs index 546d0b5c3c..697fa628b9 100644 --- a/core/src/text.rs +++ b/core/src/text.rs @@ -9,7 +9,7 @@ pub use highlighter::Highlighter; pub use paragraph::Paragraph; use crate::alignment; -use crate::{Color, Pixels, Point, Size}; +use crate::{Color, Pixels, Point, Rectangle, Size}; use std::borrow::Cow; use std::hash::{Hash, Hasher}; @@ -202,6 +202,7 @@ pub trait Renderer: crate::Renderer { text: &Self::Paragraph, position: Point, color: Color, + viewport: Rectangle, ); /// Draws the given [`Editor`] at the given position and with the given @@ -211,6 +212,7 @@ pub trait Renderer: crate::Renderer { editor: &Self::Editor, position: Point, color: Color, + viewport: Rectangle, ); /// Draws the given [`Text`] at the given position and with the given @@ -220,5 +222,6 @@ pub trait Renderer: crate::Renderer { text: Text<'_, Self::Font>, position: Point, color: Color, + viewport: Rectangle, ); } diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs index 97e0acacdc..e020b0303e 100644 --- a/core/src/widget/text.rs +++ b/core/src/widget/text.rs @@ -172,7 +172,7 @@ where style: &renderer::Style, layout: Layout<'_>, _cursor_position: mouse::Cursor, - _viewport: &Rectangle, + viewport: &Rectangle, ) { let state = tree.state.downcast_ref::>(); @@ -182,6 +182,7 @@ where layout, state, theme.appearance(self.style.clone()), + viewport, ); } } @@ -244,6 +245,7 @@ pub fn draw( layout: Layout<'_>, state: &State, appearance: Appearance, + viewport: &Rectangle, ) where Renderer: text::Renderer, { @@ -266,6 +268,7 @@ pub fn draw( paragraph, Point::new(x, y), appearance.color.unwrap_or(style.text_color), + *viewport, ); } diff --git a/graphics/src/primitive.rs b/graphics/src/primitive.rs index 4ed512c1d3..837eb77ad9 100644 --- a/graphics/src/primitive.rs +++ b/graphics/src/primitive.rs @@ -14,24 +14,26 @@ use std::sync::Arc; pub enum Primitive { /// A text primitive Text { - /// The contents of the text + /// The contents of the text. content: String, - /// The bounds of the text + /// The bounds of the text. bounds: Rectangle, - /// The color of the text + /// The color of the text. color: Color, - /// The size of the text in logical pixels + /// The size of the text in logical pixels. size: Pixels, - /// The line height of the text + /// The line height of the text. line_height: text::LineHeight, - /// The font of the text + /// The font of the text. font: Font, - /// The horizontal alignment of the text + /// The horizontal alignment of the text. horizontal_alignment: alignment::Horizontal, - /// The vertical alignment of the text + /// The vertical alignment of the text. vertical_alignment: alignment::Vertical, /// The shaping strategy of the text. shaping: text::Shaping, + /// The viewport of the text. + viewport: Rectangle, }, /// A paragraph primitive Paragraph { @@ -41,15 +43,19 @@ pub enum Primitive { position: Point, /// The color of the paragraph. color: Color, + /// The viewport of the paragraph. + viewport: Rectangle, }, /// An editor primitive Editor { /// The [`editor::Weak`] reference. editor: editor::Weak, - /// The position of the paragraph. + /// The position of the editor. position: Point, - /// The color of the paragraph. + /// The color of the editor. color: Color, + /// The viewport of the editor. + viewport: Rectangle, }, /// A quad primitive Quad { diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index d7613e3617..0d3b11a74f 100644 --- a/graphics/src/renderer.rs +++ b/graphics/src/renderer.rs @@ -164,11 +164,13 @@ where paragraph: &Self::Paragraph, position: Point, color: Color, + viewport: Rectangle, ) { self.primitives.push(Primitive::Paragraph { paragraph: paragraph.downgrade(), position, color, + viewport, }); } @@ -177,11 +179,13 @@ where editor: &Self::Editor, position: Point, color: Color, + viewport: Rectangle, ) { self.primitives.push(Primitive::Editor { editor: editor.downgrade(), position, color, + viewport, }); } @@ -190,6 +194,7 @@ where text: Text<'_, Self::Font>, position: Point, color: Color, + viewport: Rectangle, ) { self.primitives.push(Primitive::Text { content: text.content.to_string(), @@ -201,6 +206,7 @@ where horizontal_alignment: text.horizontal_alignment, vertical_alignment: text.vertical_alignment, shaping: text.shaping, + viewport, }); } } diff --git a/renderer/src/lib.rs b/renderer/src/lib.rs index 1fc4c86ba7..90a7262b89 100644 --- a/renderer/src/lib.rs +++ b/renderer/src/lib.rs @@ -175,11 +175,12 @@ impl text::Renderer for Renderer { paragraph: &Self::Paragraph, position: Point, color: Color, + viewport: Rectangle, ) { delegate!( self, renderer, - renderer.fill_paragraph(paragraph, position, color) + renderer.fill_paragraph(paragraph, position, color, viewport) ); } @@ -188,11 +189,12 @@ impl text::Renderer for Renderer { editor: &Self::Editor, position: Point, color: Color, + viewport: Rectangle, ) { delegate!( self, renderer, - renderer.fill_editor(editor, position, color) + renderer.fill_editor(editor, position, color, viewport) ); } @@ -201,8 +203,13 @@ impl text::Renderer for Renderer { text: Text<'_, Self::Font>, position: Point, color: Color, + viewport: Rectangle, ) { - delegate!(self, renderer, renderer.fill_text(text, position, color)); + delegate!( + self, + renderer, + renderer.fill_text(text, position, color, viewport) + ); } } diff --git a/tiny_skia/src/backend.rs b/tiny_skia/src/backend.rs index f2905b00e7..cc0f72d17e 100644 --- a/tiny_skia/src/backend.rs +++ b/tiny_skia/src/backend.rs @@ -1,6 +1,6 @@ use crate::core::{Background, Color, Gradient, Rectangle, Vector}; use crate::graphics::backend; -use crate::graphics::{Damage, Viewport}; +use crate::graphics::Viewport; use crate::primitive::{self, Primitive}; use std::borrow::Cow; @@ -361,11 +361,9 @@ impl Backend { paragraph, position, color, + viewport, } => { - let physical_bounds = - (Rectangle::new(*position, paragraph.min_bounds) - + translation) - * scale_factor; + let physical_bounds = (*viewport + translation) * scale_factor; if !clip_bounds.intersects(&physical_bounds) { return; @@ -387,10 +385,9 @@ impl Backend { editor, position, color, + viewport, } => { - let physical_bounds = - (Rectangle::new(*position, editor.bounds) + translation) - * scale_factor; + let physical_bounds = (*viewport + translation) * scale_factor; if !clip_bounds.intersects(&physical_bounds) { return; @@ -418,9 +415,9 @@ impl Backend { horizontal_alignment, vertical_alignment, shaping, + viewport, } => { - let physical_bounds = - (primitive.bounds() + translation) * scale_factor; + let physical_bounds = (*viewport + translation) * scale_factor; if !clip_bounds.intersects(&physical_bounds) { return; diff --git a/tiny_skia/src/geometry.rs b/tiny_skia/src/geometry.rs index 1d14aa037a..b73f84a9d6 100644 --- a/tiny_skia/src/geometry.rs +++ b/tiny_skia/src/geometry.rs @@ -109,15 +109,17 @@ impl Frame { Point::new(transformed[0].x, transformed[0].y) }; + let bounds = Rectangle { + x: position.x, + y: position.y, + width: f32::INFINITY, + height: f32::INFINITY, + }; + // TODO: Use vectorial text instead of primitive self.primitives.push(Primitive::Text { content: text.content, - bounds: Rectangle { - x: position.x, - y: position.y, - width: f32::INFINITY, - height: f32::INFINITY, - }, + bounds, color: text.color, size: text.size, line_height: text.line_height, @@ -125,6 +127,7 @@ impl Frame { horizontal_alignment: text.horizontal_alignment, vertical_alignment: text.vertical_alignment, shaping: text.shaping, + viewport: bounds, }); } diff --git a/wgpu/src/geometry.rs b/wgpu/src/geometry.rs index 655362b793..c82b9ffb77 100644 --- a/wgpu/src/geometry.rs +++ b/wgpu/src/geometry.rs @@ -328,15 +328,17 @@ impl Frame { Point::new(transformed.x, transformed.y) }; + let bounds = Rectangle { + x: position.x, + y: position.y, + width: f32::INFINITY, + height: f32::INFINITY, + }; + // TODO: Use vectorial text instead of primitive self.primitives.push(Primitive::Text { content: text.content, - bounds: Rectangle { - x: position.x, - y: position.y, - width: f32::INFINITY, - height: f32::INFINITY, - }, + bounds, color: text.color, size: text.size, line_height: text.line_height, @@ -344,6 +346,7 @@ impl Frame { horizontal_alignment: text.horizontal_alignment, vertical_alignment: text.vertical_alignment, shaping: text.shaping, + viewport: bounds, }); } diff --git a/wgpu/src/layer.rs b/wgpu/src/layer.rs index 98e49f1af7..60da3543d8 100644 --- a/wgpu/src/layer.rs +++ b/wgpu/src/layer.rs @@ -75,6 +75,7 @@ impl<'a> Layer<'a> { horizontal_alignment: alignment::Horizontal::Left, vertical_alignment: alignment::Vertical::Top, shaping: core::text::Shaping::Basic, + viewport: Rectangle::with_size(Size::INFINITY), }; overlay.text.push(Text::Cached(text.clone())); @@ -123,6 +124,7 @@ impl<'a> Layer<'a> { paragraph, position, color, + viewport, } => { let layer = &mut layers[current_layer]; @@ -130,12 +132,14 @@ impl<'a> Layer<'a> { paragraph: paragraph.clone(), position: *position + translation, color: *color, + viewport: *viewport + translation, }); } Primitive::Editor { editor, position, color, + viewport, } => { let layer = &mut layers[current_layer]; @@ -143,6 +147,7 @@ impl<'a> Layer<'a> { editor: editor.clone(), position: *position + translation, color: *color, + viewport: *viewport + translation, }); } Primitive::Text { @@ -155,6 +160,7 @@ impl<'a> Layer<'a> { horizontal_alignment, vertical_alignment, shaping, + viewport, } => { let layer = &mut layers[current_layer]; @@ -168,6 +174,7 @@ impl<'a> Layer<'a> { horizontal_alignment: *horizontal_alignment, vertical_alignment: *vertical_alignment, shaping: *shaping, + viewport: *viewport + translation, })); } Primitive::Quad { diff --git a/wgpu/src/layer/text.rs b/wgpu/src/layer/text.rs index 66417cecbe..c4ea918596 100644 --- a/wgpu/src/layer/text.rs +++ b/wgpu/src/layer/text.rs @@ -13,6 +13,7 @@ pub enum Text<'a> { paragraph: paragraph::Weak, position: Point, color: Color, + viewport: Rectangle, }, /// An editor. #[allow(missing_docs)] @@ -20,6 +21,7 @@ pub enum Text<'a> { editor: editor::Weak, position: Point, color: Color, + viewport: Rectangle, }, /// A cached text. Cached(Cached<'a>), @@ -53,4 +55,7 @@ pub struct Cached<'a> { /// The shaping strategy of the text. pub shaping: text::Shaping, + + /// The viewport of the text. + pub viewport: Rectangle, } diff --git a/wgpu/src/text.rs b/wgpu/src/text.rs index 08a8bea67f..7d73c87ba7 100644 --- a/wgpu/src/text.rs +++ b/wgpu/src/text.rs @@ -120,9 +120,13 @@ impl Pipeline { horizontal_alignment, vertical_alignment, color, + viewport, ) = match section { Text::Paragraph { - position, color, .. + position, + color, + viewport, + .. } => { use crate::core::text::Paragraph as _; @@ -137,10 +141,14 @@ impl Pipeline { paragraph.horizontal_alignment(), paragraph.vertical_alignment(), *color, + *viewport, ) } Text::Editor { - position, color, .. + position, + color, + viewport, + .. } => { use crate::core::text::Editor as _; @@ -155,6 +163,7 @@ impl Pipeline { alignment::Horizontal::Left, alignment::Vertical::Top, *color, + *viewport, ) } Text::Cached(text) => { @@ -173,6 +182,7 @@ impl Pipeline { text.horizontal_alignment, text.vertical_alignment, text.color, + text.viewport, ) } }; @@ -195,13 +205,8 @@ impl Pipeline { alignment::Vertical::Bottom => bounds.y - bounds.height, }; - let section_bounds = Rectangle { - x: left, - y: top, - ..bounds - }; - - let clip_bounds = layer_bounds.intersection(§ion_bounds)?; + let clip_bounds = + layer_bounds.intersection(&(viewport * scale_factor))?; Some(glyphon::TextArea { buffer, diff --git a/widget/src/checkbox.rs b/widget/src/checkbox.rs index d7fdf33961..a0d9559b93 100644 --- a/widget/src/checkbox.rs +++ b/widget/src/checkbox.rs @@ -266,7 +266,7 @@ where style: &renderer::Style, layout: Layout<'_>, cursor: mouse::Cursor, - _viewport: &Rectangle, + viewport: &Rectangle, ) { let is_mouse_over = cursor.is_over(layout.bounds()); @@ -315,6 +315,7 @@ where }, bounds.center(), custom_style.icon_color, + *viewport, ); } } @@ -330,6 +331,7 @@ where crate::text::Appearance { color: custom_style.text_color, }, + viewport, ); } } diff --git a/widget/src/column.rs b/widget/src/column.rs index 42e90ac105..abb522be99 100644 --- a/widget/src/column.rs +++ b/widget/src/column.rs @@ -224,15 +224,17 @@ where cursor: mouse::Cursor, viewport: &Rectangle, ) { - for ((child, state), layout) in self - .children - .iter() - .zip(&tree.children) - .zip(layout.children()) - { - child - .as_widget() - .draw(state, renderer, theme, style, layout, cursor, viewport); + if let Some(viewport) = layout.bounds().intersection(viewport) { + for ((child, state), layout) in self + .children + .iter() + .zip(&tree.children) + .zip(layout.children()) + { + child.as_widget().draw( + state, renderer, theme, style, layout, cursor, &viewport, + ); + } } } diff --git a/widget/src/combo_box.rs b/widget/src/combo_box.rs index 768c240287..31ec27fcb2 100644 --- a/widget/src/combo_box.rs +++ b/widget/src/combo_box.rs @@ -622,7 +622,7 @@ where _style: &renderer::Style, layout: Layout<'_>, cursor: mouse::Cursor, - _viewport: &Rectangle, + viewport: &Rectangle, ) { let is_focused = { let text_input_state = tree.children[0] @@ -645,6 +645,7 @@ where layout, cursor, selection, + viewport, ); } diff --git a/widget/src/container.rs b/widget/src/container.rs index ee7a496583..5dd7705b03 100644 --- a/widget/src/container.rs +++ b/widget/src/container.rs @@ -252,21 +252,23 @@ where ) { let style = theme.appearance(&self.style); - draw_background(renderer, &style, layout.bounds()); - - self.content.as_widget().draw( - tree, - renderer, - theme, - &renderer::Style { - text_color: style - .text_color - .unwrap_or(renderer_style.text_color), - }, - layout.children().next().unwrap(), - cursor, - viewport, - ); + if let Some(viewport) = layout.bounds().intersection(viewport) { + draw_background(renderer, &style, layout.bounds()); + + self.content.as_widget().draw( + tree, + renderer, + theme, + &renderer::Style { + text_color: style + .text_color + .unwrap_or(renderer_style.text_color), + }, + layout.children().next().unwrap(), + cursor, + &viewport, + ); + } } fn overlay<'b>( diff --git a/widget/src/overlay/menu.rs b/widget/src/overlay/menu.rs index 5098fa1714..e45b44ae60 100644 --- a/widget/src/overlay/menu.rs +++ b/widget/src/overlay/menu.rs @@ -544,6 +544,7 @@ where } else { appearance.text_color }, + *viewport, ); } } diff --git a/widget/src/pick_list.rs b/widget/src/pick_list.rs index 00c1a7ff91..022ca8d92a 100644 --- a/widget/src/pick_list.rs +++ b/widget/src/pick_list.rs @@ -235,7 +235,7 @@ where _style: &renderer::Style, layout: Layout<'_>, cursor: mouse::Cursor, - _viewport: &Rectangle, + viewport: &Rectangle, ) { let font = self.font.unwrap_or_else(|| renderer.default_font()); draw( @@ -253,6 +253,7 @@ where &self.handle, &self.style, || tree.state.downcast_ref::>(), + viewport, ); } @@ -631,6 +632,7 @@ pub fn draw<'a, T, Renderer>( handle: &Handle, style: &::Style, state: impl FnOnce() -> &'a State, + viewport: &Rectangle, ) where Renderer: text::Renderer, Renderer::Theme: StyleSheet, @@ -715,6 +717,7 @@ pub fn draw<'a, T, Renderer>( bounds.center_y(), ), style.handle_color, + *viewport, ); } @@ -743,6 +746,7 @@ pub fn draw<'a, T, Renderer>( } else { style.placeholder_color }, + *viewport, ); } } diff --git a/widget/src/radio.rs b/widget/src/radio.rs index 57acc03384..ae2365dddc 100644 --- a/widget/src/radio.rs +++ b/widget/src/radio.rs @@ -291,7 +291,7 @@ where style: &renderer::Style, layout: Layout<'_>, cursor: mouse::Cursor, - _viewport: &Rectangle, + viewport: &Rectangle, ) { let is_mouse_over = cursor.is_over(layout.bounds()); @@ -349,6 +349,7 @@ where crate::text::Appearance { color: custom_style.text_color, }, + viewport, ); } } diff --git a/widget/src/row.rs b/widget/src/row.rs index 7ca90fbbe9..d52b8c43a6 100644 --- a/widget/src/row.rs +++ b/widget/src/row.rs @@ -213,15 +213,17 @@ where cursor: mouse::Cursor, viewport: &Rectangle, ) { - for ((child, state), layout) in self - .children - .iter() - .zip(&tree.children) - .zip(layout.children()) - { - child - .as_widget() - .draw(state, renderer, theme, style, layout, cursor, viewport); + if let Some(viewport) = layout.bounds().intersection(viewport) { + for ((child, state), layout) in self + .children + .iter() + .zip(&tree.children) + .zip(layout.children()) + { + child.as_widget().draw( + state, renderer, theme, style, layout, cursor, &viewport, + ); + } } } diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index 1708a2e5ce..63d488684b 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -429,7 +429,7 @@ where style: &renderer::Style, layout: Layout<'_>, cursor: mouse::Cursor, - _viewport: &Rectangle, + viewport: &Rectangle, ) { let bounds = layout.bounds(); @@ -470,6 +470,7 @@ where bounds.position() + Vector::new(self.padding.left, self.padding.top), style.text_color, + *viewport, ); let translation = Vector::new( diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index 27efe755c9..b56e316724 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -238,6 +238,7 @@ where layout: Layout<'_>, cursor: mouse::Cursor, value: Option<&Value>, + viewport: &Rectangle, ) { draw( renderer, @@ -250,6 +251,7 @@ where self.is_secure, self.icon.as_ref(), &self.style, + viewport, ); } } @@ -362,7 +364,7 @@ where _style: &renderer::Style, layout: Layout<'_>, cursor: mouse::Cursor, - _viewport: &Rectangle, + viewport: &Rectangle, ) { draw( renderer, @@ -375,6 +377,7 @@ where self.is_secure, self.icon.as_ref(), &self.style, + viewport, ); } @@ -1055,6 +1058,7 @@ pub fn draw( is_secure: bool, icon: Option<&Icon>, style: &::Style, + viewport: &Rectangle, ) where Renderer: text::Renderer, Renderer::Theme: StyleSheet, @@ -1096,6 +1100,7 @@ pub fn draw( &state.icon, icon_layout.bounds().center(), appearance.icon_color, + *viewport, ); } @@ -1189,39 +1194,31 @@ pub fn draw( (None, 0.0) }; - let text_width = state.value.min_width(); - - let render = |renderer: &mut Renderer| { - if let Some((cursor, color)) = cursor { - renderer.fill_quad(cursor, color); - } else { - renderer.with_translation(Vector::ZERO, |_| {}); - } - - renderer.fill_paragraph( - if text.is_empty() { - &state.placeholder - } else { - &state.value - }, - Point::new(text_bounds.x, text_bounds.center_y()), - if text.is_empty() { - theme.placeholder_color(style) - } else if is_disabled { - theme.disabled_color(style) - } else { - theme.value_color(style) - }, - ); - }; - - if text_width > text_bounds.width { - renderer.with_layer(text_bounds, |renderer| { - renderer.with_translation(Vector::new(-offset, 0.0), render); + if let Some((cursor, color)) = cursor { + renderer.with_translation(Vector::new(-offset, 0.0), |renderer| { + renderer.fill_quad(cursor, color) }); } else { - render(renderer); + renderer.with_translation(Vector::ZERO, |_| {}); } + + renderer.fill_paragraph( + if text.is_empty() { + &state.placeholder + } else { + &state.value + }, + Point::new(text_bounds.x, text_bounds.center_y()) + - Vector::new(offset, 0.0), + if text.is_empty() { + theme.placeholder_color(style) + } else if is_disabled { + theme.disabled_color(style) + } else { + theme.value_color(style) + }, + text_bounds, + ); } /// Computes the current [`mouse::Interaction`] of the [`TextInput`]. diff --git a/widget/src/toggler.rs b/widget/src/toggler.rs index 476c8330c1..d872308061 100644 --- a/widget/src/toggler.rs +++ b/widget/src/toggler.rs @@ -266,7 +266,7 @@ where style: &renderer::Style, layout: Layout<'_>, cursor: mouse::Cursor, - _viewport: &Rectangle, + viewport: &Rectangle, ) { /// Makes sure that the border radius of the toggler looks good at every size. const BORDER_RADIUS_RATIO: f32 = 32.0 / 13.0; @@ -287,6 +287,7 @@ where label_layout, tree.state.downcast_ref(), crate::text::Appearance::default(), + viewport, ); } From 43a7cc2222750b1cada1663b29278b29d3ea232c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Fri, 1 Dec 2023 16:10:37 +0100 Subject: [PATCH 3/4] Fix `clippy` lint --- widget/src/text_input.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index b56e316724..ab0e241213 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -1196,7 +1196,7 @@ pub fn draw( if let Some((cursor, color)) = cursor { renderer.with_translation(Vector::new(-offset, 0.0), |renderer| { - renderer.fill_quad(cursor, color) + renderer.fill_quad(cursor, color); }); } else { renderer.with_translation(Vector::ZERO, |_| {}); From b526ce4958b28208395276dd4078ffe0d780e1d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sat, 2 Dec 2023 15:53:02 +0100 Subject: [PATCH 4/4] Rename `viewport` to `clip_bounds` --- core/src/renderer/null.rs | 6 +++--- core/src/text.rs | 6 +++--- graphics/src/primitive.rs | 12 ++++++------ graphics/src/renderer.rs | 12 ++++++------ renderer/src/lib.rs | 12 ++++++------ tiny_skia/src/backend.rs | 15 +++++++++------ tiny_skia/src/geometry.rs | 2 +- wgpu/src/geometry.rs | 2 +- wgpu/src/layer.rs | 14 +++++++------- wgpu/src/layer/text.rs | 8 ++++---- wgpu/src/text.rs | 14 +++++++------- 11 files changed, 53 insertions(+), 50 deletions(-) diff --git a/core/src/renderer/null.rs b/core/src/renderer/null.rs index 3ce6a4f570..7accd34e10 100644 --- a/core/src/renderer/null.rs +++ b/core/src/renderer/null.rs @@ -64,7 +64,7 @@ impl text::Renderer for Null { _paragraph: &Self::Paragraph, _position: Point, _color: Color, - _viewport: Rectangle, + _clip_bounds: Rectangle, ) { } @@ -73,7 +73,7 @@ impl text::Renderer for Null { _editor: &Self::Editor, _position: Point, _color: Color, - _viewport: Rectangle, + _clip_bounds: Rectangle, ) { } @@ -82,7 +82,7 @@ impl text::Renderer for Null { _paragraph: Text<'_, Self::Font>, _position: Point, _color: Color, - _viewport: Rectangle, + _clip_bounds: Rectangle, ) { } } diff --git a/core/src/text.rs b/core/src/text.rs index 697fa628b9..edef79c236 100644 --- a/core/src/text.rs +++ b/core/src/text.rs @@ -202,7 +202,7 @@ pub trait Renderer: crate::Renderer { text: &Self::Paragraph, position: Point, color: Color, - viewport: Rectangle, + clip_bounds: Rectangle, ); /// Draws the given [`Editor`] at the given position and with the given @@ -212,7 +212,7 @@ pub trait Renderer: crate::Renderer { editor: &Self::Editor, position: Point, color: Color, - viewport: Rectangle, + clip_bounds: Rectangle, ); /// Draws the given [`Text`] at the given position and with the given @@ -222,6 +222,6 @@ pub trait Renderer: crate::Renderer { text: Text<'_, Self::Font>, position: Point, color: Color, - viewport: Rectangle, + clip_bounds: Rectangle, ); } diff --git a/graphics/src/primitive.rs b/graphics/src/primitive.rs index 837eb77ad9..ed75776c15 100644 --- a/graphics/src/primitive.rs +++ b/graphics/src/primitive.rs @@ -32,8 +32,8 @@ pub enum Primitive { vertical_alignment: alignment::Vertical, /// The shaping strategy of the text. shaping: text::Shaping, - /// The viewport of the text. - viewport: Rectangle, + /// The clip bounds of the text. + clip_bounds: Rectangle, }, /// A paragraph primitive Paragraph { @@ -43,8 +43,8 @@ pub enum Primitive { position: Point, /// The color of the paragraph. color: Color, - /// The viewport of the paragraph. - viewport: Rectangle, + /// The clip bounds of the paragraph. + clip_bounds: Rectangle, }, /// An editor primitive Editor { @@ -54,8 +54,8 @@ pub enum Primitive { position: Point, /// The color of the editor. color: Color, - /// The viewport of the editor. - viewport: Rectangle, + /// The clip bounds of the editor. + clip_bounds: Rectangle, }, /// A quad primitive Quad { diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index 0d3b11a74f..1b0f5c5b3a 100644 --- a/graphics/src/renderer.rs +++ b/graphics/src/renderer.rs @@ -164,13 +164,13 @@ where paragraph: &Self::Paragraph, position: Point, color: Color, - viewport: Rectangle, + clip_bounds: Rectangle, ) { self.primitives.push(Primitive::Paragraph { paragraph: paragraph.downgrade(), position, color, - viewport, + clip_bounds, }); } @@ -179,13 +179,13 @@ where editor: &Self::Editor, position: Point, color: Color, - viewport: Rectangle, + clip_bounds: Rectangle, ) { self.primitives.push(Primitive::Editor { editor: editor.downgrade(), position, color, - viewport, + clip_bounds, }); } @@ -194,7 +194,7 @@ where text: Text<'_, Self::Font>, position: Point, color: Color, - viewport: Rectangle, + clip_bounds: Rectangle, ) { self.primitives.push(Primitive::Text { content: text.content.to_string(), @@ -206,7 +206,7 @@ where horizontal_alignment: text.horizontal_alignment, vertical_alignment: text.vertical_alignment, shaping: text.shaping, - viewport, + clip_bounds, }); } } diff --git a/renderer/src/lib.rs b/renderer/src/lib.rs index 90a7262b89..f2acfa00f5 100644 --- a/renderer/src/lib.rs +++ b/renderer/src/lib.rs @@ -175,12 +175,12 @@ impl text::Renderer for Renderer { paragraph: &Self::Paragraph, position: Point, color: Color, - viewport: Rectangle, + clip_bounds: Rectangle, ) { delegate!( self, renderer, - renderer.fill_paragraph(paragraph, position, color, viewport) + renderer.fill_paragraph(paragraph, position, color, clip_bounds) ); } @@ -189,12 +189,12 @@ impl text::Renderer for Renderer { editor: &Self::Editor, position: Point, color: Color, - viewport: Rectangle, + clip_bounds: Rectangle, ) { delegate!( self, renderer, - renderer.fill_editor(editor, position, color, viewport) + renderer.fill_editor(editor, position, color, clip_bounds) ); } @@ -203,12 +203,12 @@ impl text::Renderer for Renderer { text: Text<'_, Self::Font>, position: Point, color: Color, - viewport: Rectangle, + clip_bounds: Rectangle, ) { delegate!( self, renderer, - renderer.fill_text(text, position, color, viewport) + renderer.fill_text(text, position, color, clip_bounds) ); } } diff --git a/tiny_skia/src/backend.rs b/tiny_skia/src/backend.rs index cc0f72d17e..3e9bd2a5e2 100644 --- a/tiny_skia/src/backend.rs +++ b/tiny_skia/src/backend.rs @@ -361,9 +361,10 @@ impl Backend { paragraph, position, color, - viewport, + clip_bounds: text_clip_bounds, } => { - let physical_bounds = (*viewport + translation) * scale_factor; + let physical_bounds = + (*text_clip_bounds + translation) * scale_factor; if !clip_bounds.intersects(&physical_bounds) { return; @@ -385,9 +386,10 @@ impl Backend { editor, position, color, - viewport, + clip_bounds: text_clip_bounds, } => { - let physical_bounds = (*viewport + translation) * scale_factor; + let physical_bounds = + (*text_clip_bounds + translation) * scale_factor; if !clip_bounds.intersects(&physical_bounds) { return; @@ -415,9 +417,10 @@ impl Backend { horizontal_alignment, vertical_alignment, shaping, - viewport, + clip_bounds: text_clip_bounds, } => { - let physical_bounds = (*viewport + translation) * scale_factor; + let physical_bounds = + (*text_clip_bounds + translation) * scale_factor; if !clip_bounds.intersects(&physical_bounds) { return; diff --git a/tiny_skia/src/geometry.rs b/tiny_skia/src/geometry.rs index b73f84a9d6..5f28b7373a 100644 --- a/tiny_skia/src/geometry.rs +++ b/tiny_skia/src/geometry.rs @@ -127,7 +127,7 @@ impl Frame { horizontal_alignment: text.horizontal_alignment, vertical_alignment: text.vertical_alignment, shaping: text.shaping, - viewport: bounds, + clip_bounds: Rectangle::with_size(Size::INFINITY), }); } diff --git a/wgpu/src/geometry.rs b/wgpu/src/geometry.rs index c82b9ffb77..e0bff67efa 100644 --- a/wgpu/src/geometry.rs +++ b/wgpu/src/geometry.rs @@ -346,7 +346,7 @@ impl Frame { horizontal_alignment: text.horizontal_alignment, vertical_alignment: text.vertical_alignment, shaping: text.shaping, - viewport: bounds, + clip_bounds: Rectangle::with_size(Size::INFINITY), }); } diff --git a/wgpu/src/layer.rs b/wgpu/src/layer.rs index 60da3543d8..557a76335a 100644 --- a/wgpu/src/layer.rs +++ b/wgpu/src/layer.rs @@ -75,7 +75,7 @@ impl<'a> Layer<'a> { horizontal_alignment: alignment::Horizontal::Left, vertical_alignment: alignment::Vertical::Top, shaping: core::text::Shaping::Basic, - viewport: Rectangle::with_size(Size::INFINITY), + clip_bounds: Rectangle::with_size(Size::INFINITY), }; overlay.text.push(Text::Cached(text.clone())); @@ -124,7 +124,7 @@ impl<'a> Layer<'a> { paragraph, position, color, - viewport, + clip_bounds, } => { let layer = &mut layers[current_layer]; @@ -132,14 +132,14 @@ impl<'a> Layer<'a> { paragraph: paragraph.clone(), position: *position + translation, color: *color, - viewport: *viewport + translation, + clip_bounds: *clip_bounds + translation, }); } Primitive::Editor { editor, position, color, - viewport, + clip_bounds, } => { let layer = &mut layers[current_layer]; @@ -147,7 +147,7 @@ impl<'a> Layer<'a> { editor: editor.clone(), position: *position + translation, color: *color, - viewport: *viewport + translation, + clip_bounds: *clip_bounds + translation, }); } Primitive::Text { @@ -160,7 +160,7 @@ impl<'a> Layer<'a> { horizontal_alignment, vertical_alignment, shaping, - viewport, + clip_bounds, } => { let layer = &mut layers[current_layer]; @@ -174,7 +174,7 @@ impl<'a> Layer<'a> { horizontal_alignment: *horizontal_alignment, vertical_alignment: *vertical_alignment, shaping: *shaping, - viewport: *viewport + translation, + clip_bounds: *clip_bounds + translation, })); } Primitive::Quad { diff --git a/wgpu/src/layer/text.rs b/wgpu/src/layer/text.rs index c4ea918596..df2f2875d4 100644 --- a/wgpu/src/layer/text.rs +++ b/wgpu/src/layer/text.rs @@ -13,7 +13,7 @@ pub enum Text<'a> { paragraph: paragraph::Weak, position: Point, color: Color, - viewport: Rectangle, + clip_bounds: Rectangle, }, /// An editor. #[allow(missing_docs)] @@ -21,7 +21,7 @@ pub enum Text<'a> { editor: editor::Weak, position: Point, color: Color, - viewport: Rectangle, + clip_bounds: Rectangle, }, /// A cached text. Cached(Cached<'a>), @@ -56,6 +56,6 @@ pub struct Cached<'a> { /// The shaping strategy of the text. pub shaping: text::Shaping, - /// The viewport of the text. - pub viewport: Rectangle, + /// The clip bounds of the text. + pub clip_bounds: Rectangle, } diff --git a/wgpu/src/text.rs b/wgpu/src/text.rs index 7d73c87ba7..888b192486 100644 --- a/wgpu/src/text.rs +++ b/wgpu/src/text.rs @@ -120,12 +120,12 @@ impl Pipeline { horizontal_alignment, vertical_alignment, color, - viewport, + clip_bounds, ) = match section { Text::Paragraph { position, color, - viewport, + clip_bounds, .. } => { use crate::core::text::Paragraph as _; @@ -141,13 +141,13 @@ impl Pipeline { paragraph.horizontal_alignment(), paragraph.vertical_alignment(), *color, - *viewport, + *clip_bounds, ) } Text::Editor { position, color, - viewport, + clip_bounds, .. } => { use crate::core::text::Editor as _; @@ -163,7 +163,7 @@ impl Pipeline { alignment::Horizontal::Left, alignment::Vertical::Top, *color, - *viewport, + *clip_bounds, ) } Text::Cached(text) => { @@ -182,7 +182,7 @@ impl Pipeline { text.horizontal_alignment, text.vertical_alignment, text.color, - text.viewport, + text.clip_bounds, ) } }; @@ -206,7 +206,7 @@ impl Pipeline { }; let clip_bounds = - layer_bounds.intersection(&(viewport * scale_factor))?; + layer_bounds.intersection(&(clip_bounds * scale_factor))?; Some(glyphon::TextArea { buffer,