From 470266f54069a1c9b6147026d018b437b6457f7b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 5 Nov 2019 03:16:46 +0100 Subject: Add horizontal offset to `Primitive::Clip` --- wgpu/src/primitive.rs | 4 ++-- wgpu/src/renderer.rs | 28 +++++++++++++++++----------- wgpu/src/renderer/scrollable.rs | 4 ++-- wgpu/src/renderer/text_input.rs | 4 ++-- 4 files changed, 23 insertions(+), 17 deletions(-) (limited to 'wgpu') diff --git a/wgpu/src/primitive.rs b/wgpu/src/primitive.rs index 354b0851..8e40e3a6 100644 --- a/wgpu/src/primitive.rs +++ b/wgpu/src/primitive.rs @@ -1,4 +1,4 @@ -use iced_native::{text, Background, Color, Rectangle}; +use iced_native::{text, Background, Color, Rectangle, Vector}; #[derive(Debug, Clone)] pub enum Primitive { @@ -25,7 +25,7 @@ pub enum Primitive { }, Clip { bounds: Rectangle, - offset: u32, + offset: Vector, content: Box, }, } diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index fbc39327..ca4cbb58 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -1,7 +1,7 @@ use crate::{quad, Image, Primitive, Quad, Transformation}; use iced_native::{ renderer::Debugger, renderer::Windowed, Background, Color, Layout, - MouseCursor, Point, Rectangle, Widget, + MouseCursor, Point, Rectangle, Vector, Widget, }; use raw_window_handle::HasRawWindowHandle; @@ -44,17 +44,17 @@ pub struct Target { pub struct Layer<'a> { bounds: Rectangle, - y_offset: u32, + offset: Vector, quads: Vec, images: Vec, text: Vec>, } impl<'a> Layer<'a> { - pub fn new(bounds: Rectangle, y_offset: u32) -> Self { + pub fn new(bounds: Rectangle, offset: Vector) -> Self { Self { bounds, - y_offset, + offset, quads: Vec::new(), images: Vec::new(), text: Vec::new(), @@ -157,7 +157,7 @@ impl Renderer { width: u32::from(target.width), height: u32::from(target.height), }, - 0, + Vector::new(0, 0), )); self.draw_primitive(primitive, &mut layers); @@ -257,7 +257,10 @@ impl Renderer { border_radius, } => { layer.quads.push(Quad { - position: [bounds.x, bounds.y - layer.y_offset as f32], + position: [ + bounds.x - layer.offset.x as f32, + bounds.y - layer.offset.y as f32, + ], scale: [bounds.width, bounds.height], color: match background { Background::Color(color) => color.into_linear(), @@ -279,15 +282,15 @@ impl Renderer { } => { let clip_layer = Layer::new( Rectangle { - x: bounds.x as u32, - y: bounds.y as u32 - layer.y_offset, + x: bounds.x as u32 - layer.offset.x, + y: bounds.y as u32 - layer.offset.y, width: bounds.width as u32, height: bounds.height as u32, }, - layer.y_offset + offset, + layer.offset + *offset, ); - let new_layer = Layer::new(layer.bounds, layer.y_offset); + let new_layer = Layer::new(layer.bounds, layer.offset); layers.push(clip_layer); @@ -307,7 +310,10 @@ impl Renderer { target: &wgpu::TextureView, ) { let translated = transformation - * Transformation::translate(0.0, -(layer.y_offset as f32)); + * Transformation::translate( + -(layer.offset.x as f32), + -(layer.offset.y as f32), + ); if layer.quads.len() > 0 { self.quad_pipeline.draw( diff --git a/wgpu/src/renderer/scrollable.rs b/wgpu/src/renderer/scrollable.rs index 72d77cc8..360759a5 100644 --- a/wgpu/src/renderer/scrollable.rs +++ b/wgpu/src/renderer/scrollable.rs @@ -1,7 +1,7 @@ use crate::{Primitive, Renderer}; use iced_native::{ scrollable, Background, Color, Layout, MouseCursor, Point, Rectangle, - Scrollable, Widget, + Scrollable, Vector, Widget, }; const SCROLLBAR_WIDTH: u16 = 10; @@ -58,7 +58,7 @@ impl scrollable::Renderer for Renderer { let clip = Primitive::Clip { bounds, - offset, + offset: Vector::new(0, offset), content: Box::new(content), }; diff --git a/wgpu/src/renderer/text_input.rs b/wgpu/src/renderer/text_input.rs index deb8eae7..95ea964e 100644 --- a/wgpu/src/renderer/text_input.rs +++ b/wgpu/src/renderer/text_input.rs @@ -2,7 +2,7 @@ use crate::{Primitive, Renderer}; use iced_native::{ text::HorizontalAlignment, text::VerticalAlignment, text_input, Background, - Color, MouseCursor, Point, Rectangle, TextInput, + Color, MouseCursor, Point, Rectangle, TextInput, Vector, }; use std::f32; @@ -89,7 +89,7 @@ impl text_input::Renderer for Renderer { let content = Primitive::Clip { bounds: text_bounds, - offset: 0, + offset: Vector::new(0, 0), content: Box::new(if text_input.state.is_focused { use wgpu_glyph::{GlyphCruncher, Scale, Section}; -- cgit From a2161586dab6837d8c641b6f93ad476f861d8580 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 5 Nov 2019 03:33:24 +0100 Subject: Implement state-less scrolling in `TextInput` --- wgpu/src/renderer/text_input.rs | 121 +++++++++++++++++++++------------------- 1 file changed, 65 insertions(+), 56 deletions(-) (limited to 'wgpu') diff --git a/wgpu/src/renderer/text_input.rs b/wgpu/src/renderer/text_input.rs index 95ea964e..cff8bf23 100644 --- a/wgpu/src/renderer/text_input.rs +++ b/wgpu/src/renderer/text_input.rs @@ -87,70 +87,79 @@ impl text_input::Renderer for Renderer { vertical_alignment: VerticalAlignment::Center, }; - let content = Primitive::Clip { - bounds: text_bounds, - offset: Vector::new(0, 0), - content: Box::new(if text_input.state.is_focused { - use wgpu_glyph::{GlyphCruncher, Scale, Section}; - - let text_before_cursor = &text_input - .value - .until(text_input.state.cursor_position(&text_input.value)) - .to_string(); - - let mut text_value_width = self - .glyph_brush - .borrow_mut() - .glyph_bounds(Section { - text: text_before_cursor, - bounds: (f32::INFINITY, text_bounds.height), - scale: Scale { x: size, y: size }, - ..Default::default() - }) - .map(|bounds| bounds.width().round()) - .unwrap_or(0.0); - - let spaces_at_the_end = text_before_cursor.len() - - text_before_cursor.trim_end().len(); - - if spaces_at_the_end > 0 { - let space_width = { - let glyph_brush = self.glyph_brush.borrow(); - - // TODO: Select appropriate font - let font = &glyph_brush.fonts()[0]; - - font.glyph(' ') - .scaled(Scale { x: size, y: size }) - .h_metrics() - .advance_width - }; - - text_value_width += spaces_at_the_end as f32 * space_width; - } - - let cursor = Primitive::Quad { - bounds: Rectangle { - x: text_bounds.x + text_value_width, - y: text_bounds.y, - width: 1.0, - height: text_bounds.height, - }, - background: Background::Color(Color::BLACK), - border_radius: 0, + let (contents_primitive, offset) = if text_input.state.is_focused { + use wgpu_glyph::{GlyphCruncher, Scale, Section}; + + let text_before_cursor = &text_input + .value + .until(text_input.state.cursor_position(&text_input.value)) + .to_string(); + + let mut text_value_width = self + .glyph_brush + .borrow_mut() + .glyph_bounds(Section { + text: text_before_cursor, + bounds: (f32::INFINITY, text_bounds.height), + scale: Scale { x: size, y: size }, + ..Default::default() + }) + .map(|bounds| bounds.width().round()) + .unwrap_or(0.0); + + let spaces_at_the_end = + text_before_cursor.len() - text_before_cursor.trim_end().len(); + + if spaces_at_the_end > 0 { + let space_width = { + let glyph_brush = self.glyph_brush.borrow(); + + // TODO: Select appropriate font + let font = &glyph_brush.fonts()[0]; + + font.glyph(' ') + .scaled(Scale { x: size, y: size }) + .h_metrics() + .advance_width }; + text_value_width += spaces_at_the_end as f32 * space_width; + } + + let cursor = Primitive::Quad { + bounds: Rectangle { + x: text_bounds.x + text_value_width, + y: text_bounds.y, + width: 1.0, + height: text_bounds.height, + }, + background: Background::Color(Color::BLACK), + border_radius: 0, + }; + + ( Primitive::Group { primitives: vec![value, cursor], - } - } else { - value - }), + }, + Vector::new( + ((text_value_width + 5.0) - text_bounds.width).max(0.0) + as u32, + 0, + ), + ) + } else { + (value, Vector::new(0, 0)) + }; + + let contents = Primitive::Clip { + bounds: text_bounds, + offset, + content: Box::new(contents_primitive), }; ( Primitive::Group { - primitives: vec![border, input, content], + primitives: vec![border, input, contents], }, if is_mouse_over { MouseCursor::Text -- cgit