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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use serde::Deserialize;
use std::sync::Arc;
use tauri::{
    plugin::Builder, plugin::Plugin, plugin::TauriPlugin, Invoke, Manager, Runtime, State, Window,
};

#[cfg(target_os = "linux")]
use std::{
    sync::{mpsc, Mutex},
    time::Duration,
};

mod menu_item;
use menu_item::MenuItem;

mod keymap;

#[cfg(target_os = "windows")]
mod win_image_handler;

#[cfg(target_os = "windows")]
#[path = "win.rs"]
mod os;

#[cfg(target_os = "macos")]
mod macos_window_holder;

#[cfg(target_os = "macos")]
#[path = "macos.rs"]
mod os;

#[cfg(target_os = "linux")]
#[path = "linux.rs"]
mod os;

#[derive(Clone, Deserialize)]
pub struct Position {
    x: f64,
    y: f64,
    is_absolute: Option<bool>,
}

pub struct ContextMenu<R: Runtime> {
    invoke_handler: Arc<dyn Fn(Invoke<R>) + Send + Sync>,
}

impl<R: Runtime> Default for ContextMenu<R> {
    fn default() -> Self {
        Self {
            invoke_handler: Arc::new(|_| {}),
        }
    }
}

impl<R: Runtime> Clone for ContextMenu<R> {
    fn clone(&self) -> Self {
        Self {
            invoke_handler: Arc::clone(&self.invoke_handler),
        }
    }
}

impl<R: Runtime> ContextMenu<R> {
    // Method to create a new ContextMenu
    pub fn new<F: 'static + Fn(Invoke<R>) + Send + Sync>(handler: F) -> Self {
        Self {
            invoke_handler: Arc::new(handler),
        }
    }

    #[cfg(target_os = "linux")]
    fn show_context_menu(
        &self,
        app_context: State<'_, os::AppContext>,
        window: Window<R>,
        pos: Option<Position>,
        items: Option<Vec<MenuItem>>,
    ) {
        let context_menu = Arc::new(self.clone());
        os::show_context_menu(context_menu, app_context, window, pos, items);
    }

    #[cfg(any(target_os = "macos", target_os = "windows"))]
    fn show_context_menu(
        &self,
        window: Window<R>,
        pos: Option<Position>,
        items: Option<Vec<MenuItem>>,
    ) {
        let context_menu = Arc::new(self.clone());
        os::show_context_menu(context_menu, window, pos, items);
    }
}

impl<R: Runtime> Plugin<R> for ContextMenu<R> {
    fn name(&self) -> &'static str {
        "context_menu"
    }

    fn extend_api(&mut self, invoke: Invoke<R>) {
        (self.invoke_handler)(invoke);
    }
}

#[cfg(target_os = "linux")]
#[tauri::command]
fn show_context_menu<R: Runtime>(
    app_context: State<'_, os::AppContext>,
    manager: State<'_, ContextMenu<R>>,
    window: Window<R>,
    pos: Option<Position>,
    items: Option<Vec<MenuItem>>,
) {
    manager.show_context_menu(app_context, window, pos, items);
}

#[cfg(any(target_os = "macos", target_os = "windows"))]
#[tauri::command]
fn show_context_menu<R: Runtime>(
    manager: State<'_, ContextMenu<R>>,
    window: Window<R>,
    pos: Option<Position>,
    items: Option<Vec<MenuItem>>,
) {
    manager.show_context_menu(window, pos, items);
}

#[cfg(any(target_os = "macos", target_os = "windows"))]
pub fn init<R: Runtime>() -> TauriPlugin<R> {
    Builder::new("context_menu")
        .invoke_handler(tauri::generate_handler![show_context_menu])
        .setup(|app| {
            app.manage(ContextMenu::<R>::default());
            Ok(())
        })
        .build()
}

#[cfg(target_os = "linux")]
pub fn init<R: Runtime>() -> TauriPlugin<R> {
    use tauri::async_runtime;

    let (tx, rx) = mpsc::channel::<os::GtkThreadCommand>();
    let rx = Arc::new(Mutex::new(rx));

    let rx = rx.clone();
    glib::timeout_add_local(Duration::from_millis(100), move || {
        let rx = rx.lock().unwrap();
        if let Ok(cmd) = rx.try_recv() {
            match cmd {
                os::GtkThreadCommand::ShowContextMenu { pos, items, window } => {
                    async_runtime::block_on(async {
                        os::on_context_menu::<R>(pos, items, window).await;
                    });
                }
            }
        }
        glib::Continue(true)
    });

    Builder::new("context_menu")
        .invoke_handler(tauri::generate_handler![show_context_menu])
        .setup(|app| {
            app.manage(ContextMenu::<R>::default());
            app.manage(os::AppContext {
                tx: Arc::new(Mutex::new(tx)),
            });
            Ok(())
        })
        .build()
}