Skip to content

Commit

Permalink
add hint_forward
Browse files Browse the repository at this point in the history
  • Loading branch information
Titaniumtown committed Jan 9, 2024
1 parent 301c72b commit 06aaf59
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions crates/egui/src/widgets/text_edit/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use super::{CCursorRange, CursorRange, TextEditOutput, TextEditState};
pub struct TextEdit<'t> {
text: &'t mut dyn TextBuffer,
hint_text: WidgetText,
hint_forward: bool,
id: Option<Id>,
id_source: Option<Id>,
font_selection: FontSelection,
Expand Down Expand Up @@ -105,6 +106,7 @@ impl<'t> TextEdit<'t> {
Self {
text,
hint_text: Default::default(),
hint_forward: false,
id: None,
id_source: None,
font_selection: Default::default(),
Expand Down Expand Up @@ -182,6 +184,12 @@ impl<'t> TextEdit<'t> {
self
}

#[inline]
pub const fn hint_forward(mut self, hint_forward: bool) -> Self {
self.hint_forward = hint_forward;
self
}

/// If true, hide the letters from view and prevent copying from the field.
#[inline]
pub fn password(mut self, password: bool) -> Self {
Expand Down Expand Up @@ -427,6 +435,7 @@ impl<'t> TextEdit<'t> {
let TextEdit {
text,
hint_text,
hint_forward,
id,
id_source,
font_selection,
Expand Down Expand Up @@ -669,14 +678,37 @@ impl<'t> TextEdit<'t> {
if ui.is_rect_visible(rect) {
painter.galley(text_draw_pos, galley.clone(), text_color);

if text.as_str().is_empty() && !hint_text.is_empty() {
let text_is_empty = text.as_str().is_empty();
if (hint_forward | text_is_empty) && !hint_text.is_empty() {
let hint_text_color = ui.visuals().weak_text_color();
let galley = if multiline {
hint_text.into_galley(ui, Some(true), desired_size.x, font_id)

let available_width = if multiline {
desired_size.x
} else {
f32::INFINITY
};

let galley =
hint_text.into_galley(ui, Some(multiline), available_width, font_id.clone());

let rows = galley.rows.len();

let vec_offset = if hint_forward && !text_is_empty {
let gallery_offset_x: f32 = ui.fonts(|fonts| {
prev_text
.chars()
.map(|chr| fonts.glyph_width(&font_id, chr))
.sum()
});

let galley_offset_y: f32 = (rows - 1) as f32 * row_height;

vec2(gallery_offset_x, galley_offset_y)
} else {
hint_text.into_galley(ui, Some(false), f32::INFINITY, font_id)
Vec2::ZERO
};
painter.galley(response.rect.min, galley, hint_text_color);

painter.galley(response.rect.min + vec_offset, galley, hint_text_color);
}

if ui.memory(|mem| mem.has_focus(id)) {
Expand Down

0 comments on commit 06aaf59

Please sign in to comment.