From 4c1d8e788263bc81866b1f6567d50c809232ef8f Mon Sep 17 00:00:00 2001 From: Nicolas Date: Tue, 1 Oct 2024 18:58:06 +0200 Subject: [PATCH 1/4] add background fn to TextEdit --- crates/egui/src/widgets/text_edit/builder.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/crates/egui/src/widgets/text_edit/builder.rs b/crates/egui/src/widgets/text_edit/builder.rs index 7bd4e8db8e55..c2a643e2f1d4 100644 --- a/crates/egui/src/widgets/text_edit/builder.rs +++ b/crates/egui/src/widgets/text_edit/builder.rs @@ -59,7 +59,7 @@ use super::{TextEditOutput, TextEditState}; /// See [`TextEdit::show`]. /// /// ## 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);`"] pub struct TextEdit<'t> { text: &'t mut dyn TextBuffer, @@ -84,6 +84,7 @@ pub struct TextEdit<'t> { clip_text: bool, char_limit: usize, return_key: Option, + background_color: Option, } impl<'t> WidgetWithState for TextEdit<'t> { @@ -142,6 +143,7 @@ impl<'t> TextEdit<'t> { clip_text: false, char_limit: usize::MAX, return_key: Some(KeyboardShortcut::new(Modifiers::NONE, Key::Enter)), + background_color: None, } } @@ -201,6 +203,13 @@ impl<'t> TextEdit<'t> { self } + /// Set the background color of the [`TextEdit`]. + #[inline] + pub fn background_color(mut self, color: Color32) -> Self { + self.background_color = Some(color); + self + } + /// Set a specific style for the hint text. #[inline] pub fn hint_text_font(mut self, hint_text_font: impl Into) -> Self { @@ -409,7 +418,9 @@ impl<'t> TextEdit<'t> { let is_mutable = self.text.is_mutable(); let frame = self.frame; 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 mut output = self.show_content(ui); @@ -427,14 +438,14 @@ impl<'t> TextEdit<'t> { epaint::RectShape::new( frame_rect, visuals.rounding, - ui.visuals().extreme_bg_color, + background_color, ui.visuals().selection.stroke, ) } else { epaint::RectShape::new( frame_rect, 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". ) } @@ -477,6 +488,7 @@ impl<'t> TextEdit<'t> { clip_text, char_limit, return_key, + background_color: _, } = self; let text_color = text_color From 16e4e047fff704ec95e22814b89576f2449a9902 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Tue, 1 Oct 2024 18:58:16 +0200 Subject: [PATCH 2/4] fix README in example --- examples/hello_world_simple/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/hello_world_simple/README.md b/examples/hello_world_simple/README.md index 08c58a7d229f..12aee1fada02 100644 --- a/examples/hello_world_simple/README.md +++ b/examples/hello_world_simple/README.md @@ -1,7 +1,7 @@ Example showing some UI controls like `Label`, `TextEdit`, `Slider`, `Button`. ```sh -cargo run -p hello_world +cargo run -p hello_world_simple ``` ![](screenshot.png) From 4f95bc1320a5607f589cbb22a22c781b57768d5d Mon Sep 17 00:00:00 2001 From: Nicolas Date: Wed, 2 Oct 2024 19:14:18 +0200 Subject: [PATCH 3/4] fixes --- crates/egui/src/widgets/text_edit/builder.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/egui/src/widgets/text_edit/builder.rs b/crates/egui/src/widgets/text_edit/builder.rs index c2a643e2f1d4..4dbbe8b0c8f0 100644 --- a/crates/egui/src/widgets/text_edit/builder.rs +++ b/crates/egui/src/widgets/text_edit/builder.rs @@ -203,7 +203,8 @@ impl<'t> TextEdit<'t> { self } - /// Set the background color of the [`TextEdit`]. + /// Set the background color of the [`TextEdit`]. The default is [`crate::Visuals::extreme_bg_color`]. + // TODO: remove this once #3284 is implemented #[inline] pub fn background_color(mut self, color: Color32) -> Self { self.background_color = Some(color); From 6fdbd688fab83d1a9e1685550dcb0c61f349c34a Mon Sep 17 00:00:00 2001 From: Nicolas Date: Wed, 2 Oct 2024 20:44:42 +0200 Subject: [PATCH 4/4] fix lint --- crates/egui/src/widgets/text_edit/builder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/egui/src/widgets/text_edit/builder.rs b/crates/egui/src/widgets/text_edit/builder.rs index 4dbbe8b0c8f0..f4824a381172 100644 --- a/crates/egui/src/widgets/text_edit/builder.rs +++ b/crates/egui/src/widgets/text_edit/builder.rs @@ -204,7 +204,7 @@ impl<'t> TextEdit<'t> { } /// Set the background color of the [`TextEdit`]. The default is [`crate::Visuals::extreme_bg_color`]. - // TODO: remove this once #3284 is implemented + // TODO(bircni): remove this once #3284 is implemented #[inline] pub fn background_color(mut self, color: Color32) -> Self { self.background_color = Some(color);