Allow easier setting of background color for TextEdit (#5203)
* Closes <https://github.com/emilk/egui/issues/5183> * [x] I have followed the instructions in the PR template
This commit is contained in:
parent
1406e8717c
commit
0f2b427ff4
|
|
@ -59,7 +59,7 @@ use super::{TextEditOutput, TextEditState};
|
||||||
/// See [`TextEdit::show`].
|
/// See [`TextEdit::show`].
|
||||||
///
|
///
|
||||||
/// ## Other
|
/// ## Other
|
||||||
/// The background color of a [`crate::TextEdit`] is [`crate::Visuals::extreme_bg_color`].
|
/// The background color of a [`crate::TextEdit`] is [`crate::Visuals::extreme_bg_color`] or can be set with [`crate::TextEdit::background_color`].
|
||||||
#[must_use = "You should put this widget in a ui with `ui.add(widget);`"]
|
#[must_use = "You should put this widget in a ui with `ui.add(widget);`"]
|
||||||
pub struct TextEdit<'t> {
|
pub struct TextEdit<'t> {
|
||||||
text: &'t mut dyn TextBuffer,
|
text: &'t mut dyn TextBuffer,
|
||||||
|
|
@ -84,6 +84,7 @@ pub struct TextEdit<'t> {
|
||||||
clip_text: bool,
|
clip_text: bool,
|
||||||
char_limit: usize,
|
char_limit: usize,
|
||||||
return_key: Option<KeyboardShortcut>,
|
return_key: Option<KeyboardShortcut>,
|
||||||
|
background_color: Option<Color32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'t> WidgetWithState for TextEdit<'t> {
|
impl<'t> WidgetWithState for TextEdit<'t> {
|
||||||
|
|
@ -142,6 +143,7 @@ impl<'t> TextEdit<'t> {
|
||||||
clip_text: false,
|
clip_text: false,
|
||||||
char_limit: usize::MAX,
|
char_limit: usize::MAX,
|
||||||
return_key: Some(KeyboardShortcut::new(Modifiers::NONE, Key::Enter)),
|
return_key: Some(KeyboardShortcut::new(Modifiers::NONE, Key::Enter)),
|
||||||
|
background_color: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -201,6 +203,14 @@ impl<'t> TextEdit<'t> {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set the background color of the [`TextEdit`]. The default is [`crate::Visuals::extreme_bg_color`].
|
||||||
|
// TODO(bircni): remove this once #3284 is implemented
|
||||||
|
#[inline]
|
||||||
|
pub fn background_color(mut self, color: Color32) -> Self {
|
||||||
|
self.background_color = Some(color);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Set a specific style for the hint text.
|
/// Set a specific style for the hint text.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn hint_text_font(mut self, hint_text_font: impl Into<FontSelection>) -> Self {
|
pub fn hint_text_font(mut self, hint_text_font: impl Into<FontSelection>) -> Self {
|
||||||
|
|
@ -409,7 +419,9 @@ impl<'t> TextEdit<'t> {
|
||||||
let is_mutable = self.text.is_mutable();
|
let is_mutable = self.text.is_mutable();
|
||||||
let frame = self.frame;
|
let frame = self.frame;
|
||||||
let where_to_put_background = ui.painter().add(Shape::Noop);
|
let where_to_put_background = ui.painter().add(Shape::Noop);
|
||||||
|
let background_color = self
|
||||||
|
.background_color
|
||||||
|
.unwrap_or(ui.visuals().extreme_bg_color);
|
||||||
let margin = self.margin;
|
let margin = self.margin;
|
||||||
let mut output = self.show_content(ui);
|
let mut output = self.show_content(ui);
|
||||||
|
|
||||||
|
|
@ -427,14 +439,14 @@ impl<'t> TextEdit<'t> {
|
||||||
epaint::RectShape::new(
|
epaint::RectShape::new(
|
||||||
frame_rect,
|
frame_rect,
|
||||||
visuals.rounding,
|
visuals.rounding,
|
||||||
ui.visuals().extreme_bg_color,
|
background_color,
|
||||||
ui.visuals().selection.stroke,
|
ui.visuals().selection.stroke,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
epaint::RectShape::new(
|
epaint::RectShape::new(
|
||||||
frame_rect,
|
frame_rect,
|
||||||
visuals.rounding,
|
visuals.rounding,
|
||||||
ui.visuals().extreme_bg_color,
|
background_color,
|
||||||
visuals.bg_stroke, // TODO(emilk): we want to show something here, or a text-edit field doesn't "pop".
|
visuals.bg_stroke, // TODO(emilk): we want to show something here, or a text-edit field doesn't "pop".
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
@ -477,6 +489,7 @@ impl<'t> TextEdit<'t> {
|
||||||
clip_text,
|
clip_text,
|
||||||
char_limit,
|
char_limit,
|
||||||
return_key,
|
return_key,
|
||||||
|
background_color: _,
|
||||||
} = self;
|
} = self;
|
||||||
|
|
||||||
let text_color = text_color
|
let text_color = text_color
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
Example showing some UI controls like `Label`, `TextEdit`, `Slider`, `Button`.
|
Example showing some UI controls like `Label`, `TextEdit`, `Slider`, `Button`.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cargo run -p hello_world
|
cargo run -p hello_world_simple
|
||||||
```
|
```
|
||||||
|
|
||||||

|

|
||||||
|
|
|
||||||
Loading…
Reference in New Issue