Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement PSD write support #63

Draft
wants to merge 16 commits into
base: master
Choose a base branch
from
Draft
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
12 changes: 2 additions & 10 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ jobs:
# Install wasm tools
- run:
name: Install wasm-pack
command: >
curl -L https://github.com/rustwasm/wasm-pack/releases/download/v0.9.1/wasm-pack-v0.9.1-x86_64-unknown-linux-musl.tar.gz
| tar --strip-components=1 --wildcards -xzf - "*/wasm-pack"
&& chmod +x wasm-pack
&& mv wasm-pack $CARGO_HOME/bin/
command: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

# Show versions
- run:
Expand Down Expand Up @@ -75,11 +71,7 @@ jobs:
# Install wasm tools
- run:
name: Install wasm-pack
command: >
curl -L https://github.com/rustwasm/wasm-pack/releases/download/v0.9.1/wasm-pack-v0.9.1-x86_64-unknown-linux-musl.tar.gz
| tar --strip-components=1 --wildcards -xzf - "*/wasm-pack"
&& chmod +x wasm-pack
&& mv wasm-pack $CARGO_HOME/bin/
command: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

# Install mdbook
- run:
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "psd"
version = "0.3.5"
version = "0.3.6"
authors = ["Chinedu Francis Nwafili <[email protected]>"]
description = "A Rust API for parsing and working with PSD files."
keywords = ["psd", "photoshop", "texture", "png", "image"]
license = "MIT/Apache-2.0"
repository = "https://github.com/chinedufn/psd"
edition = "2018"
edition = "2021"

