-
-
Notifications
You must be signed in to change notification settings - Fork 455
feat: add Rust-side webview screenshot API #1674
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
Open
josh-richardson
wants to merge
4
commits into
tauri-apps:dev
Choose a base branch
from
vibefi:feat/screenshots
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
030f8c9
feat: add webview screenshot capture API
josh-richardson feaa8db
fix: windows borrow checker
josh-richardson dd55250
fix: more in keeping with general wry conventions vs features and err…
josh-richardson bb37d12
Address PR comments
josh-richardson 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
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,150 @@ | ||
| // Copyright 2020-2023 Tauri Programme within The Commons Conservancy | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| use std::sync::{ | ||
| atomic::{AtomicBool, Ordering}, | ||
| Arc, | ||
| }; | ||
| use std::time::Duration; | ||
|
|
||
| use tao::{ | ||
| event::{Event, WindowEvent}, | ||
| event_loop::{ControlFlow, EventLoopBuilder}, | ||
| window::WindowBuilder, | ||
| }; | ||
| use wry::{PageLoadEvent, WebViewBuilder}; | ||
|
|
||
| #[derive(Debug, Clone, Copy)] | ||
| enum UserEvent { | ||
| Capture, | ||
| Exit, | ||
| } | ||
|
|
||
| fn main() -> wry::Result<()> { | ||
| let event_loop = EventLoopBuilder::<UserEvent>::with_user_event().build(); | ||
| let proxy = event_loop.create_proxy(); | ||
| let window = WindowBuilder::new() | ||
| .with_title("wry screenshot smoke") | ||
| .build(&event_loop) | ||
| .unwrap(); | ||
|
|
||
| let already_requested = Arc::new(AtomicBool::new(false)); | ||
| let already_requested_ = already_requested.clone(); | ||
| let proxy_for_load = proxy.clone(); | ||
|
|
||
| let builder = WebViewBuilder::new() | ||
| .with_html( | ||
| r#"<!doctype html> | ||
| <html> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
| <title>WRY Screenshot Smoke</title> | ||
| <style> | ||
| html, body { | ||
| margin: 0; | ||
| width: 100%; | ||
| height: 100%; | ||
| font-family: sans-serif; | ||
| } | ||
| body { | ||
| display: grid; | ||
| place-items: center; | ||
| background: #1f2937; | ||
| color: white; | ||
| } | ||
| .card { | ||
| padding: 24px 28px; | ||
| border-radius: 16px; | ||
| background: rgba(255,255,255,0.12); | ||
| border: 1px solid rgba(255,255,255,0.18); | ||
| box-shadow: 0 20px 50px rgba(0,0,0,0.35); | ||
| backdrop-filter: blur(8px); | ||
| } | ||
| h1 { margin: 0 0 8px; font-size: 28px; } | ||
| p { margin: 0; opacity: 0.9; } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <div class="card"> | ||
| <h1>Screenshot Smoke Test</h1> | ||
| <p>If you can read this in screenshot.png, capture worked.</p> | ||
| </div> | ||
| </body> | ||
| </html>"#, | ||
| ) | ||
| .with_on_page_load_handler(move |event, _url| { | ||
| if matches!(event, PageLoadEvent::Finished) | ||
| && !already_requested_.swap(true, Ordering::SeqCst) | ||
| { | ||
| let proxy = proxy_for_load.clone(); | ||
| std::thread::spawn(move || { | ||
| std::thread::sleep(Duration::from_millis(1000)); | ||
| let _ = proxy.send_event(UserEvent::Capture); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| #[cfg(any( | ||
| target_os = "windows", | ||
| target_os = "macos", | ||
| target_os = "ios", | ||
| target_os = "android" | ||
| ))] | ||
| let webview = builder.build(&window)?; | ||
| #[cfg(not(any( | ||
| target_os = "windows", | ||
| target_os = "macos", | ||
| target_os = "ios", | ||
| target_os = "android" | ||
| )))] | ||
| let webview = { | ||
| use tao::platform::unix::WindowExtUnix; | ||
| use wry::WebViewBuilderExtUnix; | ||
| let vbox = window.default_vbox().unwrap(); | ||
| builder.build_gtk(vbox)? | ||
| }; | ||
|
|
||
| event_loop.run(move |event, _, control_flow| { | ||
| *control_flow = ControlFlow::Wait; | ||
|
|
||
| match event { | ||
| Event::WindowEvent { | ||
| event: WindowEvent::CloseRequested, | ||
| .. | ||
| } => *control_flow = ControlFlow::Exit, | ||
| Event::UserEvent(UserEvent::Capture) => { | ||
| // screenshot is not supported on Android or iOS; the handler would | ||
| // never be called, so we exit immediately on those platforms. | ||
| #[cfg(any(target_os = "android", target_os = "ios"))] | ||
| { | ||
| let _ = proxy.send_event(UserEvent::Exit); | ||
| return; | ||
| } | ||
|
|
||
| #[cfg(not(any(target_os = "android", target_os = "ios")))] | ||
| { | ||
| let proxy = proxy.clone(); | ||
| webview | ||
| .screenshot(move |result| { | ||
| match result { | ||
| Ok(bytes) => { | ||
| if let Err(err) = std::fs::write("screenshot.png", bytes) { | ||
| eprintln!("failed to write screenshot.png: {err}"); | ||
| } else { | ||
| println!("wrote screenshot.png"); | ||
| } | ||
| } | ||
| Err(err) => eprintln!("screenshot failed: {err}"), | ||
| } | ||
| let _ = proxy.send_event(UserEvent::Exit); | ||
| }) | ||
| .expect("failed to request screenshot"); | ||
| } | ||
| } | ||
| Event::UserEvent(UserEvent::Exit) => *control_flow = ControlFlow::Exit, | ||
| _ => {} | ||
| } | ||
| }); | ||
| } |
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
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.
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.
would be so awesome if we could abstract away the handler but i guess that's more something for crate consumers like tauri