Add `Visuals::text_edit_bg_color` (#7283)

* Closes https://github.com/emilk/egui/issues/7263
This commit is contained in:
Emil Ernerfeldt 2025-07-01 15:13:16 +02:00 committed by GitHub
parent 9e021f78da
commit 737c61867b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 8 deletions

View File

@ -11,6 +11,7 @@ use crate::{
WidgetText,
ecolor::Color32,
emath::{Rangef, Rect, Vec2, pos2, vec2},
reset_button_with,
};
/// How to format numbers in e.g. a [`crate::DragValue`].
@ -952,6 +953,11 @@ pub struct Visuals {
/// that needs to look different from other interactive stuff.
pub extreme_bg_color: Color32,
/// The background color of [`crate::TextEdit`].
///
/// Defaults to [`Self::extreme_bg_color`].
pub text_edit_bg_color: Option<Color32>,
/// Background color behind code-styled monospaced labels.
pub code_bg_color: Color32,
@ -1045,6 +1051,11 @@ impl Visuals {
self.widgets.active.text_color()
}
/// The background color of [`crate::TextEdit`].
pub fn text_edit_bg_color(&self) -> Color32 {
self.text_edit_bg_color.unwrap_or(self.extreme_bg_color)
}
/// Window background color.
#[inline(always)]
pub fn window_fill(&self) -> Color32 {
@ -1357,6 +1368,7 @@ impl Visuals {
hyperlink_color: Color32::from_rgb(90, 170, 255),
faint_bg_color: Color32::from_additive_luminance(5), // visible, but barely so
extreme_bg_color: Color32::from_gray(10), // e.g. TextEdit background
text_edit_bg_color: None, // use `extreme_bg_color` by default
code_bg_color: Color32::from_gray(64),
warn_fg_color: Color32::from_rgb(255, 143, 0), // orange
error_fg_color: Color32::from_rgb(255, 0, 0), // red
@ -1699,11 +1711,11 @@ impl Style {
ui.end_row();
});
ui.collapsing("🔠 Text Styles", |ui| text_styles_ui(ui, text_styles));
ui.collapsing("🔠 Text styles", |ui| text_styles_ui(ui, text_styles));
ui.collapsing("📏 Spacing", |ui| spacing.ui(ui));
ui.collapsing("☝ Interaction", |ui| interaction.ui(ui));
ui.collapsing("🎨 Visuals", |ui| visuals.ui(ui));
ui.collapsing("🔄 Scroll Animation", |ui| scroll_animation.ui(ui));
ui.collapsing("🔄 Scroll animation", |ui| scroll_animation.ui(ui));
#[cfg(debug_assertions)]
ui.collapsing("🐛 Debug", |ui| debug.ui(ui));
@ -2041,13 +2053,14 @@ impl WidgetVisuals {
impl Visuals {
pub fn ui(&mut self, ui: &mut crate::Ui) {
let Self {
dark_mode: _,
dark_mode,
override_text_color: _,
widgets,
selection,
hyperlink_color,
faint_bg_color,
extreme_bg_color,
text_edit_bg_color,
code_bg_color,
warn_fg_color,
error_fg_color,
@ -2085,7 +2098,7 @@ impl Visuals {
disabled_alpha,
} = self;
ui.collapsing("Background Colors", |ui| {
ui.collapsing("Background colors", |ui| {
ui_color(ui, &mut widgets.inactive.weak_bg_fill, "Buttons");
ui_color(ui, window_fill, "Windows");
ui_color(ui, panel_fill, "Panels");
@ -2094,6 +2107,13 @@ impl Visuals {
);
ui_color(ui, extreme_bg_color, "Extreme")
.on_hover_text("Background of plots and paintings");
ui_color(
ui,
text_edit_bg_color.get_or_insert(*extreme_bg_color),
"TextEdit",
)
.on_hover_text("Background of TextEdit");
});
ui.collapsing("Text color", |ui| {
@ -2215,7 +2235,19 @@ impl Visuals {
ui.add(Slider::new(disabled_alpha, 0.0..=1.0).text("Disabled element alpha"));
});
ui.vertical_centered(|ui| reset_button(ui, self, "Reset visuals"));
let dark_mode = *dark_mode;
ui.vertical_centered(|ui| {
reset_button_with(
ui,
self,
"Reset visuals",
if dark_mode {
Self::dark()
} else {
Self::light()
},
);
});
}
}

View File

@ -63,7 +63,7 @@ type LayouterFn<'t> = &'t mut dyn FnMut(&Ui, &dyn TextBuffer, f32) -> Arc<Galley
/// See [`TextEdit::show`].
///
/// ## Other
/// The background color of a [`crate::TextEdit`] is [`crate::Visuals::extreme_bg_color`] or can be set with [`crate::TextEdit::background_color`].
/// The background color of a [`crate::TextEdit`] is [`crate::Visuals::text_edit_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,
@ -207,7 +207,7 @@ impl<'t> TextEdit<'t> {
self
}
/// Set the background color of the [`TextEdit`]. The default is [`crate::Visuals::extreme_bg_color`].
/// Set the background color of the [`TextEdit`]. The default is [`crate::Visuals::text_edit_bg_color`].
// TODO(bircni): remove this once #3284 is implemented
#[inline]
pub fn background_color(mut self, color: Color32) -> Self {
@ -428,7 +428,7 @@ impl TextEdit<'_> {
let where_to_put_background = ui.painter().add(Shape::Noop);
let background_color = self
.background_color
.unwrap_or(ui.visuals().extreme_bg_color);
.unwrap_or_else(|| ui.visuals().text_edit_bg_color());
let output = self.show_content(ui);
if frame {