Browse Source

Galley: Return Rect when asking for cursor position

pull/43/head
Emil Ernerfeldt 4 years ago
parent
commit
96befb0736
  1. 26
      egui/src/paint/galley.rs
  2. 6
      egui/src/widgets/text_edit.rs

26
egui/src/paint/galley.rs

@ -16,7 +16,7 @@
//! and the start of the second row. //! and the start of the second row.
//! The `prefer_next_row` selects which. //! The `prefer_next_row` selects which.
use crate::math::{vec2, NumExt, Vec2}; use crate::math::{pos2, NumExt, Rect, Vec2};
/// Character cursor /// Character cursor
#[derive(Clone, Copy, Debug, Default)] #[derive(Clone, Copy, Debug, Default)]
@ -224,15 +224,18 @@ impl Galley {
/// ## Physical positions /// ## Physical positions
impl Galley { impl Galley {
pub fn last_pos(&self) -> Vec2 { fn last_pos(&self) -> Rect {
if let Some(last) = self.rows.last() { if let Some(row) = self.rows.last() {
vec2(last.max_x(), last.y_min) let x = row.max_x();
return Rect::from_min_max(pos2(x, row.y_min), pos2(x, row.y_max));
} else { } else {
vec2(0.0, 0.0) // Empty galley // Empty galley
Rect::from_min_max(pos2(0.0, 0.0), pos2(0.0, 0.0))
} }
} }
pub fn pos_from_pcursor(&self, pcursor: PCursor) -> Vec2 { /// Returns a 0-width Rect.
pub fn pos_from_pcursor(&self, pcursor: PCursor) -> Rect {
let mut it = PCursor::default(); let mut it = PCursor::default();
for row in &self.rows { for row in &self.rows {
@ -250,7 +253,8 @@ impl Galley {
&& !row.ends_with_newline && !row.ends_with_newline
&& column >= row.char_count_excluding_newline(); && column >= row.char_count_excluding_newline();
if !select_next_row_instead { if !select_next_row_instead {
return vec2(row.x_offsets[column], row.y_min); let x = row.x_offsets[column];
return Rect::from_min_max(pos2(x, row.y_min), pos2(x, row.y_max));
} }
} }
} }
@ -266,8 +270,8 @@ impl Galley {
self.last_pos() self.last_pos()
} }
pub fn pos_from_cursor(&self, cursor: &Cursor) -> Vec2 { /// Returns a 0-width Rect.
// self.pos_from_rcursor(cursor.rcursor) pub fn pos_from_cursor(&self, cursor: &Cursor) -> Rect {
self.pos_from_pcursor(cursor.pcursor) // The one TextEdit stores self.pos_from_pcursor(cursor.pcursor) // The one TextEdit stores
} }
@ -558,7 +562,7 @@ impl Galley {
} }
} else { } else {
// keep same X coord // keep same X coord
let x = self.pos_from_cursor(cursor).x; let x = self.pos_from_cursor(cursor).center().x;
let column = if x > self.rows[new_row].max_x() { let column = if x > self.rows[new_row].max_x() {
// beyond the end of this row - keep same colum // beyond the end of this row - keep same colum
cursor.rcursor.column cursor.rcursor.column
@ -589,7 +593,7 @@ impl Galley {
} }
} else { } else {
// keep same X coord // keep same X coord
let x = self.pos_from_cursor(cursor).x; let x = self.pos_from_cursor(cursor).center().x;
let column = if x > self.rows[new_row].max_x() { let column = if x > self.rows[new_row].max_x() {
// beyond the end of the next row - keep same column // beyond the end of the next row - keep same column
cursor.rcursor.column cursor.rcursor.column

6
egui/src/widgets/text_edit.rs

@ -288,9 +288,11 @@ impl<'t> Widget for TextEdit<'t> {
if ui.memory().has_kb_focus(id) { if ui.memory().has_kb_focus(id) {
if let Some(pcursor) = state.pcursor { if let Some(pcursor) = state.pcursor {
let cursor_pos = response.rect.min + galley.pos_from_pcursor(pcursor); let cursor_pos = galley
.pos_from_pcursor(pcursor)
.translate(response.rect.min.to_vec2());
painter.line_segment( painter.line_segment(
[cursor_pos, cursor_pos + vec2(0.0, line_spacing)], [cursor_pos.center_top(), cursor_pos.center_bottom()],
(ui.style().visuals.text_cursor_width, color::WHITE), (ui.style().visuals.text_cursor_width, color::WHITE),
); );
} }

Loading…
Cancel
Save