Skip to content

Commit

Permalink
Remove trait WebPainter
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Mar 13, 2022
1 parent 9b515ed commit b891c22
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 68 deletions.
16 changes: 4 additions & 12 deletions egui_web/src/backend.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
use crate::*;
use crate::{glow_wrapping::WrappedGlowPainter, *};

use egui::TexturesDelta;
pub use egui::{pos2, Color32};

// ----------------------------------------------------------------------------

fn create_painter(canvas_id: &str) -> Result<Box<dyn WebPainter>, JsValue> {
Ok(Box::new(
crate::glow_wrapping::WrappedGlowPainter::new(canvas_id).map_err(JsValue::from)?,
))
}

// ----------------------------------------------------------------------------

/// Data gathered between frames.
#[derive(Default)]
pub struct WebInput {
Expand Down Expand Up @@ -140,7 +132,7 @@ fn test_parse_query() {
pub struct AppRunner {
pub(crate) frame: epi::Frame,
egui_ctx: egui::Context,
painter: Box<dyn WebPainter>,
painter: WrappedGlowPainter,
pub(crate) input: WebInput,
app: Box<dyn epi::App>,
pub(crate) needs_repaint: std::sync::Arc<NeedRepaint>,
Expand All @@ -154,15 +146,15 @@ pub struct AppRunner {

impl AppRunner {
pub fn new(canvas_id: &str, app: Box<dyn epi::App>) -> Result<Self, JsValue> {
let painter = create_painter(canvas_id)?;
let painter = WrappedGlowPainter::new(canvas_id).map_err(JsValue::from)?;

let prefer_dark_mode = crate::prefer_dark_mode();

let needs_repaint: std::sync::Arc<NeedRepaint> = Default::default();

let frame = epi::Frame::new(epi::backend::FrameData {
info: epi::IntegrationInfo {
name: painter.name(),
name: "egui_web",
web_info: Some(epi::WebInfo {
location: web_location(),
}),
Expand Down
37 changes: 26 additions & 11 deletions egui_web/src/glow_wrapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,29 @@ impl WrappedGlowPainter {
}
}

impl crate::WebPainter for WrappedGlowPainter {
fn name(&self) -> &'static str {
"egui_web"
}

fn max_texture_side(&self) -> usize {
impl WrappedGlowPainter {
pub fn max_texture_side(&self) -> usize {
self.painter.max_texture_side()
}

fn canvas_id(&self) -> &str {
pub fn canvas_id(&self) -> &str {
&self.canvas_id
}

fn set_texture(&mut self, tex_id: egui::TextureId, delta: &egui::epaint::ImageDelta) {
pub fn set_texture(&mut self, tex_id: egui::TextureId, delta: &egui::epaint::ImageDelta) {
self.painter.set_texture(tex_id, delta);
}

fn free_texture(&mut self, tex_id: egui::TextureId) {
pub fn free_texture(&mut self, tex_id: egui::TextureId) {
self.painter.free_texture(tex_id);
}

fn clear(&mut self, clear_color: Rgba) {
pub fn clear(&mut self, clear_color: Rgba) {
let canvas_dimension = [self.canvas.width(), self.canvas.height()];
egui_glow::painter::clear(self.painter.gl(), canvas_dimension, clear_color)
}

fn paint_primitives(
pub fn paint_primitives(
&mut self,
clipped_primitives: Vec<ClippedPrimitive>,
pixels_per_point: f32,
Expand All @@ -67,6 +63,25 @@ impl crate::WebPainter for WrappedGlowPainter {
.paint_primitives(canvas_dimension, pixels_per_point, clipped_primitives);
Ok(())
}

pub fn paint_and_update_textures(
&mut self,
clipped_primitives: Vec<egui::ClippedPrimitive>,
pixels_per_point: f32,
textures_delta: &egui::TexturesDelta,
) -> Result<(), JsValue> {
for (id, image_delta) in &textures_delta.set {
self.set_texture(*id, image_delta);
}

self.paint_primitives(clipped_primitives, pixels_per_point)?;

for &id in &textures_delta.free {
self.free_texture(id);
}

Ok(())
}
}

/// Returns glow context and shader prefix.
Expand Down
2 changes: 0 additions & 2 deletions egui_web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
pub mod backend;
mod glow_wrapping;
mod input;
mod painter;
pub mod screen_reader;
mod text_agent;

Expand All @@ -28,7 +27,6 @@ pub use wasm_bindgen;
pub use web_sys;

use input::*;
pub use painter::WebPainter;
use web_sys::EventTarget;

use std::collections::BTreeMap;
Expand Down
43 changes: 0 additions & 43 deletions egui_web/src/painter.rs

This file was deleted.

0 comments on commit b891c22

Please sign in to comment.