Skip to content

Commit

Permalink
Added initial window module functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
bicarlsen committed Jul 23, 2024
1 parent 125136a commit b372e01
Show file tree
Hide file tree
Showing 9 changed files with 931 additions and 29 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ wasm-bindgen-test = "0.3.42"
all-features = true

[features]
all = ["core", "event"]
all = ["core", "dpi", "event", "window"]
core = []
dpi = []
event = ["dep:futures"]
window = ["dpi", "event"]

[workspace]
members = ["examples/test", "examples/test/src-tauri"]
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ All modules are gated by accordingly named Cargo features. It is recommended you
- **all**: Enables all modules.
- **core**: Enables the `core` module. (Only `invoke` and `convertFileSrc` currently implemented.)
- **event**: Enables the `event` module.
- **window**: Enables the `windows` module. (~20% implemented)

## Are we Tauri yet?

These API bindings are not completely on-par with `@tauri-apps/api` yet, but here is the current status-quo:

- [ ] `app`
- [x] `core` (partial implementation)
- [ ] `dpi`
- [x] `dpi`
- [x] `event`
- [ ] `image`
- [ ] `menu`
Expand All @@ -69,7 +70,7 @@ These API bindings are not completely on-par with `@tauri-apps/api` yet, but her
- [ ] `tray`
- [ ] `webview`
- [ ] `webviewWindow`
- [ ] `window`
- [x] `window` (partial implementation)

The current API also very closely mirrors the JS API even though that might not be the most ergonomic choice, ideas for improving the API with quality-of-life features beyond the regular JS API interface are very welcome.

