Skip to content

Commit 0f2b427

Browse files
authored
Allow easier setting of background color for TextEdit (emilk#5203)
* Closes <emilk#5183> * [x] I have followed the instructions in the PR template
1 parent 1406e87 commit 0f2b427

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

crates/egui/src/widgets/text_edit/builder.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ use super::{TextEditOutput, TextEditState};
5959
/// See [`TextEdit::show`].
6060
///
6161
/// ## Other
62-
/// The background color of a [`crate::TextEdit`] is [`crate::Visuals::extreme_bg_color`].
62+
/// The background color of a [`crate::TextEdit`] is [`crate::Visuals::extreme_bg_color`] or can be set with [`crate::TextEdit::background_color`].
6363
#[must_use = "You should put this widget in a ui with `ui.add(widget);`"]
6464
pub struct TextEdit<'t> {
6565
text: &'t mut dyn TextBuffer,
@@ -84,6 +84,7 @@ pub struct TextEdit<'t> {
8484
clip_text: bool,
8585
char_limit: usize,
8686
return_key: Option<KeyboardShortcut>,
87+
background_color: Option<Color32>,
8788
}
8889

8990
impl<'t> WidgetWithState for TextEdit<'t> {
@@ -142,6 +143,7 @@ impl<'t> TextEdit<'t> {
142143
clip_text: false,
143144
char_limit: usize::MAX,
144145
return_key: Some(KeyboardShortcut::new(Modifiers::NONE, Key::Enter)),
146+
background_color: None,
145147
}
146148
}
147149

@@ -201,6 +203,14 @@ impl<'t> TextEdit<'t> {
201203
self
202204
}
203205

206+
/// Set the background color of the [`TextEdit`]. The default is [`crate::Visuals::extreme_bg_color`].
207+
// TODO(bircni): remove this once #3284 is implemented
208+
#[inline]
209+
pub fn background_color(mut self, color: Color32) -> Self {
210+
self.background_color = Some(color);
211+
self
212+
}
213+
204214
/// Set a specific style for the hint text.
205215
#[inline]
206216
pub fn hint_text_font(mut self, hint_text_font: impl Into<FontSelection>) -> Self {
@@ -409,7 +419,9 @@ impl<'t> TextEdit<'t> {
409419
let is_mutable = self.text.is_mutable();
410420
let frame = self.frame;
411421
let where_to_put_background = ui.painter().add(Shape::Noop);
412-
422+
let background_color = self
423+
.background_color
424+
.unwrap_or(ui.visuals().extreme_bg_color);
413425
let margin = self.margin;
414426
let mut output = self.show_content(ui);
415427

@@ -427,14 +439,14 @@ impl<'t> TextEdit<'t> {
427439
epaint::RectShape::new(
428440
frame_rect,
429441
visuals.rounding,
430-
ui.visuals().extreme_bg_color,
442+
background_color,
431443
ui.visuals().selection.stroke,
432444
)
433445
} else {
434446
epaint::RectShape::new(
435447
frame_rect,
436448
visuals.rounding,
437-
ui.visuals().extreme_bg_color,
449+
background_color,
438450
visuals.bg_stroke, // TODO(emilk): we want to show something here, or a text-edit field doesn't "pop".
439451
)
440452
}
@@ -477,6 +489,7 @@ impl<'t> TextEdit<'t> {
477489
clip_text,
478490
char_limit,
479491
return_key,
492+
background_color: _,
480493
} = self;
481494

482495
let text_color = text_color

examples/hello_world_simple/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Example showing some UI controls like `Label`, `TextEdit`, `Slider`, `Button`.
22

33
```sh
4-
cargo run -p hello_world
4+
cargo run -p hello_world_simple
55
```
66

77
![](screenshot.png)

0 commit comments

Comments
 (0)