TextEdit: You must explicitly choose singleline or multiline.
Multiline TextEdit now has a default height of 4 rows. Added `ui.text_edit_singleline` and `ui.text_edit_multiline`.
This commit is contained in:
parent
8a0bc97e8c
commit
0340e2e6de
|
|
@ -9,10 +9,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
|||
### Added ⭐
|
||||
|
||||
* You can now check if a `TextEdit` lost keyboard focus with `response.lost_kb_focus`.
|
||||
* Added `ui.text_edit_singleline` and `ui.text_edit_multiline`.
|
||||
|
||||
### Changed 🔧
|
||||
|
||||
* Pressing enter in a single-line `TextEdit` will now surrender keyboard focus for it
|
||||
* Pressing enter in a single-line `TextEdit` will now surrender keyboard focus for it.
|
||||
* You must now be explicit when creating a `TextEdit` if you want it to be singeline or multiline.
|
||||
|
||||
### Fixed 🐛
|
||||
|
||||
|
|
|
|||
|
|
@ -133,19 +133,14 @@ impl Widgets {
|
|||
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Single line text input:");
|
||||
let response = ui.add(
|
||||
TextEdit::new(&mut self.single_line_text_input)
|
||||
.multiline(false)
|
||||
.id_source("single line"),
|
||||
);
|
||||
|
||||
let response = ui.text_edit_singleline(&mut self.single_line_text_input);
|
||||
if response.lost_kb_focus {
|
||||
// The user pressed enter.
|
||||
}
|
||||
});
|
||||
|
||||
ui.label("Multiline text input:");
|
||||
ui.add(TextEdit::new(&mut self.multiline_text_input).id_source("multiline"));
|
||||
ui.text_edit_multiline(&mut self.multiline_text_input);
|
||||
|
||||
ui.separator();
|
||||
super::toggle_switch::demo(ui, &mut self.toggle_switch);
|
||||
|
|
|
|||
|
|
@ -508,8 +508,19 @@ impl Ui {
|
|||
self.add(Hyperlink::new(url))
|
||||
}
|
||||
|
||||
#[deprecated = "Use `text_edit_singleline` or `text_edit_multiline`"]
|
||||
pub fn text_edit(&mut self, text: &mut String) -> Response {
|
||||
self.add(TextEdit::new(text))
|
||||
self.text_edit_multiline(text)
|
||||
}
|
||||
|
||||
/// Now newlines (`\n`) allowed. Pressing enter key will result in the `TextEdit` loosing focus (`response.lost_kb_focus`).
|
||||
pub fn text_edit_singleline(&mut self, text: &mut String) -> Response {
|
||||
self.add(TextEdit::singleline(text))
|
||||
}
|
||||
|
||||
/// A `TextEdit` for multiple lines. Pressing enter key will create a new line.
|
||||
pub fn text_edit_multiline(&mut self, text: &mut String) -> Response {
|
||||
self.add(TextEdit::multiline(text))
|
||||
}
|
||||
|
||||
/// Shortcut for `add(Button::new(text))`
|
||||
|
|
|
|||
|
|
@ -128,9 +128,8 @@ impl<'a> Widget for DragValue<'a> {
|
|||
.take()
|
||||
.unwrap_or_else(|| value_text);
|
||||
let response = ui.add(
|
||||
TextEdit::new(&mut value_text)
|
||||
TextEdit::singleline(&mut value_text)
|
||||
.id(kb_edit_id)
|
||||
.multiline(false)
|
||||
.desired_width(button_width)
|
||||
.text_style(TextStyle::Monospace),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -309,9 +309,8 @@ impl<'a> Slider<'a> {
|
|||
.take()
|
||||
.unwrap_or_else(|| value_text);
|
||||
ui.add(
|
||||
TextEdit::new(&mut value_text)
|
||||
TextEdit::singleline(&mut value_text)
|
||||
.id(kb_edit_id)
|
||||
.multiline(false)
|
||||
.desired_width(button_width)
|
||||
.text_color_opt(self.text_color)
|
||||
.text_style(TextStyle::Monospace),
|
||||
|
|
|
|||
|
|
@ -30,10 +30,32 @@ pub struct TextEdit<'t> {
|
|||
multiline: bool,
|
||||
enabled: bool,
|
||||
desired_width: Option<f32>,
|
||||
desired_height_rows: usize,
|
||||
}
|
||||
|
||||
impl<'t> TextEdit<'t> {
|
||||
#[deprecated = "Use `TextEdit::singleline` or `TextEdit::multiline` (or the helper `ui.text_edit_singleline`, `ui.text_edit_multiline`) instead"]
|
||||
pub fn new(text: &'t mut String) -> Self {
|
||||
Self::multiline(text)
|
||||
}
|
||||
|
||||
/// Now newlines (`\n`) allowed. Pressing enter key will result in the `TextEdit` loosing focus (`response.lost_kb_focus`).
|
||||
pub fn singleline(text: &'t mut String) -> Self {
|
||||
TextEdit {
|
||||
text,
|
||||
id: None,
|
||||
id_source: None,
|
||||
text_style: None,
|
||||
text_color: None,
|
||||
multiline: false,
|
||||
enabled: true,
|
||||
desired_width: None,
|
||||
desired_height_rows: 1,
|
||||
}
|
||||
}
|
||||
|
||||
/// A `TextEdit` for multiple lines. Pressing enter key will create a new line.
|
||||
pub fn multiline(text: &'t mut String) -> Self {
|
||||
TextEdit {
|
||||
text,
|
||||
id: None,
|
||||
|
|
@ -43,6 +65,7 @@ impl<'t> TextEdit<'t> {
|
|||
multiline: true,
|
||||
enabled: true,
|
||||
desired_width: None,
|
||||
desired_height_rows: 4,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -71,11 +94,6 @@ impl<'t> TextEdit<'t> {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn multiline(mut self, multiline: bool) -> Self {
|
||||
self.multiline = multiline;
|
||||
self
|
||||
}
|
||||
|
||||
/// Default is `true`. If set to `false` then you cannot edit the text.
|
||||
pub fn enabled(mut self, enabled: bool) -> Self {
|
||||
self.enabled = enabled;
|
||||
|
|
@ -87,6 +105,14 @@ impl<'t> TextEdit<'t> {
|
|||
self.desired_width = Some(desired_width);
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the number of rows to show by default.
|
||||
/// The default for singleline text is `1`.
|
||||
/// The default for multiline text is `4`.
|
||||
pub fn desired_rows(mut self, desired_width: f32) -> Self {
|
||||
self.desired_width = Some(desired_width);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<'t> Widget for TextEdit<'t> {
|
||||
|
|
@ -100,10 +126,9 @@ impl<'t> Widget for TextEdit<'t> {
|
|||
multiline,
|
||||
enabled,
|
||||
desired_width,
|
||||
desired_height_rows,
|
||||
} = self;
|
||||
|
||||
let desired_width = desired_width.unwrap_or_else(|| ui.style().spacing.text_edit_width);
|
||||
|
||||
let id = id.unwrap_or_else(|| ui.make_persistent_id(id_source));
|
||||
|
||||
let mut state = ui.memory().text_edit.get(&id).cloned().unwrap_or_default();
|
||||
|
|
@ -117,9 +142,12 @@ impl<'t> Widget for TextEdit<'t> {
|
|||
} else {
|
||||
font.layout_single_line(text.clone())
|
||||
};
|
||||
|
||||
let desired_width = desired_width.unwrap_or_else(|| ui.style().spacing.text_edit_width);
|
||||
let desired_height = (desired_height_rows.at_least(1) as f32) * line_spacing;
|
||||
let desired_size = vec2(
|
||||
galley.size.x.max(desired_width.min(available_width)),
|
||||
galley.size.y.max(line_spacing),
|
||||
galley.size.y.max(desired_height),
|
||||
);
|
||||
let rect = ui.allocate_space(desired_size);
|
||||
let sense = if enabled {
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ impl egui::app::App for MyApp {
|
|||
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Your name: ");
|
||||
ui.text_edit(name);
|
||||
ui.text_edit_singleline(name);
|
||||
});
|
||||
|
||||
ui.add(egui::Slider::u32(age, 0..=120).text("age"));
|
||||
|
|
|
|||
Loading…
Reference in New Issue