Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,22 @@ jobs:
run: scripts/build-ohos-example.sh
env:
OHOS_SDK_HOME: ${{ steps.setup-ohos-sdk.outputs.ohos-base-sdk-home }}

ci-wasm:
name: Wasm
runs-on: windows-2022
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2
- uses: mymindstorm/setup-emsdk@v14
with:
version: 4.0.9
actions-cache-folder: emsdk-cache
- name: Setup Rust
uses: dtolnay/rust-toolchain@stable
- name: Install
run: |
rustup target add wasm32-unknown-emscripten
- name: Build
run: cd examples/gallery && cargo build --target wasm32-unknown-emscripten --release
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ edition = "2021"
[dependencies]
deft-macros = { path = "packages/deft-macros", version = "0.3.1" }

quick-js = { package = "deft-quick-js", version = "0.6.1"}
quick-js = { package = "deft-quick-js", version = "0.7.0"}
yoga = { package = "deft-yoga", version = "0.7.0" }
winit = { package = "deft-winit", version = "0.32.1", features = ["android-native-activity", "rwh_06"] }
skia-window = { path = "skia-window", version = "0.8.0"}
Expand Down Expand Up @@ -102,9 +102,9 @@ cfg_aliases = "0.2.1"
[target.'cfg(target_env = "ohos")'.build-dependencies]
napi-build-ohos = "1.0.4"

[profile.release]
lto = true
opt-level = "z"
#[profile.release]
#lto = true
#opt-level = "z"

#[lib]
#name = "deft"
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,15 @@ apt install build-essential libssl-dev libclang-dev libc++-dev \

Native:
```
cargo run --example gallery
cd examples/gallery
cargo run
```

Wasm:
> Make sure that [Emscripten SDK](https://emscripten.org/docs/getting_started/downloads.html) is installed.
```bash
./scripts/build-wasm.sh --release
cd examples/gallery
cargo build --target wasm32-unknown-emscripten --release
python3 -m http.server
```
Visit `http://localhost:8000/web/` to preview.
Expand Down
14 changes: 9 additions & 5 deletions examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use deft::app::{App, IApp};
use deft::bootstrap;
use deft::js::js_engine::JsEngine;
use quick_js::loader::{FsJsModuleLoader, JsModuleLoader};
use quick_js::loader::JsModuleLoader;

struct AppImpl {}

Expand All @@ -21,23 +21,25 @@ impl IApp for AppImpl {
fn create_module_loader(&mut self) -> Box<dyn JsModuleLoader + Send + Sync + 'static> {
use deft::loader::StaticModuleLoader;
let mut ml = StaticModuleLoader::new();
ml.add_module("index.js".to_string(), include_str!("./demo-js/index.js").to_owned());
ml.add_module(
"index.js".to_string(),
include_str!("./demo-js/index.js").to_owned(),
);
Box::new(ml)
}
}

fn bootstrap_app() {
env_logger::init();
let app = App::new(AppImpl {});
bootstrap(app);
}

#[cfg(not(target_arch = "wasm32"))]
fn main() {
env_logger::init();
bootstrap_app();
}


