Skip to content

Commit

Permalink
Update builder.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
rustbasic authored Mar 5, 2024
1 parent ed05458 commit 955476f
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions crates/egui/src/widgets/text_edit/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub struct TextEdit<'t> {
desired_height_rows: usize,
event_filter: EventFilter,
cursor_at_end: bool,
blink: bool,
min_size: Vec2,
align: Align2,
clip_text: bool,
Expand Down Expand Up @@ -132,6 +133,7 @@ impl<'t> TextEdit<'t> {
..Default::default()
},
cursor_at_end: true,
blink: false,
min_size: Vec2::ZERO,
align: Align2::LEFT_TOP,
clip_text: false,
Expand Down Expand Up @@ -305,6 +307,15 @@ impl<'t> TextEdit<'t> {
self
}

/// When `false` (default), the text cursor will not blink.
///
/// When `true`, the text cursor will blink.
#[inline]
pub fn blink(mut self, b: bool) -> Self {
self.blink = b;
self
}

/// When `true` (default), overflowing text will be clipped.
///
/// When `false`, widget width will expand to make all text visible.
Expand Down Expand Up @@ -413,7 +424,7 @@ impl<'t> TextEdit<'t> {
frame_rect,
visuals.rounding,
ui.visuals().extreme_bg_color,
visuals.bg_stroke, // TODO(emilk): we want to show something here, or a text-edit field doesn't "pop".
ui.visuals().widgets.unhovered.bg_stroke, // TODO(emilk): we want to show something here, or a text-edit field doesn't "pop".
)
}
} else {
Expand Down Expand Up @@ -449,6 +460,7 @@ impl<'t> TextEdit<'t> {
desired_height_rows,
event_filter,
cursor_at_end,
blink,
min_size,
align,
clip_text,
Expand Down Expand Up @@ -508,6 +520,7 @@ impl<'t> TextEdit<'t> {
}
});
let mut state = TextEditState::load(ui.ctx(), id).unwrap_or_default();
let save_ccursor_range = state.cursor.char_range();

// On touch screens (e.g. mobile in `eframe` web), should
// dragging select text, or scroll the enclosing [`ScrollArea`] (if any)?
Expand All @@ -528,6 +541,7 @@ impl<'t> TextEdit<'t> {
let mut response = ui.interact(rect, id, sense);
let text_clip_rect = rect;
let painter = ui.painter_at(text_clip_rect.expand(1.0)); // expand to avoid clipping cursor
let i_time = ui.input(|i| i.time);

if interactive {
if let Some(pointer_pos) = ui.ctx().pointer_interact_pos() {
Expand All @@ -548,7 +562,10 @@ impl<'t> TextEdit<'t> {
// preview:
let cursor_rect =
cursor_rect(response.rect.min, &galley, &cursor_at_pointer, row_height);
paint_cursor(&painter, ui.visuals(), cursor_rect);
let is_drawn = paint_cursor(&painter, ui.visuals(), cursor_rect, i_time, false);
if is_drawn {
ui.ctx().request_repaint();
}
}

let is_being_dragged = ui.ctx().is_being_dragged(response.id);
Expand Down Expand Up @@ -680,7 +697,17 @@ impl<'t> TextEdit<'t> {
}

if text.is_mutable() {
paint_cursor(&painter, ui.visuals(), primary_cursor_rect);
let is_blink = blink && (save_ccursor_range == state.cursor.char_range());
let is_drawn = paint_cursor(
&painter,
ui.visuals(),
primary_cursor_rect,
i_time,
is_blink,
);
if is_drawn {
ui.ctx().request_repaint();
}

if interactive {
// For IME, so only set it when text is editable and visible!
Expand Down Expand Up @@ -940,19 +967,24 @@ fn events(
if !text_mark.is_empty() {
text.insert_text_at(&mut ccursor, text_mark, char_limit);
}
state.ime_cursor_range = cursor_range;
Some(CCursorRange::two(start_cursor, ccursor))
} else {
None
}
}

Event::CompositionEnd(prediction) => {
// CompositionEnd only characters may be typed into TextEdit without trigger CompositionStart first, so do not check `state.has_ime = true` in the following statement.
// CompositionEnd only characters may be typed into TextEdit without trigger CompositionStart first,
// so do not check `state.has_ime = true` in the following statement.
if prediction != "\n" && prediction != "\r" {
state.has_ime = false;
let mut ccursor = text.delete_selected(&cursor_range);
if !prediction.is_empty() {
let mut ccursor;
if !prediction.is_empty() && cursor_range == state.ime_cursor_range {
ccursor = text.delete_selected(&cursor_range);
text.insert_text_at(&mut ccursor, prediction, char_limit);
} else {
ccursor = cursor_range.primary.ccursor;
}
Some(CCursorRange::one(ccursor))
} else {
Expand Down

0 comments on commit 955476f

Please sign in to comment.