Commit 1ce2e8e3 authored by Florian Blasius's avatar Florian Blasius 🤘

[#157] start backend refactoring.

Initial commit web window crate. Remove common window crate.
parent a3704415
This diff is collapsed.
......@@ -21,21 +21,22 @@ edition = "2018"
name = "orbtk"
path = "src/lib.rs"
[target.wasm32-unknown-unknown.dependencies]
orbtk-web-window = { version = "0.1.0", path = "crates/web-window" }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
orbgl = { git = "https://gitlab.redox-os.org/redox-os/orbgl.git" }
orbfont = { git = "https://gitlab.redox-os.org/redox-os/orbfont.git" }
orbimage = "0.1.17"
orbclient = "0.3.21"
[dependencies]
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" }
orbclient = "0.3.9"
orbfont = { git = "https://gitlab.redox-os.org/redox-os/orbfont.git" }
orbimage = "0.1.9"
# orbgl_api = { path = "../orbgl/orbgl_api" }
orbgl_api = { git = "https://gitlab.redox-os.org/redox-os/orbgl.git" }
# orbgl = { path = "../orbgl/orbgl" }
orbgl = { git = "https://gitlab.redox-os.org/redox-os/orbgl.git" }
# orbgl_shapes = { path = "../orbgl/orbgl_shapes" }
orbgl_shapes = { git = "https://gitlab.redox-os.org/redox-os/orbgl.git" }
orbtk-windows = { version = "0.1.0", path = "crates/windows" }
[workspace]
\ No newline at end of file
CMakeLists.txt not found in /Users/florian.blasius/Projects/Focus-Time/orbtk Select CMakeLists.txt file...
[package]
name = "orbtk-windows"
name = "orbtk-web-window"
version = "0.1.0"
authors = ["Florian Blasius <flovanpt@posteo.de>"]
description = "OrbTk Window Abstraction Layer."
description = "OrbTk window library for wasm."
repository = "https://gitlab.redox-os.org/redox-os/orbtk"
license = "MIT"
keywords = ["windowing"]
keywords = ["windowing", "wasm"]
edition = "2018"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
orbclient = "0.3.9"
[features]
default = []
[dependencies]
stdweb = "0.4.15"
use stdweb::{
self, _js_impl, js,
traits::*,
unstable::TryInto,
web::{
self, document, event,
html_element::{CanvasElement, ImageElement},
window, CanvasRenderingContext2d, FillRule,
},
};
/// Used to initializes the web engine.
///
/// This method must be called first!
pub fn initialize() {
stdweb::initialize();
}
/// Used to build a new web window.
#[derive(Default, Debug)]
pub struct WebWindowBuilder {
title: String,
size: (f64, f64),
}
impl WebWindowBuilder {
/// Creates a new web window builder with default values.
pub fn new() -> Self {
WebWindowBuilder::default()
}
/// Used to set the `title` of the window.
pub fn title(mut self, title: impl Into<String>) -> Self {
self.title = title.into();
self
}
/// Used to set the size of the window.
pub fn size(mut self, width: f64, height: f64) -> Self {
self.size.0 = width;
self.size.1 = height;
self
}
pub fn build(self) -> WebWindow {
document().set_title(&self.title[..]);
let canvas: CanvasElement = document()
.create_element("canvas")
.unwrap()
.try_into()
.unwrap();
canvas.set_width(self.size.0 as u32);
canvas.set_height(self.size.1 as u32);
stdweb::event_loop();
WebWindow {
title: self.title,
size: self.size,
canvas,
}
}
}
/// Web implementation of a window. Uses internal a canvas as window.
pub struct WebWindow {
title: String,
canvas: CanvasElement,
size: (f64, f64),
}
\ No newline at end of file
/// Used to identify a specific key by code.
#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)]
pub enum KeyCode {
// Numeric keys
Key1,
Key2,
Key3,
Key4,
Key5,
Key6,
Key7,
Key8,
Key9,
Key0,
// Letter keys
A,
B,
C,
D,
E,
F,
G,
H,
I,
J,
K,
L,
M,
N,
O,
P,
Q,
R,
S,
T,
U,
V,
W,
X,
Y,
Z,
// Function keys.
F1,
F2,
F3,
F4,
F5,
F6,
F7,
F8,
F9,
F10,
F11,
F12,
F13,
F14,
F15,
F16,
F17,
F18,
F19,
F20,
F21,
F22,
F23,
F24,
// Actions.
Escape,
Snapshot,
Scroll,
Pause,
Insert,
Home,
Delete,
End,
PageDown,
PageUp,
Back,
Return,
Space,
Compose,
Caret,
// Arrow keys.
Left,
Up,
Right,
Down,
// NumPad keys.
Numlock,
Numpad0,
Numpad1,
Numpad2,
Numpad3,
Numpad4,
Numpad5,
Numpad6,
Numpad7,
Numpad8,
Numpad9,
// Additional keys.
AbntC1,
AbntC2,
Add,
Apostrophe,
Apps,
At,
Ax,
Backslash,
Calculator,
Capital,
Colon,
Comma,
Convert,
Decimal,
Divide,
Equals,
Grave,
Kana,
Kanji,
LAlt,
LBracket,
LControl,
LShift,
LWin,
Mail,
MediaSelect,
MediaStop,
Minus,
Multiply,
Mute,
MyComputer,
NavigateForward,
NavigateBackward,
NextTrack,
NoConvert,
NumpadComma,
NumpadEnter,
NumpadEquals,
OEM102,
Period,
PlayPause,
Power,
PrevTrack,
RAlt,
RBracket,
RControl,
RShift,
RWin,
Semicolon,
Slash,
Sleep,
Stop,
Subtract,
Sysrq,
Tab,
Underline,
Unlabeled,
VolumeDown,
VolumeUp,
Wake,
WebBack,
WebFavorites,
WebForward,
WebHome,
WebRefresh,
WebSearch,
WebStop,
Yen,
Copy,
Paste,
Cut,
}
/// Represents keyboard modifier keys.
#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)]
pub struct Modifier {
pub shift: bool,
pub ctrl: bool,
pub alt: bool,
pub command: bool
}
/// Represents the current state of a key.
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
pub enum KeyState {
Pressed,
Released
}
/// Keyboard key scan code. Depends on hardware.
pub type ScanCode = u32;
/// Describes a keyboard input event.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct KeyEvent {
/// Identifies the physical key pressed.
pub scan_code: ScanCode,
pub state: KeyState,
/// Represents the current code of the key.
pub key_code: Option<KeyCode>,
/// Represents the active keyboard modifier state.
pub modifiers: Modifier
}
\ No newline at end of file
//! This module contains all resources to handles events.
//!
pub use self::key::*;
pub use self::mouse::*;
pub use self::window::*;
mod key;
mod mouse;
mod window;
/// Describes a event form a window.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Event {
KeyEvent,
MouseEvent,
WindowEvent
}
\ No newline at end of file
/// Represents the current state of a mouse button.
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
pub enum ButtonState {
Pressed,
Released
}
/// Represents a mouse button.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MouseButton {
Left,
Middle,
Right,
Other
}
/// Represents a mouse button event.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MouseEvent {
pub button: MouseButton,
pub state: ButtonState
}
\ No newline at end of file
/// Represents a mouse button event.
pub enum WindowEvent {
Resized((f64, f64)),
Closed,
}
\ No newline at end of file
pub mod window;
pub mod events;
\ No newline at end of file
use orbclient::{Window as OrbWindow, EventOption, Renderer, WindowFlag};
use crate::events::*;
/// Window implementation for OrbClient.
pub struct Window {
inner: OrbWindow,
}
impl super::Window for Window {
fn set_title(&mut self, title: impl Into<String>) {
self.inner.set_title(&title.into());
}
fn title(&self) -> String {
self.inner.title()
}
fn set_position(&mut self, x: f64, y: f64) {
self.inner.set_pos(x as i32, y as i32);
}
fn position(&self) -> (f64, f64) {
(self.inner.x() as f64, self.inner.y() as f64)
}
fn set_size(&mut self, width: f64, height: f64) {
self.inner.set_size(width as u32, height as u32);
}
fn size(&self) -> (f64, f64) {
(self.inner.width() as f64, self.inner.height() as f64)
}
fn sync(&mut self) {
self.inner.sync();
}
fn events(&mut self) -> Vec<Event> {
let mut events: Vec<Event> = vec![];
for event in self.inner.events() {
match event.to_option() {
// EventOption::Quit(_) => events.push(Event::WindowEvent::Close),
_ => {}
}
}
events
}
}
impl Drop for Window {
fn drop(&mut self) {
self.inner.sync();
}
}
impl From<super::WindowBuilder> for Window {
fn from(builder: super::WindowBuilder) -> Self {
let mut flags = vec![];
if builder.resizable {
flags.push(WindowFlag::Resizable);
}
Window {
inner: OrbWindow::new_flags(
builder.position.0 as i32,
builder.position.1 as i32,
builder.size.0 as u32,
builder.size.1 as u32,
&builder.title,
&flags,
).unwrap()
}
}
}
//! This module contains all resources to create and use a window.
use super::events::*;
#[cfg(not(target_arch = "wasm32"))]
#[path = "impl_orbclient.rs"]
mod implementation;
/// Used to define a window.
pub trait Window {
/// Creates a new `WindowBuilder` with default values.
fn create() -> WindowBuilder {
WindowBuilder::new()
}
/// Sets the title.
fn set_title(&mut self, title: impl Into<String>);
/// Gets the title.
fn title(&self) -> String;
/// Sets the position.
fn set_position(&mut self, x: f64, y: f64);
/// Gets the position.
fn position(&self) -> (f64, f64);
/// Sets the size.
fn set_size(&mut self, width: f64, height: f64);
/// Gets the size.
fn size(&self) -> (f64, f64);
/// Swaps the buffers.
fn sync(&mut self);
/// Request current window events.
fn events(&mut self) -> Vec<Event>;
}
/// Used to build a window, specifying additional details.
#[derive(Default, Debug, PartialEq)]
pub struct WindowBuilder {
title: String,
size: (f64, f64),
position: (f64, f64),
resizable: bool,
}
impl WindowBuilder {
/// Creates a new border builder with default values.
pub fn new() -> WindowBuilder {
WindowBuilder::default()
}
pub fn title(mut self, title: impl Into<String>) -> Self {
self.title = title.into();
self
}
/// Inserts the window bounds.
pub fn bounds(mut self, bounds: impl Into<(f64, f64, f64, f64)>) -> Self {
let bounds = bounds.into();
self.position = (bounds.0, bounds.1);
self.size = (bounds.2, bounds.3);
self
}
/// Inserts a position.
pub fn position(mut self, position: impl Into<(f64, f64)>) -> Self {
self.position = position.into();
self
}
/// Inserts a size.
pub fn size(mut self, size: impl Into<(f64, f64)>) -> Self {
self.size = size.into();
self
}
/// Inserts a value that indicates wether the window is resizable.
pub fn resizable(mut self, resizable: impl Into<bool>) -> Self {
self.resizable = resizable.into();
self
}
}
\ No newline at end of file
......@@ -85,8 +85,6 @@ pub trait Renderer {
/// This trait is used to define a backend for OrbTk.
pub trait Backend {
fn drain_events(&mut self);
fn bounds(&mut self, bounds: &Bounds);
fn size(&self) -> (u32, u32);
fn init_context(&mut self) -> InitContext<'_>;
fn render_context(&mut self) -> RenderContext<'_>;
fn layout_context(&mut self) -> LayoutContext<'_>;
......
......@@ -2,7 +2,7 @@ use std::cell::{Cell, RefCell};
use std::collections::BTreeMap;
use std::rc::Rc;
use orbclient::{self, Color, Mode, Renderer as OrbRenderer, Window as OrbWindow};
use orbclient::{self, Renderer as OrbRenderer, Window as OrbWindow};
use dces::prelude::{Entity, World};
use orbgl::prelude::{CairoRenderEngine, FramebufferSurface};
......@@ -16,8 +16,8 @@ use crate::event::{
EventQueue, Key, KeyDownEvent, KeyUpEvent, MouseButton, MouseDownEvent, MouseUpEvent,
SystemEvent, WindowEvent,
};
use crate::properties::Bounds;
use crate::structs::{Point, Position, Size};
use crate::structs::Point;
use crate::theme::Theme;
use crate::widgets::MessageBox;
......@@ -58,42 +58,6 @@ impl OrbitalBackend {
}
}
impl OrbRenderer for OrbitalBackend {
fn width(&self) -> u32 {
self.inner.width()
}
fn height(&self) -> u32 {
self.inner.height()
}
fn data(&self) -> &[Color] {
self.inner.data()
}
fn data_mut(&mut self) -> &mut [Color] {
self.inner.data_mut()
}
fn sync(&mut self) -> bool {
self.inner.sync()
}
fn mode(&self) -> &Cell<Mode> {
&self.inner.mode()
}
fn char(&mut self, x: i32, y: i32, c: char, color: Color) {
// if let Some(ref font) = self.font {
// let mut buf = [0; 4];
// font.render(&c.encode_utf8(&mut buf), 16.0)
// .draw(&mut self.inner, x, y, color)
// } else {
self.inner.char(x, y, c, color);
// }
}
}
impl Drop for OrbitalBackend {
fn drop(&mut self) {
self.inner.sync();
......@@ -198,16 +162,6 @@ impl Backend for OrbitalBackend {
}
}
fn size(&self) -> (u32, u32) {
(self.width(), self.height())
}
fn bounds(&mut self, bounds: &Bounds) {
self.inner.set_pos(bounds.x() as i32, bounds.y() as i32);
self.inner
.set_size(bounds.width() as u32, bounds.height() as u32);
}
fn init_context(&mut self) -> InitContext<'_> {
InitContext { theme: &self.theme }
}
......@@ -223,7 +177,7 @@ impl Backend for OrbitalBackend {
fn layout_context(&mut self) -> LayoutContext<'_> {
LayoutContext {
window_size: self.size(),
window_size: (self.inner.width(), self.inner.height()),
theme: &self.theme,
}
}
......
......@@ -11,7 +11,7 @@ use crate::{
backend::Backend,
properties::*,
render_object::RenderObject,
structs::{Point, Position},
structs::{Point, Position, Brush, Size},
widgets::Context,
};
......@@ -85,26 +85,20 @@ impl System<Tree> for RenderSystem {
}
// render debug border for each widget
// if self.debug_flag.get() {
// if let Ok(bounds) = ecm.borrow_component::<Bounds>(node) {
// if let Some(parent) = tree.parent[&node] {
// if let Ok(parent_bounds) = ecm.borrow_component::<Bounds>(parent) {
// let selector = Selector::from("debugborder");
// render_context.renderer.render_rectangle(
// bounds,
// parent_bounds,
// &global_position,
// render_context.theme.uint("border-radius", &selector),
// render_context.theme.brush("background", &selector).into(),
// render_context.theme.uint("border-width", &selector),
// render_context.theme.brush("border-color", &selector).into(),
// render_context.theme.float("opcaity", &selector),
// );
// }
// }
// }
// }
if self.debug_flag.get() {
if let Ok(bounds) = ecm.borrow_component::<Bounds>(node) {
let selector = Selector::from("debug-border");
let brush = render_context.theme.brush("border-color", &selector).unwrap();
match brush {
Brush::SolidColor(color) => render_context.canvas.set_stroke_style(color),
_ => {} // todo: gradient
}
render_context.canvas.stroke_rect(global_position.x + bounds.x(), global_position.y + bounds.y(), bounds.width(), bounds.height());
}
}
let mut global_pos = (0.0, 0.0);
......
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