#[cfg(target_os = "emscripten")]
pub fn main() {
// Do nothing
Expand All @@ -46,5 +48,7 @@ pub fn main() {
#[cfg(target_os = "emscripten")]
#[no_mangle]
pub extern "C" fn asm_main() {
use deft::log::SimpleLogger;
SimpleLogger::init_with_max_level(log::LevelFilter::Info);
bootstrap_app();
}
}
12 changes: 12 additions & 0 deletions examples/gallery/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "gallery"
version = "0.1.0"
edition = "2024"

[dependencies]
deft = { path = "../..", default-features = false}
env_logger = "0.11.8"
log = "0.4.27"

[build-dependencies]
cfg_aliases = "0.2.1"
17 changes: 17 additions & 0 deletions examples/gallery/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use cfg_aliases::cfg_aliases;
fn main() {
println!("cargo:rustc-env=EMCC_CFLAGS=-s MAX_WEBGL_VERSION=2 -s MODULARIZE=1 -s EXPORT_NAME=loadDeftApp -s EXPORTED_RUNTIME_METHODS=GL,cwrap");
println!("cargo:rerun-if-changed=build.rs");

cfg_aliases! {
// Systems.
web_platform: { all(target_family = "wasm", target_os = "unknown") },
emscripten_platform: { all(target_family = "wasm", target_os = "emscripten") },
macos_platform: { target_os = "macos" },
windows_platform: { target_os = "windows" },
linux_platform: { all(target_os = "linux", not(ohos)) },
desktop_platform: { any(windows_platform, linux_platform, macos_platform) },
ohos: { target_env = "ohos" },

}
}
File renamed without changes
File renamed without changes.
16 changes: 10 additions & 6 deletions examples/gallery.rs → examples/gallery/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
use deft::app::{App, IApp};
use deft::bootstrap;
use deft::js::js_engine::JsEngine;
use quick_js::loader::JsModuleLoader;
use deft::resource::Resource;
use deft::js::loader::JsModuleLoader;

struct AppImpl {}

Expand All @@ -14,7 +14,7 @@ impl IApp for AppImpl {

#[cfg(desktop_platform)]
fn create_module_loader(&mut self) -> Box<dyn JsModuleLoader + Send + Sync + 'static> {
use quick_js::loader::FsJsModuleLoader;
use deft::js::loader::FsJsModuleLoader;
let ml = FsJsModuleLoader::new("examples/gallery-js");
Box::new(ml)
}
Expand All @@ -23,7 +23,10 @@ impl IApp for AppImpl {
fn create_module_loader(&mut self) -> Box<dyn JsModuleLoader + Send + Sync + 'static> {
use deft::loader::StaticModuleLoader;
let mut ml = StaticModuleLoader::new();
ml.add_module("index.js".to_string(), include_str!("./gallery-js/index.js").to_owned());
ml.add_module(
"index.js".to_string(),
include_str!("./gallery-js/index.js").to_owned(),
);
Box::new(ml)
}
}
Expand All @@ -43,14 +46,15 @@ fn main() {
bootstrap_app();
}


#[cfg(target_os = "emscripten")]
pub fn main() {
// Do nothing
}

#[cfg(target_os = "emscripten")]
#[no_mangle]
#[unsafe(no_mangle)]
pub extern "C" fn asm_main() {
use deft::log::SimpleLogger;
SimpleLogger::init_with_max_level(log::LevelFilter::Info);
bootstrap_app();
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions scripts/build-wasm.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@echo off
cargo build --target wasm32-unknown-emscripten --no-default-features
3 changes: 1 addition & 2 deletions scripts/build-wasm.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#!/bin/bash
export EMCC_CFLAGS="-s MAX_WEBGL_VERSION=2 -s MODULARIZE=1 -s EXPORT_NAME=loadDeftApp -s EXPORTED_RUNTIME_METHODS=GL,cwrap"
cargo build --target wasm32-unknown-emscripten --no-default-features --example gallery $@
cargo build --target wasm32-unknown-emscripten --no-default-features $@
7 changes: 4 additions & 3 deletions src/element/common/image_object.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::element::common::svg_object::SvgObject;
use crate::img_manager::{dyn_image_to_skia_image, IMG_MANAGER};
use crate::render::RenderFn;
use crate::resource::Resource;
use anyhow::Error;
use base64::prelude::BASE64_STANDARD;
use base64::Engine;
use image::{EncodableLayout, ImageReader};
use log::error;
use skia_safe::Color;
use std::io::Cursor;
use crate::resource::Resource;

#[derive(Clone)]
enum ImageSrc {
Expand All @@ -22,7 +22,7 @@ unsafe impl Send for ImageSrc {}
impl ImageSrc {
pub fn get_size(&self) -> (f32, f32) {
match self {
ImageSrc::Svg(dom) => unsafe { dom.container_size() },
ImageSrc::Svg(dom) => dom.container_size(),
ImageSrc::Img(img) => (img.width() as f32, img.height() as f32),
ImageSrc::None => (0.0, 0.0),
}
Expand Down Expand Up @@ -122,7 +122,8 @@ impl ImageObject {
} else {
Self::load_image_from_data(data)
}
}).unwrap_or_else(|| {
})
.unwrap_or_else(|| {
error!("Image not found: {:?}", src);
ImageSrc::None
});
Expand Down
8 changes: 4 additions & 4 deletions src/element/common/svg_object.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use anyhow::Error;
use resvg::usvg::Tree;
use resvg::{tiny_skia, usvg, RenderOptions};
use skia_safe::{Color};
use skia_safe::Color;
use skia_safe::{surfaces, AlphaType, Canvas, ColorType, ImageInfo};
use std::fs;
use std::sync::{Arc, Mutex};

pub struct SvgState{
pub struct SvgState {
tree: Tree,
options: RenderOptions,
options: RenderOptions,
}

#[derive(Clone)]
Expand All @@ -21,7 +21,7 @@ impl SvgObject {
let options = usvg::Options::default();
let tree = Tree::from_data(bytes, &options)?;
let options = RenderOptions::default();
Ok(Self {
Ok(Self {
state: Arc::new(Mutex::new(SvgState { tree, options })),
})
}
Expand Down
11 changes: 6 additions & 5 deletions src/element/font_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use font_kit::sources::mem::MemSource as SystemSource;
use skia_safe::font_style::Slant;
use skia_safe::wrapper::NativeTransmutableWrapper;
use std::collections::HashMap;
use std::sync::Arc;
use swash::{ObliqueAngle, Style, Weight};

#[mrc_object]
Expand All @@ -35,10 +34,12 @@ impl FontManager {
#[cfg(target_os = "emscripten")]
let mut source = {
let mut source = SystemSource::empty();
source.add_font(Handle::Memory {
bytes: Arc::new(include_bytes!("../../fonts/NotoSerif-Regular.ttf").to_vec()),
font_index: 0,
}).unwrap();
source
.add_font(Handle::Memory {
bytes: std::sync::Arc::new(include_bytes!("../../fonts/NotoSerif-Regular.ttf").to_vec()),
font_index: 0,
})
.unwrap();
source
};

Expand Down
6 changes: 0 additions & 6 deletions src/element/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,8 @@ use crate::style::StylePropKey;
use crate::text::textbox::{TextBox, TextElement, TextUnit};
use crate::text::TextAlign;
use deft_macros::{element_backend, js_methods};
use skia_safe::FontMgr;
use yoga::Size;

thread_local! {
//TODO remove
pub static FONT_MGR: FontMgr = FontMgr::new();
}

pub fn parse_align(align: &str) -> TextAlign {
match align {
"left" => TextAlign::Left,
Expand Down
1 change: 0 additions & 1 deletion src/element/paragraph.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![allow(unused)]
pub mod simple_paragraph_builder;
pub mod typeface_mgr;

use crate as deft;
use crate::base::{EventContext, MouseDetail, MouseEventType};
Expand Down
48 changes: 0 additions & 48 deletions src/element/paragraph/typeface_mgr.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/element/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ impl ElementBackend for Select {
fn on_event(&mut self, event: &mut Event, _ctx: &mut EventContext<ElementWeak>) {
let el = ok_or_return!(self.element_weak.upgrade());
let w = some_or_return!(el.get_window());
let window = ok_or_return!(w.upgrade_mut());
let bounds = el.get_origin_bounds();
if let Some(_) = ClickEvent::cast(event) {
let mut popup: Mrc<Option<Popup>> = Mrc::new(None);
Expand Down
Loading
Loading