From b2344a852e98af734e9d8f7fe3d493f9e82ca80c Mon Sep 17 00:00:00 2001 From: FabianLars Date: Sat, 22 Feb 2020 21:33:45 +0100 Subject: inital patch by Finnerale --- wgpu/src/renderer/widget/text_input.rs | 46 +++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) (limited to 'wgpu/src') diff --git a/wgpu/src/renderer/widget/text_input.rs b/wgpu/src/renderer/widget/text_input.rs index e2a1b3a9..72da85f4 100644 --- a/wgpu/src/renderer/widget/text_input.rs +++ b/wgpu/src/renderer/widget/text_input.rs @@ -46,7 +46,7 @@ impl text_input::Renderer for Renderer { text_bounds, value, size, - state.cursor_position(value), + state.cursor_position(value).position(), font, ); @@ -111,15 +111,55 @@ impl text_input::Renderer for Renderer { }; let (contents_primitive, offset) = if state.is_focused() { + let cursor = state.cursor_position(value); let (text_value_width, offset) = measure_cursor_and_scroll_offset( self, text_bounds, value, size, - state.cursor_position(value), + cursor.position(), font, ); + let selection = match cursor { + text_input::Cursor::Index(_) => Primitive::None, + text_input::Cursor::Selection { .. } => { + let (cursor_left_offset, _) = + measure_cursor_and_scroll_offset( + self, + text_bounds, + value, + size, + cursor.left(), + font, + ); + let (cursor_right_offset, _) = + measure_cursor_and_scroll_offset( + self, + text_bounds, + value, + size, + cursor.right(), + font, + ); + let width = cursor_right_offset - cursor_left_offset; + Primitive::Quad { + bounds: Rectangle { + x: text_bounds.x + cursor_left_offset, + y: text_bounds.y, + width, + height: text_bounds.height, + }, + background: Background::Color( + style_sheet.selection_color(), + ), + border_radius: 0, + border_width: 0, + border_color: Color::TRANSPARENT, + } + } + }; + let cursor = Primitive::Quad { bounds: Rectangle { x: text_bounds.x + text_value_width, @@ -135,7 +175,7 @@ impl text_input::Renderer for Renderer { ( Primitive::Group { - primitives: vec![text_value, cursor], + primitives: vec![selection, text_value, cursor], }, Vector::new(offset as u32, 0), ) -- cgit From 190dcef1553efa181b68caa91b109f83fe289412 Mon Sep 17 00:00:00 2001 From: FabianLars Date: Mon, 24 Feb 2020 04:14:32 +0100 Subject: Text Selection completely rewritten --- wgpu/src/renderer/widget/text_input.rs | 47 +++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 6 deletions(-) (limited to 'wgpu/src') diff --git a/wgpu/src/renderer/widget/text_input.rs b/wgpu/src/renderer/widget/text_input.rs index 72da85f4..4e0274b8 100644 --- a/wgpu/src/renderer/widget/text_input.rs +++ b/wgpu/src/renderer/widget/text_input.rs @@ -46,7 +46,7 @@ impl text_input::Renderer for Renderer { text_bounds, value, size, - state.cursor_position(value).position(), + state.cursor.draw_position(value), font, ); @@ -111,17 +111,16 @@ impl text_input::Renderer for Renderer { }; let (contents_primitive, offset) = if state.is_focused() { - let cursor = state.cursor_position(value); let (text_value_width, offset) = measure_cursor_and_scroll_offset( self, text_bounds, value, size, - cursor.position(), + state.cursor.draw_position(value), font, ); - let selection = match cursor { + /*let selection = match cursor { text_input::Cursor::Index(_) => Primitive::None, text_input::Cursor::Selection { .. } => { let (cursor_left_offset, _) = @@ -130,7 +129,7 @@ impl text_input::Renderer for Renderer { text_bounds, value, size, - cursor.left(), + state.cursor.left(), font, ); let (cursor_right_offset, _) = @@ -139,7 +138,7 @@ impl text_input::Renderer for Renderer { text_bounds, value, size, - cursor.right(), + state.cursor.right(), font, ); let width = cursor_right_offset - cursor_left_offset; @@ -158,6 +157,42 @@ impl text_input::Renderer for Renderer { border_color: Color::TRANSPARENT, } } + };*/ + + let selection = if !state.cursor.is_selection() { + Primitive::None + } else { + let (cursor_left_offset, _) = measure_cursor_and_scroll_offset( + self, + text_bounds, + value, + size, + state.cursor.left(), + font, + ); + let (cursor_right_offset, _) = measure_cursor_and_scroll_offset( + self, + text_bounds, + value, + size, + state.cursor.right(), + font, + ); + let width = cursor_right_offset - cursor_left_offset; + Primitive::Quad { + bounds: Rectangle { + x: text_bounds.x + cursor_left_offset, + y: text_bounds.y, + width, + height: text_bounds.height, + }, + background: Background::Color( + style_sheet.selection_color(), + ), + border_radius: 0, + border_width: 0, + border_color: Color::TRANSPARENT, + } }; let cursor = Primitive::Quad { -- cgit From c6c8cabdaf03f90b1739be828cf140d8ddb92e65 Mon Sep 17 00:00:00 2001 From: FabianLars Date: Mon, 24 Feb 2020 18:03:42 +0100 Subject: moved cursor into own file moved click tracking as a new State struct to input::mouse made cursor field of text_input state private brought back cursor type(Index, Selection) representation with a state enum cleaned out some stuff (but not enough/all) TODO: Documentation (sigh) TODO: Editor struct TODO: some (hopefully) small improvements here and there --- wgpu/src/renderer/widget/text_input.rs | 50 +++++----------------------------- 1 file changed, 7 insertions(+), 43 deletions(-) (limited to 'wgpu/src') diff --git a/wgpu/src/renderer/widget/text_input.rs b/wgpu/src/renderer/widget/text_input.rs index 4e0274b8..fa108d68 100644 --- a/wgpu/src/renderer/widget/text_input.rs +++ b/wgpu/src/renderer/widget/text_input.rs @@ -46,7 +46,7 @@ impl text_input::Renderer for Renderer { text_bounds, value, size, - state.cursor.draw_position(value), + state.cursor().cursor_position(value), font, ); @@ -116,20 +116,20 @@ impl text_input::Renderer for Renderer { text_bounds, value, size, - state.cursor.draw_position(value), + state.cursor().cursor_position(value), font, ); - /*let selection = match cursor { - text_input::Cursor::Index(_) => Primitive::None, - text_input::Cursor::Selection { .. } => { + let selection = match state.cursor().selection_position() { + None => Primitive::None, + Some(_) => { let (cursor_left_offset, _) = measure_cursor_and_scroll_offset( self, text_bounds, value, size, - state.cursor.left(), + state.cursor().left(), font, ); let (cursor_right_offset, _) = @@ -138,7 +138,7 @@ impl text_input::Renderer for Renderer { text_bounds, value, size, - state.cursor.right(), + state.cursor().right(), font, ); let width = cursor_right_offset - cursor_left_offset; @@ -157,42 +157,6 @@ impl text_input::Renderer for Renderer { border_color: Color::TRANSPARENT, } } - };*/ - - let selection = if !state.cursor.is_selection() { - Primitive::None - } else { - let (cursor_left_offset, _) = measure_cursor_and_scroll_offset( - self, - text_bounds, - value, - size, - state.cursor.left(), - font, - ); - let (cursor_right_offset, _) = measure_cursor_and_scroll_offset( - self, - text_bounds, - value, - size, - state.cursor.right(), - font, - ); - let width = cursor_right_offset - cursor_left_offset; - Primitive::Quad { - bounds: Rectangle { - x: text_bounds.x + cursor_left_offset, - y: text_bounds.y, - width, - height: text_bounds.height, - }, - background: Background::Color( - style_sheet.selection_color(), - ), - border_radius: 0, - border_width: 0, - border_color: Color::TRANSPARENT, - } }; let cursor = Primitive::Quad { -- cgit From 6b89dd7db9715dd46738677e13ca9d9bd12f9636 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 24 Mar 2020 20:23:31 +0100 Subject: Improve `text_input::cursor` API --- wgpu/src/renderer/widget/text_input.rs | 121 ++++++++++++++++++++------------- 1 file changed, 75 insertions(+), 46 deletions(-) (limited to 'wgpu/src') diff --git a/wgpu/src/renderer/widget/text_input.rs b/wgpu/src/renderer/widget/text_input.rs index fa108d68..74f30be1 100644 --- a/wgpu/src/renderer/widget/text_input.rs +++ b/wgpu/src/renderer/widget/text_input.rs @@ -1,8 +1,9 @@ use crate::{text_input::StyleSheet, Primitive, Renderer}; use iced_native::{ - text_input, Background, Color, Font, HorizontalAlignment, MouseCursor, - Point, Rectangle, Size, Vector, VerticalAlignment, + text_input::{self, cursor}, + Background, Color, Font, HorizontalAlignment, MouseCursor, Point, + Rectangle, Size, Vector, VerticalAlignment, }; use std::f32; @@ -41,12 +42,19 @@ impl text_input::Renderer for Renderer { font: Font, ) -> f32 { if state.is_focused() { + let cursor = state.cursor(); + + let focus_position = match cursor.state(value) { + cursor::State::Index(i) => i, + cursor::State::Selection { end, .. } => end, + }; + let (_, offset) = measure_cursor_and_scroll_offset( self, text_bounds, value, size, - state.cursor().cursor_position(value), + focus_position, font, ); @@ -111,70 +119,91 @@ impl text_input::Renderer for Renderer { }; let (contents_primitive, offset) = if state.is_focused() { - let (text_value_width, offset) = measure_cursor_and_scroll_offset( - self, - text_bounds, - value, - size, - state.cursor().cursor_position(value), - font, - ); + let cursor = state.cursor(); + + let (cursor_primitive, offset) = match cursor.state(value) { + cursor::State::Index(position) => { + let (text_value_width, offset) = + measure_cursor_and_scroll_offset( + self, + text_bounds, + value, + size, + position, + font, + ); + + ( + 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( + style_sheet.value_color(), + ), + border_radius: 0, + border_width: 0, + border_color: Color::TRANSPARENT, + }, + offset, + ) + } + cursor::State::Selection { start, end } => { + let left = start.min(end); + let right = end.max(start); - let selection = match state.cursor().selection_position() { - None => Primitive::None, - Some(_) => { - let (cursor_left_offset, _) = + let (left_position, left_offset) = measure_cursor_and_scroll_offset( self, text_bounds, value, size, - state.cursor().left(), + left, font, ); - let (cursor_right_offset, _) = + + let (right_position, right_offset) = measure_cursor_and_scroll_offset( self, text_bounds, value, size, - state.cursor().right(), + right, font, ); - let width = cursor_right_offset - cursor_left_offset; - Primitive::Quad { - bounds: Rectangle { - x: text_bounds.x + cursor_left_offset, - y: text_bounds.y, - width, - height: text_bounds.height, + + let width = right_position - left_position; + + ( + Primitive::Quad { + bounds: Rectangle { + x: text_bounds.x + left_position, + y: text_bounds.y, + width, + height: text_bounds.height, + }, + background: Background::Color( + style_sheet.selection_color(), + ), + border_radius: 0, + border_width: 0, + border_color: Color::TRANSPARENT, + }, + if end == right { + right_offset + } else { + left_offset }, - background: Background::Color( - style_sheet.selection_color(), - ), - border_radius: 0, - border_width: 0, - border_color: Color::TRANSPARENT, - } + ) } }; - 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(style_sheet.value_color()), - border_radius: 0, - border_width: 0, - border_color: Color::TRANSPARENT, - }; - ( Primitive::Group { - primitives: vec![selection, text_value, cursor], + primitives: vec![cursor_primitive, text_value], }, Vector::new(offset as u32, 0), ) -- cgit From 30f02345a88fc03a5a71d6563f385ac090063bce Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 25 Mar 2020 13:57:02 +0100 Subject: Implement `Renderer::find_cursor_position` --- wgpu/src/renderer/widget/text_input.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'wgpu/src') diff --git a/wgpu/src/renderer/widget/text_input.rs b/wgpu/src/renderer/widget/text_input.rs index 74f30be1..170ac3c5 100644 --- a/wgpu/src/renderer/widget/text_input.rs +++ b/wgpu/src/renderer/widget/text_input.rs @@ -36,10 +36,10 @@ impl text_input::Renderer for Renderer { fn offset( &self, text_bounds: Rectangle, + font: Font, size: u16, value: &text_input::Value, state: &text_input::State, - font: Font, ) -> f32 { if state.is_focused() { let cursor = state.cursor(); @@ -69,8 +69,8 @@ impl text_input::Renderer for Renderer { bounds: Rectangle, text_bounds: Rectangle, cursor_position: Point, - size: u16, font: Font, + size: u16, placeholder: &str, value: &text_input::Value, state: &text_input::State, -- cgit