Skip to main content

re_ui/theme/
text_edit.rs

1//! How [`egui::TextEdit`] is styled.
2
3use egui::class::HasClasses as _;
4use egui::theme::StyleProvider;
5use egui::widget_style::{AtomLayoutStyle, StyleArgs, TextEditStyle, TextVisuals, WidgetState};
6use egui::{Align2, CornerRadius, Frame, Margin, Stroke, TextStyle, Vec2};
7
8use super::class::StyleArgsExt as _;
9use super::{ReTheme, class};
10use crate::HasDesignTokens as _;
11use crate::button::Size;
12use crate::text_edit::TextEditVariant;
13
14/// The vertical padding of a text edit, which keeps its text off the frame edge.
15///
16/// The height comes from the size class instead; this only decides how tight a field is that has
17/// more rows than its height allows for.
18const PADDING_Y: i8 = 2;
19
20impl StyleProvider<TextEditStyle> for ReTheme {
21    fn style(&mut self, args: &StyleArgs<'_>) -> TextEditStyle {
22        let StyleArgs { classes, style, .. } = args;
23
24        let tokens = style.tokens();
25        let variant = TextEditVariant::from_classes(classes).unwrap_or_default();
26        let size = args.inherited(Size::from_classes).unwrap_or_default();
27        let visuals = variant.visuals(tokens);
28
29        // A focused text edit reads as `Active`; there is no separate pressed look.
30        let (fill, mut stroke) = match args.state {
31            WidgetState::Hovered => (visuals.fill_hovered, visuals.stroke_hovered),
32            WidgetState::Active => (visuals.fill_focused, visuals.stroke_focused),
33            WidgetState::Inactive | WidgetState::Noninteractive => (visuals.fill, visuals.stroke),
34        };
35
36        // Invalid content is outlined in the error color, whatever the state, so the field reads as
37        // wrong whether or not the pointer happens to be over it.
38        if classes.has_class(class::INVALID) {
39            stroke = Stroke::new(1.0, style.visuals.error_fg_color);
40        }
41
42        // A text edit over an immutable buffer has no background, like egui's own text edit style:
43        // callers that show a read-only value draw their own frame around it.
44        let read_only = classes.has_class(egui::TextEdit::CLASS_READ_ONLY.as_str());
45        let fill = if read_only {
46            egui::Color32::TRANSPARENT
47        } else {
48            fill
49        };
50
51        // `egui_commonmark` outlines its read-only code blocks itself, so a second outline here
52        // would show as a double border.
53        if read_only && args.has_class(class::UNCONTROLLED_EXTERNAL_UI) {
54            stroke = Stroke::NONE;
55        }
56
57        TextEditStyle {
58            atom_layout: AtomLayoutStyle {
59                // The same height a button of this size gets, so the two line up on a row.
60                min_size: Vec2::new(0.0, size.height()),
61                gap: style.spacing.icon_spacing,
62                frame: Frame {
63                    fill,
64                    stroke,
65                    corner_radius: CornerRadius::same(tokens.normal_corner_radius()),
66                    // The stroke is painted centered on the frame edge, so half of it eats into
67                    // the padding; compensate like the button does.
68                    inner_margin: Margin::symmetric(size.padding().x as i8, PADDING_Y)
69                        - Margin::same(stroke.width.round() as i8),
70                    ..Default::default()
71                },
72                text_style: TextVisuals::new(style, TextStyle::Body, visuals.text),
73                // Without this the text follows the alignment of the `Ui`, and centers in a
74                // centered layout.
75                align2: Some(Align2::LEFT_TOP),
76                ..Default::default()
77            },
78            hint_text_color: visuals.text_placeholder,
79            prefix_suffix_color: tokens.text_strong,
80        }
81    }
82}