[dependencies]
thiserror = "1"
Expand All @@ -21,4 +21,4 @@ members = [

[profile.release]
# We can re-enable lto for the demo when wasm-pack 0.2.38 is released. There's a bug in 0.2.37
# lto = true
# lto = true
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
psd
===

[![Build status](https://circleci.com/gh/chinedufn/psd.svg?style=shield&circle-token=:circle-token)](https://circleci.com/gh/chinedufn/psd) [![docs](https://docs.rs/psd/badge.svg)](https://docs.rs/psd)
[![Build status](https://circleci.com/gh/chinedufn/psd.svg?style=shield)](https://circleci.com/gh/chinedufn/psd) [![docs](https://docs.rs/psd/badge.svg)](https://docs.rs/psd)

> A Rust API for parsing and working with PSD files.

Expand Down
19 changes: 12 additions & 7 deletions examples/drag-drop-browser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
[package]
name = "drag-drop-browser"
version = "0.1.0"
version = "0.1.1"
authors = ["Chinedu Francis Nwafili <[email protected]>"]
edition = "2018"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
console_error_panic_hook = "0.1.6"
css-rs-macro = "0.1.0"
js-sys = "0.3.40"
console_error_panic_hook = "0.1.7"
percy-css-macro = "0.1.1"
js-sys = "0.3.70"
psd = {path = "../../"}
percy-dom = "0.7"
wasm-bindgen = "0.2.63"
percy-dom = "0.9.9"
wasm-bindgen = "0.2.93"

[dependencies.web-sys]
version = "0.3"
Expand All @@ -36,3 +36,8 @@ features = [
"Window",
"console"
]

[patch.crates-io]
syn = "2"
proc-macro2 = "1"
quote = "1"
2 changes: 1 addition & 1 deletion examples/drag-drop-browser/build-dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ cd "$(dirname "$0")"
mkdir -p public

CSS_FILE="$(pwd)/public/app.css"
OUTPUT_CSS=$CSS_FILE wasm-pack build --no-typescript --dev --target web --out-dir ./public
OUTPUT_CSS=$CSS_FILE wasm-pack build --no-typescript --dev --target web --out-dir ./public --no-opt
cp index.html public/
2 changes: 1 addition & 1 deletion examples/drag-drop-browser/build-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ cd "$(dirname "$0")"
mkdir -p public

CSS_FILE="$(pwd)/public/app.css"
OUTPUT_CSS=$CSS_FILE wasm-pack build --no-typescript --release --target web --out-dir ./public
OUTPUT_CSS=$CSS_FILE wasm-pack build --no-typescript --release --target web --out-dir ./public --no-opt
cp index.html public/
41 changes: 26 additions & 15 deletions examples/drag-drop-browser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use wasm_bindgen::Clamped;
use wasm_bindgen::JsCast;
use web_sys::*;

use css_rs_macro::css;
use percy_css_macro::css;

use psd::Psd;
use std::cell::RefCell;
Expand All @@ -25,10 +25,11 @@ struct AppWrapper(Rc<RefCell<App>>);
impl AppWrapper {
/// Create a new AppWrapper. We'll call this in a script tag in index.html
#[wasm_bindgen(constructor)]
#[allow(dead_code)]
pub fn new() -> AppWrapper {
console_error_panic_hook::set_once();

let mut app = App::new();
let app = App::new();

let closure_holder = Rc::clone(&app.raf_closure_holder);

Expand All @@ -49,13 +50,13 @@ impl AppWrapper {
let app = Rc::clone(&app);

let vdom = app.borrow().render();
app.borrow_mut().update(vdom);
let _ = app.borrow_mut().update(vdom);

store.borrow_mut().msg(&Msg::SetIsRendering(false));
};
let mut re_render = Closure::wrap(Box::new(re_render) as Box<dyn FnMut()>);
let re_render = Closure::wrap(Box::new(re_render) as Box<dyn FnMut()>);

window().request_animation_frame(&re_render.as_ref().unchecked_ref());
let _ = window().request_animation_frame(&re_render.as_ref().unchecked_ref());

*closure_holder.borrow_mut() = Some(Box::new(re_render));
};
Expand All @@ -73,6 +74,7 @@ impl AppWrapper {

/// Our client side web application
#[wasm_bindgen]
#[allow(dead_code)]
struct App {
store: Rc<RefCell<Store>>,
dom_updater: PercyDom,
Expand All @@ -83,9 +85,10 @@ struct App {
#[wasm_bindgen]
impl App {
/// Create a new App
#[allow(dead_code)]
fn new() -> App {
let vdom = html! { <div> </div> };
let mut dom_updater = PercyDom::new_append_to_mount(vdom, &body());
let dom_updater = PercyDom::new_append_to_mount(vdom, &body());

let state = State {
psd: None,
Expand All @@ -111,7 +114,7 @@ impl App {
self.store.borrow_mut().msg(&Msg::ReplacePsd(demo_psd));

let vdom = self.render();
self.update(vdom);
let _ = self.update(vdom);
}

/// Render the virtual-dom
Expand Down Expand Up @@ -200,7 +203,7 @@ impl App {
let file_reader = web_sys::FileReader::new().unwrap();
file_reader.read_as_array_buffer(&psd).unwrap();

let mut onload = Closure::wrap(Box::new(move |event: Event| {
let onload = Closure::wrap(Box::new(move |event: Event| {
let file_reader: FileReader = event.target().unwrap().dyn_into().unwrap();
let psd = file_reader.result().unwrap();
let psd = js_sys::Uint8Array::new(&psd);
Expand Down Expand Up @@ -239,8 +242,8 @@ impl App {

// Flatten the PSD into only the pixels from the layers that are currently
// toggled on.
let mut psd_pixels = psd
.flatten_layers_rgba(&|(idx, layer)| {
let psd_pixels = psd
.flatten_layers_rgba(&|(_idx, layer)| {
let layer_visible = *self
.store
.borrow()
Expand Down Expand Up @@ -276,7 +279,8 @@ impl App {

/// A light wrapper around State, useful when you want to accept a Msg and handle
/// anything impure (such as working with local storage) before passing the Msg
/// along the State. Allowing you to keep State pure.
/// along the State. Allowing you to keep State pure.\
#[allow(dead_code)]
struct Store {
state: State,
on_msg: Option<Box<dyn Fn()>>,
Expand All @@ -292,6 +296,7 @@ impl Deref for Store {
}

/// Handles application state
#[allow(dead_code)]
struct State {
/// The current PSD that is being displayed
psd: Option<Psd>,
Expand All @@ -317,6 +322,7 @@ impl Store {

impl State {
/// Update State given some new Msg
#[allow(dead_code)]
fn msg(&mut self, msg: &Msg) {
match msg {
// Replace the current PSD with a new one
Expand Down Expand Up @@ -351,27 +357,32 @@ impl State {
}

/// All of our Msg variants that are used to update application state
#[allow(dead_code)]
enum Msg<'a> {
/// Replace the current PSD with a new one, usually after drag and drop
ReplacePsd(&'a [u8]),
/// Set whether or not a layer (by index) should be visible
/// Set whether a layer (by index) should be visible
SetLayerVisibility(usize, bool),
/// Set that the application is planning to render on the next request animation frame
SetIsRendering(bool),
}

fn window() -> web_sys::Window {
#[allow(dead_code)]
fn window() -> Window {
web_sys::window().unwrap()
}

fn document() -> web_sys::Document {
#[allow(dead_code)]
fn document() -> Document {
window().document().unwrap()
}

fn body() -> web_sys::HtmlElement {
#[allow(dead_code)]
fn body() -> HtmlElement {
document().body().unwrap()
}

#[allow(dead_code)]
static APP_CONTAINER: &'static str = css! {r#"
:host {
display: flex;
Expand Down
Loading