Skip to content

Commit

Permalink
refactor: use new builder pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasKruckenberg committed Feb 26, 2022
1 parent 7b96fee commit 14837a8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 39 deletions.
5 changes: 5 additions & 0 deletions .changes/use-builder-pattern.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri-plugin-positioner": major
---

***Breaking Change**: Uses the new Tauri plugin builder pattern. Use `tauri_plugin_positioner::init()` instead of `tauri_plugin_positioner::Positioner::default()`.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[mit-badge]: https://img.shields.io/badge/license-MIT-blue.svg
[mit-url]: LICENSE

A plugin for tauri that helps positioning you windows at well known locations.
A plugin for Tauri that helps position your windows at well-known locations.

This plugin is a port of [electron-positioner](https://github.com/jenslind/electron-positioner) for [tauri](https://tauri.studio).

Expand Down Expand Up @@ -39,17 +39,15 @@ yarn add tauri-plugin-positioner
You need to register the plugin first:

```rust
use tauri_plugin_positioner::{Positioner, Position};

fn main() {
tauri::Builder::default()
.plugin(Positioner::default())
.plugin(tauri_plugin_positioner::init())
.build()
.run();
}
```

Now you can import the JavaScript API package and move to window:
Now you can import the JavaScript API package and move the window:

```javascript
import { move_window, Position } from 'tauri-plugin-positioner-api'
Expand All @@ -59,7 +57,7 @@ move_window(Position.TopRight)

### Rust only

If you only intend on moving the window from rust code, you can just import the `Window` trait extension instead of registering the plugin:
If you only intend on moving the window from rust code, you can import the `Window` trait extension instead of registering the plugin:

> Note: `Window.move_window` method must be called from a different thread!
Expand Down
52 changes: 19 additions & 33 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
// Copyright 2021 Jonas Kruckenberg
// SPDX-License-Identifier: MIT

//! A plugin for Tauri that helps position your windows at well-known locations.
//!
//! # Cargo features
//!
//! - **system-tray**: Enables system-tray-relative positions.
//!
//! Note: This requires attaching the Tauri plugin, *even* when using the trait extension only.
mod ext;

pub use ext::*;
use tauri::{plugin::Plugin, Invoke, Result, Runtime};
use tauri::{plugin::{self, TauriPlugin}, Result, Runtime};

#[cfg(feature = "system-tray")]
use tauri::{AppHandle, Manager, PhysicalPosition, PhysicalSize, SystemTrayEvent};
Expand Down Expand Up @@ -48,35 +56,13 @@ async fn move_window<R: Runtime>(window: tauri::Window<R>, position: Position) -
window.move_window(position)
}

/// The tauri plugin that exposes [`WindowExt::move_window`] to the webview.
pub struct Positioner<R: Runtime> {
invoke_handler: Box<dyn Fn(Invoke<R>) + Send + Sync>,
}

impl<R: Runtime> Default for Positioner<R> {
fn default() -> Self {
Self {
invoke_handler: Box::new(tauri::generate_handler![move_window]),
}
}
}

impl<R: Runtime> Plugin<R> for Positioner<R> {
fn name(&self) -> &'static str {
"positioner"
}

#[cfg(feature = "system-tray")]
fn initialize(
&mut self,
app: &AppHandle<R>,
_config: serde_json::Value,
) -> tauri::plugin::Result<()> {
app.manage(Tray(std::sync::Mutex::new(None)));
Ok(())
}

fn extend_api(&mut self, message: Invoke<R>) {
(self.invoke_handler)(message)
}
}
/// The Tauri plugin that exposes [`WindowExt::move_window`] to the webview.
pub fn init<R: Runtime>() -> TauriPlugin<R> {
plugin::Builder::new("positioner")
.invoke_handler(tauri::generate_handler![move_window])
.setup(|app_handle| {
app_handle.manage(Tray(std::sync::Mutex::new(None)));
Ok(())
})
.build()
}

0 comments on commit 14837a8

Please sign in to comment.