Add ui.visuals() as short for ui.style().visuals

This commit is contained in:
Emil Ernerfeldt 2021-02-03 00:20:39 +01:00
parent 0f33bc7c34
commit ca886ea998
17 changed files with 64 additions and 56 deletions

View file

@ -16,7 +16,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
* `egui::popup::popup_below_widget`: show a popup area below another widget. * `egui::popup::popup_below_widget`: show a popup area below another widget.
* Add `Slider::clamp_to_range(bool)`: if set, clamp the incoming and outgoing values to the slider range. * Add `Slider::clamp_to_range(bool)`: if set, clamp the incoming and outgoing values to the slider range.
* Text will now wrap at newlines, spaces, dashes, punctuation or in the middle of a words if necessary, in that order of priority. * Text will now wrap at newlines, spaces, dashes, punctuation or in the middle of a words if necessary, in that order of priority.
* Add `Ui::spacing()` and `Ui::spacing_mut()`. * Add: `ui.spacing()`, `ui.spacing_mut()`, `ui.visuals()`, `ui.visuals_mut()`.
### Changed 🔧 ### Changed 🔧

View file

@ -186,7 +186,7 @@ impl Resize {
let corner_response = if self.resizable { let corner_response = if self.resizable {
// Resize-corner: // Resize-corner:
let corner_size = Vec2::splat(ui.style().visuals.resize_corner_size); let corner_size = Vec2::splat(ui.visuals().resize_corner_size);
let corner_rect = let corner_rect =
Rect::from_min_size(position + state.desired_size - corner_size, corner_size); Rect::from_min_size(position + state.desired_size - corner_size, corner_size);
let corner_response = ui.interact(corner_rect, id.with("corner"), Sense::drag()); let corner_response = ui.interact(corner_rect, id.with("corner"), Sense::drag());
@ -219,7 +219,7 @@ impl Resize {
let inner_rect = Rect::from_min_size(position, state.desired_size); let inner_rect = Rect::from_min_size(position, state.desired_size);
let mut content_clip_rect = inner_rect.expand(ui.style().visuals.clip_rect_margin); let mut content_clip_rect = inner_rect.expand(ui.visuals().clip_rect_margin);
// If we pull the resize handle to shrink, we want to TRY to shrink it. // If we pull the resize handle to shrink, we want to TRY to shrink it.
// After laying out the contents, we might be much bigger. // After laying out the contents, we might be much bigger.
@ -227,9 +227,7 @@ impl Resize {
// then we will clip the contents of the region even thought the result gets larger. This is simply ugly! // then we will clip the contents of the region even thought the result gets larger. This is simply ugly!
// So we use the memory of last_content_size to make the clip rect large enough. // So we use the memory of last_content_size to make the clip rect large enough.
content_clip_rect.max = content_clip_rect.max.max( content_clip_rect.max = content_clip_rect.max.max(
inner_rect.min inner_rect.min + state.last_content_size + Vec2::splat(ui.visuals().clip_rect_margin),
+ state.last_content_size
+ Vec2::splat(ui.style().visuals.clip_rect_margin),
); );
content_clip_rect = content_clip_rect.intersect(ui.clip_rect()); // Respect parent region content_clip_rect = content_clip_rect.intersect(ui.clip_rect()); // Respect parent region
@ -287,7 +285,7 @@ impl Resize {
rect, rect,
corner_radius: 3.0, corner_radius: 3.0,
fill: Default::default(), fill: Default::default(),
stroke: ui.style().visuals.widgets.noninteractive.bg_stroke, stroke: ui.visuals().widgets.noninteractive.bg_stroke,
}); });
} }

View file

