Skip to content
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

refactor(core): remove on_page_load #8112

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/remove-on-page-load.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch:breaking
---

Removed the `on_page_load` hook from `Plugin` and `Builder`. Use `on_navigation` instead.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But on_navigation is different from on_page_load and we have on_page_load in wry now

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we do??? totally missed that
the on_page_load we have right now is yiky

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new implementation is using native hooks

18 changes: 3 additions & 15 deletions core/tauri/scripts/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

;(function () {
;
(function () {
__RAW_freeze_prototype__

__RAW_pattern_script__
Expand All @@ -12,21 +13,8 @@
__RAW_core_script__

__RAW_event_initialization_script__
;(function () {
__RAW_bundle_script__
})()

if (window.ipc) {
window.__TAURI_INTERNALS__.invoke('__initialized', {
url: window.location.href
})
} else {
window.addEventListener('DOMContentLoaded', function () {
window.__TAURI_INTERNALS__.invoke('__initialized', {
url: window.location.href
})
})
}
__RAW_bundle_script__

__RAW_plugin_initialization_script__
})()
31 changes: 0 additions & 31 deletions core/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use crate::tray::{TrayIcon, TrayIconBuilder, TrayIconEvent, TrayIconId};
#[cfg(desktop)]
use crate::window::WindowMenu;
use raw_window_handle::HasRawDisplayHandle;
use serde::Deserialize;
use serialize_to_javascript::{default_template, DefaultTemplate, Template};
use tauri_macros::default_runtime;
#[cfg(desktop)]
Expand Down Expand Up @@ -67,21 +66,6 @@ pub(crate) type GlobalWindowEventListener<R> = Box<dyn Fn(GlobalWindowEvent<R>)
/// A closure that is run when the Tauri application is setting up.
pub type SetupHook<R> =
Box<dyn FnOnce(&mut App<R>) -> Result<(), Box<dyn std::error::Error>> + Send>;
/// A closure that is run once every time a window is created and loaded.
pub type OnPageLoad<R> = dyn Fn(Window<R>, PageLoadPayload) + Send + Sync + 'static;

/// The payload for the [`OnPageLoad`] hook.
#[derive(Debug, Clone, Deserialize)]
pub struct PageLoadPayload {
url: String,
}

impl PageLoadPayload {
/// The page URL.
pub fn url(&self) -> &str {
&self.url
}
}

/// Api exposed on the `ExitRequested` event.
#[derive(Debug)]
Expand Down Expand Up @@ -981,9 +965,6 @@ pub struct Builder<R: Runtime> {
/// The setup hook.
setup: SetupHook<R>,

/// Page load hook.
on_page_load: Box<OnPageLoad<R>>,

/// windows to create when starting up.
pending_windows: Vec<PendingWindow<EventLoopMessage, R>>,

Expand Down Expand Up @@ -1040,7 +1021,6 @@ impl<R: Runtime> Builder<R> {
.render_default(&Default::default())
.unwrap()
.into_string(),
on_page_load: Box::new(|_, _| ()),
pending_windows: Default::default(),
plugins: PluginStore::default(),
uri_scheme_protocols: Default::default(),
Expand Down Expand Up @@ -1126,16 +1106,6 @@ impl<R: Runtime> Builder<R> {
self
}

/// Defines the page load hook.
#[must_use]
pub fn on_page_load<F>(mut self, on_page_load: F) -> Self
where
F: Fn(Window<R>, PageLoadPayload) + Send + Sync + 'static,
{
self.on_page_load = Box::new(on_page_load);
self
}

/// Adds a Tauri application plugin.
///
/// A plugin is created using the [`crate::plugin::Builder`] struct.Check its documentation for more information.
Expand Down Expand Up @@ -1477,7 +1447,6 @@ impl<R: Runtime> Builder<R> {
context,
self.plugins,
self.invoke_handler,
self.on_page_load,
self.uri_scheme_protocols,
self.state,
self.window_event_listeners,
Expand Down
21 changes: 1 addition & 20 deletions core/tauri/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ use tauri_utils::{

use crate::event::EmitArgs;
use crate::{
app::{
AppHandle, GlobalWindowEvent, GlobalWindowEventListener, OnPageLoad, PageLoadPayload,
UriSchemeResponder,
},
app::{AppHandle, GlobalWindowEvent, GlobalWindowEventListener, UriSchemeResponder},
event::{assert_event_name_is_valid, Event, EventId, Listeners},
ipc::{Invoke, InvokeHandler, InvokeResponder},
pattern::PatternJavascript,
Expand Down Expand Up @@ -231,9 +228,6 @@ pub struct InnerWindowManager<R: Runtime> {
/// The JS message handler.
invoke_handler: Box<InvokeHandler<R>>,

/// The page load hook, invoked when the webview performs a navigation.
on_page_load: Box<OnPageLoad<R>>,

config: Arc<Config>,
assets: Arc<dyn Assets>,
pub(crate) default_window_icon: Option<Icon>,
Expand Down Expand Up @@ -339,7 +333,6 @@ impl<R: Runtime> WindowManager<R> {
#[allow(unused_mut)] mut context: Context<impl Assets>,
plugins: PluginStore<R>,
invoke_handler: Box<InvokeHandler<R>>,
on_page_load: Box<OnPageLoad<R>>,
uri_scheme_protocols: HashMap<String, Arc<UriSchemeProtocol<R>>>,
state: StateManager,
window_event_listeners: Vec<GlobalWindowEventListener<R>>,
Expand All @@ -362,7 +355,6 @@ impl<R: Runtime> WindowManager<R> {
listeners: Listeners::default(),
state: Arc::new(state),
invoke_handler,
on_page_load,
config: Arc::new(context.config),
assets: context.assets,
default_window_icon: context.default_window_icon,
Expand Down Expand Up @@ -869,16 +861,6 @@ impl<R: Runtime> WindowManager<R> {
(self.inner.invoke_handler)(invoke)
}

pub fn run_on_page_load(&self, window: Window<R>, payload: PageLoadPayload) {
(self.inner.on_page_load)(window.clone(), payload.clone());
self
.inner
.plugins
.lock()
.expect("poisoned plugin store")
.on_page_load(window, payload);
}

pub fn extend_api(&self, plugin: &str, invoke: Invoke<R>) -> bool {
self
.inner
Expand Down Expand Up @@ -1376,7 +1358,6 @@ mod test {
context,
PluginStore::default(),
Box::new(|_| false),
Box::new(|_, _| ()),
Default::default(),
StateManager::new(),
Default::default(),
Expand Down
47 changes: 1 addition & 46 deletions core/tauri/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! The Tauri plugin extension to expand Tauri functionality.

use crate::{
app::{PageLoadPayload, UriSchemeResponder},
app::UriSchemeResponder,
error::Error,
ipc::{Invoke, InvokeHandler},
manager::UriSchemeProtocol,
Expand Down Expand Up @@ -60,10 +60,6 @@ pub trait Plugin<R: Runtime>: Send {
true
}

/// Callback invoked when the webview performs a navigation to a page.
#[allow(unused_variables)]
fn on_page_load(&mut self, window: Window<R>, payload: PageLoadPayload) {}

/// Callback invoked when the event loop receives a new event.
#[allow(unused_variables)]
fn on_event(&mut self, app: &AppHandle<R>, event: &RunEvent) {}
Expand All @@ -80,7 +76,6 @@ type SetupHook<R, C> =
type OnWebviewReady<R> = dyn FnMut(Window<R>) + Send;
type OnEvent<R> = dyn FnMut(&AppHandle<R>, &RunEvent) + Send;
type OnNavigation<R> = dyn Fn(&Window<R>, &Url) -> bool + Send;
type OnPageLoad<R> = dyn FnMut(Window<R>, PageLoadPayload) + Send;
type OnDrop<R> = dyn FnOnce(AppHandle<R>) + Send;

/// A handle to a plugin.
Expand Down Expand Up @@ -207,7 +202,6 @@ pub struct Builder<R: Runtime, C: DeserializeOwned = ()> {
setup: Option<Box<SetupHook<R, C>>>,
js_init_script: Option<String>,
on_navigation: Box<OnNavigation<R>>,
on_page_load: Box<OnPageLoad<R>>,
on_webview_ready: Box<OnWebviewReady<R>>,
on_event: Box<OnEvent<R>>,
on_drop: Option<Box<OnDrop<R>>>,
Expand All @@ -223,7 +217,6 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
js_init_script: None,
invoke_handler: Box::new(|_| false),
on_navigation: Box::new(|_, _| true),
on_page_load: Box::new(|_, _| ()),
on_webview_ready: Box::new(|_| ()),
on_event: Box::new(|_, _| ()),
on_drop: None,
Expand Down Expand Up @@ -358,30 +351,6 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
self
}

/// Callback invoked when the webview performs a navigation to a page.
///
/// # Examples
///
/// ```rust
/// use tauri::{plugin::{Builder, TauriPlugin}, Runtime};
///
/// fn init<R: Runtime>() -> TauriPlugin<R> {
/// Builder::new("example")
/// .on_page_load(|window, payload| {
/// println!("Loaded URL {} in window {}", payload.url(), window.label());
/// })
/// .build()
/// }
/// ```
#[must_use]
pub fn on_page_load<F>(mut self, on_page_load: F) -> Self
where
F: FnMut(Window<R>, PageLoadPayload) + Send + 'static,
{
self.on_page_load = Box::new(on_page_load);
self
}

/// Callback invoked when the webview is created.
///
/// # Examples
Expand Down Expand Up @@ -577,7 +546,6 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
setup: self.setup,
js_init_script: self.js_init_script,
on_navigation: self.on_navigation,
on_page_load: self.on_page_load,
on_webview_ready: self.on_webview_ready,
on_event: self.on_event,
on_drop: self.on_drop,
Expand All @@ -594,7 +562,6 @@ pub struct TauriPlugin<R: Runtime, C: DeserializeOwned = ()> {
setup: Option<Box<SetupHook<R, C>>>,
js_init_script: Option<String>,
on_navigation: Box<OnNavigation<R>>,
on_page_load: Box<OnPageLoad<R>>,
on_webview_ready: Box<OnWebviewReady<R>>,
on_event: Box<OnEvent<R>>,
on_drop: Option<Box<OnDrop<R>>>,
Expand Down Expand Up @@ -652,10 +619,6 @@ impl<R: Runtime, C: DeserializeOwned> Plugin<R> for TauriPlugin<R, C> {
(self.on_navigation)(window, url)
}

fn on_page_load(&mut self, window: Window<R>, payload: PageLoadPayload) {
(self.on_page_load)(window, payload)
}

fn on_event(&mut self, app: &AppHandle<R>, event: &RunEvent) {
(self.on_event)(app, event)
}
Expand Down Expand Up @@ -749,14 +712,6 @@ impl<R: Runtime> PluginStore<R> {
true
}

/// Runs the on_page_load hook for all plugins in the store.
pub(crate) fn on_page_load(&mut self, window: Window<R>, payload: PageLoadPayload) {
self
.store
.iter_mut()
.for_each(|plugin| plugin.on_page_load(window.clone(), payload.clone()))
}

/// Runs the on_event hook for all plugins in the store.
pub(crate) fn on_event(&mut self, app: &AppHandle<R>, event: &RunEvent) {
self
Expand Down
Loading
Loading