File size: 4,763 Bytes
2d8be8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use std::sync::atomic::{AtomicBool, Ordering};
use tauri::{
    menu::{Menu, MenuItem},
    tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent},
    Manager, Runtime, WebviewUrl, WebviewWindowBuilder,
};

pub fn create_tray<R: Runtime>(app: &tauri::AppHandle<R>) -> tauri::Result<()> {
    let toggle_i = MenuItem::with_id(app, "toggle", "Toggle", true, None::<&str>)?;
    let new_window_i = MenuItem::with_id(app, "new-window", "New window", true, None::<&str>)?;
    let icon_i_1 = MenuItem::with_id(app, "icon-1", "Icon 1", true, None::<&str>)?;
    let icon_i_2 = MenuItem::with_id(app, "icon-2", "Icon 2", true, None::<&str>)?;
    #[cfg(target_os = "macos")]
    let set_title_i = MenuItem::with_id(app, "set-title", "Set Title", true, None::<&str>)?;
    let switch_i = MenuItem::with_id(app, "switch-menu", "Switch Menu", true, None::<&str>)?;
    let quit_i = MenuItem::with_id(app, "quit", "Quit", true, None::<&str>)?;
    let remove_tray_i =
        MenuItem::with_id(app, "remove-tray", "Remove Tray icon", true, None::<&str>)?;
    let menu1 = Menu::with_items(
        app,
        &[
            &toggle_i,
            &new_window_i,
            &icon_i_1,
            &icon_i_2,
            #[cfg(target_os = "macos")]
            &set_title_i,
            &switch_i,
            &quit_i,
            &remove_tray_i,
        ],
    )?;
    let menu2 = Menu::with_items(
        app,
        &[&toggle_i, &new_window_i, &switch_i, &quit_i, &remove_tray_i],
    )?;

    let is_menu1 = AtomicBool::new(true);

    let _ = TrayIconBuilder::with_id("tray-1")
        .tooltip("Tauri")
        .icon(app.default_window_icon().unwrap().clone())
        .menu(&menu1)
        .show_menu_on_left_click(false)
        .on_menu_event(move |app, event| match event.id.as_ref() {
            "quit" => {
                app.exit(0);
            }
            "remove-tray" => {
                app.remove_tray_by_id("tray-1");
            }
            "toggle" => {
                if let Some(window) = app.get_webview_window("main") {
                    let new_title = if window.is_visible().unwrap_or_default() {
                        let _ = window.hide();
                        "Show"
                    } else {
                        let _ = window.show();
                        let _ = window.set_focus();
                        "Hide"
                    };
                    toggle_i.set_text(new_title).unwrap();
                }
            }
            "new-window" => {
                let _ = WebviewWindowBuilder::new(app, "new", WebviewUrl::App("index.html".into()))
                    .title("Tauri")
                    .build();
            }
            #[cfg(target_os = "macos")]
            "set-title" => {
                if let Some(tray) = app.tray_by_id("tray-1") {
                    let _ = tray.set_title(Some("Tauri"));
                }
            }
            i @ "icon-1" | i @ "icon-2" => {
                if let Some(tray) = app.tray_by_id("tray-1") {
                    let _ = tray.set_icon(Some(if i == "icon-1" {
                        tauri::image::Image::from_bytes(include_bytes!("../icons/icon.ico"))
                            .unwrap()
                    } else {
                        tauri::image::Image::from_bytes(include_bytes!(
                            "../icons/tray_icon_with_transparency.png"
                        ))
                        .unwrap()
                    }));
                }
            }
            "switch-menu" => {
                let flag = is_menu1.load(Ordering::Relaxed);
                let (menu, tooltip) = if flag {
                    (menu2.clone(), "Menu 2")
                } else {
                    (menu1.clone(), "Tauri")
                };
                if let Some(tray) = app.tray_by_id("tray-1") {
                    let _ = tray.set_menu(Some(menu));
                    let _ = tray.set_tooltip(Some(tooltip));
                }
                is_menu1.store(!flag, Ordering::Relaxed);
            }

            _ => {}
        })
        .on_tray_icon_event(|tray, event| {
            if let TrayIconEvent::Click {
                button_state: MouseButtonState::Down,
                button: MouseButton::Left,
                ..
            } = event
            {
                let app = tray.app_handle();
                if let Some(window) = app.get_webview_window("main") {
                    let _ = window.show();
                    let _ = window.set_focus();
                }
            }
        })
        .build(app);

    Ok(())
}