Skip to content

Commit

Permalink
basic tray example
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Oct 23, 2023
1 parent d894b16 commit e974000
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 15 deletions.
2 changes: 1 addition & 1 deletion core/tauri/scripts/bundle.global.js

Large diffs are not rendered by default.

12 changes: 5 additions & 7 deletions core/tauri/src/tray/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
plugin::{Builder, TauriPlugin},
resources::ResourceId,
tray::TrayIconBuilder,
AppHandle, IconDto, Manager, Runtime,
AppHandle, IconDto, Runtime,
};

use super::TrayIcon;
Expand All @@ -35,19 +35,17 @@ struct TrayIconOptions {
fn new<R: Runtime>(
app: AppHandle<R>,
options: TrayIconOptions,
handler: Option<Channel>,
handler: Channel,
) -> crate::Result<(ResourceId, String)> {
let mut builder = if let Some(id) = options.id {
TrayIconBuilder::<R>::with_id(id)
} else {
TrayIconBuilder::<R>::new()
};

if let Some(handler) = handler {
builder = builder.on_tray_event(|tray, e| {
let _ = handler.send(e);
});
}
builder = builder.on_tray_icon_event(move |_tray, e| {
let _ = handler.send(e);
});

let mut resources_table = app.manager.resources_table();

Expand Down
6 changes: 3 additions & 3 deletions examples/api/src-tauri/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 examples/api/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Window from './views/Window.svelte'
import WebRTC from './views/WebRTC.svelte'
import App from './views/App.svelte'
import Tray from './views/Tray.svelte'
document.addEventListener('keydown', (event) => {
if (event.ctrlKey && event.key === 'b') {
Expand Down Expand Up @@ -40,6 +40,11 @@
component: Window,
icon: 'i-codicon-window'
},
{
label: 'Tray',
component: Tray,
icon: 'i-ph-broadcast'
},
{
label: 'WebRTC',
component: WebRTC,
Expand Down
65 changes: 65 additions & 0 deletions examples/api/src/views/Tray.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<script>
import { TrayIcon } from '@tauri-apps/api/tray'
export let onMessage
let icon = null
let tooltip = null
let title = null
let iconAsTemplate = false
let menuOnLeftClick = true
function create() {
TrayIcon.new({
icon,
tooltip,
title,
iconAsTemplate,
menuOnLeftClick,
action: (event) => onMessage(event)
}).catch(onMessage)
}
</script>

<div class="flex flex-col children:grow gap-2">
<div class="flex gap-1">
<input
class="input grow"
type="text"
placeholder="Title"
bind:value={title}
/>

<input
class="input grow"
type="text"
placeholder="Tooltip"
bind:value={tooltip}
/>

<label>
Menu on left click
<input type="checkbox" bind:checked={menuOnLeftClick} />
</label>
</div>

<div class="flex gap-1">
<input
class="input grow"
type="text"
placeholder="Icon path"
bind:value={icon}
/>

<label>
Icon as template
<input type="checkbox" bind:checked={iconAsTemplate} />
</label>
</div>

<div class="flex">
<button class="btn" on:click={create} title="Creates the tray icon"
>Create</button
>
</div>
</div>
1 change: 0 additions & 1 deletion examples/api/src/views/Window.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
ProgressBarStatus,
Window
} from '@tauri-apps/api/window'
import { invoke } from '@tauri-apps/api/primitives'
const appWindow = getCurrent()
Expand Down
4 changes: 2 additions & 2 deletions tooling/api/src/tray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export interface TrayIconOptions {
/** The tray icon tooltip */
tooltip?: string
/**
* The tooltip text
* The tray title
*
* #### Platform-specific
*
Expand All @@ -87,7 +87,7 @@ export interface TrayIconOptions {
*/
iconAsTemplate?: boolean
/** Whether to show the tray menu on left click or not, default is `true`. **macOS only**. */
menuOnLeftClieck?: boolean
menuOnLeftClick?: boolean
/** A handler for an event on the tray icon. */
action?: (event: TrayIconEvent) => void
}
Expand Down

0 comments on commit e974000

Please sign in to comment.