Added App::clear_color() that apps can use to specify background color

This commit is contained in:
Emil Ernerfeldt 2020-12-18 22:34:48 +01:00
parent 9ea8d907fd
commit c3c4f28a9d
6 changed files with 33 additions and 11 deletions

View file

@ -25,7 +25,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
* `ui.image` now takes `impl Into<Vec2>` as a `size` argument. * `ui.image` now takes `impl Into<Vec2>` as a `size` argument.
* Made some more fields of `RawInput` optional. * Made some more fields of `RawInput` optional.
* `Slider` and `DragValue` uses fewer decimals by default. See the full precision by hovering over the value. * `Slider` and `DragValue` uses fewer decimals by default. See the full precision by hovering over the value.
* `egui::App`: added `fn name(&self)` * `egui::App`: added `fn name(&self)` and `fn clear_color(&self)`
### Deprecated ### Deprecated
* Deprecated `RawInput::screen_size` - use `RawInput::screen_rect` instead. * Deprecated `RawInput::screen_size` - use `RawInput::screen_rect` instead.

View file

@ -14,6 +14,11 @@ use crate::Context;
pub trait App { pub trait App {
/// The name of your App. /// The name of your App.
fn name(&self) -> &str; fn name(&self) -> &str;
/// Background color for the app.
/// e.g. what is sent to `gl.clearColor`
fn clear_color(&self) -> crate::Rgba {
crate::Srgba::from_rgb(16, 16, 16).into()
} }
/// Called once before the first frame. /// Called once before the first frame.

View file

@ -111,7 +111,13 @@ pub fn run(mut storage: Box<dyn egui::app::Storage>, mut app: Box<dyn App>) -> !
let frame_time = (Instant::now() - egui_start).as_secs_f64() as f32; let frame_time = (Instant::now() - egui_start).as_secs_f64() as f32;
previous_frame_time = Some(frame_time); previous_frame_time = Some(frame_time);
painter.paint_jobs(&display, ctx.pixels_per_point(), paint_jobs, &ctx.texture()); painter.paint_jobs(
&display,
ctx.pixels_per_point(),
app.clear_color(),
paint_jobs,
&ctx.texture(),
);
{ {
let egui::app::AppOutput { let egui::app::AppOutput {

View file

@ -126,6 +126,7 @@ impl Painter {
&mut self, &mut self,
display: &glium::Display, display: &glium::Display,
pixels_per_point: f32, pixels_per_point: f32,
clear_color: egui::Rgba,
jobs: PaintJobs, jobs: PaintJobs,
egui_texture: &egui::Texture, egui_texture: &egui::Texture,
) { ) {
@ -133,7 +134,13 @@ impl Painter {
self.upload_pending_user_textures(display); self.upload_pending_user_textures(display);
let mut target = display.draw(); let mut target = display.draw();
target.clear_color(0.0, 0.0, 0.0, 0.0); // Verified to be gamma-correct.
target.clear_color(
clear_color[0],
clear_color[1],
clear_color[2],
clear_color[3],
);
for (clip_rect, triangles) in jobs { for (clip_rect, triangles) in jobs {
self.paint_job( self.paint_job(
&mut target, &mut target,

View file

@ -55,10 +55,13 @@ impl WebBackend {
Ok((output, paint_jobs)) Ok((output, paint_jobs))
} }
pub fn paint(&mut self, paint_jobs: egui::PaintJobs) -> Result<(), JsValue> { pub fn paint(
let bg_color = egui::color::TRANSPARENT; // Use background css color. &mut self,
clear_color: egui::Rgba,
paint_jobs: egui::PaintJobs,
) -> Result<(), JsValue> {
self.painter.paint_jobs( self.painter.paint_jobs(
bg_color, clear_color,
paint_jobs, paint_jobs,
&self.ctx.texture(), &self.ctx.texture(),
self.ctx.pixels_per_point(), self.ctx.pixels_per_point(),
@ -214,7 +217,7 @@ impl AppRunner {
} }
pub fn paint(&mut self, paint_jobs: egui::PaintJobs) -> Result<(), JsValue> { pub fn paint(&mut self, paint_jobs: egui::PaintJobs) -> Result<(), JsValue> {
self.web_backend.paint(paint_jobs) self.web_backend.paint(self.app.clear_color(), paint_jobs)
} }
} }

View file

@ -328,11 +328,12 @@ impl Painter {
self.canvas.width() as i32, self.canvas.width() as i32,
self.canvas.height() as i32, self.canvas.height() as i32,
); );
let clear_color: Srgba = clear_color.into();
gl.clear_color( gl.clear_color(
clear_color[0], clear_color[0] as f32 / 255.0,
clear_color[1], clear_color[1] as f32 / 255.0,
clear_color[2], clear_color[2] as f32 / 255.0,
clear_color[3], clear_color[3] as f32 / 255.0,
); );
gl.clear(Gl::COLOR_BUFFER_BIT); gl.clear(Gl::COLOR_BUFFER_BIT);