Commit e6f79581 authored by Florian Blasius's avatar Florian Blasius 🤘

[#157] base setup web backend.

parent 618acef3
......@@ -468,7 +468,6 @@ dependencies = [
name = "orbtk"
version = "0.3.0"
dependencies = [
"cssparser 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)",
"dces 0.1.6 (git+https://gitlab.redox-os.org/redox-os/dces-rust.git)",
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"orbclient 0.3.21 (registry+https://github.com/rust-lang/crates.io-index)",
......
......@@ -33,7 +33,6 @@ orbclient = "0.3.21"
[dependencies]
orbtk-structs = { version = "0.1.0", path = "crates/structs" }
orbtk-css-engine = { version = "0.1.0", path = "crates/css-engine" }
cssparser = "0.17.0"
lazy_static = "1.3.0"
# dces = { path = "../dces-rust"}
dces = { git = "https://gitlab.redox-os.org/redox-os/dces-rust.git" }
......@@ -41,4 +40,9 @@ dces = { git = "https://gitlab.redox-os.org/redox-os/dces-rust.git" }
orbgl_api = { git = "https://gitlab.redox-os.org/redox-os/orbgl.git" }
# orbgl = { path = "../orbgl/orbgl" }
[workspace]
\ No newline at end of file
[workspace]
members = [
"crates/css-engine",
"crates/structs",
"crates/web-window"
]
\ No newline at end of file
CMakeLists.txt not found in /Users/florian.blasius/Projects/Focus-Time/orbtk Select CMakeLists.txt file...
......@@ -114,3 +114,4 @@ impl Size for WebWindow {
}
}
pub mod prelude;
\ No newline at end of file
pub use crate::*;
\ No newline at end of file
......@@ -103,5 +103,10 @@ pub trait FontMeasure {
pub use self::target::target_backend;
pub use self::target::FONT_MEASURE;
#[cfg(target_arch = "wasm32")]
#[path = "web/mod.rs"]
mod target;
#[cfg(not(target_arch = "wasm32"))]
#[path = "orbital/mod.rs"]
mod target;
......@@ -8,7 +8,6 @@ use dces::prelude::{Entity, World};
use orbgl::prelude::{CairoRenderEngine, FramebufferSurface};
use orbgl_api::Canvas;
use crate::prelude::*;
use crate::backend::*;
/// Implementation of the OrbClient based backend.
......@@ -197,6 +196,7 @@ impl BackendRunner for OrbitalBackendRunner {
fn world(&mut self, world: World<Tree>) {
self.world = Some(world);
}
fn run(&mut self, update: Rc<Cell<bool>>, running: Rc<Cell<bool>>) {
loop {
if !running.get() {
......
......@@ -8,7 +8,6 @@ use orbclient::{Window as OrbWindow, WindowFlag};
use orbfont::Font;
use self::backend::{OrbitalBackend, OrbitalBackendRunner};
use crate::prelude::*;
use crate::backend::*;
mod backend;
......
use std::cell::{Cell, RefCell};
use std::collections::BTreeMap;
use std::rc::Rc;
use dces::prelude::{Entity, World};
use orbgl_web::prelude::*;
use orbgl_api::prelude::*;
use orbtk_web_window::prelude::*;
use crate::prelude::*;
use crate::backend::*;
// todo will be removed after orbgl supports text rendering
pub struct DummyRenderer;
impl Renderer for DummyRenderer {
fn render(&mut self, background: Color) {}
fn render_rectangle(&mut self, bounds: &Bounds, parent_bounds: &Bounds, global_position: &Point, border_radius: u32, background: Color, border_width: u32, border_color: Color, opacity: f32) {}
fn render_text(&mut self, text: &str, bounds: &Bounds, parent_bounds: &Bounds, global_position: &Point, font_size: u32, color: Color, font: &Font) {}
fn render_image(&mut self, image: &[Color], bounds: &Bounds, parent_bounds: &Bounds, global_position: &Point) {}
}
/// Implementation of the OrbClient based backend.
pub struct WebBackend {
inner: WebWindow,
theme: Theme,
event_queue: RefCell<EventQueue>,
messages: RefCell<BTreeMap<Entity, Vec<MessageBox>>>,
canvas: Canvas,
}
impl WebBackend {
pub fn new(theme: Theme, inner: WebWindow) -> WebBackend {
let mut inner = inner;
let surface = WebSurface::new(inner.width() as u32, inner.height() as u32, canvas.get_context().unwrap());
let render_engine = WebRenderEngine::new(surface);
let canvas = Canvas::new(render_engine.clone());
WebBackend {
inner,
theme,
event_queue: RefCell::new(EventQueue::default()),
messages: RefCell::new(BTreeMap::new()),
canvas,
}
}
}
impl Drop for WebBackend {
fn drop(&mut self) {
self.inner.sync();
}
}
impl Backend for WebBackend {
fn drain_events(&mut self) {}
fn init_context(&mut self) -> InitContext<'_> {
InitContext { theme: &self.theme }
}
fn render_context(&mut self) -> RenderContext<'_> {
RenderContext {
canvas: &mut self.canvas,
renderer: &mut self.inner,
theme: &self.theme,
event_queue: &self.event_queue,
}
}
fn layout_context(&mut self) -> LayoutContext<'_> {
LayoutContext {
window_size: (self.inner.width() as u32, self.inner.height() as u32),
theme: &self.theme,
}
}
fn event_context(&mut self) -> EventContext<'_> {
EventContext {
event_queue: &self.event_queue,
}
}
fn state_context(&mut self) -> StateContext<'_> {
StateContext {
theme: &self.theme,
event_queue: &self.event_queue,
messages: &mut self.messages,
}
}
}
/// Implementation of the Web based backend runner.
pub struct WebBackendRunner {
pub world: Option<World<Tree>>,
pub backend: Rc<RefCell<WebBackend>>,
}
impl BackendRunner for WebBackendRunner {
fn world(&mut self, world: World<Tree>) {
self.world = Some(world);
}
fn run(&mut self, update: Rc<Cell<bool>>, running: Rc<Cell<bool>>) {
window().request_animation_frame(|| {
if !running.get() {
break;
}
if let Some(world) = &mut self.world {
world.run();
}
update.set(false);
self.backend.borrow_mut().drain_events();
});
}
}
pub fn target_backend(
title: &str,
bounds: Bounds,
resizable: bool,
theme: Theme,
) -> (Box<WebBackendRunner>, Rc<RefCell<dyn Backend>>) {
let mut flags = vec![];
let backend = Rc::new(RefCell::new(WebBackend::new(
theme,
WebWindow::create().title(title).size(bounds.x(), bounds.y()).build(),
)));
let backend_runner = Box::new(WebBackendRunner {
backend: backend.clone(),
world: None,
});
(backend_runner, backend)
}
pub struct DummyFontMeasure;
impl FontMeasure for DummyFontMeasure {
fn measure(&self, text: &str, font: &Font, font_size: u32) -> (u32, u32) {
if font_size == 0 {
return (0, 0);
}
let text = font.render(text, font_size as f32);
(text.width(), text.height())
}
}
lazy_static! {
pub static ref FONT_MEASURE: Arc<DummyFontMeasure> = { Arc::new(DummyFontMeasure) };
}
\ No newline at end of file
......@@ -7,7 +7,7 @@ use std::{
use dces::prelude::{Entity, EntityComponentManager};
use crate::prelude::*;
use crate::backend::{FontMeasure, FONT_MEASURE};
use crate::backend::FontMeasure;
use super::Layout;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment