Skip to content

Commit

Permalink
feat: move app plugin back to core (#8039)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Nogueira <[email protected]>
  • Loading branch information
amrbashir and lucasfernog committed Oct 17, 2023
1 parent 1490567 commit fb10b87
Show file tree
Hide file tree
Showing 20 changed files with 303 additions and 207 deletions.
5 changes: 5 additions & 0 deletions .changes/api-app.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tauri-apps/api": 'minor:feat'
---

Add the `app` module back.
2 changes: 1 addition & 1 deletion .changes/api-window.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"@tauri-apps/api": 'minor:feat'
---

Add the `window` module back
Add the `window` module back.
5 changes: 5 additions & 0 deletions .changes/app-plugin-core.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tauri': patch:changes
---

Added the `app` plugin back into core.
2 changes: 1 addition & 1 deletion .changes/window-plugin-core.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
"tauri": patch:changes
'tauri': patch:changes
---

Added the `window` plugin back into core.
2 changes: 1 addition & 1 deletion core/tauri/scripts/bundle.global.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions core/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ use crate::runtime::RuntimeHandle;
#[cfg(target_os = "macos")]
use crate::ActivationPolicy;

pub(crate) mod plugin;

#[cfg(desktop)]
pub(crate) type GlobalMenuEventListener<T> = Box<dyn Fn(&T, crate::menu::MenuEvent) + Send + Sync>;
#[cfg(all(desktop, feature = "tray-icon"))]
Expand Down Expand Up @@ -808,6 +810,7 @@ impl<R: Runtime> App<R> {
self.handle.plugin(crate::path::init())?;
self.handle.plugin(crate::event::init())?;
self.handle.plugin(crate::window::plugin::init())?;
self.handle.plugin(crate::app::plugin::init())?;
Ok(())
}

Expand Down
52 changes: 52 additions & 0 deletions core/tauri/src/app/plugin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

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

#[command(root = "crate")]
pub fn version<R: Runtime>(app: AppHandle<R>) -> String {
app.package_info().version.to_string()
}

#[command(root = "crate")]
pub fn name<R: Runtime>(app: AppHandle<R>) -> String {
app.package_info().name.clone()
}

#[command(root = "crate")]
pub fn tauri_version() -> &'static str {
crate::VERSION
}

#[command(root = "crate")]
#[allow(unused_variables)]
pub fn app_show<R: Runtime>(app: AppHandle<R>) -> crate::Result<()> {
#[cfg(target_os = "macos")]
app.show()?;
Ok(())
}

#[command(root = "crate")]
#[allow(unused_variables)]
pub fn app_hide<R: Runtime>(app: AppHandle<R>) -> crate::Result<()> {
#[cfg(target_os = "macos")]
app.hide()?;
Ok(())
}

pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("app")
.invoke_handler(crate::generate_handler![
version,
name,
tauri_version,
app_show,
app_hide
])
.build()
}
14 changes: 7 additions & 7 deletions core/tauri/src/window/scripts/drag.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

document.addEventListener("mousedown", (e) => {
if (e.target.hasAttribute("data-tauri-drag-region") && e.button === 0) {
document.addEventListener('mousedown', (e) => {
if (e.target.hasAttribute('data-tauri-drag-region') && e.button === 0) {
// prevents text cursor
e.preventDefault();
e.preventDefault()
// fix #2549: double click on drag region edge causes content to maximize without window sizing change
// https://github.com/tauri-apps/tauri/issues/2549#issuecomment-1250036908
e.stopImmediatePropagation();
e.stopImmediatePropagation()

// start dragging if the element has a `tauri-drag-region` data attribute and maximize on double-clicking it
const cmd = e.detail === 2 ? "internal_toggle_maximize" : "start_dragging";
window.__TAURI_INVOKE__("plugin:window|" + cmd);
const cmd = e.detail === 2 ? 'internal_toggle_maximize' : 'start_dragging'
window.__TAURI_INTERNALS__.invoke('plugin:window|' + cmd)
}
});
})
4 changes: 2 additions & 2 deletions core/tauri/src/window/scripts/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
// SPDX-License-Identifier: MIT

window.print = function () {
return window.__TAURI_INVOKE__("plugin:window|print");
};
return window.__TAURI_INTERNALS__.invoke('plugin:window|print')
}
4 changes: 3 additions & 1 deletion core/tauri/src/window/scripts/toggle-devtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

document.addEventListener('keydown', (event) => {
if (isHotkey(event)) {
window.__TAURI_INVOKE__('plugin:window|internal_toggle_devtools')
window.__TAURI_INTERNALS__.invoke(
'plugin:window|internal_toggle_devtools'
)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion examples/api/dist/assets/index.css

Large diffs are not rendered by default.

71 changes: 37 additions & 34 deletions examples/api/dist/assets/index.js

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions examples/api/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
import Communication from './views/Communication.svelte'
import Window from './views/Window.svelte'
import WebRTC from './views/WebRTC.svelte'
import App from './views/App.svelte'
document.addEventListener('keydown', (event) => {
if (event.ctrlKey && event.key === 'b') {
invoke('toggle_menu')
}
})
const userAgent = navigator.userAgent.toLowerCase()
const isMobile = userAgent.includes('android') || userAgent.includes('iphone')
const views = [
{
label: 'Welcome',
Expand All @@ -25,6 +30,11 @@
component: Communication,
icon: 'i-codicon-radio-tower'
},
!isMobile && {
label: 'App',
component: App,
icon: 'i-codicon-hubot'
},
{
label: 'Window',
component: Window,
Expand Down
33 changes: 33 additions & 0 deletions examples/api/src/views/App.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<script>
import { show, hide } from '@tauri-apps/api/app'
export let onMessage
function showApp() {
hideApp()
.then(() => {
setTimeout(() => {
show()
.then(() => onMessage('Shown app'))
.catch(onMessage)
}, 2000)
})
.catch(onMessage)
}
function hideApp() {
return hide()
.then(() => onMessage('Hide app'))
.catch(onMessage)
}
</script>

<div>
<button
class="btn"
id="show"
title="Hides and shows the app after 2 seconds"
on:click={showApp}>Show</button
>
<button class="btn" id="hide" on:click={hideApp}>Hide</button>
</div>
23 changes: 23 additions & 0 deletions examples/api/src/views/Welcome.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
<script>
import { invoke } from '@tauri-apps/api/primitives'
import { getName, getVersion, getTauriVersion } from '@tauri-apps/api/app'
let version = '1.0.0'
let tauriVersion = '1.0.0'
let appName = 'Unknown'
getName().then((n) => {
appName = n
})
getVersion().then((v) => {
version = v
})
getTauriVersion().then((v) => {
tauriVersion = v
})
function contextMenu() {
invoke('popup_context_menu')
Expand All @@ -14,6 +29,14 @@
development process. In the future, this app will be used on Tauri's integration
tests.
</p>
<br />
<br />
<pre>
App name: <code>{appName}</code>
App version: <code>{version}</code>
Tauri version: <code>{tauriVersion}</code>
</pre>
<br />

<button class="btn" on:click={contextMenu}>Context menu</button>
</div>
2 changes: 1 addition & 1 deletion tooling/api/docs/js-api.json

Large diffs are not rendered by default.

86 changes: 86 additions & 0 deletions tooling/api/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

import { invoke } from './primitives'

/**
* Application metadata and related APIs.
*
* @module
*/

/**
* Gets the application version.
* @example
* ```typescript
* import { getVersion } from '@tauri-apps/api/app';
* const appVersion = await getVersion();
* ```
*
* @since 1.0.0
*/
async function getVersion(): Promise<string> {
return invoke('plugin:app|version')
}

/**
* Gets the application name.
* @example
* ```typescript
* import { getName } from '@tauri-apps/api/app';
* const appName = await getName();
* ```
*
* @since 1.0.0
*/
async function getName(): Promise<string> {
return invoke('plugin:app|name')
}

/**
* Gets the Tauri version.
*
* @example
* ```typescript
* import { getTauriVersion } from '@tauri-apps/api/app';
* const tauriVersion = await getTauriVersion();
* ```
*
* @since 1.0.0
*/
async function getTauriVersion(): Promise<string> {
return invoke('plugin:app|tauri_version')
}

/**
* Shows the application on macOS. This function does not automatically focus any specific app window.
*
* @example
* ```typescript
* import { show } from '@tauri-apps/api/app';
* await show();
* ```
*
* @since 1.2.0
*/
async function show(): Promise<void> {
return invoke('plugin:app|app_show')
}

/**
* Hides the application on macOS.
*
* @example
* ```typescript
* import { hide } from '@tauri-apps/api/app';
* await hide();
* ```
*
* @since 1.2.0
*/
async function hide(): Promise<void> {
return invoke('plugin:app|app_hide')
}

export { getName, getVersion, getTauriVersion, show, hide }
3 changes: 2 additions & 1 deletion tooling/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
* @module
*/

import * as app from './app'
import * as event from './event'
import * as primitives from './primitives'
import * as window from './window'
import * as path from './path'
import * as dpi from './dpi'

export { dpi, event, path, primitives, window }
export { app, dpi, event, path, primitives, window }
Loading

0 comments on commit fb10b87

Please sign in to comment.