diff options
author | 2020-03-25 14:31:25 +0100 | |
---|---|---|
committer | 2020-03-25 14:31:25 +0100 | |
commit | 643fa18cae19fa1418a23b652b6b4b8bf8ef79fc (patch) | |
tree | c40bf003b7702cdc751c4d6b4adfd79c3ff63633 /wgpu | |
parent | fd7d9622e333a0a2cd5c2e8e6cc38cc09d7981e4 (diff) | |
parent | bc10ca501ba012dbd379ade93e27bc012c08c2f1 (diff) | |
download | iced-643fa18cae19fa1418a23b652b6b4b8bf8ef79fc.tar.gz iced-643fa18cae19fa1418a23b652b6b4b8bf8ef79fc.tar.bz2 iced-643fa18cae19fa1418a23b652b6b4b8bf8ef79fc.zip |
Merge pull request #202 from FabianLars/master
Text Selection for text_input widget
Diffstat (limited to 'wgpu')
-rw-r--r-- | wgpu/src/renderer/widget/text_input.rs | 120 |
1 files changed, 94 insertions, 26 deletions
diff --git a/wgpu/src/renderer/widget/text_input.rs b/wgpu/src/renderer/widget/text_input.rs index e2a1b3a9..170ac3c5 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; @@ -35,18 +36,25 @@ 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(); + + 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_position(value), + focus_position, font, ); @@ -61,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, @@ -111,31 +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_position(value), - font, - ); - - 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, + 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 (left_position, left_offset) = + measure_cursor_and_scroll_offset( + self, + text_bounds, + value, + size, + left, + font, + ); + + let (right_position, right_offset) = + measure_cursor_and_scroll_offset( + self, + text_bounds, + value, + size, + right, + font, + ); + + 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 + }, + ) + } }; ( Primitive::Group { - primitives: vec![text_value, cursor], + primitives: vec![cursor_primitive, text_value], }, Vector::new(offset as u32, 0), ) |