Render TextInput cursor inside the clipping area
This commit is contained in:
parent
51a0e99097
commit
d3cdee1d9b
2 changed files with 88 additions and 86 deletions
|
|
@ -121,6 +121,7 @@ impl State {
|
|||
}
|
||||
}
|
||||
|
||||
// TODO: Use `unicode-segmentation`
|
||||
pub struct Value(Vec<char>);
|
||||
|
||||
impl Value {
|
||||
|
|
|
|||
|
|
@ -57,10 +57,7 @@ impl text_input::Renderer for Renderer {
|
|||
let size = f32::from(text_input.size.unwrap_or(self.default_size()));
|
||||
let text = text_input.value.to_string();
|
||||
|
||||
let value = Primitive::Clip {
|
||||
bounds: text_bounds,
|
||||
offset: 0,
|
||||
content: Box::new(Primitive::Text {
|
||||
let value = Primitive::Text {
|
||||
content: if text.is_empty() {
|
||||
text_input.placeholder.clone()
|
||||
} else {
|
||||
|
|
@ -88,19 +85,17 @@ impl text_input::Renderer for Renderer {
|
|||
size,
|
||||
horizontal_alignment: HorizontalAlignment::Left,
|
||||
vertical_alignment: VerticalAlignment::Center,
|
||||
}),
|
||||
};
|
||||
|
||||
(
|
||||
Primitive::Group {
|
||||
primitives: if text_input.state.is_focused {
|
||||
let content = Primitive::Clip {
|
||||
bounds: text_bounds,
|
||||
offset: 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),
|
||||
)
|
||||
.until(text_input.state.cursor_position(&text_input.value))
|
||||
.to_string();
|
||||
|
||||
let mut text_value_width = self
|
||||
|
|
@ -131,8 +126,7 @@ impl text_input::Renderer for Renderer {
|
|||
.advance_width
|
||||
};
|
||||
|
||||
text_value_width +=
|
||||
spaces_at_the_end as f32 * space_width;
|
||||
text_value_width += spaces_at_the_end as f32 * space_width;
|
||||
}
|
||||
|
||||
let cursor = Primitive::Quad {
|
||||
|
|
@ -146,10 +140,17 @@ impl text_input::Renderer for Renderer {
|
|||
border_radius: 0,
|
||||
};
|
||||
|
||||
vec![border, input, value, cursor]
|
||||
Primitive::Group {
|
||||
primitives: vec![value, cursor],
|
||||
}
|
||||
} else {
|
||||
vec![border, input, value]
|
||||
},
|
||||
value
|
||||
}),
|
||||
};
|
||||
|
||||
(
|
||||
Primitive::Group {
|
||||
primitives: vec![border, input, content],
|
||||
},
|
||||
if is_mouse_over {
|
||||
MouseCursor::Text
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue