Skip to content

Commit

Permalink
Expose event interface. fixes #2733 (#3321)
Browse files Browse the repository at this point in the history
Co-authored-by: Cobalt <c0balt@disroot.org>
Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
  • Loading branch information
3 people committed Feb 4, 2022
1 parent bff86ee commit 15358b1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 26 deletions.
6 changes: 6 additions & 0 deletions .changes/api-change-events.md
@@ -0,0 +1,6 @@
---
"tauri": patch
---

* **Breaking change**: Renamed `tauri::Event` to `tauri::RunEvent`
* Exported `tauri::Event` and `tauri::EventHandler` so you can define a function and pass it to `Window::listen`
37 changes: 21 additions & 16 deletions core/tauri/src/app.rs
Expand Up @@ -17,7 +17,7 @@ use crate::{
http::{Request as HttpRequest, Response as HttpResponse},
webview::{WebviewAttributes, WindowBuilder},
window::{PendingWindow, WindowEvent},
Dispatch, ExitRequestedEventAction, RunEvent, Runtime,
Dispatch, ExitRequestedEventAction, RunEvent as RuntimeRunEvent, Runtime,
},
scope::FsScope,
sealed::{ManagerBase, RuntimeOrDispatch},
Expand Down Expand Up @@ -80,7 +80,7 @@ impl CloseRequestApi {
/// An application event, triggered from the event loop.
#[derive(Debug)]
#[non_exhaustive]
pub enum Event {
pub enum RunEvent {
/// Event loop is exiting.
Exit,
/// The app is about to exit
Expand Down Expand Up @@ -502,13 +502,18 @@ impl<R: Runtime> App<R> {
/// });
/// }
/// ```
pub fn run<F: FnMut(&AppHandle<R>, Event) + 'static>(mut self, mut callback: F) {
pub fn run<F: FnMut(&AppHandle<R>, RunEvent) + 'static>(mut self, mut callback: F) {
let app_handle = self.handle();
let manager = self.manager.clone();
self.runtime.take().unwrap().run(move |event| match event {
RunEvent::Exit => {
RuntimeRunEvent::Exit => {
app_handle.cleanup_before_exit();
on_event_loop_event(&app_handle, RunEvent::Exit, &manager, Some(&mut callback));
on_event_loop_event(
&app_handle,
RuntimeRunEvent::Exit,
&manager,
Some(&mut callback),
);
}
_ => {
on_event_loop_event(&app_handle, event, &manager, Some(&mut callback));
Expand Down Expand Up @@ -545,7 +550,7 @@ impl<R: Runtime> App<R> {
&app_handle,
event,
&manager,
Option::<&mut Box<dyn FnMut(&AppHandle<R>, Event)>>::None,
Option::<&mut Box<dyn FnMut(&AppHandle<R>, RunEvent)>>::None,
)
})
}
Expand Down Expand Up @@ -1187,30 +1192,30 @@ impl<R: Runtime> Builder<R> {
}
}

fn on_event_loop_event<R: Runtime, F: FnMut(&AppHandle<R>, Event) + 'static>(
fn on_event_loop_event<R: Runtime, F: FnMut(&AppHandle<R>, RunEvent) + 'static>(
app_handle: &AppHandle<R>,
event: RunEvent,
event: RuntimeRunEvent,
manager: &WindowManager<R>,
callback: Option<&mut F>,
) {
if let RunEvent::WindowClose(label) = &event {
if let RuntimeRunEvent::WindowClose(label) = &event {
manager.on_window_close(label);
}

let event = match event {
RunEvent::Exit => Event::Exit,
RunEvent::ExitRequested { window_label, tx } => Event::ExitRequested {
RuntimeRunEvent::Exit => RunEvent::Exit,
RuntimeRunEvent::ExitRequested { window_label, tx } => RunEvent::ExitRequested {
window_label,
api: ExitRequestApi(tx),
},
RunEvent::CloseRequested { label, signal_tx } => Event::CloseRequested {
RuntimeRunEvent::CloseRequested { label, signal_tx } => RunEvent::CloseRequested {
label,
api: CloseRequestApi(signal_tx),
},
RunEvent::WindowClose(label) => Event::WindowClosed(label),
RunEvent::Ready => Event::Ready,
RunEvent::Resumed => Event::Resumed,
RunEvent::MainEventsCleared => Event::MainEventsCleared,
RuntimeRunEvent::WindowClose(label) => RunEvent::WindowClosed(label),
RuntimeRunEvent::Ready => RunEvent::Ready,
RuntimeRunEvent::Resumed => RunEvent::Resumed,
RuntimeRunEvent::MainEventsCleared => RunEvent::MainEventsCleared,
_ => unimplemented!(),
};

Expand Down
13 changes: 6 additions & 7 deletions core/tauri/src/lib.rs
Expand Up @@ -172,10 +172,7 @@ pub type Result<T> = std::result::Result<T, Error>;
/// A task to run on the main thread.
pub type SyncTask = Box<dyn FnOnce() + Send>;

use crate::{
event::{Event as EmittedEvent, EventHandler},
runtime::window::PendingWindow,
};
use crate::runtime::window::PendingWindow;
use serde::Serialize;
use std::{collections::HashMap, fmt, sync::Arc};

Expand All @@ -197,12 +194,14 @@ pub use {
};
pub use {
self::app::WindowMenuEvent,
self::event::{Event, EventHandler},
self::runtime::menu::{CustomMenuItem, Menu, MenuEntry, MenuItem, Submenu},
self::window::menu::MenuEvent,
};
pub use {
self::app::{
App, AppHandle, AssetResolver, Builder, CloseRequestApi, Event, GlobalWindowEvent, PathResolver,
App, AppHandle, AssetResolver, Builder, CloseRequestApi, GlobalWindowEvent, PathResolver,
RunEvent,
},
self::hooks::{
Invoke, InvokeError, InvokeHandler, InvokeMessage, InvokePayload, InvokeResolver,
Expand Down Expand Up @@ -418,15 +417,15 @@ pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
/// Listen to a global event.
fn listen_global<F>(&self, event: impl Into<String>, handler: F) -> EventHandler
where
F: Fn(EmittedEvent) + Send + 'static,
F: Fn(Event) + Send + 'static,
{
self.manager().listen(event.into(), None, handler)
}

/// Listen to a global event only once.
fn once_global<F>(&self, event: impl Into<String>, handler: F) -> EventHandler
where
F: Fn(EmittedEvent) + Send + 'static,
F: Fn(Event) + Send + 'static,
{
self.manager().once(event.into(), None, handler)
}
Expand Down
7 changes: 4 additions & 3 deletions core/tauri/src/plugin.rs
Expand Up @@ -5,7 +5,8 @@
//! The Tauri plugin extension to expand Tauri functionality.

use crate::{
runtime::Runtime, utils::config::PluginConfig, AppHandle, Event, Invoke, PageLoadPayload, Window,
runtime::Runtime, utils::config::PluginConfig, AppHandle, Invoke, PageLoadPayload, RunEvent,
Window,
};
use serde_json::Value as JsonValue;
use tauri_macros::default_runtime;
Expand Down Expand Up @@ -45,7 +46,7 @@ pub trait Plugin<R: Runtime>: Send {

/// Callback invoked when the event loop receives a new event.
#[allow(unused_variables)]
fn on_event(&mut self, app: &AppHandle<R>, event: &Event) {}
fn on_event(&mut self, app: &AppHandle<R>, event: &RunEvent) {}

/// Extend commands to [`crate::Builder::invoke_handler`].
#[allow(unused_variables)]
Expand Down Expand Up @@ -126,7 +127,7 @@ impl<R: Runtime> PluginStore<R> {
}

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

0 comments on commit 15358b1

Please sign in to comment.