Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions crates/egui/src/widgets/text_edit/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ impl<'t> TextEdit<'t> {

/// Set to 0.0 to keep as small as possible.
/// Set to [`f32::INFINITY`] to take up all available space (i.e. disable automatic word wrap).
/// Set to a negative value to subtract from the available width (e.g., `-10.0` means `available_width - 10.0`).
#[inline]
pub fn desired_width(mut self, desired_width: f32) -> Self {
self.desired_width = Some(desired_width);
Expand Down Expand Up @@ -508,10 +509,14 @@ impl TextEdit<'_> {
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_width = if ui.layout().horizontal_justify() || desired_width.is_infinite() {
available_width
} else {
} else if desired_width >= 0.0 {
desired_width.min(available_width)
} else {
// Negative desired width: subtract from available width
// (e.g., -10.0 means "available_width - 10.0")
(available_width + desired_width).max(0.0)
};

let font_id_clone = font_id.clone();
Expand Down
17 changes: 16 additions & 1 deletion crates/egui_demo_lib/src/demo/text_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl crate::Demo for TextEditDemo {
fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name())
.open(open)
.resizable(false)
.resizable(true)
.show(ctx, |ui| {
use crate::View as _;
self.ui(ui);
Expand All @@ -45,6 +45,21 @@ impl crate::View for TextEditDemo {
ui.label(".");
});

ui.separator();
ui.label("Singleline text edit that reserves space for a Clear button:");
ui.horizontal(|ui| {
ui.add(egui::TextEdit::singleline(text).desired_width(-50.0))
.on_hover_text("Resize the window to see how the TextEdit resizes.");
if ui
.add_sized([40.0, 20.0], egui::Button::new("Clear"))
.clicked()
{
text.clear();
}
});

ui.separator();
ui.label("Multiline text edit with hint text:");
let output = egui::TextEdit::multiline(text)
.hint_text("Type something!")
.show(ui);
Expand Down
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/tests/snapshots/demos/TextEdit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading