Skip to content

Commit 0021580

Browse files
authored
Add Frame::canvas - bright in bright mode, dark in dark mode (#1362)
and use it in the demo app
1 parent 29c52e8 commit 0021580

File tree

6 files changed

+42
-15
lines changed

6 files changed

+42
-15
lines changed

CHANGELOG.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w
55

66

77
## Unreleased
8-
* Fixed ComboBoxes always being rendered left-aligned ([1304](https://github.com/emilk/egui/pull/1304))
8+
9+
### Added ⭐
10+
* Added `Frame::canvas` ([1362](https://github.com/emilk/egui/pull/1362)).
11+
12+
### Changed 🔧
13+
14+
### Fixed 🐛
15+
* Fixed ComboBoxes always being rendered left-aligned ([1304](https://github.com/emilk/egui/pull/1304)).
16+
917

1018
## 0.17.0 - 2022-02-22 - Improved font selection and image handling
1119

egui/src/containers/frame.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,27 @@ impl Frame {
8080
}
8181
}
8282

83-
/// dark canvas to draw on
84-
pub fn dark_canvas(style: &Style) -> Self {
83+
/// A canvas to draw on.
84+
///
85+
/// In bright mode this will be very bright,
86+
/// and in dark mode this will be very dark.
87+
pub fn canvas(style: &Style) -> Self {
8588
Self {
8689
margin: Margin::symmetric(10.0, 10.0),
8790
rounding: style.visuals.widgets.noninteractive.rounding,
88-
fill: Color32::from_black_alpha(250),
91+
fill: style.visuals.extreme_bg_color,
8992
stroke: style.visuals.window_stroke(),
9093
..Default::default()
9194
}
9295
}
96+
97+
/// A dark canvas to draw on.
98+
pub fn dark_canvas(style: &Style) -> Self {
99+
Self {
100+
fill: Color32::from_black_alpha(250),
101+
..Self::canvas(style)
102+
}
103+
}
93104
}
94105

95106
impl Frame {

egui_demo_lib/src/apps/demo/dancing_strings.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ impl super::Demo for DancingStrings {
2222

2323
impl super::View for DancingStrings {
2424
fn ui(&mut self, ui: &mut Ui) {
25-
Frame::dark_canvas(ui.style()).show(ui, |ui| {
25+
let color = if ui.visuals().dark_mode {
26+
Color32::from_additive_luminance(196)
27+
} else {
28+
Color32::from_black_alpha(240)
29+
};
30+
31+
Frame::canvas(ui.style()).show(ui, |ui| {
2632
ui.ctx().request_repaint();
2733
let time = ui.input().time;
2834

@@ -49,10 +55,7 @@ impl super::View for DancingStrings {
4955
.collect();
5056

5157
let thickness = 10.0 / mode as f32;
52-
shapes.push(epaint::Shape::line(
53-
points,
54-
Stroke::new(thickness, Color32::from_additive_luminance(196)),
55-
));
58+
shapes.push(epaint::Shape::line(points, Stroke::new(thickness, color)));
5659
}
5760

5861
ui.painter().extend(shapes);

egui_demo_lib/src/apps/demo/multi_touch.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ impl super::View for MultiTouch {
5252
let num_touches = ui.input().multi_touch().map_or(0, |mt| mt.num_touches);
5353
ui.label(format!("Current touches: {}", num_touches));
5454

55-
Frame::dark_canvas(ui.style()).show(ui, |ui| {
55+
let color = if ui.visuals().dark_mode {
56+
Color32::WHITE
57+
} else {
58+
Color32::BLACK
59+
};
60+
61+
Frame::canvas(ui.style()).show(ui, |ui| {
5662
// Note that we use `Sense::drag()` although we do not use any pointer events. With
5763
// the current implementation, the fact that a touch event of two or more fingers is
5864
// recognized, does not mean that the pointer events are suppressed, which are always
@@ -76,7 +82,6 @@ impl super::View for MultiTouch {
7682
// check for touch input (or the lack thereof) and update zoom and scale factors, plus
7783
// color and width:
7884
let mut stroke_width = 1.;
79-
let color = Color32::GRAY;
8085
if let Some(multi_touch) = ui.ctx().multi_touch() {
8186
// This adjusts the current zoom factor and rotation angle according to the dynamic
8287
// change (for the current frame) of the touch gesture:

egui_demo_lib/src/apps/demo/paint_bezier.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl Default for PaintBezier {
3131
pos2(200.0, 200.0),
3232
pos2(250.0, 50.0),
3333
],
34-
stroke: Stroke::new(1.0, Color32::LIGHT_BLUE),
34+
stroke: Stroke::new(1.0, Color32::from_rgb(25, 200, 100)),
3535
fill: Color32::from_rgb(50, 100, 150).linear_multiply(0.25),
3636
aux_stroke: Stroke::new(1.0, Color32::RED.linear_multiply(0.25)),
3737
bounding_box_stroke: Stroke::new(0.0, Color32::LIGHT_GREEN.linear_multiply(0.25)),
@@ -164,7 +164,7 @@ impl super::View for PaintBezier {
164164
});
165165
self.ui_control(ui);
166166

167-
Frame::dark_canvas(ui.style()).show(ui, |ui| {
167+
Frame::canvas(ui.style()).show(ui, |ui| {
168168
self.ui_content(ui);
169169
});
170170
}

egui_demo_lib/src/apps/demo/painting.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ impl Default for Painting {
1212
fn default() -> Self {
1313
Self {
1414
lines: Default::default(),
15-
stroke: Stroke::new(1.0, Color32::LIGHT_BLUE),
15+
stroke: Stroke::new(1.0, Color32::from_rgb(25, 200, 100)),
1616
}
1717
}
1818
}
@@ -91,7 +91,7 @@ impl super::View for Painting {
9191
});
9292
self.ui_control(ui);
9393
ui.label("Paint with your mouse/touch!");
94-
Frame::dark_canvas(ui.style()).show(ui, |ui| {
94+
Frame::canvas(ui.style()).show(ui, |ui| {
9595
self.ui_content(ui);
9696
});
9797
}

0 commit comments

Comments
 (0)