@ -138,7 +138,7 @@ impl ScrollArea {
), ),
*ui.layout(), *ui.layout(),
); );
let mut content_clip_rect = inner_rect.expand(ui.style().visuals.clip_rect_margin); let mut content_clip_rect = inner_rect.expand(ui.visuals().clip_rect_margin);
content_clip_rect = content_clip_rect.intersect(ui.clip_rect()); content_clip_rect = content_clip_rect.intersect(ui.clip_rect());
content_clip_rect.max.x = ui.clip_rect().max.x - current_scroll_bar_width; // Nice handling of forced resizing beyond the possible content_clip_rect.max.x = ui.clip_rect().max.x - current_scroll_bar_width; // Nice handling of forced resizing beyond the possible
content_ui.set_clip_rect(content_clip_rect); content_ui.set_clip_rect(content_clip_rect);
@ -331,7 +331,7 @@ impl Prepared {
ui.painter().add(paint::Shape::Rect { ui.painter().add(paint::Shape::Rect {
rect: outer_scroll_rect, rect: outer_scroll_rect,
corner_radius, corner_radius,
fill: ui.style().visuals.dark_bg_color, fill: ui.visuals().dark_bg_color,
stroke: Default::default(), stroke: Default::default(),
// fill: visuals.bg_fill, // fill: visuals.bg_fill,
// stroke: visuals.bg_stroke, // stroke: visuals.bg_stroke,

View file

@ -380,7 +380,7 @@ impl<'open> Window<'open> {
} }
fn paint_resize_corner(ui: &mut Ui, outer_rect: Rect, stroke: Stroke) { fn paint_resize_corner(ui: &mut Ui, outer_rect: Rect, stroke: Stroke) {
let corner_size = Vec2::splat(ui.style().visuals.resize_corner_size); let corner_size = Vec2::splat(ui.visuals().resize_corner_size);
let handle_offset = -Vec2::splat(2.0); let handle_offset = -Vec2::splat(2.0);
let corner_rect = let corner_rect =
Rect::from_min_size(outer_rect.max - corner_size + handle_offset, corner_size); Rect::from_min_size(outer_rect.max - corner_size + handle_offset, corner_size);
@ -608,7 +608,7 @@ fn paint_frame_interaction(
) { ) {
use paint::tessellator::path::add_circle_quadrant; use paint::tessellator::path::add_circle_quadrant;
let cr = ui.style().visuals.window_corner_radius; let cr = ui.visuals().window_corner_radius;
let Rect { min, max } = rect; let Rect { min, max } = rect;
let mut points = Vec::new(); let mut points = Vec::new();
@ -739,7 +739,7 @@ impl TitleBar {
// Always have inactive style for the window. // Always have inactive style for the window.
// It is VERY annoying to e.g. change it when moving the window. // It is VERY annoying to e.g. change it when moving the window.
let style = ui.style().visuals.widgets.inactive; let style = ui.visuals().widgets.inactive;
self.title_label = self.title_label.text_color(style.fg_stroke.color); self.title_label = self.title_label.text_color(style.fg_stroke.color);
@ -757,7 +757,7 @@ impl TitleBar {
// let y = lerp(self.rect.bottom()..=content_response.rect.top(), 0.5); // let y = lerp(self.rect.bottom()..=content_response.rect.top(), 0.5);
ui.painter().line_segment( ui.painter().line_segment(
[pos2(left, y), pos2(right, y)], [pos2(left, y), pos2(right, y)],
ui.style().visuals.widgets.noninteractive.bg_stroke, ui.visuals().widgets.noninteractive.bg_stroke,
); );
} }

View file

@ -48,7 +48,7 @@ pub fn item_ui(ui: &mut Ui, item: easy_mark::Item<'_>) {
let rect = rect.expand2(ui.style().spacing.item_spacing * 0.5); let rect = rect.expand2(ui.style().spacing.item_spacing * 0.5);
ui.painter().line_segment( ui.painter().line_segment(
[rect.center_top(), rect.center_bottom()], [rect.center_top(), rect.center_bottom()],
(1.0, ui.style().visuals.weak_text_color()), (1.0, ui.visuals().weak_text_color()),
); );
} }
easy_mark::Item::BulletPoint => { easy_mark::Item::BulletPoint => {
@ -66,7 +66,7 @@ pub fn item_ui(ui: &mut Ui, item: easy_mark::Item<'_>) {
let mut rect = ui.monospace(code).rect; let mut rect = ui.monospace(code).rect;
rect = rect.expand(1.0); // looks better rect = rect.expand(1.0); // looks better
rect.max.x = ui.max_rect_finite().max.x; rect.max.x = ui.max_rect_finite().max.x;
let code_bg_color = ui.style().visuals.code_bg_color; let code_bg_color = ui.visuals().code_bg_color;
ui.painter().set( ui.painter().set(
where_to_put_background, where_to_put_background,
Shape::rect_filled(rect, 1.0, code_bg_color), Shape::rect_filled(rect, 1.0, code_bg_color),
@ -116,7 +116,7 @@ fn bullet_point(ui: &mut Ui, width: f32) -> Response {
ui.painter().circle_filled( ui.painter().circle_filled(
rect.center(), rect.center(),
rect.height() / 8.0, rect.height() / 8.0,
ui.style().visuals.strong_text_color(), ui.visuals().strong_text_color(),
); );
response response
} }
@ -125,7 +125,7 @@ fn numbered_point(ui: &mut Ui, width: f32, number: &str) -> Response {
let row_height = ui.fonts()[TextStyle::Body].row_height(); let row_height = ui.fonts()[TextStyle::Body].row_height();
let (rect, response) = ui.allocate_exact_size(vec2(width, row_height), Sense::hover()); let (rect, response) = ui.allocate_exact_size(vec2(width, row_height), Sense::hover());
let text = format!("{}.", number); let text = format!("{}.", number);
let text_color = ui.style().visuals.strong_text_color(); let text_color = ui.visuals().strong_text_color();
ui.painter().text( ui.painter().text(
rect.right_center(), rect.right_center(),
Align2::RIGHT_CENTER, Align2::RIGHT_CENTER,

View file

@ -79,7 +79,7 @@ fn menu_impl<'c>(
let mut button = Button::new(title); let mut button = Button::new(title);
if bar_state.open_menu == Some(menu_id) { if bar_state.open_menu == Some(menu_id) {
button = button.fill(Some(ui.style().visuals.selection.bg_fill)); button = button.fill(Some(ui.visuals().selection.bg_fill));
} }
let button_response = ui.add(button); let button_response = ui.add(button);

View file

@ -120,6 +120,18 @@ impl Ui {
&mut self.style_mut().spacing &mut self.style_mut().spacing
} }
/// Short for `&self.style().visuals`
/// visuals options for this `Ui` and its children.
pub fn visuals(&self) -> &crate::style::Visuals {
&self.style.visuals
}
/// Mutably borrow internal `visuals`.
/// Changes apply to this `Ui` and its subsequent children.
pub fn visuals_mut(&mut self) -> &mut crate::style::Visuals {
&mut self.style_mut().visuals
}
/// Get a reference to the parent [`CtxRef`]. /// Get a reference to the parent [`CtxRef`].
pub fn ctx(&self) -> &CtxRef { pub fn ctx(&self) -> &CtxRef {
self.painter.ctx() self.painter.ctx()
@ -493,8 +505,8 @@ impl Ui {
let rect = self.allocate_space_impl(desired_size); let rect = self.allocate_space_impl(desired_size);
let debug_expand_width = self.style().visuals.debug_expand_width; let debug_expand_width = self.visuals().debug_expand_width;
let debug_expand_height = self.style().visuals.debug_expand_height; let debug_expand_height = self.visuals().debug_expand_height;
if (debug_expand_width && too_wide) || (debug_expand_height && too_high) { if (debug_expand_width && too_wide) || (debug_expand_height && too_high) {
self.painter self.painter
@ -956,7 +968,7 @@ impl Ui {
let line_end = pos2(line_start.x, line_start.y + size.y - 2.0); let line_end = pos2(line_start.x, line_start.y + size.y - 2.0);
self.painter.line_segment( self.painter.line_segment(
[line_start, line_end], [line_start, line_end],
self.style().visuals.widgets.noninteractive.bg_stroke, self.visuals().widgets.noninteractive.bg_stroke,
); );
let response = self.allocate_response(indent + size, Sense::hover()); let response = self.allocate_response(indent + size, Sense::hover());

View file

@ -126,7 +126,7 @@ impl Widget for Button {
} }
let text_color = text_color let text_color = text_color
.or(ui.style().visuals.override_text_color) .or(ui.visuals().override_text_color)
.unwrap_or_else(|| visuals.text_color()); .unwrap_or_else(|| visuals.text_color());
ui.painter() ui.painter()
.galley(text_cursor, galley, text_style, text_color); .galley(text_cursor, galley, text_style, text_color);
@ -217,12 +217,12 @@ impl<'a> Widget for Checkbox<'a> {
pos2(small_icon_rect.right(), small_icon_rect.top()), pos2(small_icon_rect.right(), small_icon_rect.top()),
], ],
visuals.fg_stroke, visuals.fg_stroke,
// ui.style().visuals.selection.stroke, // too much color // ui.visuals().selection.stroke, // too much color
)); ));
} }
let text_color = text_color let text_color = text_color
.or(ui.style().visuals.override_text_color) .or(ui.visuals().override_text_color)
.unwrap_or_else(|| visuals.text_color()); .unwrap_or_else(|| visuals.text_color());
ui.painter() ui.painter()
.galley(text_cursor, galley, text_style, text_color); .galley(text_cursor, galley, text_style, text_color);
@ -307,13 +307,13 @@ impl Widget for RadioButton {
center: small_icon_rect.center(), center: small_icon_rect.center(),
radius: small_icon_rect.width() / 3.0, radius: small_icon_rect.width() / 3.0,
fill: visuals.fg_stroke.color, // Intentional to use stroke and not fill fill: visuals.fg_stroke.color, // Intentional to use stroke and not fill
// fill: ui.style().visuals.selection.stroke.color, // too much color // fill: ui.visuals().selection.stroke.color, // too much color
stroke: Default::default(), stroke: Default::default(),
}); });
} }
let text_color = text_color let text_color = text_color
.or(ui.style().visuals.override_text_color) .or(ui.visuals().override_text_color)
.unwrap_or_else(|| visuals.text_color()); .unwrap_or_else(|| visuals.text_color());
painter.galley(text_cursor, galley, text_style, text_color); painter.galley(text_cursor, galley, text_style, text_color);
response response
@ -391,7 +391,7 @@ impl Widget for ImageButton {
let visuals = ui.style().interact(&response); let visuals = ui.style().interact(&response);
if selected { if selected {
let selection = ui.style().visuals.selection; let selection = ui.visuals().selection;
ui.painter() ui.painter()
.rect(rect, 0.0, selection.bg_fill, selection.stroke); .rect(rect, 0.0, selection.bg_fill, selection.stroke);
} else if frame { } else if frame {

View file

@ -53,7 +53,7 @@ impl Widget for Hyperlink {
ui.ctx().output().open_url = Some(url.clone()); ui.ctx().output().open_url = Some(url.clone());
} }
let color = ui.style().visuals.hyperlink_color; let color = ui.visuals().hyperlink_color;
let visuals = ui.style().interact(&response); let visuals = ui.style().interact(&response);
if response.hovered() { if response.hovered() {

View file

@ -156,16 +156,16 @@ impl Label {
let text_color = self.text_color.unwrap_or_else(|| { let text_color = self.text_color.unwrap_or_else(|| {
if strong { if strong {
ui.style().visuals.strong_text_color() ui.visuals().strong_text_color()
} else if weak { } else if weak {
ui.style().visuals.weak_text_color() ui.visuals().weak_text_color()
} else { } else {
ui.style().visuals.text_color() ui.visuals().text_color()
} }
}); });
if code { if code {
background_color = ui.style().visuals.code_bg_color; background_color = ui.visuals().code_bg_color;
} }
let mut lines = vec![]; let mut lines = vec![];

View file

@ -45,12 +45,12 @@ impl Widget for SelectableLabel {
if selected || response.hovered() { if selected || response.hovered() {
let rect = rect.expand(visuals.expansion); let rect = rect.expand(visuals.expansion);
let fill = if selected { let fill = if selected {
ui.style().visuals.selection.bg_fill ui.visuals().selection.bg_fill
} else { } else {
Default::default() Default::default()
}; };
let stroke = if selected { let stroke = if selected {
ui.style().visuals.selection.stroke ui.visuals().selection.stroke
} else { } else {
visuals.bg_stroke visuals.bg_stroke
}; };

View file

@ -68,7 +68,7 @@ impl Widget for Separator {
pos2(rect.center().x, rect.bottom()), pos2(rect.center().x, rect.bottom()),
] ]
}; };
let stroke = ui.style().visuals.widgets.noninteractive.bg_stroke; let stroke = ui.visuals().widgets.noninteractive.bg_stroke;
ui.painter().line_segment(points, stroke); ui.painter().line_segment(points, stroke);
response response
} }

View file

@ -304,12 +304,12 @@ impl<'a> Slider<'a> {
rect: rail_rect, rect: rail_rect,
corner_radius: rail_radius, corner_radius: rail_radius,
fill: ui.style().visuals.widgets.inactive.bg_fill, fill: ui.visuals().widgets.inactive.bg_fill,
// fill: visuals.bg_fill, // fill: visuals.bg_fill,
// fill: ui.style().visuals.dark_bg_color, // fill: ui.visuals().dark_bg_color,
stroke: Default::default(), stroke: Default::default(),
// stroke: visuals.bg_stroke, // stroke: visuals.bg_stroke,
// stroke: ui.style().visuals.widgets.inactive.bg_stroke, // stroke: ui.visuals().widgets.inactive.bg_stroke,
}); });
ui.painter().add(Shape::Circle { ui.painter().add(Shape::Circle {
@ -323,9 +323,7 @@ impl<'a> Slider<'a> {
fn label_ui(&mut self, ui: &mut Ui) { fn label_ui(&mut self, ui: &mut Ui) {
if let Some(label_text) = self.text.as_deref() { if let Some(label_text) = self.text.as_deref() {
let text_color = self let text_color = self.text_color.unwrap_or_else(|| ui.visuals().text_color());
.text_color
.unwrap_or_else(|| ui.style().visuals.text_color());
ui.add( ui.add(
Label::new(label_text) Label::new(label_text)

View file

@ -244,15 +244,15 @@ impl<'t> Widget for TextEdit<'t> {
Shape::Rect { Shape::Rect {
rect: frame_rect, rect: frame_rect,
corner_radius: visuals.corner_radius, corner_radius: visuals.corner_radius,
// fill: ui.style().visuals.selection.bg_fill, // fill: ui.visuals().selection.bg_fill,
fill: ui.style().visuals.dark_bg_color, fill: ui.visuals().dark_bg_color,
stroke: ui.style().visuals.selection.stroke, stroke: ui.visuals().selection.stroke,
} }
} else { } else {
Shape::Rect { Shape::Rect {
rect: frame_rect, rect: frame_rect,
corner_radius: visuals.corner_radius, corner_radius: visuals.corner_radius,
fill: ui.style().visuals.dark_bg_color, fill: ui.visuals().dark_bg_color,
stroke: visuals.bg_stroke, // TODO: we want to show something here, or a text-edit field doesn't "pop". stroke: visuals.bg_stroke, // TODO: we want to show something here, or a text-edit field doesn't "pop".
} }
}; };
@ -505,9 +505,9 @@ impl<'t> TextEdit<'t> {
} }
let text_color = text_color let text_color = text_color
.or(ui.style().visuals.override_text_color) .or(ui.visuals().override_text_color)
// .unwrap_or_else(|| ui.style().interact(&response).text_color()); // too bright // .unwrap_or_else(|| ui.style().interact(&response).text_color()); // too bright
.unwrap_or_else(|| ui.style().visuals.widgets.inactive.text_color()); .unwrap_or_else(|| ui.visuals().widgets.inactive.text_color());
ui.painter() ui.painter()
.galley(response.rect.min, galley, text_style, text_color); .galley(response.rect.min, galley, text_style, text_color);
@ -523,7 +523,7 @@ impl<'t> TextEdit<'t> {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
fn paint_cursor_selection(ui: &mut Ui, pos: Pos2, galley: &Galley, cursorp: &CursorPair) { fn paint_cursor_selection(ui: &mut Ui, pos: Pos2, galley: &Galley, cursorp: &CursorPair) {
let color = ui.style().visuals.selection.bg_fill; let color = ui.visuals().selection.bg_fill;
if cursorp.is_empty() { if cursorp.is_empty() {
return; return;
} }
@ -554,7 +554,7 @@ fn paint_cursor_selection(ui: &mut Ui, pos: Pos2, galley: &Galley, cursorp: &Cur
} }
fn paint_cursor_end(ui: &mut Ui, pos: Pos2, galley: &Galley, cursor: &Cursor) { fn paint_cursor_end(ui: &mut Ui, pos: Pos2, galley: &Galley, cursor: &Cursor) {
let stroke = ui.style().visuals.selection.stroke; let stroke = ui.visuals().selection.stroke;
let cursor_pos = galley.pos_from_cursor(cursor).translate(pos.to_vec2()); let cursor_pos = galley.pos_from_cursor(cursor).translate(pos.to_vec2());
let cursor_pos = cursor_pos.expand(1.5); // slightly above/below row let cursor_pos = cursor_pos.expand(1.5); // slightly above/below row
@ -564,7 +564,7 @@ fn paint_cursor_end(ui: &mut Ui, pos: Pos2, galley: &Galley, cursor: &Cursor) {
ui.painter().line_segment( ui.painter().line_segment(
[top, bottom], [top, bottom],
(ui.style().visuals.text_cursor_width, stroke.color), (ui.visuals().text_cursor_width, stroke.color),
); );
if false { if false {

View file

@ -50,13 +50,13 @@ pub fn drop_target<R>(
let (rect, response) = ui.allocate_at_least(outer_rect.size(), Sense::hover()); let (rect, response) = ui.allocate_at_least(outer_rect.size(), Sense::hover());
let style = if is_being_dragged && can_accept_what_is_being_dragged && response.hovered() { let style = if is_being_dragged && can_accept_what_is_being_dragged && response.hovered() {
ui.style().visuals.widgets.active ui.visuals().widgets.active
} else if is_being_dragged && can_accept_what_is_being_dragged { } else if is_being_dragged && can_accept_what_is_being_dragged {
ui.style().visuals.widgets.inactive ui.visuals().widgets.inactive
} else if is_being_dragged && !can_accept_what_is_being_dragged { } else if is_being_dragged && !can_accept_what_is_being_dragged {
ui.style().visuals.widgets.disabled ui.visuals().widgets.disabled
} else { } else {
ui.style().visuals.widgets.inactive ui.visuals().widgets.inactive
}; };
ui.painter().set( ui.painter().set(

View file

@ -107,7 +107,7 @@ impl super::View for Scrolling {
ui.scroll_to_cursor(Align::bottom()); ui.scroll_to_cursor(Align::bottom());
} }
let margin = ui.style().visuals.clip_rect_margin; let margin = ui.visuals().clip_rect_margin;
let current_scroll = ui.clip_rect().top() - ui.min_rect().top() + margin; let current_scroll = ui.clip_rect().top() - ui.min_rect().top() + margin;
let max_scroll = ui.min_rect().height() - ui.clip_rect().height() + 2.0 * margin; let max_scroll = ui.min_rect().height() - ui.clip_rect().height() + 2.0 * margin;

View file

@ -72,7 +72,7 @@ impl FrameHistory {
let mut shapes = vec![Shape::Rect { let mut shapes = vec![Shape::Rect {
rect, rect,
corner_radius: style.corner_radius, corner_radius: style.corner_radius,
fill: ui.style().visuals.dark_bg_color, fill: ui.visuals().dark_bg_color,
stroke: ui.style().noninteractive().bg_stroke, stroke: ui.style().noninteractive().bg_stroke,
}]; }];