Browse Source

[refactor] Remove Option wrappers around colors and line styles

Just use transparency instead of None
pull/20/head
Emil Ernerfeldt 4 years ago
parent
commit
96153a86e5
  1. 8
      egui/src/containers/collapsing_header.rs
  2. 22
      egui/src/containers/frame.rs
  3. 4
      egui/src/containers/resize.rs
  4. 6
      egui/src/containers/scroll_area.rs
  5. 7
      egui/src/containers/window.rs
  6. 8
      egui/src/demos/app.rs
  7. 6
      egui/src/demos/fractal_clock.rs
  8. 6
      egui/src/demos/toggle_switch.rs
  9. 2
      egui/src/lib.rs
  10. 26
      egui/src/menu.rs
  11. 18
      egui/src/paint/command.rs
  12. 35
      egui/src/paint/tessellator.rs
  13. 20
      egui/src/painter.rs
  14. 26
      egui/src/style.rs
  15. 18
      egui/src/widgets.rs
  16. 10
      egui/src/widgets/slider.rs
  17. 2
      egui/src/widgets/text_edit.rs

8
egui/src/containers/collapsing_header.rs

@ -123,8 +123,8 @@ pub fn paint_icon(ui: &mut Ui, openness: f32, response: &Response) {
ui.painter().add(PaintCmd::Path {
path: Path::from_point_loop(&points),
closed: true,
fill: None,
outline: Some(LineStyle::new(stroke_width, stroke_color)),
fill: Default::default(),
outline: LineStyle::new(stroke_width, stroke_color),
});
}
@ -229,10 +229,10 @@ impl CollapsingHeader {
painter.set(
bg_index,
PaintCmd::Rect {
rect: response.rect,
corner_radius: ui.style().interact(&response).corner_radius,
fill: ui.style().interact(&response).bg_fill,
outline: None,
rect: response.rect,
outline: Default::default(),
},
);

22
egui/src/containers/frame.rs

@ -8,8 +8,8 @@ pub struct Frame {
// On each side
pub margin: Vec2,
pub corner_radius: f32,
pub fill: Option<Srgba>,
pub outline: Option<LineStyle>,
pub fill: Srgba,
pub outline: LineStyle,
}
impl Frame {
@ -17,7 +17,7 @@ impl Frame {
Self {
margin: style.spacing.window_padding,
corner_radius: style.visuals.window_corner_radius,
fill: Some(style.visuals.background_fill),
fill: style.visuals.background_fill,
outline: style.visuals.interacted.inactive.bg_outline, // because we can resize windows
}
}
@ -26,8 +26,8 @@ impl Frame {
Self {
margin: Vec2::splat(1.0),
corner_radius: 0.0,
fill: None,
outline: Some(LineStyle::new(0.5, Srgba::gray(128))),
fill: Default::default(),
outline: LineStyle::new(0.5, Srgba::gray(128)),
}
}
@ -35,8 +35,8 @@ impl Frame {
Self {
margin: Vec2::splat(1.0),
corner_radius: 2.0,
fill: Some(style.visuals.background_fill),
outline: Some(LineStyle::new(1.0, Srgba::gray(128))),
fill: style.visuals.background_fill,
outline: LineStyle::new(1.0, Srgba::gray(128)),
}
}
@ -44,17 +44,17 @@ impl Frame {
Self {
margin: style.spacing.window_padding,
corner_radius: 5.0,
fill: Some(style.visuals.background_fill),
outline: Some(LineStyle::new(1.0, Srgba::gray(128))),
fill: style.visuals.background_fill,
outline: LineStyle::new(1.0, Srgba::gray(128)),
}
}
pub fn fill(mut self, fill: Option<Srgba>) -> Self {
pub fn fill(mut self, fill: Srgba) -> Self {
self.fill = fill;
self
}
pub fn outline(mut self, outline: Option<LineStyle>) -> Self {
pub fn outline(mut self, outline: LineStyle) -> Self {
self.outline = outline;
self
}

4
egui/src/containers/resize.rs

@ -237,8 +237,8 @@ impl Resize {
ui.painter().add(paint::PaintCmd::Rect {
rect,
corner_radius: 3.0,
fill: None,
outline: Some(ui.style().visuals.thin_outline),
fill: Default::default(),
outline: ui.style().visuals.thin_outline,
});
}

6
egui/src/containers/scroll_area.rs

@ -243,14 +243,14 @@ impl Prepared {
ui.painter().add(paint::PaintCmd::Rect {
rect: outer_scroll_rect,
corner_radius,
fill: Some(ui.style().visuals.dark_bg_color),
outline: None,
fill: ui.style().visuals.dark_bg_color,
outline: Default::default(),
});
ui.painter().add(paint::PaintCmd::Rect {
rect: handle_rect.expand(-2.0),
corner_radius,
fill: Some(handle_fill),
fill: handle_fill,
outline: handle_outline,
});
}

7
egui/src/containers/window.rs

@ -311,12 +311,11 @@ impl<'open> Window<'open> {
}
}
fn paint_resize_corner(ui: &mut Ui, outer_rect: Rect, frame_outline: Option<LineStyle>) {
fn paint_resize_corner(ui: &mut Ui, outer_rect: Rect, outline: LineStyle) {
let corner_size = Vec2::splat(ui.style().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);
let outline = frame_outline.unwrap_or_else(|| LineStyle::new(1.0, color::GRAY));
crate::resize::paint_resize_corner_with_style(ui, &corner_rect, outline);
}
@ -566,7 +565,7 @@ fn paint_frame_interaction(
ui.painter().add(PaintCmd::Path {
path,
closed: false,
fill: None,
fill: Default::default(),
outline: visuals.bg_outline,
});
}
@ -671,7 +670,7 @@ impl TitleBar {
let y = content_rect.top() + ui.style().spacing.item_spacing.y * 0.5;
ui.painter().line_segment(
[pos2(left, y), pos2(right, y)],
ui.style().visuals.interacted.inactive.bg_outline.unwrap(),
ui.style().visuals.interacted.inactive.bg_outline,
);
}

8
egui/src/demos/app.rs

@ -579,12 +579,12 @@ impl BoxPainting {
for i in 0..self.num_boxes {
cmds.push(PaintCmd::Rect {
corner_radius: self.corner_radius,
fill: Some(Srgba::gray(64)),
fill: Srgba::gray(64),
rect: Rect::from_min_size(
pos2(10.0 + pos.x + (i as f32) * (self.size.x * 1.1), pos.y),
self.size,
),
outline: Some(LineStyle::new(self.stroke_width, WHITE)),
outline: LineStyle::new(self.stroke_width, WHITE),
});
}
ui.painter().extend(cmds);
@ -655,8 +655,8 @@ impl Painting {
painter.add(PaintCmd::Path {
path: Path::from_open_points(&points),
closed: false,
outline: Some(LineStyle::new(self.line_width, LIGHT_GRAY)),
fill: None,
outline: LineStyle::new(self.line_width, LIGHT_GRAY),
fill: Default::default(),
});
}
}

6
egui/src/demos/fractal_clock.rs

@ -37,7 +37,7 @@ impl FractalClock {
.default_rect(ctx.rect().expand(-42.0))
.scroll(false)
// Dark background frame to make it pop:
.frame(Frame::window(&ctx.style()).fill(Some(Srgba::black_alpha(250))))
.frame(Frame::window(&ctx.style()).fill(Srgba::black_alpha(250)))
.show(ctx, |ui| self.ui(ui));
}
@ -54,8 +54,8 @@ impl FractalClock {
self.fractal_ui(&painter);
Frame::popup(ui.style())
.fill(Some(Rgba::luminance_alpha(0.02, 0.5).into()))
.outline(None)
.fill(Rgba::luminance_alpha(0.02, 0.5).into())
.outline(LineStyle::none())
.show(&mut ui.left_column(320.0), |ui| {
CollapsingHeader::new("Settings").show(ui, |ui| self.options_ui(ui));
});

6
egui/src/demos/toggle_switch.rs

@ -36,16 +36,16 @@ pub fn toggle(ui: &mut Ui, on: &mut bool) -> Response {
ui.painter().add(PaintCmd::Rect {
rect,
corner_radius: radius,
fill: lerp(off_color..=on_color, how_on).into(),
outline: style.bg_outline,
fill: Some(lerp(off_color..=on_color, how_on).into()),
});
// Animate the circle from left to right:
let circle_x = lerp((rect.left() + radius)..=(rect.right() - radius), how_on);
ui.painter().add(PaintCmd::Circle {
center: pos2(circle_x, rect.center().y),
radius: 0.75 * radius,
outline: Some(style.line_style()),
fill: Some(style.main_fill),
fill: style.main_fill,
outline: style.line_style(),
});
// All done! Return the response so the user can check what happened

2
egui/src/lib.rs

@ -74,7 +74,7 @@ pub use {
layout::*,
math::*,
memory::Memory,
paint::{color, PaintJobs, Rgba, Srgba, TextStyle, Texture},
paint::{color, LineStyle, PaintJobs, Rgba, Srgba, TextStyle, Texture},
painter::Painter,
style::Style,
types::*,

26
egui/src/menu.rs

@ -15,7 +15,7 @@
//! }
//! ```
use crate::{widgets::*, *};
use crate::{color::TRANSPARENT, paint::LineStyle, widgets::*, *};
/// What is saved between frames.
#[derive(Clone, Copy, Debug)]
@ -62,12 +62,12 @@ pub fn bar<R>(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> (R, Rect)
Frame::menu_bar(ui.style()).show(ui, |ui| {
let mut style = ui.style().clone();
style.spacing.button_padding = vec2(2.0, 0.0);
// style.visuals.interacted.active.bg_fill = None;
style.visuals.interacted.active.bg_outline = None;
// style.visuals.interacted.hovered.bg_fill = None;
style.visuals.interacted.hovered.bg_outline = None;
style.visuals.interacted.inactive.bg_fill = None;
style.visuals.interacted.inactive.bg_outline = None;
// style.visuals.interacted.active.bg_fill = TRANSPARENT;
style.visuals.interacted.active.bg_outline = LineStyle::none();
// style.visuals.interacted.hovered.bg_fill = TRANSPARENT;
style.visuals.interacted.hovered.bg_outline = LineStyle::none();
style.visuals.interacted.inactive.bg_fill = TRANSPARENT;
style.visuals.interacted.inactive.bg_outline = LineStyle::none();
ui.set_style(style);
// Take full width and fixed height:
@ -128,12 +128,12 @@ fn menu_impl<'c>(
resize.show(ui, |ui| {
let mut style = ui.style().clone();
style.spacing.button_padding = vec2(2.0, 0.0);
// style.visuals.interacted.active.bg_fill = None;
style.visuals.interacted.active.bg_outline = None;
// style.visuals.interacted.hovered.bg_fill = None;
style.visuals.interacted.hovered.bg_outline = None;
style.visuals.interacted.inactive.bg_fill = None;
style.visuals.interacted.inactive.bg_outline = None;
// style.visuals.interacted.active.bg_fill = TRANSPARENT;
style.visuals.interacted.active.bg_outline = LineStyle::none();
// style.visuals.interacted.hovered.bg_fill = TRANSPARENT;
style.visuals.interacted.hovered.bg_outline = LineStyle::none();
style.visuals.interacted.inactive.bg_fill = TRANSPARENT;
style.visuals.interacted.inactive.bg_outline = LineStyle::none();
ui.set_style(style);
ui.set_layout(Layout::justified(Direction::Vertical));
add_contents(ui)

18
egui/src/paint/command.rs

@ -11,8 +11,8 @@ pub enum PaintCmd {
Circle {
center: Pos2,
radius: f32,
fill: Option<Srgba>,
outline: Option<LineStyle>,
fill: Srgba,
outline: LineStyle,
},
LineSegment {
points: [Pos2; 2],
@ -21,14 +21,14 @@ pub enum PaintCmd {
Path {
path: Path,
closed: bool,
fill: Option<Srgba>,
outline: Option<LineStyle>,
fill: Srgba,
outline: LineStyle,
},
Rect {
rect: Rect,
corner_radius: f32,
fill: Option<Srgba>,
outline: Option<LineStyle>,
fill: Srgba,
outline: LineStyle,
},
Text {
/// Top left corner of the first character.
@ -41,7 +41,7 @@ pub enum PaintCmd {
Triangles(Triangles),
}
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct LineStyle {
pub width: f32,
@ -49,6 +49,10 @@ pub struct LineStyle {
}
impl LineStyle {
pub fn none() -> Self {
Self::new(0.0, crate::color::TRANSPARENT)
}
pub fn new(width: impl Into<f32>, color: impl Into<Srgba>) -> Self {
Self {
width: width.into(),

35
egui/src/paint/tessellator.rs

@ -7,7 +7,7 @@
use {
super::{
color::{self, srgba, Rgba, Srgba},
color::{self, srgba, Rgba, Srgba, TRANSPARENT},
fonts::Fonts,
LineStyle, PaintCmd,
},
@ -427,7 +427,7 @@ pub fn paint_path_outline(
options: PaintOptions,
out: &mut Triangles,
) {
if style.color == color::TRANSPARENT {
if style.width <= 0.0 || style.color == color::TRANSPARENT {
return;
}
@ -604,12 +604,8 @@ pub fn tessellate_paint_command(
} => {
if radius > 0.0 {
path.add_circle(center, radius);
if let Some(fill) = fill {
fill_closed_path(&path.0, fill, options, out);
}
if let Some(line_style) = outline {
paint_path_outline(&path.0, Closed, line_style, options, out);
}
fill_closed_path(&path.0, fill, options, out);
paint_path_outline(&path.0, Closed, outline, options, out);
}
}
PaintCmd::Triangles(triangles) => {
@ -626,17 +622,15 @@ pub fn tessellate_paint_command(
outline,
} => {
if path.len() >= 2 {
if let Some(fill) = fill {
if fill != TRANSPARENT {
debug_assert!(
closed,
"You asked to fill a path that is not closed. That makes no sense."
);
fill_closed_path(&path.0, fill, options, out);
}
if let Some(line_style) = outline {
let typ = if closed { Closed } else { Open };
paint_path_outline(&path.0, typ, line_style, options, out);
}
let typ = if closed { Closed } else { Open };
paint_path_outline(&path.0, typ, outline, options, out);
}
}
PaintCmd::Rect {
@ -652,12 +646,8 @@ pub fn tessellate_paint_command(
rect.max = rect.max.min(pos2(1e7, 1e7));
path.add_rounded_rectangle(rect, corner_radius);
if let Some(fill) = fill {
fill_closed_path(&path.0, fill, options, out);
}
if let Some(line_style) = outline {
paint_path_outline(&path.0, Closed, line_style, options, out);
}
fill_closed_path(&path.0, fill, options, out);
paint_path_outline(&path.0, Closed, outline, options, out);
}
}
PaintCmd::Text {
@ -666,6 +656,9 @@ pub fn tessellate_paint_command(
text_style,
color,
} => {
if color == TRANSPARENT {
return;
}
galley.sanity_check();
let num_chars = galley.text.chars().count();
@ -737,8 +730,8 @@ pub fn tessellate_paint_commands(
PaintCmd::Rect {
rect: *clip_rect,
corner_radius: 0.0,
fill: None,
outline: Some(LineStyle::new(2.0, srgba(150, 255, 150, 255))),
fill: Default::default(),
outline: LineStyle::new(2.0, srgba(150, 255, 150, 255)),
},
options,
fonts,

20
egui/src/painter.rs

@ -131,8 +131,8 @@ impl Painter {
self.add(PaintCmd::Rect {
rect: rect.expand(2.0),
corner_radius: 0.0,
fill: Some(Srgba::black_alpha(240)),
outline: Some(LineStyle::new(1.0, color::RED)),
fill: Srgba::black_alpha(240),
outline: LineStyle::new(1.0, color::RED),
});
self.galley(rect.min, galley, text_style, color::RED);
}
@ -151,8 +151,8 @@ impl Painter {
self.add(PaintCmd::Circle {
center,
radius,
fill: Some(fill_color.into()),
outline: None,
fill: fill_color.into(),
outline: Default::default(),
});
}
@ -160,8 +160,8 @@ impl Painter {
self.add(PaintCmd::Circle {
center,
radius,
fill: None,
outline: Some(outline.into()),
fill: Default::default(),
outline: outline.into(),
});
}
@ -169,8 +169,8 @@ impl Painter {
self.add(PaintCmd::Rect {
rect,
corner_radius,
fill: Some(fill_color.into()),
outline: None,
fill: fill_color.into(),
outline: Default::default(),
});
}
@ -178,8 +178,8 @@ impl Painter {
self.add(PaintCmd::Rect {
rect,
corner_radius,
fill: None,
outline: Some(outline.into()),
fill: Default::default(),
outline: outline.into(),
});
}
}

26
egui/src/style.rs

@ -139,11 +139,11 @@ impl Interacted {
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct WidgetVisuals {
/// Background color of widget
pub bg_fill: Option<Srgba>,
pub bg_fill: Srgba,
/// For surrounding rectangle of things that need it,
/// like buttons, the box of the checkbox, etc.
pub bg_outline: Option<LineStyle>,
pub bg_outline: LineStyle,
/// Button frames etc
pub corner_radius: f32,
@ -226,32 +226,32 @@ impl Default for Interacted {
fn default() -> Self {
Self {
active: WidgetVisuals {
bg_fill: Some(Srgba::black_alpha(128)),
bg_outline: Some(LineStyle::new(2.0, WHITE)),
bg_fill: Srgba::black_alpha(128),
bg_outline: LineStyle::new(2.0, WHITE),
corner_radius: 0.0,
main_fill: srgba(120, 120, 200, 255),
stroke_color: WHITE,
stroke_width: 2.0,
},
hovered: WidgetVisuals {
bg_fill: None,
bg_outline: Some(LineStyle::new(1.0, WHITE)),
bg_fill: TRANSPARENT,
bg_outline: LineStyle::new(1.0, WHITE),
corner_radius: 2.0,
main_fill: srgba(100, 100, 150, 255),
stroke_color: Srgba::gray(240),
stroke_width: 1.5,
},
inactive: WidgetVisuals {
bg_fill: None,
bg_outline: Some(LineStyle::new(1.0, Srgba::gray(128))),
bg_fill: TRANSPARENT,
bg_outline: LineStyle::new(1.0, Srgba::gray(128)),
corner_radius: 4.0,
main_fill: srgba(60, 60, 80, 255),
stroke_color: Srgba::gray(200), // Mustn't look grayed out!
stroke_width: 1.0,
},
disabled: WidgetVisuals {
bg_fill: None,
bg_outline: Some(LineStyle::new(0.5, Srgba::gray(128))),
bg_fill: TRANSPARENT,
bg_outline: LineStyle::new(0.5, Srgba::gray(128)),
corner_radius: 4.0,
main_fill: srgba(50, 50, 50, 255),
stroke_color: Srgba::gray(128), // Should look grayed out
@ -361,8 +361,8 @@ impl WidgetVisuals {
stroke_width,
} = self;
let _ = bg_fill; // ui_color(ui, bg_fill, "bg_fill"); // TODO
let _ = bg_outline; // bg_outline.ui(ui, "bg_outline");// TODO
ui_color(ui, bg_fill, "bg_fill");
bg_outline.ui(ui, "bg_outline");
ui.add(Slider::f32(corner_radius, 0.0..=10.0).text("corner_radius"));
ui_color(ui, main_fill, "main_fill");
ui_color(ui, stroke_color, "stroke_color");
@ -417,7 +417,7 @@ impl LineStyle {
let Self { width, color } = self;
ui.horizontal_centered(|ui| {
ui.label(format!("{}: ", text));
ui.add(Slider::f32(width, 0.0..=10.0));
ui.add(Slider::f32(width, 0.0..=10.0).text("width"));
ui_color(ui, color, "color");
});
}

18
egui/src/widgets.rs

@ -233,7 +233,7 @@ impl Button {
text: text.into(),
text_color: None,
text_style: TextStyle::Button,
fill: None,
fill: Default::default(),
sense: Sense::click(),
}
}
@ -289,11 +289,11 @@ impl Widget for Button {
let rect = ui.allocate_space(size);
let response = ui.interact(rect, id, sense);
let text_cursor = response.rect.left_center() + vec2(padding.x, -0.5 * galley.size.y);
let bg_fill = fill.or(ui.style().interact(&response).bg_fill);
let fill = fill.unwrap_or(ui.style().interact(&response).bg_fill);
ui.painter().add(PaintCmd::Rect {
rect: response.rect,
corner_radius: ui.style().interact(&response).corner_radius,
fill: bg_fill,
fill,
outline: ui.style().interact(&response).bg_outline,
});
let stroke_color = ui.style().interact(&response).stroke_color;
@ -372,8 +372,8 @@ impl<'a> Widget for Checkbox<'a> {
pos2(small_icon_rect.right(), small_icon_rect.top()),
]),
closed: false,
outline: Some(LineStyle::new(ui.style().visuals.line_width, stroke_color)),
fill: None,
outline: LineStyle::new(ui.style().visuals.line_width, stroke_color),
fill: Default::default(),
});
}
@ -448,8 +448,8 @@ impl Widget for RadioButton {
painter.add(PaintCmd::Circle {
center: small_icon_rect.center(),
radius: small_icon_rect.width() / 3.0,
fill: Some(stroke_color),
outline: None,
fill: stroke_color,
outline: Default::default(),
});
}
@ -515,10 +515,6 @@ impl Widget for Separator {
let available_space = ui.available_finite().size();
// TODO: only allocate `spacing`, but not our full width/height
// as that would make the false impression that we *need* all that space,
// which would prevent regions from auto-shrinking
let (points, rect) = match ui.layout().dir() {
Direction::Horizontal => {
let rect = ui.allocate_space(vec2(spacing, available_space.y));

10
egui/src/widgets/slider.rs

@ -164,18 +164,18 @@ impl<'a> Slider<'a> {
ui.painter().add(PaintCmd::Rect {
rect: rail_rect,
corner_radius: rail_radius,
fill: Some(ui.style().visuals.background_fill),
outline: Some(LineStyle::new(1.0, Srgba::gray(200))), // TODO
fill: ui.style().visuals.background_fill,
outline: LineStyle::new(1.0, Srgba::gray(200)), // TODO
});
ui.painter().add(PaintCmd::Circle {
center: pos2(marker_center_x, rail_rect.center().y),
radius: handle_radius(rect),
fill: Some(ui.style().interact(response).main_fill),
outline: Some(LineStyle::new(
fill: ui.style().interact(response).main_fill,
outline: LineStyle::new(
ui.style().interact(response).stroke_width,
ui.style().interact(response).stroke_color,
)),
),
});
}
}

2
egui/src/widgets/text_edit.rs

@ -183,7 +183,7 @@ impl<'t> Widget for TextEdit<'t> {
painter.add(PaintCmd::Rect {
rect: bg_rect,
corner_radius: ui.style().interact(&response).corner_radius,
fill: Some(ui.style().visuals.dark_bg_color),
fill: ui.style().visuals.dark_bg_color,
outline: ui.style().interact(&response).bg_outline,
});
}

Loading…
Cancel
Save