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.
* 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.
* Add `Ui::spacing()` and `Ui::spacing_mut()`.
* Add: `ui.spacing()`, `ui.spacing_mut()`, `ui.visuals()`, `ui.visuals_mut()`.
### Changed 🔧

View file

@ -186,7 +186,7 @@ impl Resize {
let corner_response = if self.resizable {
// 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 =
Rect::from_min_size(position + state.desired_size - corner_size, corner_size);
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 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.
// 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!
// 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(
inner_rect.min
+ state.last_content_size
+ Vec2::splat(ui.style().visuals.clip_rect_margin),
inner_rect.min + state.last_content_size + Vec2::splat(ui.visuals().clip_rect_margin),
);
content_clip_rect = content_clip_rect.intersect(ui.clip_rect()); // Respect parent region
@ -287,7 +285,7 @@ impl Resize {
rect,
corner_radius: 3.0,
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(),
);
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.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);
@ -331,7 +331,7 @@ impl Prepared {
ui.painter().add(paint::Shape::Rect {
rect: outer_scroll_rect,
corner_radius,
fill: ui.style().visuals.dark_bg_color,
fill: ui.visuals().dark_bg_color,
stroke: Default::default(),
// fill: visuals.bg_fill,
// 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) {
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 corner_rect =
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;
let cr = ui.style().visuals.window_corner_radius;
let cr = ui.visuals().window_corner_radius;
let Rect { min, max } = rect;
let mut points = Vec::new();
@ -739,7 +739,7 @@ impl TitleBar {
// Always have inactive style for 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);
@ -757,7 +757,7 @@ impl TitleBar {
// let y = lerp(self.rect.bottom()..=content_response.rect.top(), 0.5);
ui.painter().line_segment(
[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);
ui.painter().line_segment(
[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 => {
@ -66,7 +66,7 @@ pub fn item_ui(ui: &mut Ui, item: easy_mark::Item<'_>) {
let mut rect = ui.monospace(code).rect;
rect = rect.expand(1.0); // looks better
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(
where_to_put_background,
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(
rect.center(),
rect.height() / 8.0,
ui.style().visuals.strong_text_color(),
ui.visuals().strong_text_color(),
);
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 (rect, response) = ui.allocate_exact_size(vec2(width, row_height), Sense::hover());
let text = format!("{}.", number);
let text_color = ui.style().visuals.strong_text_color();
let text_color = ui.visuals().strong_text_color();
ui.painter().text(
rect.right_center(),
Align2::RIGHT_CENTER,

View file

@ -79,7 +79,7 @@ fn menu_impl<'c>(
let mut button = Button::new(title);
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);

View file

@ -120,6 +120,18 @@ impl Ui {
&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`].
pub fn ctx(&self) -> &CtxRef {
self.painter.ctx()
@ -493,8 +505,8 @@ impl Ui {
let rect = self.allocate_space_impl(desired_size);
let debug_expand_width = self.style().visuals.debug_expand_width;
let debug_expand_height = self.style().visuals.debug_expand_height;
let debug_expand_width = self.visuals().debug_expand_width;
let debug_expand_height = self.visuals().debug_expand_height;
if (debug_expand_width && too_wide) || (debug_expand_height && too_high) {
self.painter
@ -956,7 +968,7 @@ impl Ui {
let line_end = pos2(line_start.x, line_start.y + size.y - 2.0);
self.painter.line_segment(
[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());

View file

@ -126,7 +126,7 @@ impl Widget for Button {
}
let text_color = text_color
.or(ui.style().visuals.override_text_color)
.or(ui.visuals().override_text_color)
.unwrap_or_else(|| visuals.text_color());
ui.painter()
.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()),
],
visuals.fg_stroke,
// ui.style().visuals.selection.stroke, // too much color
// ui.visuals().selection.stroke, // too much 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());
ui.painter()
.galley(text_cursor, galley, text_style, text_color);
@ -307,13 +307,13 @@ impl Widget for RadioButton {
center: small_icon_rect.center(),
radius: small_icon_rect.width() / 3.0,
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(),
});
}
let text_color = text_color
.or(ui.style().visuals.override_text_color)
.or(ui.visuals().override_text_color)
.unwrap_or_else(|| visuals.text_color());
painter.galley(text_cursor, galley, text_style, text_color);
response
@ -391,7 +391,7 @@ impl Widget for ImageButton {
let visuals = ui.style().interact(&response);
if selected {
let selection = ui.style().visuals.selection;
let selection = ui.visuals().selection;
ui.painter()
.rect(rect, 0.0, selection.bg_fill, selection.stroke);
} else if frame {

View file

@ -53,7 +53,7 @@ impl Widget for Hyperlink {
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);
if response.hovered() {

View file

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

View file

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

View file

@ -68,7 +68,7 @@ impl Widget for Separator {
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);
response
}

View file

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

View file

@ -244,15 +244,15 @@ impl<'t> Widget for TextEdit<'t> {
Shape::Rect {
rect: frame_rect,
corner_radius: visuals.corner_radius,
// fill: ui.style().visuals.selection.bg_fill,
fill: ui.style().visuals.dark_bg_color,
stroke: ui.style().visuals.selection.stroke,
// fill: ui.visuals().selection.bg_fill,
fill: ui.visuals().dark_bg_color,
stroke: ui.visuals().selection.stroke,
}
} else {
Shape::Rect {
rect: frame_rect,
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".
}
};
@ -505,9 +505,9 @@ impl<'t> TextEdit<'t> {
}
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().visuals.widgets.inactive.text_color());
.unwrap_or_else(|| ui.visuals().widgets.inactive.text_color());
ui.painter()
.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) {
let color = ui.style().visuals.selection.bg_fill;
let color = ui.visuals().selection.bg_fill;
if cursorp.is_empty() {
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) {
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 = 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(
[top, bottom],
(ui.style().visuals.text_cursor_width, stroke.color),
(ui.visuals().text_cursor_width, stroke.color),
);
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 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 {
ui.style().visuals.widgets.inactive
ui.visuals().widgets.inactive
} else if is_being_dragged && !can_accept_what_is_being_dragged {
ui.style().visuals.widgets.disabled
ui.visuals().widgets.disabled
} else {
ui.style().visuals.widgets.inactive
ui.visuals().widgets.inactive
};
ui.painter().set(

View file

@ -107,7 +107,7 @@ impl super::View for Scrolling {
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 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 {
rect,
corner_radius: style.corner_radius,
fill: ui.style().visuals.dark_bg_color,
fill: ui.visuals().dark_bg_color,
stroke: ui.style().noninteractive().bg_stroke,
}];