-
Notifications
You must be signed in to change notification settings - Fork 52
general code improvement #60
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +0,0 @@ | ||
| # Drag an Drop PSD Demo | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why were these files removed from the example? |
||
|
|
||
| The demo can be [viewed live](https://chinedufn.github.io/psd/drag-drop-demo/). | ||
|
|
||
| To run it locally | ||
|
|
||
| ``` | ||
| git clone git@github.com:chinedufn/psd.git | ||
| cd examples/drag-drop-browser | ||
|
|
||
| # ./build-dev.sh | ||
| ./build-release.sh | ||
|
|
||
| npm install -g http-server # Or any other wasm compatible server | ||
| http-server -o -c -p 12000 public | ||
| ``` | ||
|
|
||
|  | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +0,0 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -e | ||
|
|
||
| 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 | ||
| cp index.html public/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +0,0 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -e | ||
|
|
||
| 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 | ||
| cp index.html public/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +0,0 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en" style="width: 100%; height: 100%;"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1"> | ||
| <link rel="stylesheet" type="text/css" href="app.css"/> | ||
| <title>PSD demo</title> | ||
| </head> | ||
| <body style='margin: 0; padding: 0; display: flex; justify-content: center;'> | ||
| <script type='module'> | ||
| import init, { AppWrapper } from '/drag_drop_browser.js' | ||
|
|
||
| async function run () { | ||
| await init('/drag_drop_browser_bg.wasm') | ||
| new AppWrapper() | ||
| } | ||
| run() | ||
| </script> | ||
|
|
||
| <a href="https://github.com/chinedufn/psd" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#70B7FD; color:#fff; position: absolute; top: 0; border: 0; left: 0; transform: scale(-1, 1);" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style> | ||
| </body> | ||
| </html> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,3 @@ | ||
| use console_error_panic_hook; | ||
|
|
||
| use percy_dom::prelude::*; | ||
| use wasm_bindgen::prelude::*; | ||
| use wasm_bindgen::Clamped; | ||
|
|
@@ -19,7 +17,13 @@ use std::rc::Rc; | |
| /// | ||
| /// If we we didn't do this our closures would get dropped and wouldn't work. | ||
| #[wasm_bindgen] | ||
| struct AppWrapper(Rc<RefCell<App>>); | ||
| pub struct AppWrapper(#[allow(dead_code)] Rc<RefCell<App>>); | ||
|
|
||
| impl Default for AppWrapper { | ||
| fn default() -> Self { | ||
| AppWrapper::new() | ||
| } | ||
| } | ||
|
Comment on lines
+22
to
+26
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can remove this if it is unused.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is to stop a clippy lint, we can allow that if you so wish, or make |
||
|
|
||
| #[wasm_bindgen] | ||
| impl AppWrapper { | ||
|
|
@@ -28,7 +32,7 @@ impl AppWrapper { | |
| 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); | ||
|
|
||
|
|
@@ -49,13 +53,15 @@ impl AppWrapper { | |
| let app = Rc::clone(&app); | ||
|
|
||
| let vdom = app.borrow().render(); | ||
| app.borrow_mut().update(vdom); | ||
| app.borrow_mut().update(vdom).unwrap(); | ||
|
|
||
| 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()); | ||
| window() | ||
| .request_animation_frame(re_render.as_ref().unchecked_ref()) | ||
| .unwrap(); | ||
|
|
||
| *closure_holder.borrow_mut() = Some(Box::new(re_render)); | ||
| }; | ||
|
|
@@ -73,7 +79,7 @@ impl AppWrapper { | |
|
|
||
| /// Our client side web application | ||
| #[wasm_bindgen] | ||
| struct App { | ||
| pub struct App { | ||
| store: Rc<RefCell<Store>>, | ||
| dom_updater: PercyDom, | ||
| /// Holds the most recent RAF closure | ||
|
|
@@ -85,7 +91,7 @@ impl App { | |
| /// Create a new App | ||
| 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, | ||
|
|
@@ -111,7 +117,7 @@ impl App { | |
| self.store.borrow_mut().msg(&Msg::ReplacePsd(demo_psd)); | ||
|
|
||
| let vdom = self.render(); | ||
| self.update(vdom); | ||
| self.update(vdom).unwrap(); | ||
| } | ||
|
|
||
| /// Render the virtual-dom | ||
|
|
@@ -158,7 +164,7 @@ impl App { | |
| // To be able to move the callback outside of the html macro.. | ||
| // | ||
| // If the attribute starts with `on` treat the value as a closure. | ||
| onchange=move |event: web_sys::Event| { | ||
| onchange=move |event: Event| { | ||
| let input: HtmlInputElement = event.target().unwrap().dyn_into().unwrap(); | ||
| let msg = Msg::SetLayerVisibility(idx, input.checked()); | ||
| store.borrow_mut().msg(&msg); | ||
|
|
@@ -179,15 +185,15 @@ impl App { | |
| <canvas id="psd-visual"></canvas> | ||
| <div | ||
| style="height: 100px; display: flex; align-items: center; justify-content: center;" | ||
| ondragenter=|event: web_sys::DragEvent| { | ||
| ondragenter=|event: DragEvent| { | ||
| event.prevent_default(); | ||
| event.stop_propagation(); | ||
| } | ||
| ondragover=|event: web_sys::DragEvent| { | ||
| ondragover=|event: DragEvent| { | ||
| event.prevent_default(); | ||
| event.stop_propagation(); | ||
| } | ||
| ondrop=move |event: web_sys::DragEvent| { | ||
| ondrop=move |event: DragEvent| { | ||
| event.prevent_default(); | ||
| event.stop_propagation(); | ||
|
|
||
|
|
@@ -197,10 +203,10 @@ impl App { | |
| let files = dt.files().unwrap(); | ||
| let psd = files.item(0).unwrap(); | ||
|
|
||
| let file_reader = web_sys::FileReader::new().unwrap(); | ||
| let file_reader = 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); | ||
|
|
@@ -239,8 +245,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(&|(_, layer)| { | ||
| let layer_visible = *self | ||
| .store | ||
| .borrow() | ||
|
|
@@ -263,7 +269,7 @@ impl App { | |
| let context = canvas | ||
| .get_context("2d")? | ||
| .unwrap() | ||
| .dyn_into::<web_sys::CanvasRenderingContext2d>()?; | ||
| .dyn_into::<CanvasRenderingContext2d>()?; | ||
|
|
||
| canvas.set_width(psd.width()); | ||
| canvas.set_height(psd.height()); | ||
|
|
@@ -360,27 +366,27 @@ enum Msg<'a> { | |
| SetIsRendering(bool), | ||
| } | ||
|
|
||
| fn window() -> web_sys::Window { | ||
| fn window() -> Window { | ||
| web_sys::window().unwrap() | ||
| } | ||
|
|
||
| fn document() -> web_sys::Document { | ||
| fn document() -> Document { | ||
| window().document().unwrap() | ||
| } | ||
|
|
||
| fn body() -> web_sys::HtmlElement { | ||
| fn body() -> HtmlElement { | ||
| document().body().unwrap() | ||
| } | ||
|
|
||
| static APP_CONTAINER: &'static str = css! {r#" | ||
| static APP_CONTAINER: &str = css! {r#" | ||
| :host { | ||
| display: flex; | ||
| width: 100%; | ||
| height: 100%; | ||
| } | ||
| "#}; | ||
|
|
||
| static _LAYOUT: &'static str = css! {r#" | ||
| static _LAYOUT: &str = css! {r#" | ||
| .left-column { | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -147,7 +147,7 @@ impl Psd { | |
| // Methods for working with layers | ||
| impl Psd { | ||
| /// Get all of the layers in the PSD | ||
| pub fn layers(&self) -> &Vec<PsdLayer> { | ||
| pub fn layers(&self) -> &[PsdLayer] { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there's a good reason to make these return Otherwise, this is an unnecessary breaking change. |
||
| &self.layer_and_mask_information_section.layers | ||
| } | ||
|
|
||
|
|
@@ -175,7 +175,7 @@ impl Psd { | |
|
|
||
| /// Get the group ID's in the order that they appear in Photoshop. | ||
| /// (i.e. from the bottom of layers view to the top of the layers view). | ||
| pub fn group_ids_in_order(&self) -> &Vec<u32> { | ||
| pub fn group_ids_in_order(&self) -> &[u32] { | ||
| self.layer_and_mask_information_section | ||
| .groups | ||
| .group_ids_in_order() | ||
|
|
@@ -252,10 +252,7 @@ impl Psd { | |
|
|
||
| let blended_pixel = renderer.flattened_pixel(pixel_coord); | ||
|
|
||
| flattened_pixels.push(blended_pixel[0]); | ||
| flattened_pixels.push(blended_pixel[1]); | ||
| flattened_pixels.push(blended_pixel[2]); | ||
| flattened_pixels.push(blended_pixel[3]); | ||
| flattened_pixels.extend_from_slice(&blended_pixel); | ||
| } | ||
|
|
||
| Ok(flattened_pixels) | ||
|
|
@@ -279,7 +276,7 @@ impl Psd { | |
| // Methods for working with the image resources section | ||
| impl Psd { | ||
| /// Resources from the image resources section of the PSD file | ||
| pub fn resources(&self) -> &Vec<ImageResource> { | ||
| pub fn resources(&self) -> &[ImageResource] { | ||
| &self.image_resources_section.resources | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for the additional dependency.
We should also eventually remove
thiserror(doesn't need to happen in this PR)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wait this should have been a test dep my bad, I was messing with utf-16 being 0 copy on big endian systems but then was like its just a bunch of added complexity I can still do it tho if you're fine with it