Expand Down
1 change: 0 additions & 1 deletion src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ mod inner {

#[wasm_bindgen(module = "/src/core.js")]
extern "C" {
#[wasm_bindgen]
pub async fn invoke(cmd: &str, args: JsValue) -> JsValue;
#[wasm_bindgen(js_name = "invoke", catch)]
pub async fn invoke_result(cmd: &str, args: JsValue) -> Result<JsValue, JsValue>;
Expand Down
110 changes: 110 additions & 0 deletions src/dpi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
use serde::Deserialize;

pub type ScaleFactor = f64;
pub type PixelCount = isize;

#[derive(Debug)]
pub enum Kind {
Logical,
Physical,
}

/// A size represented in logical pixels.
#[derive(Deserialize, Clone, Debug)]
pub struct LogicalSize {
width: PixelCount,
height: PixelCount,
}

impl LogicalSize {
pub fn kind() -> Kind {
Kind::Logical
}

pub fn width(&self) -> PixelCount {
self.width
}

pub fn height(&self) -> PixelCount {
self.height
}
}

/// A size represented in physical pixels.
#[derive(Deserialize, Clone, Debug)]
pub struct PhysicalSize {
width: PixelCount,
height: PixelCount,
}

impl PhysicalSize {
pub fn kind() -> Kind {
Kind::Physical
}

pub fn width(&self) -> PixelCount {
self.width
}

pub fn height(&self) -> PixelCount {
self.height
}

/// Converts the physical size to a logical one.
pub fn as_logical(&self, scale_factor: ScaleFactor) -> LogicalSize {
LogicalSize {
width: (self.width as f64 / scale_factor) as PixelCount,
height: (self.height as f64 / scale_factor) as PixelCount,
}
}
}

/// A position represented in logical pixels.
#[derive(Deserialize, Clone, Debug)]
pub struct LogicalPosition {
x: PixelCount,
y: PixelCount,
}

impl LogicalPosition {
pub fn kind() -> Kind {
Kind::Logical
}

pub fn x(&self) -> PixelCount {
self.x
}

pub fn y(&self) -> PixelCount {
self.y
}
}

/// A position represented in physical pixels.
#[derive(Deserialize, Clone, Debug)]
pub struct PhysicalPosition {
x: PixelCount,
y: PixelCount,
}

impl PhysicalPosition {
pub fn kind() -> Kind {
Kind::Physical
}

pub fn x(&self) -> PixelCount {
self.x
}

pub fn y(&self) -> PixelCount {
self.y
}

/// Converts the physical position to a logical one.
pub fn as_logical(&self, scale_factor: ScaleFactor) -> LogicalPosition {
LogicalPosition {
x: (self.x as f64 / scale_factor) as PixelCount,
y: (self.y as f64 / scale_factor) as PixelCount,
}
}
}
21 changes: 1 addition & 20 deletions src/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,9 @@ async function once(event, handler, options) {
)
}

// tauri/tooling/api/src/event.ts
var TauriEvent = /* @__PURE__ */ ((TauriEvent2) => {
TauriEvent2["WINDOW_RESIZED"] = 'tauri://resize';
TauriEvent2["WINDOW_MOVED"] = 'tauri://move';
TauriEvent2["WINDOW_CLOSE_REQUESTED"] = 'tauri://close-requested';
TauriEvent2["WINDOW_DESTROYED"] = 'tauri://destroyed';
TauriEvent2["WINDOW_FOCUS"] = 'tauri://focus';
TauriEvent2["WINDOW_BLUR"] = 'tauri://blur';
TauriEvent2["WINDOW_SCALE_FACTOR_CHANGED"] = 'tauri://scale-change';
TauriEvent2["WINDOW_THEME_CHANGED"] = 'tauri://theme-changed';
TauriEvent2["WINDOW_CREATED"] = 'tauri://window-created';
TauriEvent2["WEBVIEW_CREATED"] = 'tauri://webview-created';
TauriEvent2["DRAG"] = 'tauri://drag';
TauriEvent2["DROP"] = 'tauri://drop';
TauriEvent2["DROP_OVER"] = 'tauri://drop-over';
TauriEvent2["DROP_CANCELLED"] = 'tauri://drag-cancelled';
return TauriEvent2;
})(TauriEvent || {});
export {
TauriEvent,
emit,
emitTo,
listen,
once
once,
};
24 changes: 19 additions & 5 deletions src/event.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! The event system allows you to emit events to the backend and listen to events from it.
use futures::{
channel::{mpsc, oneshot},
Future, FutureExt, Stream, StreamExt,
Expand All @@ -8,13 +7,28 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::fmt::Debug;
use wasm_bindgen::{prelude::Closure, JsValue};

pub const WINDOW_RESIZED: &str = "tauri://resize";
pub const WINDOW_MOVED: &str = "tauri://move";
pub const WINDOW_CLOSE_REQUESTED: &str = "tauri://close-requested";
pub const WINDOW_DESTROYED: &str = "tauri://destroyed";
pub const WINDOW_FOCUS: &str = "tauri://focus";
pub const WINDOW_BLUR: &str = "tauri://blur";
pub const WINDOW_SCALE_FACTOR_CHANGED: &str = "tauri://scale-change";
pub const WINDOW_THEME_CHANGED: &str = "tauri://theme-changed";
pub const WINDOW_CREATED: &str = "tauri://window-created";
pub const WEBVIEW_CREATED: &str = "tauri://webview-created";
pub const DRAG: &str = "tauri://drag";
pub const DROP: &str = "tauri://drop";
pub const DROP_OVER: &str = "tauri://drop-over";
pub const DROP_CANCELLED: &str = "tauri://drag-cancelled";

#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Event<T> {
/// Event name
pub event: String,
/// Event identifier used to unlisten
pub id: f32,
pub id: isize,
/// Event payload
pub payload: T,
}
Expand All @@ -31,8 +45,8 @@ pub enum EventTarget {
}

#[derive(Debug, Clone, PartialEq, Serialize)]
struct Options {
target: EventTarget,
pub(crate) struct Options {
pub target: EventTarget,
}

/// Emits an event to the backend.
Expand Down Expand Up @@ -334,7 +348,7 @@ impl<T> Future for Once<T> {
}
}

mod inner {
pub(crate) mod inner {
use wasm_bindgen::{
prelude::{wasm_bindgen, Closure},
JsValue,
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ pub mod event;
#[cfg(feature = "core")]
pub mod core;

#[cfg(feature = "dpi")]
pub mod dpi;

#[cfg(feature = "window")]
pub mod window;

pub use error::Error;
pub(crate) type Result<T> = std::result::Result<T, Error>;

Expand Down
38 changes: 38 additions & 0 deletions src/window.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// tauri/tooling/api/src/core.ts
async function invoke(cmd, args = {}) {
// NB: `options` ignored as not used here.
return window.__TAURI_INTERNALS__.invoke(cmd, args)
}

// tauri/tooling/api/src/window.ts
function getCurrent() {
return window.__TAURI_INTERNALS__.metadata.currentWindow
}
function getAll() {
return window.__TAURI_INTERNALS__.metadata.windows
}
async function currentMonitor() {
return invoke('plugin:window|current_monitor')
}
async function primaryMonitor() {
return invoke('plugin:window|primary_monitor')
}
async function monitorFromPoint(x, y) {
return invoke('plugin:window|monitor_from_point', { x, y })
}
async function availableMonitors() {
return invoke('plugin:window|available_monitors')
}
async function cursorPosition() {
return invoke('plugin:window|cursor_position')
}

export {
getCurrent,
getAll,
currentMonitor,
primaryMonitor,
monitorFromPoint,
availableMonitors,
cursorPosition,
}
Loading

0 comments on commit b372e01

Please sign in to comment.