From 79ccbcc817a301b8a558cd63a87a16db7ed3585c Mon Sep 17 00:00:00 2001 From: rustbasic <127506429+rustbasic@users.noreply.github.com> Date: Wed, 31 Jul 2024 19:47:59 +0900 Subject: [PATCH] Update builder.rs --- crates/egui/src/widgets/text_edit/builder.rs | 55 +++++++++++++------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/crates/egui/src/widgets/text_edit/builder.rs b/crates/egui/src/widgets/text_edit/builder.rs index 86d281256b7c..dc1b765f5957 100644 --- a/crates/egui/src/widgets/text_edit/builder.rs +++ b/crates/egui/src/widgets/text_edit/builder.rs @@ -425,7 +425,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.noninteractive.bg_stroke, // TODO(emilk): we want to show something here, or a text-edit field doesn't "pop". ) } } else { @@ -481,12 +481,23 @@ impl<'t> TextEdit<'t> { const MIN_WIDTH: f32 = 24.0; // Never make a [`TextEdit`] more narrow than this. let available_width = (ui.available_width() - margin.sum().x).at_least(MIN_WIDTH); let desired_width = desired_width.unwrap_or_else(|| ui.spacing().text_edit_width); - let wrap_width = if ui.layout().horizontal_justify() { + + let wrap_mode = ui.style().wrap_mode.unwrap_or(if multiline { + TextWrapMode::Wrap + } else { + TextWrapMode::Extend + }); + let clip_wrap_width = if ui.layout().horizontal_justify() { available_width } else { desired_width.min(available_width) }; - + let wrap_width = if wrap_mode == TextWrapMode::Extend { + f32::INFINITY + } else { + clip_wrap_width + }; + let font_id_clone = font_id.clone(); let mut default_layouter = move |ui: &Ui, text: &str, wrap_width: f32| { let text = mask_if_password(password, text); @@ -503,9 +514,9 @@ impl<'t> TextEdit<'t> { let mut galley = layouter(ui, text.as_str(), wrap_width); let desired_width = if clip_text { - wrap_width // visual clipping with scroll in singleline input. + clip_wrap_width // visual clipping with scroll in singleline input. } else { - galley.size().x.max(wrap_width) + galley.size().x.max(clip_wrap_width) }; let desired_height = (desired_height_rows.at_least(1) as f32) * row_height; let desired_inner_size = vec2(desired_width, galley.size().y.max(desired_height)); @@ -522,6 +533,10 @@ impl<'t> TextEdit<'t> { }); let mut state = TextEditState::load(ui.ctx(), id).unwrap_or_default(); + if !ui.input(|i| i.focused) { + ui.memory_mut(|mem| mem.surrender_focus(id)); + } + // On touch screens (e.g. mobile in `eframe` web), should // dragging select text, or scroll the enclosing [`ScrollArea`] (if any)? // Since currently copying selected text in not supported on `eframe` web, @@ -567,14 +582,19 @@ impl<'t> TextEdit<'t> { text_selection::visuals::paint_cursor_end(&painter, ui.visuals(), cursor_rect); } - let is_being_dragged = ui.ctx().is_being_dragged(response.id); - let did_interact = state.cursor.pointer_interaction( - ui, - &response, - cursor_at_pointer, - &galley, - is_being_dragged, - ); + let mut did_interact = false; + if response.has_focus() { + let is_being_dragged = ui.ctx().is_being_dragged(response.id); + did_interact = state.cursor.pointer_interaction( + ui, + &response, + cursor_at_pointer, + &galley, + is_being_dragged, + ); + } else if response.is_pointer_button_down_on() { + did_interact = true; + } if did_interact { ui.memory_mut(|mem| mem.request_focus(response.id)); @@ -670,7 +690,7 @@ impl<'t> TextEdit<'t> { let galley = if multiline { hint_text.into_galley( ui, - Some(TextWrapMode::Wrap), + Some(wrap_mode), desired_inner_size.x, hint_text_font_id, ) @@ -682,11 +702,7 @@ impl<'t> TextEdit<'t> { hint_text_font_id, ) }; - let galley_pos = align - .align_size_within_rect(galley.size(), rect) - .intersect(rect) - .min; - painter.galley(galley_pos, galley, hint_text_color); + painter.galley(rect.min, galley, hint_text_color); } if ui.memory(|mem| mem.has_focus(id)) { @@ -737,6 +753,7 @@ impl<'t> TextEdit<'t> { ui.ctx().output_mut(|o| { o.ime = Some(crate::output::IMEOutput { + visible: ui.visuals().text_cursor.ime_visible, rect: transform * rect, cursor_rect: transform * primary_cursor_rect, });