pub struct Webview<R: Runtime = Wry> { /* private fields */ }
Expand description
Webview.
Implementations§
source§impl<R: Runtime> Webview<R>
impl<R: Runtime> Webview<R>
Base webview functions.
sourcepub fn builder<L: Into<String>>(label: L, url: WebviewUrl) -> WebviewBuilder<R>
Available on crate feature unstable
only.
pub fn builder<L: Into<String>>(label: L, url: WebviewUrl) -> WebviewBuilder<R>
unstable
only.Initializes a webview builder with the given window label and URL to load on the webview.
Data URLs are only supported with the webview-data-url
feature flag.
sourcepub fn run_on_main_thread<F: FnOnce() + Send + 'static>(
&self,
f: F
) -> Result<()>
pub fn run_on_main_thread<F: FnOnce() + Send + 'static>( &self, f: F ) -> Result<()>
Runs the given closure on the main thread.
sourcepub fn on_webview_event<F: Fn(&WebviewEvent) + Send + 'static>(&self, f: F)
pub fn on_webview_event<F: Fn(&WebviewEvent) + Send + 'static>(&self, f: F)
Registers a window event listener.
source§impl<R: Runtime> Webview<R>
impl<R: Runtime> Webview<R>
Desktop webview setters and actions.
sourcepub fn print(&self) -> Result<()>
pub fn print(&self) -> Result<()>
Opens the dialog to prints the contents of the webview.
Currently only supported on macOS on wry
.
window.print()
works on all platforms.
sourcepub fn set_position<Pos: Into<Position>>(&self, position: Pos) -> Result<()>
pub fn set_position<Pos: Into<Position>>(&self, position: Pos) -> Result<()>
Sets this webviews’s position.
sourcepub fn position(&self) -> Result<PhysicalPosition<i32>>
pub fn position(&self) -> Result<PhysicalPosition<i32>>
Returns the webview position.
- For child webviews, returns the position of the top-left hand corner of the webviews’s client area relative to the top-left hand corner of the parent window.
- For webview window, returns the inner position of the window.
sourcepub fn size(&self) -> Result<PhysicalSize<u32>>
pub fn size(&self) -> Result<PhysicalSize<u32>>
Returns the physical size of the webviews’s client area.
source§impl<R: Runtime> Webview<R>
impl<R: Runtime> Webview<R>
Webview APIs.
sourcepub fn with_webview<F: FnOnce(PlatformWebview) + Send + 'static>(
&self,
f: F
) -> Result<()>
pub fn with_webview<F: FnOnce(PlatformWebview) + Send + 'static>( &self, f: F ) -> Result<()>
Executes a closure, providing it with the webview handle that is specific to the current platform.
The closure is executed on the main thread.
§Examples
#[cfg(target_os = "macos")]
#[macro_use]
extern crate objc;
use tauri::Manager;
fn main() {
tauri::Builder::default()
.setup(|app| {
let main_webview = app.get_webview("main").unwrap();
main_webview.with_webview(|webview| {
#[cfg(target_os = "linux")]
{
// see https://docs.rs/webkit2gtk/2.0.0/webkit2gtk/struct.WebView.html
// and https://docs.rs/webkit2gtk/2.0.0/webkit2gtk/trait.WebViewExt.html
use webkit2gtk::WebViewExt;
webview.inner().set_zoom_level(4.);
}
#[cfg(windows)]
unsafe {
// see https://docs.rs/webview2-com/0.19.1/webview2_com/Microsoft/Web/WebView2/Win32/struct.ICoreWebView2Controller.html
webview.controller().SetZoomFactor(4.).unwrap();
}
#[cfg(target_os = "macos")]
unsafe {
let () = msg_send![webview.inner(), setPageZoom: 4.];
let () = msg_send![webview.controller(), removeAllUserScripts];
let bg_color: cocoa::base::id = msg_send![class!(NSColor), colorWithDeviceRed:0.5 green:0.2 blue:0.4 alpha:1.];
let () = msg_send![webview.ns_window(), setBackgroundColor: bg_color];
}
#[cfg(target_os = "android")]
{
use jni::objects::JValue;
webview.jni_handle().exec(|env, _, webview| {
env.call_method(webview, "zoomBy", "(F)V", &[JValue::Float(4.)]).unwrap();
})
}
});
Ok(())
});
}
Navigates the webview to the defined url.
sourcepub fn on_message(
self,
request: InvokeRequest,
responder: Box<OwnedInvokeResponder<R>>
)
pub fn on_message( self, request: InvokeRequest, responder: Box<OwnedInvokeResponder<R>> )
Handles this window receiving an InvokeRequest
.
sourcepub fn open_devtools(&self)
Available on debug-assertions enabled or crate feature devtools
only.
pub fn open_devtools(&self)
devtools
only.Opens the developer tools window (Web Inspector).
The devtools is only enabled on debug builds or with the devtools
feature flag.
§Platform-specific
- macOS: Only supported on macOS 10.15+. This is a private API on macOS, so you cannot use this if your application will be published on the App Store.
§Examples
use tauri::Manager;
tauri::Builder::default()
.setup(|app| {
#[cfg(debug_assertions)]
app.get_webview("main").unwrap().open_devtools();
Ok(())
});
sourcepub fn close_devtools(&self)
Available on debug-assertions enabled or crate feature devtools
only.
pub fn close_devtools(&self)
devtools
only.Closes the developer tools window (Web Inspector).
The devtools is only enabled on debug builds or with the devtools
feature flag.
§Platform-specific
- macOS: Only supported on macOS 10.15+. This is a private API on macOS, so you cannot use this if your application will be published on the App Store.
- Windows: Unsupported.
§Examples
use tauri::Manager;
tauri::Builder::default()
.setup(|app| {
#[cfg(debug_assertions)]
{
let webview = app.get_webview("main").unwrap();
webview.open_devtools();
std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_secs(10));
webview.close_devtools();
});
}
Ok(())
});
sourcepub fn is_devtools_open(&self) -> bool
Available on debug-assertions enabled or crate feature devtools
only.
pub fn is_devtools_open(&self) -> bool
devtools
only.Checks if the developer tools window (Web Inspector) is opened.
The devtools is only enabled on debug builds or with the devtools
feature flag.
§Platform-specific
- macOS: Only supported on macOS 10.15+. This is a private API on macOS, so you cannot use this if your application will be published on the App Store.
- Windows: Unsupported.
§Examples
use tauri::Manager;
tauri::Builder::default()
.setup(|app| {
#[cfg(debug_assertions)]
{
let webview = app.get_webview("main").unwrap();
if !webview.is_devtools_open() {
webview.open_devtools();
}
}
Ok(())
});
source§impl<R: Runtime> Webview<R>
impl<R: Runtime> Webview<R>
Event system APIs.
sourcepub fn listen<F>(&self, event: impl Into<String>, handler: F) -> EventId
pub fn listen<F>(&self, event: impl Into<String>, handler: F) -> EventId
Listen to an event on this webview.
§Examples
use tauri::Manager;
tauri::Builder::default()
.setup(|app| {
let webview = app.get_webview("main").unwrap();
webview.listen("component-loaded", move |event| {
println!("window just loaded a component");
});
Ok(())
});
sourcepub fn unlisten(&self, id: EventId)
pub fn unlisten(&self, id: EventId)
Unlisten to an event on this webview.
§Examples
use tauri::Manager;
tauri::Builder::default()
.setup(|app| {
let webview = app.get_webview("main").unwrap();
let webview_ = webview.clone();
let handler = webview.listen("component-loaded", move |event| {
println!("webview just loaded a component");
// we no longer need to listen to the event
// we also could have used `webview.once` instead
webview_.unlisten(event.id());
});
// stop listening to the event when you do not need it anymore
webview.unlisten(handler);
Ok(())
});
Trait Implementations§
source§impl<'de, R: Runtime> CommandArg<'de, R> for Webview<R>
impl<'de, R: Runtime> CommandArg<'de, R> for Webview<R>
source§fn from_command(command: CommandItem<'de, R>) -> Result<Self, InvokeError>
fn from_command(command: CommandItem<'de, R>) -> Result<Self, InvokeError>
Grabs the Webview
from the CommandItem
. This will never fail.
source§impl<R: Runtime> Manager<R> for Webview<R>
impl<R: Runtime> Manager<R> for Webview<R>
source§fn app_handle(&self) -> &AppHandle<R>
fn app_handle(&self) -> &AppHandle<R>
source§fn package_info(&self) -> &PackageInfo
fn package_info(&self) -> &PackageInfo
PackageInfo
the manager was created with.source§fn get_window(&self, label: &str) -> Option<Window<R>>
fn get_window(&self, label: &str) -> Option<Window<R>>
unstable
only.source§fn get_focused_window(&self) -> Option<Window<R>>
fn get_focused_window(&self) -> Option<Window<R>>
unstable
only.None
if there is not any focused window.source§fn windows(&self) -> HashMap<String, Window<R>>
fn windows(&self) -> HashMap<String, Window<R>>
unstable
only.source§fn get_webview(&self, label: &str) -> Option<Webview<R>>
fn get_webview(&self, label: &str) -> Option<Webview<R>>
unstable
only.source§fn webviews(&self) -> HashMap<String, Webview<R>>
fn webviews(&self) -> HashMap<String, Webview<R>>
unstable
only.source§fn get_webview_window(&self, label: &str) -> Option<WebviewWindow<R>>
fn get_webview_window(&self, label: &str) -> Option<WebviewWindow<R>>
source§fn webview_windows(&self) -> HashMap<String, WebviewWindow<R>>
fn webview_windows(&self) -> HashMap<String, WebviewWindow<R>>
source§fn manage<T>(&self, state: T) -> bool
fn manage<T>(&self, state: T) -> bool
state
to the state managed by the application. Read moresource§fn try_state<T>(&self) -> Option<State<'_, T>>
fn try_state<T>(&self) -> Option<State<'_, T>>
T
. Read moresource§fn resources_table(&self) -> MutexGuard<'_, ResourceTable>
fn resources_table(&self) -> MutexGuard<'_, ResourceTable>
source§fn asset_protocol_scope(&self) -> Scope
fn asset_protocol_scope(&self) -> Scope
source§fn path(&self) -> &PathResolver<R>
fn path(&self) -> &PathResolver<R>
impl<R: Runtime> Eq for Webview<R>
Auto Trait Implementations§
impl<R = Wry<EventLoopMessage>> !RefUnwindSafe for Webview<R>
impl<R> Send for Webview<R>
impl<R> Sync for Webview<R>
impl<R> Unpin for Webview<R>where
<R as Runtime<EventLoopMessage>>::Handle: Unpin,
<R as Runtime<EventLoopMessage>>::WebviewDispatcher: Unpin,
impl<R = Wry<EventLoopMessage>> !UnwindSafe for Webview<R>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.