Skip to content

Commit

Permalink
Add Style::override_text_style
Browse files Browse the repository at this point in the history
Add Style::override_text_style to easily change the text style
of everything in a `Ui` (or globally).

You can now change `TextStyle` on checkboxes,
radio buttons and `SelectableLabel`.

Closes #406
Closes #407
  • Loading branch information
emilk committed May 20, 2021
1 parent 57981d4 commit a892519
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 17 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ NOTE: [`eframe`](eframe/CHANGELOG.md), [`egui_web`](egui_web/CHANGELOG.md) and [
## Unreleased

### Added ⭐
* Add `Style::override_text_style` to easily change the text style of everything in a `Ui` (or globally).
* You can now change `TextStyle` on checkboxes, radio buttons and `SelectableLabel`.
* Add support for [cint](https://crates.io/crates/cint) under `cint` feature.
* Add features `extra_asserts` and `extra_debug_asserts` to enable additional checks.
* Add an option to overwrite frame of SidePanel and TopPanel.
* Add an option to overwrite frame of `SidePanel` and `TopPanel`.
* `TextEdit` now supports edits on a generic buffer using `TextBuffer`.

## 0.12.0 - 2021-05-10 - Multitouch, user memory, window pivots, and improved plots
Expand Down
9 changes: 7 additions & 2 deletions egui/src/containers/collapsing_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl CollapsingHeader {
/// but if it changes or there are several `CollapsingHeader` with the same title
/// you need to provide a unique id source with [`Self::id_source`].
pub fn new(label: impl ToString) -> Self {
let label = Label::new(label).text_style(TextStyle::Button).wrap(false);
let label = Label::new(label).wrap(false);
let id_source = Id::new(label.text());
Self {
label,
Expand Down Expand Up @@ -202,12 +202,17 @@ impl CollapsingHeader {
"Horizontal collapsing is unimplemented"
);
let Self {
label,
mut label,
default_open,
id_source,
enabled: _,
} = self;

label.text_style = label
.text_style
.or(ui.style().override_text_style)
.or(Some(TextStyle::Button));

// TODO: horizontal layout, with icon and text as labels. Insert background behind using Frame.

let id = ui.make_persistent_id(id_source);
Expand Down
6 changes: 2 additions & 4 deletions egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,11 @@
//!
//! // A `scope` creates a temporary [`Ui`] in which you can change settings:
//! ui.scope(|ui|{
//! // Change text color on subsequent widgets:
//! ui.visuals_mut().override_text_color = Some(egui::Color32::RED);
//!
//! // Turn off text wrapping on subsequent widgets:
//! ui.style_mut().override_text_style = Some(egui::TextStyle::Monospace);
//! ui.style_mut().wrap = Some(false);
//!
//! ui.label("This text will be red, and won't wrap to a new line");
//! ui.label("This text will be red, monospace, and won't wrap to a new line");
//! }); // the temporary settings are reverted here
//! ```
Expand Down
21 changes: 21 additions & 0 deletions egui/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ pub struct Style {
/// Default `TextStyle` for normal text (i.e. for `Label` and `TextEdit`).
pub body_text_style: TextStyle,

/// If set this will change the default [`TextStyle`] for all widgets.
///
/// On most widgets you can also set an explicit text style,
/// which will take precedence over this.
pub override_text_style: Option<TextStyle>,

/// If set, labels buttons wtc will use this to determine whether or not
/// to wrap the text at the right edge of the `Ui` they are in.
/// By default this is `None`.
Expand Down Expand Up @@ -321,6 +327,7 @@ impl Default for Style {
fn default() -> Self {
Self {
body_text_style: TextStyle::Body,
override_text_style: None,
wrap: None,
spacing: Spacing::default(),
interaction: Interaction::default(),
Expand Down Expand Up @@ -502,6 +509,7 @@ impl Style {
pub fn ui(&mut self, ui: &mut crate::Ui) {
let Self {
body_text_style,
override_text_style,
wrap: _,
spacing,
interaction,
Expand All @@ -518,6 +526,19 @@ impl Style {
ui.radio_value(body_text_style, value, format!("{:?}", value));
}
});

crate::ComboBox::from_label("Global text style override")
.selected_text(match override_text_style {
None => "None".to_owned(),
Some(override_text_style) => format!("{:?}", override_text_style),
})
.show_ui(ui, |ui| {
ui.selectable_value(override_text_style, None, "None");
for style in TextStyle::all() {
ui.selectable_value(override_text_style, Some(style), format!("{:?}", style));
}
});

ui.add(
Slider::new(animation_time, 0.0..=1.0)
.text("animation durations")
Expand Down
38 changes: 32 additions & 6 deletions egui/src/widgets/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::*;
pub struct Button {
text: String,
text_color: Option<Color32>,
text_style: TextStyle,
text_style: Option<TextStyle>,
/// None means default for interact
fill: Option<Color32>,
sense: Sense,
Expand All @@ -31,7 +31,7 @@ impl Button {
Self {
text: text.to_string(),
text_color: None,
text_style: TextStyle::Button,
text_style: None,
fill: Default::default(),
sense: Sense::click(),
small: false,
Expand All @@ -52,7 +52,7 @@ impl Button {
}

pub fn text_style(mut self, text_style: TextStyle) -> Self {
self.text_style = text_style;
self.text_style = Some(text_style);
self
}

Expand All @@ -63,7 +63,7 @@ impl Button {

/// Make this a small button, suitable for embedding into text.
pub fn small(mut self) -> Self {
self.text_style = TextStyle::Body;
self.text_style = Some(TextStyle::Body);
self.small = true;
self
}
Expand Down Expand Up @@ -123,6 +123,10 @@ impl Button {
min_size,
} = self;

let text_style = text_style
.or(ui.style().override_text_style)
.unwrap_or(TextStyle::Button);

let mut button_padding = ui.spacing().button_padding;
if small {
button_padding.y = 0.0;
Expand Down Expand Up @@ -209,6 +213,7 @@ pub struct Checkbox<'a> {
checked: &'a mut bool,
text: String,
text_color: Option<Color32>,
text_style: Option<TextStyle>,
}

impl<'a> Checkbox<'a> {
Expand All @@ -218,13 +223,19 @@ impl<'a> Checkbox<'a> {
checked,
text: text.to_string(),
text_color: None,
text_style: None,
}
}

pub fn text_color(mut self, text_color: Color32) -> Self {
self.text_color = Some(text_color);
self
}

pub fn text_style(mut self, text_style: TextStyle) -> Self {
self.text_style = Some(text_style);
self
}
}

impl<'a> Widget for Checkbox<'a> {
Expand All @@ -233,15 +244,19 @@ impl<'a> Widget for Checkbox<'a> {
checked,
text,
text_color,
text_style,
} = self;

let text_style = text_style
.or(ui.style().override_text_style)
.unwrap_or(TextStyle::Button);

let spacing = &ui.spacing();
let icon_width = spacing.icon_width;
let icon_spacing = ui.spacing().icon_spacing;
let button_padding = spacing.button_padding;
let total_extra = button_padding + vec2(icon_width + icon_spacing, 0.0) + button_padding;

let text_style = TextStyle::Button;
let galley = if ui.wrap_text() {
ui.fonts()
.layout_multiline(text_style, text, ui.available_width() - total_extra.x)
Expand Down Expand Up @@ -320,6 +335,7 @@ pub struct RadioButton {
checked: bool,
text: String,
text_color: Option<Color32>,
text_style: Option<TextStyle>,
}

impl RadioButton {
Expand All @@ -329,13 +345,19 @@ impl RadioButton {
checked,
text: text.to_string(),
text_color: None,
text_style: None,
}
}

pub fn text_color(mut self, text_color: Color32) -> Self {
self.text_color = Some(text_color);
self
}

pub fn text_style(mut self, text_style: TextStyle) -> Self {
self.text_style = Some(text_style);
self
}
}

impl Widget for RadioButton {
Expand All @@ -344,14 +366,18 @@ impl Widget for RadioButton {
checked,
text,
text_color,
text_style,
} = self;

let text_style = text_style
.or(ui.style().override_text_style)
.unwrap_or(TextStyle::Button);

let icon_width = ui.spacing().icon_width;
let icon_spacing = ui.spacing().icon_spacing;
let button_padding = ui.spacing().button_padding;
let total_extra = button_padding + vec2(icon_width + icon_spacing, 0.0) + button_padding;

let text_style = TextStyle::Button;
let galley = if ui.wrap_text() {
ui.fonts()
.layout_multiline(text_style, text, ui.available_width() - total_extra.x)
Expand Down
4 changes: 3 additions & 1 deletion egui/src/widgets/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@ impl Label {

/// Read the text style, or get the default for the current style
pub fn text_style_or_default(&self, style: &Style) -> TextStyle {
self.text_style.unwrap_or(style.body_text_style)
self.text_style
.or(style.override_text_style)
.unwrap_or(style.body_text_style)
}

fn should_wrap(&self, ui: &Ui) -> bool {
Expand Down
18 changes: 16 additions & 2 deletions egui/src/widgets/selected_label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::*;
pub struct SelectableLabel {
selected: bool,
text: String,
text_style: Option<TextStyle>,
}

impl SelectableLabel {
Expand All @@ -33,18 +34,31 @@ impl SelectableLabel {
Self {
selected,
text: text.to_string(),
text_style: None,
}
}

pub fn text_style(mut self, text_style: TextStyle) -> Self {
self.text_style = Some(text_style);
self
}
}

impl Widget for SelectableLabel {
fn ui(self, ui: &mut Ui) -> Response {
let Self { selected, text } = self;
let Self {
selected,
text,
text_style,
} = self;

let text_style = text_style
.or(ui.style().override_text_style)
.unwrap_or(TextStyle::Button);

let button_padding = ui.spacing().button_padding;
let total_extra = button_padding + button_padding;

let text_style = TextStyle::Button;
let galley = if ui.wrap_text() {
ui.fonts()
.layout_multiline(text_style, text, ui.available_width() - total_extra.x)
Expand Down
4 changes: 3 additions & 1 deletion egui/src/widgets/text_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,9 @@ impl<'t, S: TextBuffer> TextEdit<'t, S> {
lock_focus,
} = self;

let text_style = text_style.unwrap_or_else(|| ui.style().body_text_style);
let text_style = text_style
.or(ui.style().override_text_style)
.unwrap_or_else(|| ui.style().body_text_style);
let line_spacing = ui.fonts().row_height(text_style);
let available_width = ui.available_width();

Expand Down

0 comments on commit a892519

Please sign in to comment.