Skip to content

Commit

Permalink
feat(positioner, window-state): impl WindowExt for WebviewWindow (#…
Browse files Browse the repository at this point in the history
…1283)

closes #1281
  • Loading branch information
amrbashir authored May 3, 2024
1 parent b4efa58 commit d9de5b1
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 35 deletions.
6 changes: 6 additions & 0 deletions .changes/impl-ext-for-webview-windows.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"positioner": "patch"
"window-state": "patch"
---

Implement `WindowExt` for `WebviewWindow`.
58 changes: 29 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion plugins/positioner/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::Tray;
use serde_repr::Deserialize_repr;
#[cfg(feature = "tray-icon")]
use tauri::Manager;
use tauri::{PhysicalPosition, PhysicalSize, Result, Runtime, Window};
use tauri::{PhysicalPosition, PhysicalSize, Result, Runtime, WebviewWindow, Window};

/// Well known window positions.
#[derive(Debug, Deserialize_repr)]
Expand Down Expand Up @@ -45,6 +45,11 @@ pub trait WindowExt {
fn move_window(&self, position: Position) -> Result<()>;
}

impl<R: Runtime> WindowExt for WebviewWindow<R> {
fn move_window(&self, pos: Position) -> Result<()> {
self.as_ref().window().move_window(pos)
}
}
impl<R: Runtime> WindowExt for Window<R> {
fn move_window(&self, pos: Position) -> Result<()> {
use Position::*;
Expand Down
2 changes: 0 additions & 2 deletions plugins/window-state/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ pub async fn restore_state<R: Runtime>(
.ok_or_else(|| format!("Invalid state flags bits: {}", flags))?;
app.get_webview_window(&label)
.ok_or_else(|| format!("Couldn't find window with label: {}", label))?
.as_ref()
.window()
.restore_state(flags)
.map_err(|e| e.to_string())?;
Ok(())
Expand Down
17 changes: 14 additions & 3 deletions plugins/window-state/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use bitflags::bitflags;
use serde::{Deserialize, Serialize};
use tauri::{
plugin::{Builder as PluginBuilder, TauriPlugin},
LogicalSize, Manager, Monitor, PhysicalPosition, PhysicalSize, RunEvent, Runtime, Window,
WindowEvent,
LogicalSize, Manager, Monitor, PhysicalPosition, PhysicalSize, RunEvent, Runtime,
WebviewWindow, Window, WindowEvent,
};

use std::{
Expand Down Expand Up @@ -118,7 +118,7 @@ impl<R: Runtime> AppHandleExt for tauri::AppHandle<R> {
let mut state = cache.0.lock().unwrap();
for (label, s) in state.iter_mut() {
if let Some(window) = self.get_webview_window(label) {
window.as_ref().window().update_state(s, flags)?;
window.update_state(s, flags)?;
}
}

Expand All @@ -141,6 +141,11 @@ pub trait WindowExt {
fn restore_state(&self, flags: StateFlags) -> tauri::Result<()>;
}

impl<R: Runtime> WindowExt for WebviewWindow<R> {
fn restore_state(&self, flags: StateFlags) -> tauri::Result<()> {
self.as_ref().window().restore_state(flags)
}
}
impl<R: Runtime> WindowExt for Window<R> {
fn restore_state(&self, flags: StateFlags) -> tauri::Result<()> {
let cache = self.state::<WindowStateCache>();
Expand Down Expand Up @@ -246,6 +251,12 @@ trait WindowExtInternal {
fn update_state(&self, state: &mut WindowState, flags: StateFlags) -> tauri::Result<()>;
}

impl<R: Runtime> WindowExtInternal for WebviewWindow<R> {
fn update_state(&self, state: &mut WindowState, flags: StateFlags) -> tauri::Result<()> {
self.as_ref().window().update_state(state, flags)
}
}

impl<R: Runtime> WindowExtInternal for Window<R> {
fn update_state(&self, state: &mut WindowState, flags: StateFlags) -> tauri::Result<()> {
let is_maximized = match flags.intersects(StateFlags::MAXIMIZED | StateFlags::SIZE) {
Expand Down

0 comments on commit d9de5b1

Please sign in to comment.