Skip to content

Commit

Permalink
feat: add title getter on window, closes #5023 (#5515)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
amrbashir and lucasfernog committed Dec 13, 2022
1 parent d0d873e commit 233e43b
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 83 deletions.
8 changes: 8 additions & 0 deletions .changes/title-getter.md
@@ -0,0 +1,8 @@
---
"tauri": "minor"
"api": "minor"
"tauri-runtime": "minor"
"tauri-runtime-wry": "minor"
---

Add `title` getter on window.
6 changes: 6 additions & 0 deletions core/tauri-runtime-wry/src/lib.rs
Expand Up @@ -1028,6 +1028,7 @@ pub enum WindowMessage {
IsDecorated(Sender<bool>),
IsResizable(Sender<bool>),
IsVisible(Sender<bool>),
Title(Sender<String>),
IsMenuVisible(Sender<bool>),
CurrentMonitor(Sender<Option<MonitorHandle>>),
PrimaryMonitor(Sender<Option<MonitorHandle>>),
Expand Down Expand Up @@ -1262,6 +1263,10 @@ impl<T: UserEvent> Dispatch<T> for WryDispatcher<T> {
window_getter!(self, WindowMessage::IsVisible)
}

fn title(&self) -> Result<String> {
window_getter!(self, WindowMessage::Title)
}

fn is_menu_visible(&self) -> Result<bool> {
window_getter!(self, WindowMessage::IsMenuVisible)
}
Expand Down Expand Up @@ -2349,6 +2354,7 @@ fn handle_user_message<T: UserEvent>(
WindowMessage::IsDecorated(tx) => tx.send(window.is_decorated()).unwrap(),
WindowMessage::IsResizable(tx) => tx.send(window.is_resizable()).unwrap(),
WindowMessage::IsVisible(tx) => tx.send(window.is_visible()).unwrap(),
WindowMessage::Title(tx) => tx.send(window.title()).unwrap(),
WindowMessage::IsMenuVisible(tx) => tx.send(window.is_menu_visible()).unwrap(),
WindowMessage::CurrentMonitor(tx) => tx.send(window.current_monitor()).unwrap(),
WindowMessage::PrimaryMonitor(tx) => tx.send(window.primary_monitor()).unwrap(),
Expand Down
2 changes: 2 additions & 0 deletions core/tauri-runtime/src/lib.rs
Expand Up @@ -536,6 +536,8 @@ pub trait Dispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + 'static

/// Gets the window's current visibility state.
fn is_visible(&self) -> Result<bool>;
/// Gets the window's current title.
fn title(&self) -> Result<String>;

/// Gets the window menu current visibility state.
fn is_menu_visible(&self) -> Result<bool>;
Expand Down
8 changes: 4 additions & 4 deletions core/tauri/scripts/bundle.global.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions core/tauri/src/endpoints/window.rs
Expand Up @@ -68,6 +68,7 @@ pub enum WindowManagerCmd {
IsDecorated,
IsResizable,
IsVisible,
Title,
CurrentMonitor,
PrimaryMonitor,
AvailableMonitors,
Expand Down Expand Up @@ -259,6 +260,7 @@ impl Cmd {
WindowManagerCmd::IsDecorated => return Ok(window.is_decorated()?.into()),
WindowManagerCmd::IsResizable => return Ok(window.is_resizable()?.into()),
WindowManagerCmd::IsVisible => return Ok(window.is_visible()?.into()),
WindowManagerCmd::Title => return Ok(window.title()?.into()),
WindowManagerCmd::CurrentMonitor => return Ok(window.current_monitor()?.into()),
WindowManagerCmd::PrimaryMonitor => return Ok(window.primary_monitor()?.into()),
WindowManagerCmd::AvailableMonitors => return Ok(window.available_monitors()?.into()),
Expand Down
4 changes: 4 additions & 0 deletions core/tauri/src/test/mock_runtime.rs
Expand Up @@ -379,6 +379,10 @@ impl<T: UserEvent> Dispatch<T> for MockDispatcher {
Ok(true)
}

fn title(&self) -> Result<String> {
Ok(String::new())
}

fn is_menu_visible(&self) -> Result<bool> {
Ok(true)
}
Expand Down
5 changes: 5 additions & 0 deletions core/tauri/src/window.rs
Expand Up @@ -913,6 +913,11 @@ impl<R: Runtime> Window<R> {
self.window.dispatcher.is_visible().map_err(Into::into)
}

/// Gets the window's current title.
pub fn title(&self) -> crate::Result<String> {
self.window.dispatcher.title().map_err(Into::into)
}

/// Returns the monitor on which the window currently resides.
///
/// Returns None if current monitor can't be detected.
Expand Down
2 changes: 1 addition & 1 deletion examples/api/dist/assets/index.css

Large diffs are not rendered by default.

30 changes: 15 additions & 15 deletions examples/api/dist/assets/index.js

Large diffs are not rendered by default.

78 changes: 15 additions & 63 deletions examples/api/src-tauri/Cargo.lock

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

25 changes: 25 additions & 0 deletions tooling/api/src/window.ts
Expand Up @@ -704,6 +704,31 @@ class WindowManager extends WebviewWindowHandle {
})
}

/**
* Gets the window's current title.
* @example
* ```typescript
* import { appWindow } from '@tauri-apps/api/window';
* const title = await appWindow.title();
* ```
*
* @since 1.3.0
* */
async title(): Promise<string> {
return invokeTauriCommand({
__tauriModule: 'Window',
message: {
cmd: 'manage',
data: {
label: this.label,
cmd: {
type: 'title'
}
}
}
})
}

/**
* Gets the window's current theme.
*
Expand Down

0 comments on commit 233e43b

Please sign in to comment.