Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow easier setting of background color for TextEdit #5203

Merged
merged 4 commits into from
Oct 2, 2024
Merged
Changes from 1 commit
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
Next Next commit
add background fn to TextEdit
  • Loading branch information
bircni committed Oct 1, 2024
commit 4c1d8e788263bc81866b1f6567d50c809232ef8f
20 changes: 16 additions & 4 deletions crates/egui/src/widgets/text_edit/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -84,6 +84,7 @@ pub struct TextEdit<'t> {
clip_text: bool,
char_limit: usize,
return_key: Option<KeyboardShortcut>,
background_color: Option<Color32>,
}

impl<'t> WidgetWithState for TextEdit<'t> {
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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<FontSelection>) -> Self {
Expand Down Expand Up @@ -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);

Expand All @@ -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".
)
}
Expand Down Expand Up @@ -477,6 +488,7 @@ impl<'t> TextEdit<'t> {
clip_text,
char_limit,
return_key,
background_color: _,
} = self;

let text_color = text_color
Expand Down