[refactor] combine stroke_color and stroke_width in WidgetStyle

This commit is contained in:
Emil Ernerfeldt 2020-09-02 01:36:52 +02:00
parent 9d4021d703
commit ecd68f4faa
8 changed files with 23 additions and 47 deletions

View file

@ -105,7 +105,7 @@ impl State {
/// Paint the arrow icon that indicated if the region is open or not /// Paint the arrow icon that indicated if the region is open or not
pub fn paint_icon(ui: &mut Ui, openness: f32, response: &Response) { pub fn paint_icon(ui: &mut Ui, openness: f32, response: &Response) {
let stroke = ui.style().interact(response).stroke(); let stroke = ui.style().interact(response).stroke;
let rect = response.rect; let rect = response.rect;
@ -224,7 +224,7 @@ impl CollapsingHeader {
text_pos, text_pos,
galley, galley,
label.text_style, label.text_style,
ui.style().interact(&response).stroke_color, ui.style().interact(&response).stroke.color,
); );
painter.set( painter.set(

View file

@ -270,9 +270,8 @@ impl Resize {
use crate::paint::Stroke; use crate::paint::Stroke;
pub fn paint_resize_corner(ui: &mut Ui, response: &Response) { pub fn paint_resize_corner(ui: &mut Ui, response: &Response) {
let color = ui.style().interact(response).stroke_color; let stroke = ui.style().interact(response).stroke;
let width = ui.style().interact(response).stroke_width; paint_resize_corner_with_style(ui, &response.rect, stroke);
paint_resize_corner_with_style(ui, &response.rect, Stroke::new(width, color));
} }
pub fn paint_resize_corner_with_style(ui: &mut Ui, rect: &Rect, stroke: Stroke) { pub fn paint_resize_corner_with_style(ui: &mut Ui, rect: &Rect, stroke: Stroke) {

View file

@ -711,15 +711,10 @@ fn close_button(ui: &mut Ui, rect: Rect) -> Response {
let response = ui.interact(rect, close_id, Sense::click()); let response = ui.interact(rect, close_id, Sense::click());
ui.expand_to_include_child(response.rect); ui.expand_to_include_child(response.rect);
let stroke_color = ui.style().interact(&response).stroke_color; let stroke = ui.style().interact(&response).stroke;
let stroke_width = ui.style().interact(&response).stroke_width; ui.painter()
ui.painter().line_segment( .line_segment([rect.left_top(), rect.right_bottom()], stroke);
[rect.left_top(), rect.right_bottom()], ui.painter()
(stroke_width, stroke_color), .line_segment([rect.right_top(), rect.left_bottom()], stroke);
);
ui.painter().line_segment(
[rect.right_top(), rect.left_bottom()],
(stroke_width, stroke_color),
);
response response
} }

View file

@ -45,7 +45,7 @@ pub fn toggle(ui: &mut Ui, on: &mut bool) -> Response {
center: pos2(circle_x, rect.center().y), center: pos2(circle_x, rect.center().y),
radius: 0.75 * radius, radius: 0.75 * radius,
fill: style.main_fill, fill: style.main_fill,
stroke: style.stroke(), stroke: style.stroke,
}); });
// All done! Return the response so the user can check what happened // All done! Return the response so the user can check what happened

View file

@ -155,19 +155,10 @@ pub struct WidgetVisuals {
/// Fill color of the interactive part of a component (slider grab, checkbox, ...) /// Fill color of the interactive part of a component (slider grab, checkbox, ...)
/// When you need a fill. /// When you need a fill.
pub main_fill: Srgba, pub main_fill: Srgba, // TODO: just fill?
/// Stroke and text color of the interactive part of a component (button text, slider grab, check-mark, ...) /// Stroke and text color of the interactive part of a component (button text, slider grab, check-mark, ...)
pub stroke_color: Srgba, pub stroke: Stroke,
/// For lines etc
pub stroke_width: f32,
}
impl WidgetVisuals {
pub fn stroke(&self) -> Stroke {
Stroke::new(self.stroke_width, self.stroke_color)
}
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -236,32 +227,28 @@ impl Default for Interacted {
bg_stroke: Stroke::new(2.0, WHITE), bg_stroke: Stroke::new(2.0, WHITE),
corner_radius: 4.0, corner_radius: 4.0,
main_fill: srgba(120, 120, 200, 255), main_fill: srgba(120, 120, 200, 255),
stroke_color: WHITE, stroke: Stroke::new(2.0, WHITE),
stroke_width: 2.0,
}, },
hovered: WidgetVisuals { hovered: WidgetVisuals {
bg_fill: Rgba::luminance_alpha(0.06, 0.5).into(), bg_fill: Rgba::luminance_alpha(0.06, 0.5).into(),
bg_stroke: Stroke::new(1.0, Rgba::white_alpha(0.5)), bg_stroke: Stroke::new(1.0, Rgba::white_alpha(0.5)),
corner_radius: 4.0, corner_radius: 4.0,
main_fill: srgba(100, 100, 150, 255), main_fill: srgba(100, 100, 150, 255),
stroke_color: Srgba::gray(240), stroke: Stroke::new(1.5, Srgba::gray(240)),
stroke_width: 1.5,
}, },
inactive: WidgetVisuals { inactive: WidgetVisuals {
bg_fill: Rgba::luminance_alpha(0.04, 0.5).into(), bg_fill: Rgba::luminance_alpha(0.04, 0.5).into(),
bg_stroke: Stroke::new(1.0, Rgba::white_alpha(0.04)), bg_stroke: Stroke::new(1.0, Rgba::white_alpha(0.04)),
corner_radius: 4.0, corner_radius: 4.0,
main_fill: srgba(60, 60, 80, 255), main_fill: srgba(60, 60, 80, 255),
stroke_color: Srgba::gray(200), // Should NOT look grayed out! stroke: Stroke::new(0.5, Srgba::gray(200)), // Should NOT look grayed out!
stroke_width: 0.5,
}, },
disabled: WidgetVisuals { disabled: WidgetVisuals {
bg_fill: TRANSPARENT, bg_fill: TRANSPARENT,
bg_stroke: Stroke::new(0.5, Srgba::gray(70)), bg_stroke: Stroke::new(0.5, Srgba::gray(70)),
corner_radius: 4.0, corner_radius: 4.0,
main_fill: srgba(50, 50, 50, 255), main_fill: srgba(50, 50, 50, 255),
stroke_color: Srgba::gray(128), // Should look grayed out stroke: Stroke::new(0.5, Srgba::gray(128)), // Should look grayed out
stroke_width: 0.5,
}, },
} }
} }
@ -365,16 +352,14 @@ impl WidgetVisuals {
bg_stroke, bg_stroke,
corner_radius, corner_radius,
main_fill, main_fill,
stroke_color, stroke,
stroke_width,
} = self; } = self;
ui_color(ui, bg_fill, "bg_fill"); ui_color(ui, bg_fill, "bg_fill");
bg_stroke.ui(ui, "bg_stroke"); bg_stroke.ui(ui, "bg_stroke");
ui.add(Slider::f32(corner_radius, 0.0..=10.0).text("corner_radius")); ui.add(Slider::f32(corner_radius, 0.0..=10.0).text("corner_radius"));
ui_color(ui, main_fill, "main_fill"); ui_color(ui, main_fill, "main_fill");
ui_color(ui, stroke_color, "stroke_color"); stroke.ui(ui, "stroke");
ui.add(Slider::f32(stroke_width, 0.0..=10.0).text("stroke_width"));
} }
} }

