summaryrefslogtreecommitdiffstats
path: root/widget/src/text_editor.rs
diff options
context:
space:
mode:
authorLibravatar gigas002 <gigas002@pm.me>2024-05-08 19:16:06 +0900
committerLibravatar gigas002 <gigas002@pm.me>2024-05-08 19:16:06 +0900
commit477887b3870aa5fbdab96c3a06f3b930462d7842 (patch)
tree2b3272bb178f8757169511966589fe6a106733f4 /widget/src/text_editor.rs
parent0ebe0629cef37aee5c48b9409fc36618a3a3e60d (diff)
parente07b42ac96b8d098a883c93afe828a439f479c7b (diff)
downloadiced-477887b3870aa5fbdab96c3a06f3b930462d7842.tar.gz
iced-477887b3870aa5fbdab96c3a06f3b930462d7842.tar.bz2
iced-477887b3870aa5fbdab96c3a06f3b930462d7842.zip
Merge branch 'master' of https://github.com/iced-rs/iced into iced-rs-master
Diffstat (limited to 'widget/src/text_editor.rs')
-rw-r--r--widget/src/text_editor.rs41
1 files changed, 27 insertions, 14 deletions
diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs
index 92cdb251..7c0b98ea 100644
--- a/widget/src/text_editor.rs
+++ b/widget/src/text_editor.rs
@@ -319,7 +319,9 @@ where
}
}
-struct State<Highlighter: text::Highlighter> {
+/// The state of a [`TextEditor`].
+#[derive(Debug)]
+pub struct State<Highlighter: text::Highlighter> {
is_focused: bool,
last_click: Option<mouse::Click>,
drag_click: Option<mouse::click::Kind>,
@@ -329,6 +331,13 @@ struct State<Highlighter: text::Highlighter> {
highlighter_format_address: usize,
}
+impl<Highlighter: text::Highlighter> State<Highlighter> {
+ /// Returns whether the [`TextEditor`] is currently focused or not.
+ pub fn is_focused(&self) -> bool {
+ self.is_focused
+ }
+}
+
impl<'a, Highlighter, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
for TextEditor<'a, Highlighter, Message, Theme, Renderer>
where
@@ -560,23 +569,27 @@ where
if state.is_focused {
match internal.editor.cursor() {
Cursor::Caret(position) => {
- let position = position + translation;
+ let cursor =
+ Rectangle::new(
+ position + translation,
+ Size::new(
+ 1.0,
+ self.line_height
+ .to_absolute(self.text_size.unwrap_or_else(
+ || renderer.default_size(),
+ ))
+ .into(),
+ ),
+ );
- if bounds.contains(position) {
+ if let Some(clipped_cursor) = bounds.intersection(&cursor) {
renderer.fill_quad(
renderer::Quad {
bounds: Rectangle {
- x: position.x.floor(),
- y: position.y,
- width: 1.0,
- height: self
- .line_height
- .to_absolute(
- self.text_size.unwrap_or_else(
- || renderer.default_size(),
- ),
- )
- .into(),
+ x: clipped_cursor.x.floor(),
+ y: clipped_cursor.y,
+ width: clipped_cursor.width,
+ height: clipped_cursor.height,
},
..renderer::Quad::default()
},