Repository navigation
cadcraft-web: window.cadcraft, the page command API from craft-web-api #2887
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
85b35fc
cadcraft-web: window.cadcraft, the page command API from craft-web-api
helenkwok 14a6cc3
cadcraft-ui-egui: ui.screenshot returns the PNG inline on wasm
helenkwok 07b6b8c
cadcraft: document that ui.screenshot ignores path in the web build
helenkwok 6304abd
cadcraft: keep the ui.screenshot web note inside the table row
helenkwok File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| //! `window.cadcraft`: the page command API (`craft-web-api`, storytold/craft-libs#413) over the app's | ||
| //! own control channel, the one the desktop control server drives (`docs/control-protocol.md`). | ||
| //! | ||
| //! The crate owns the JavaScript side (`request`, `execute`, `commands`, `docInspect`, `uiInspect`, | ||
| //! `screenshot`, as promises with a timeout). This module is CADCraft's part: a [`Request`] that | ||
| //! queues a [`ControlRequest`] for the app, which answers it on its next frame, and waits for the | ||
| //! reply. A call the page gave up on drops its [`PendingReply`], which cancels a request the app has | ||
| //! not started, so the app skips it instead of running it late. | ||
|
|
||
| use std::rc::Rc; | ||
| use std::sync::mpsc::{Receiver, channel}; | ||
|
|
||
| use cadcraft_ui_egui::{ControlRequest, PendingReply}; | ||
| use craft_web_api::methods::Methods; | ||
| use craft_web_api::page::{PageApi, Request}; | ||
| use serde_json::{Value, json}; | ||
| use wasm_bindgen_futures::JsFuture; | ||
|
|
||
| /// The app id in `window.<app>`. | ||
| const APP: &str = "cadcraft"; | ||
|
|
||
| /// How often a waiting call looks for the app's reply, in milliseconds (about a frame). | ||
| const POLL_MS: i32 = 16; | ||
|
|
||
| /// Put `window.cadcraft` on the page and return the queue the app drains each frame (hand it to | ||
| /// `CadApp::with_control`). An existing `window.cadcraft` keeps the functions it already has. | ||
| pub fn install(ctx: egui::Context) -> Receiver<ControlRequest> { | ||
| let (queue, requests) = channel::<ControlRequest>(); | ||
| let request: Request = Rc::new(move |method, params| { | ||
| let (control, reply) = ControlRequest::new(method, params); | ||
| if queue.send(control).is_err() { | ||
| return Box::pin(async { json!({"ok": false, "error": "the app is not running"}) }); | ||
| } | ||
| ctx.request_repaint(); | ||
| Box::pin(wait(reply)) | ||
| }); | ||
| if let Err(e) = Rc::new(PageApi::new(APP, Methods::default(), request)).install() { | ||
| log::warn!("couldn't install window.{APP}: {e:?}"); | ||
| } | ||
| requests | ||
| } | ||
|
|
||
| /// The app's reply, polled about once a frame. Dropping this future (the page's timeout) drops | ||
| /// `reply`, which cancels the request if the app has not started it. | ||
| async fn wait(reply: PendingReply) -> Value { | ||
| loop { | ||
| if let Some(reply) = reply.try_reply() { | ||
| return reply; | ||
| } | ||
| sleep(POLL_MS).await; | ||
| } | ||
| } | ||
|
|
||
| async fn sleep(ms: i32) { | ||
| let promise = js_sys::Promise::new(&mut |resolve, _| { | ||
| if let Some(window) = web_sys::window() { | ||
| let _ = window.set_timeout_with_callback_and_timeout_and_arguments_0(&resolve, ms); | ||
| } | ||
| }); | ||
| let _ = JsFuture::from(promise).await; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Script executed:
Repository: storytold/cadcraft
Length of output: 9522
Script executed:
Repository: storytold/cadcraft
Length of output: 28685
Script executed:
Repository: storytold/cadcraft
Length of output: 43131
Script executed:
Repository: storytold/cadcraft
Length of output: 20353
Reject screenshot paths on the web.
When a web caller supplies
path,save_screenshotcan return success without writing the requested file. Reject explicit paths on wasm so the caller can request inline PNG data instead.Suggested fix
if cfg!(target_arch = "wasm32") { + if path.is_some() { + return json!({"ok": false, "error": "screenshot paths are not supported on the web; omit path to receive PNG data", "request": token}); + } return inline_screenshot(&img, token); }Committable suggestion