1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
use crate::{Primitive, Renderer};
use iced_native::{
text::HorizontalAlignment, text::VerticalAlignment, text_input, Background,
Color, MouseCursor, Point, Rectangle, TextInput, Vector,
};
use std::f32;
impl text_input::Renderer for Renderer {
fn default_size(&self) -> u16 {
// TODO: Make this configurable
20
}
fn draw<Message>(
&mut self,
text_input: &TextInput<Message>,
bounds: Rectangle,
text_bounds: Rectangle,
cursor_position: Point,
) -> Self::Output {
let is_mouse_over = bounds.contains(cursor_position);
let border = Primitive::Quad {
bounds,
background: Background::Color(
if is_mouse_over || text_input.state.is_focused {
[0.5, 0.5, 0.5]
} else {
[0.7, 0.7, 0.7]
}
.into(),
),
border_radius: 5,
};
let input = Primitive::Quad {
bounds: Rectangle {
x: bounds.x + 1.0,
y: bounds.y + 1.0,
width: bounds.width - 2.0,
height: bounds.height - 2.0,
},
background: Background::Color(Color::WHITE),
border_radius: 5,
};
let size = f32::from(text_input.size.unwrap_or(self.default_size()));
let text = text_input.value.to_string();
let value = Primitive::Text {
content: if text.is_empty() {
text_input.placeholder.clone()
} else {
text.clone()
},
color: if text.is_empty() {
[0.7, 0.7, 0.7]
} else {
[0.3, 0.3, 0.3]
}
.into(),
bounds: Rectangle {
width: f32::INFINITY,
..text_bounds
},
size,
horizontal_alignment: HorizontalAlignment::Left,
vertical_alignment: VerticalAlignment::Center,
};
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],
},
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, contents],
},
if is_mouse_over {
MouseCursor::Text
} else {
MouseCursor::OutOfBounds
},
)
}
}
|