Skip to content

Commit 0f4d6a9

Browse files
committed
Added example of managing state with rust impl and console log macros, updated icons and assets
1 parent ef8c757 commit 0f4d6a9

File tree

22 files changed

+960
-304
lines changed

22 files changed

+960
-304
lines changed

wasm-rust-sample/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ node_modules
22
dist/
33
target/
44
pkg/
5+
wasm/
56
wasm-pack.log
67
*.wasm.js

wasm-rust-sample/Cargo.lock

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wasm-rust-sample/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ edition = "2018"
99
crate-type = ["cdylib"]
1010

1111
[dependencies]
12-
js-sys = "0.3.52"
1312
wasm-bindgen = "0.2.75"
14-
web-sys = { version = "0.3.52", features = ["console"] }
1513

1614
[dev-dependencies]
1715
wasm-bindgen-test = "0.3.25"

wasm-rust-sample/README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
This plugin is a starting point for leveraging Rust and WebAssembly in your UXP plugins. It comes defined with most of the dependencies you need to get started. As this plugin does rely on the [Rust Programming Language](https://www.rust-lang.org/), an environment configured for Rust development will be required before this will be usable in Photoshop.
44

5-
65
# Configuration
76

87
## Rust Environment
@@ -13,16 +12,18 @@ Follow the instructions and the `rustup` installation process. Once completed, y
1312

1413
> You **must** have the Rust toolchain installed in order to build the project
1514
16-
### 1. Install [wasm-pack](https://github.com/rustwasm/wasm-pack)
15+
**Note:** For Windows users, the [Microsoft C++ Build Tools](https://visualstudio.microsoft.com/visual-cpp-build-tools/) must be installed for the Rust compiler to work properly. For more information, consult the [Rust documentation](https://doc.rust-lang.org/book/ch01-01-installation.html#installing-rustup-on-windows).
16+
17+
### 1. Install [wasm-pack](https://github.com/rustwasm/wasm-pack)
1718

1819
```
1920
$ cargo install wasm-pack
2021
```
2122

22-
### 2. Test local Rust configuration and [wasm-pack](https://github.com/rustwasm/wasm-pack) installation
23+
### 2. Test local Rust configuration and [wasm-pack](https://github.com/rustwasm/wasm-pack) installation
2324

2425
```
25-
$ yarn test # or cargo test && wasm-pack test
26+
$ yarn test # or cargo test && wasm-pack test --node
2627
```
2728

2829
## Node.js
@@ -33,22 +34,25 @@ $ yarn test # or cargo test && wasm-pack test
3334
$ yarn install # or npm install
3435
```
3536

36-
### 2. Build plugin
37-
38-
To perform a basic, automated build of the plugin:
37+
### 2. Run plugin in watch or build mode
3938

4039
```
40+
$ yarn watch # or npm run watch
41+
42+
# OR
43+
4144
$ yarn build # or npm run build
4245
```
4346

4447
> You **must** run `build` prior to trying to use this plugin within Photoshop!
4548
46-
This will build a production version of the plugin and place it in `dist`. It will not update every time you make a change to the source files.
49+
- `yarn watch` or `npm run watch` will build a development version of the plugin, and recompile everytime you make a change to the source files. The result is placed in `dist`.
50+
- `yarn build` or `npm run build` will build a production version of the plugin and place it in `dist`. It will not update every time you make a change to the source files.
4751

4852
**Note:** Since UXP does not have implicit access to `localhost` for leveraging a development server, this plugin uses inline WebAssembly to work properly. As such, you'll find the following import in `js/index.js`:
4953

5054
```
51-
import encodedRust from './uxp.wasm';
55+
import encodedRust from '../wasm/uxp.wasm';
5256
```
5357

5458
When the build script is triggerred, this JS file with the inline WebAssembly is generated. Should you wish to generate this file yourself, execute the following commands:
@@ -67,4 +71,4 @@ Once added, you can load it into Photoshop by clicking the ••• button on t
6771

6872
## What this plugin does
6973

70-
This plugin does some basic calculations in Rust and demonstrates a potential build process for leveraging Rust and WebAssembly in your UXP plugins.
74+
This plugin uses a basic Rust implementation to hold state for a counter component that is surfaced on the UI. It also uses the `wasm-bindgen` API to make use of JavaScript API methods in Rust, such as `console.log`. Finally, the plugin demonstrates a potential build process for leveraging Rust and WebAssembly in your UXP plugins.

wasm-rust-sample/js/index.css

Lines changed: 0 additions & 8 deletions
This file was deleted.

wasm-rust-sample/js/index.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { entrypoints } from 'uxp';
22
import { decodeWebAssembly } from './utils';
33

4-
import encodedRust from './uxp.wasm';
5-
import init, { run, add, multiply } from '../pkg/uxp_wasm.js';
4+
import encodedRust from '../wasm/uxp.wasm';
5+
import init, { add, multiply, global_sys, Counter } from '../pkg/uxp_wasm.js';
66

77
entrypoints.setup({
88
plugin: {
99
create(plugin) {
1010
console.log('Plugin created successfully.', plugin);
1111
},
12+
panels: {
13+
plugin: this,
14+
},
1215
},
1316
});
1417

@@ -17,8 +20,15 @@ const main = async () => {
1720

1821
// Manually pass WebAssembly to prevent `wasm-bindgen` from using `fetch()`
1922
await init(decodedRust);
20-
console.log(`2 + 2 = ${add(2, 2)}`);
21-
console.log(`2 * 2 = ${multiply(2, 2)}`);
23+
24+
console.log(`Log 3: Sent from JavaScript! (2 + 2 = ${add(2, 2)})`);
25+
console.log(`Log 4: Sent from JavaScript! (12 * 12 = ${multiply(12, 12)})`);
26+
27+
const counter = Counter.new();
28+
setInterval(() => {
29+
counter.increment();
30+
document.getElementById('timer').textContent = counter.get_count();
31+
}, 1000);
2232
};
2333

2434
await main().catch((err) => {

wasm-rust-sample/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"private": true,
66
"scripts": {
77
"build": "rimraf dist && webpack --mode development",
8-
"inlinewasm": "npx crlf --set=LF node_modules/.bin/inlinewasm && inlinewasm pkg/uxp_wasm_bg.wasm -o js/uxp.wasm.js -t encoded",
8+
"watch": "nodemon -w src -w js -w webpack.config.js -e js,rs,json,css,html -x npm run build",
9+
"inlinewasm": "npx crlf --set=LF node_modules/.bin/inlinewasm && inlinewasm pkg/uxp_wasm_bg.wasm -o wasm/uxp.wasm.js -t encoded",
910
"test": "cargo test && wasm-pack test --node"
1011
},
1112
"devDependencies": {
@@ -18,6 +19,7 @@
1819
"copy-webpack-plugin": "^9.0.1",
1920
"css-loader": "^5.2.6",
2021
"js-inline-wasm": "^0.0.7",
22+
"nodemon": "^2.0.12",
2123
"rimraf": "^3.0.2",
2224
"style-loader": "^3.0.0",
2325
"text-encoding": "^0.7.0",

wasm-rust-sample/plugin/ferris.png

5.24 KB
Loading
-124 Bytes
Loading
-810 Bytes
Loading

0 commit comments

Comments
 (0)