View file

@ -297,7 +297,7 @@ impl Widget for Button {
fill, fill,
stroke: ui.style().interact(&response).bg_stroke, stroke: ui.style().interact(&response).bg_stroke,
}); });
let stroke_color = ui.style().interact(&response).stroke_color; let stroke_color = ui.style().interact(&response).stroke.color;
let text_color = text_color.unwrap_or(stroke_color); let text_color = text_color.unwrap_or(stroke_color);
ui.painter() ui.painter()
.galley(text_cursor, galley, text_style, text_color); .galley(text_cursor, galley, text_style, text_color);
@ -368,7 +368,7 @@ impl<'a> Widget for Checkbox<'a> {
stroke: ui.style().interact(&response).bg_stroke, stroke: ui.style().interact(&response).bg_stroke,
}); });
let stroke = ui.style().interact(&response).stroke(); let stroke = ui.style().interact(&response).stroke;
if *checked { if *checked {
ui.painter().add(PaintCmd::Path { ui.painter().add(PaintCmd::Path {
@ -442,7 +442,7 @@ impl Widget for RadioButton {
); );
let bg_fill = ui.style().interact(&response).bg_fill; let bg_fill = ui.style().interact(&response).bg_fill;
let stroke_color = ui.style().interact(&response).stroke_color; let stroke_color = ui.style().interact(&response).stroke.color;
let (small_icon_rect, big_icon_rect) = ui.style().spacing.icon_rectangles(response.rect); let (small_icon_rect, big_icon_rect) = ui.style().spacing.icon_rectangles(response.rect);

View file

@ -172,10 +172,7 @@ impl<'a> Slider<'a> {
center: pos2(marker_center_x, rail_rect.center().y), center: pos2(marker_center_x, rail_rect.center().y),
radius: handle_radius(rect), radius: handle_radius(rect),
fill: ui.style().interact(response).main_fill, fill: ui.style().interact(response).main_fill,
stroke: Stroke::new( stroke: ui.style().interact(response).stroke,
ui.style().interact(response).stroke_width,
ui.style().interact(response).stroke_color,
),
}); });
} }
} }

View file

@ -208,7 +208,7 @@ impl<'t> Widget for TextEdit<'t> {
} }
} }
let text_color = text_color.unwrap_or_else(|| ui.style().interact(&response).stroke_color); let text_color = text_color.unwrap_or_else(|| ui.style().interact(&response).stroke.color);
painter.galley(response.rect.min, galley, text_style, text_color); painter.galley(response.rect.min, galley, text_style, text_color);
ui.memory().text_edit.insert(id, state); ui.memory().text_edit.insert(id, state);
response response