Skip to content

Commit

Permalink
refactor: change path and event plugins to follow the same convention…
Browse files Browse the repository at this point in the history
… as `window` (#8040)
  • Loading branch information
amrbashir authored Oct 17, 2023
1 parent fb10b87 commit a6ad540
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 75 deletions.
4 changes: 2 additions & 2 deletions core/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,8 +807,8 @@ shared_app_impl!(AppHandle<R>);

impl<R: Runtime> App<R> {
fn register_core_plugins(&self) -> crate::Result<()> {
self.handle.plugin(crate::path::init())?;
self.handle.plugin(crate::event::init())?;
self.handle.plugin(crate::path::plugin::init())?;
self.handle.plugin(crate::event::plugin::init())?;
self.handle.plugin(crate::window::plugin::init())?;
self.handle.plugin(crate::app::plugin::init())?;
Ok(())
Expand Down
18 changes: 1 addition & 17 deletions core/tauri/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

mod commands;
mod listener;
pub(crate) mod plugin;
pub(crate) use listener::Listeners;

use crate::{
plugin::{Builder, TauriPlugin},
Runtime,
};

/// Checks if an event name is valid.
pub fn is_event_name_valid(event: &str) -> bool {
event
Expand Down Expand Up @@ -47,17 +42,6 @@ impl Event {
}
}

/// Initializes the event plugin.
pub(crate) fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("event")
.invoke_handler(crate::generate_handler![
commands::listen,
commands::unlisten,
commands::emit,
])
.build()
}

pub fn unlisten_js(listeners_object_name: &str, event_name: &str, event_id: EventId) -> String {
format!(
"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use crate::{command, ipc::CallbackFn, EventId, Manager, Result, Runtime, Window};
use serde::{Deserialize, Deserializer};
use serde_json::Value as JsonValue;
use tauri_runtime::window::is_label_valid;

use crate::plugin::{Builder, TauriPlugin};
use crate::{command, ipc::CallbackFn, EventId, Manager, Result, Runtime, Window};

use super::is_event_name_valid;

pub struct EventName(String);
Expand Down Expand Up @@ -94,3 +96,10 @@ pub fn emit<R: Runtime>(
window.emit_all(&event.0, payload)
}
}

/// Initializes the event plugin.
pub(crate) fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("event")
.invoke_handler(crate::generate_handler![listen, unlisten, emit,])
.build()
}
57 changes: 3 additions & 54 deletions core/tauri/src/path/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@

use std::path::{Component, Display, Path, PathBuf};

use crate::{
plugin::{Builder, TauriPlugin},
Manager, Runtime,
};
use crate::Runtime;

use serde::{de::Error as DeError, Deserialize, Deserializer};
use serde_repr::{Deserialize_repr, Serialize_repr};
use serialize_to_javascript::{default_template, DefaultTemplate, Template};

mod commands;
pub(crate) mod plugin;

pub use crate::error::*;

#[cfg(target_os = "android")]
Expand Down Expand Up @@ -330,54 +327,6 @@ fn resolve_path<R: Runtime>(
Ok(base_dir_path)
}

#[derive(Template)]
#[default_template("./init.js")]
struct InitJavascript {
sep: &'static str,
delimiter: &'static str,
}

/// Initializes the plugin.
pub(crate) fn init<R: Runtime>() -> TauriPlugin<R> {
#[cfg(windows)]
let (sep, delimiter) = ("\\", ";");
#[cfg(not(windows))]
let (sep, delimiter) = ("/", ":");

let init_js = InitJavascript { sep, delimiter }
.render_default(&Default::default())
// this will never fail with the above sep and delimiter values
.unwrap();

Builder::new("path")
.invoke_handler(crate::generate_handler![
commands::resolve_directory,
commands::resolve,
commands::normalize,
commands::join,
commands::dirname,
commands::extname,
commands::basename,
commands::is_absolute
])
.js_init_script(init_js.to_string())
.setup(|app, _api| {
#[cfg(target_os = "android")]
{
let handle = _api.register_android_plugin("app.tauri", "PathPlugin")?;
app.manage(PathResolver(handle));
}

#[cfg(not(target_os = "android"))]
{
app.manage(PathResolver(app.clone()));
}

Ok(())
})
.build()
}

#[cfg(test)]
mod test {
use super::SafePathBuf;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@

use std::path::{Component, Path, PathBuf, MAIN_SEPARATOR};

use serialize_to_javascript::{default_template, DefaultTemplate, Template};

use super::{BaseDirectory, Error, PathResolver, Result};
use crate::{command, AppHandle, Runtime, State};
use crate::{
command,
plugin::{Builder, TauriPlugin},
AppHandle, Manager, Runtime, State,
};

/// Normalize a path, removing things like `.` and `..`, this snippet is taken from cargo's paths util.
/// https://github.com/rust-lang/cargo/blob/46fa867ff7043e3a0545bf3def7be904e1497afd/crates/cargo-util/src/paths.rs#L73-L106
Expand Down Expand Up @@ -191,3 +197,51 @@ pub fn basename(path: String, ext: Option<String>) -> Result<String> {
pub fn is_absolute(path: String) -> bool {
Path::new(&path).is_absolute()
}

#[derive(Template)]
#[default_template("./init.js")]
struct InitJavascript {
sep: &'static str,
delimiter: &'static str,
}

/// Initializes the plugin.
pub(crate) fn init<R: Runtime>() -> TauriPlugin<R> {
#[cfg(windows)]
let (sep, delimiter) = ("\\", ";");
#[cfg(not(windows))]
let (sep, delimiter) = ("/", ":");

let init_js = InitJavascript { sep, delimiter }
.render_default(&Default::default())
// this will never fail with the above sep and delimiter values
.unwrap();

Builder::new("path")
.invoke_handler(crate::generate_handler![
resolve_directory,
resolve,
normalize,
join,
dirname,
extname,
basename,
is_absolute
])
.js_init_script(init_js.to_string())
.setup(|app, _api| {
#[cfg(target_os = "android")]
{
let handle = _api.register_android_plugin("app.tauri", "PathPlugin")?;
app.manage(PathResolver(handle));
}

#[cfg(not(target_os = "android"))]
{
app.manage(PathResolver(app.clone()));
}

Ok(())
})
.build()
}

0 comments on commit a6ad540

Please sign in to comment.