eov-plugin-api 0.4.4

Shared plugin API for EOV plugins
Documentation
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! Stable ABI types for the plugin dynamic library interface.
//!
//! Uses [`abi_stable`] to define types that can safely cross the Rust dynamic
//! library boundary. The plugin exports a `#[no_mangle]` function that returns
//! a [`PluginVTable`], and the host loads it with
//! [`abi_stable::library::RawLibrary`].

use abi_stable::StableAbi;
use abi_stable::std_types::{ROption, RResult, RString, RVec};

#[repr(C)]
#[derive(StableAbi, Clone, Debug)]
pub struct ActiveSidebarFFI {
    pub plugin_id: RString,
    pub button_id: ROption<RString>,
    pub width_px: u32,
    pub ui_path: RString,
    pub component: RString,
}

/// FFI-safe toolbar button registration data.
#[repr(C)]
#[derive(StableAbi, Clone, Debug)]
pub struct ToolbarButtonFFI {
    pub button_id: RString,
    pub tooltip: RString,
    pub icon_svg: RString,
    pub action_id: RString,
    pub tool_mode: ROption<HostToolModeFFI>,
    pub hotkey: ROption<RString>,
}

/// FFI-safe HUD toolbar button registration data.
#[repr(C)]
#[derive(StableAbi, Clone, Debug)]
pub struct HudToolbarButtonFFI {
    pub button_id: RString,
    pub tooltip: RString,
    pub icon_svg: RString,
    pub toggled_icon_svg: ROption<RString>,
    pub action_id: RString,
}

#[repr(C)]
#[derive(StableAbi, Clone, Debug)]
pub struct UiPropertyFFI {
    pub name: RString,
    pub json_value: RString,
}

#[repr(C)]
#[derive(StableAbi, Clone, Debug)]
pub struct ViewportContextMenuItemFFI {
    pub item_id: RString,
    pub label: RString,
    pub icon: RString,
    pub enabled: bool,
}

#[repr(C)]
#[derive(StableAbi, Clone, Debug)]
pub struct ViewportOverlayPointFFI {
    pub annotation_id: RString,
    pub x_level0: f64,
    pub y_level0: f64,
    pub diameter_px: f32,
    pub ring_red: u8,
    pub ring_green: u8,
    pub ring_blue: u8,
}

#[repr(C)]
#[derive(StableAbi, Clone, Debug)]
pub struct ViewportOverlayVertexFFI {
    pub x_level0: f64,
    pub y_level0: f64,
}

#[repr(C)]
#[derive(StableAbi, Clone, Debug)]
pub struct ViewportOverlayPolygonFFI {
    pub annotation_id: RString,
    pub vertices: RVec<ViewportOverlayVertexFFI>,
    pub fill_red: u8,
    pub fill_green: u8,
    pub fill_blue: u8,
}

#[repr(C)]
#[derive(StableAbi, Clone, Debug)]
pub struct ConfirmationDialogRequestFFI {
    pub title: RString,
    pub message: RString,
    pub confirm_label: RString,
    pub cancel_label: RString,
    pub confirm_callback: ROption<RString>,
    pub confirm_args_json: ROption<RString>,
    pub cancel_callback: ROption<RString>,
    pub cancel_args_json: ROption<RString>,
}

#[repr(C)]
#[derive(StableAbi, Clone, Debug)]
pub struct ModalDialogRequestFFI {
    pub ui_path: RString,
    pub component: RString,
    pub width_px: u32,
    pub height_px: u32,
}

#[repr(C)]
#[derive(StableAbi, Clone, Debug)]
pub struct ViewportOverlayComponentRequestFFI {
    pub ui_path: RString,
    pub component: RString,
    pub z_index: i32,
}

#[repr(C)]
#[derive(StableAbi, Clone, Debug, Default)]
pub struct PluginUndoRedoStateFFI {
    pub enabled: bool,
    pub undo_available: bool,
    pub redo_available: bool,
}

/// FFI-safe response from a plugin action handler.
#[repr(C)]
#[derive(StableAbi, Clone, Debug)]
pub struct ActionResponseFFI {
    /// If `true`, the host should open this plugin's `.slint` UI window.
    pub open_window: bool,
}

#[repr(u32)]
#[derive(StableAbi, Clone, Copy, Debug, PartialEq, Eq)]
pub enum HostLogLevelFFI {
    Trace = 0,
    Debug = 1,
    Info = 2,
    Warn = 3,
    Error = 4,
}

#[repr(u32)]
#[derive(StableAbi, Clone, Copy, Debug, PartialEq, Eq)]
pub enum HostToolModeFFI {
    Navigate = 0,
    RegionOfInterest = 1,
    MeasureDistance = 2,
    PointAnnotation = 3,
    PolygonAnnotation = 4,
}

#[repr(C)]
#[derive(StableAbi, Clone, Debug)]
pub struct OpenFileInfoFFI {
    pub file_id: i32,
    pub path: RString,
    pub filename: RString,
    pub width: u64,
    pub height: u64,
    pub level_count: u32,
    pub vendor: ROption<RString>,
    pub mpp_x: ROption<f64>,
    pub mpp_y: ROption<f64>,
    pub objective_power: ROption<f64>,
    pub scan_date: ROption<RString>,
}

#[repr(C)]
#[derive(StableAbi, Clone, Debug)]
pub struct ViewportSnapshotFFI {
    pub pane_index: u32,
    pub file_id: i32,
    pub file_path: RString,
    pub filename: RString,
    pub center_x: f64,
    pub center_y: f64,
    pub zoom: f64,
    pub width: f64,
    pub height: f64,
    pub image_width: f64,
    pub image_height: f64,
    pub bounds_left: f64,
    pub bounds_top: f64,
    pub bounds_right: f64,
    pub bounds_bottom: f64,
}

#[repr(C)]
#[derive(StableAbi, Clone, Debug)]
pub struct HostSnapshotFFI {
    pub app_name: RString,
    pub app_version: RString,
    pub render_backend: RString,
    pub filtering_mode: RString,
    pub split_enabled: bool,
    pub focused_pane: u32,
    pub open_files: RVec<OpenFileInfoFFI>,
    pub active_file: ROption<OpenFileInfoFFI>,
    pub active_viewport: ROption<ViewportSnapshotFFI>,
    pub recent_files: RVec<RString>,
    pub active_sidebar: ROption<ActiveSidebarFFI>,
}

#[repr(C)]
#[derive(StableAbi, Copy, Clone)]
pub struct HostApiVTable {
    pub context: u64,
    pub get_snapshot: extern "C" fn(context: u64) -> HostSnapshotFFI,
    pub read_region: extern "C" fn(
        context: u64,
        file_id: i32,
        level: u32,
        x: i64,
        y: i64,
        width: u32,
        height: u32,
    ) -> RResult<RVec<u8>, RString>,
    pub open_file: extern "C" fn(context: u64, path: RString) -> RResult<(), RString>,
    pub set_active_viewport: extern "C" fn(
        context: u64,
        center_x: f64,
        center_y: f64,
        zoom: f64,
    ) -> RResult<(), RString>,
    pub fit_active_viewport: extern "C" fn(context: u64) -> RResult<(), RString>,
    pub frame_active_rect: extern "C" fn(
        context: u64,
        x: f64,
        y: f64,
        width: f64,
        height: f64,
    ) -> RResult<(), RString>,
    pub cycle_focused_pane: extern "C" fn(context: u64, offset: i32) -> RResult<(), RString>,
    pub cycle_active_tab: extern "C" fn(context: u64, offset: i32) -> RResult<(), RString>,
    pub toggle_series_bar: extern "C" fn(context: u64) -> RResult<(), RString>,
    pub cycle_series_selection: extern "C" fn(context: u64, offset: i32) -> RResult<(), RString>,
    pub activate_selected_series_entry: extern "C" fn(context: u64) -> RResult<(), RString>,
    pub set_active_tool: extern "C" fn(context: u64, tool: HostToolModeFFI) -> RResult<(), RString>,
    pub request_render: extern "C" fn(context: u64) -> RResult<(), RString>,
    pub set_toolbar_button_active:
        extern "C" fn(context: u64, button_id: RString, active: bool) -> RResult<(), RString>,
    pub set_hud_toolbar_button_active:
        extern "C" fn(context: u64, button_id: RString, active: bool) -> RResult<(), RString>,
    pub show_sidebar: extern "C" fn(
        context: u64,
        button_id: RString,
        width_px: u32,
        ui_path: RString,
        component: RString,
    ) -> RResult<(), RString>,
    pub refresh_sidebar: extern "C" fn(context: u64) -> RResult<(), RString>,
    pub hide_sidebar: extern "C" fn(context: u64) -> RResult<(), RString>,
    pub show_confirmation_dialog:
        extern "C" fn(context: u64, request: ConfirmationDialogRequestFFI) -> RResult<(), RString>,
    pub show_modal_dialog:
        extern "C" fn(context: u64, request: ModalDialogRequestFFI) -> RResult<(), RString>,
    pub hide_modal_dialog: extern "C" fn(context: u64) -> RResult<(), RString>,
    pub set_undo_redo_state:
        extern "C" fn(context: u64, state: PluginUndoRedoStateFFI) -> RResult<(), RString>,
    pub open_file_dialog: extern "C" fn(
        context: u64,
        filter_name: RString,
        extension: RString,
    ) -> RResult<RString, RString>,
    pub save_file_dialog: extern "C" fn(
        context: u64,
        default_file_name: RString,
        filter_name: RString,
        extension: RString,
    ) -> RResult<RString, RString>,
    pub log_message: extern "C" fn(context: u64, level: HostLogLevelFFI, message: RString),
}

/// VTable of function pointers exported by each plugin shared library.
///
/// Each plugin crate exports:
/// ```ignore
/// #[unsafe(no_mangle)]
/// pub extern "C" fn eov_get_plugin_vtable() -> PluginVTable { ... }
/// ```
#[repr(C)]
#[derive(StableAbi, Copy, Clone)]
pub struct PluginVTable {
    /// Supplies the plugin with a host API vtable so it can query the app and
    /// issue host commands later during callbacks.
    pub set_host_api: extern "C" fn(host_api: HostApiVTable),

    /// Returns the toolbar buttons this plugin wants to register.
    pub get_toolbar_buttons: extern "C" fn() -> RVec<ToolbarButtonFFI>,

    /// Returns HUD toolbar buttons shown inside each viewport.
    pub get_hud_toolbar_buttons: extern "C" fn() -> RVec<HudToolbarButtonFFI>,

    /// Called when a toolbar button registered by this plugin is clicked.
    pub on_action: extern "C" fn(action_id: RString) -> ActionResponseFFI,

    /// Called when a HUD toolbar button is clicked in a specific viewport.
    pub on_hud_action:
        extern "C" fn(action_id: RString, viewport: ViewportSnapshotFFI) -> ActionResponseFFI,

    /// Called when a callback defined in the plugin's `.slint` UI is invoked.
    /// `callback_name` is the kebab-case name of the callback as declared in
    /// the `.slint` file.
    pub on_ui_callback: extern "C" fn(callback_name: RString, args_json: RString),

    /// Returns the current values for public properties on the active sidebar.
    /// Values are encoded as JSON and converted to typed Slint values by the host.
    pub get_sidebar_properties: extern "C" fn() -> RVec<UiPropertyFFI>,

    /// Returns plugin-contributed viewport context menu items for the given viewport.
    pub get_viewport_context_menu_items:
        extern "C" fn(viewport: ViewportSnapshotFFI) -> RVec<ViewportContextMenuItemFFI>,

    /// Called when a plugin-contributed viewport context menu item is selected.
    pub on_viewport_context_menu_action:
        extern "C" fn(item_id: RString, viewport: ViewportSnapshotFFI) -> ActionResponseFFI,

    /// Returns point overlay data that should be rendered over the given viewport.
    pub get_viewport_overlay_points:
        extern "C" fn(viewport: ViewportSnapshotFFI) -> RVec<ViewportOverlayPointFFI>,

    /// Returns polygon overlay data that should be rendered over the given viewport.
    pub get_viewport_overlay_polygons:
        extern "C" fn(viewport: ViewportSnapshotFFI) -> RVec<ViewportOverlayPolygonFFI>,

    /// Returns a viewport-scoped Slint component request for this plugin, if any.
    pub get_viewport_overlay_component:
        extern "C" fn() -> ROption<ViewportOverlayComponentRequestFFI>,

    /// Returns the current public properties for the plugin viewport overlay component.
    pub get_viewport_overlay_properties:
        extern "C" fn(viewport: ViewportSnapshotFFI) -> RVec<UiPropertyFFI>,

    /// Called when the host commits a point annotation click for the active point tool.
    pub on_point_annotation_placed:
        extern "C" fn(viewport: ViewportSnapshotFFI, x_level0: f64, y_level0: f64),

    /// Called when the host commits a polygon annotation for the active polygon tool.
    pub on_polygon_annotation_placed:
        extern "C" fn(viewport: ViewportSnapshotFFI, vertices: RVec<ViewportOverlayVertexFFI>),

    /// Called when the host dispatches an undo request for this plugin.
    pub on_undo: extern "C" fn() -> ActionResponseFFI,

    /// Called when the host dispatches a redo request for this plugin.
    pub on_redo: extern "C" fn() -> ActionResponseFFI,

    /// Called when the host moves an existing point annotation for the active point tool.
    pub on_point_annotation_moved: extern "C" fn(
        viewport: ViewportSnapshotFFI,
        annotation_id: RString,
        x_level0: f64,
        y_level0: f64,
    ),

    /// Called when the host moves an existing polygon annotation for the active polygon tool.
    pub on_polygon_annotation_moved: extern "C" fn(
        viewport: ViewportSnapshotFFI,
        annotation_id: RString,
        vertices: RVec<ViewportOverlayVertexFFI>,
    ),

    /// Called when the host selects or deselects a plugin-owned overlay annotation.
    /// An empty annotation id means the current selection should be cleared.
    pub on_viewport_annotation_selected:
        extern "C" fn(viewport: ViewportSnapshotFFI, annotation_id: RString),

    /// Returns viewport filter descriptors this plugin provides.
    /// May return an empty vec if the plugin has no viewport filters.
    pub get_viewport_filters: extern "C" fn() -> RVec<ViewportFilterFFI>,

    /// Apply a CPU viewport filter in-place.
    /// `filter_id` identifies which filter to apply.
    /// `rgba_data` is a mutable pointer to width*height*4 bytes of RGBA8 data.
    /// Returns `true` if the filter was applied successfully.
    pub apply_filter_cpu: extern "C" fn(
        filter_id: RString,
        rgba_data: *mut u8,
        len: u32,
        width: u32,
        height: u32,
        viewport: *const ViewportSnapshotFFI,
    ) -> bool,

    /// Apply a GPU viewport filter using raw Vulkan handles.
    /// `filter_id` identifies which filter to apply.
    /// `ctx` is a pointer to a `GpuFilterContextFFI` struct with the Vulkan
    /// device, image, and sync fence. The plugin must signal the fence when done.
    /// Returns `true` if the filter was applied successfully.
    pub apply_filter_gpu:
        extern "C" fn(filter_id: RString, ctx: *const GpuFilterContextFFI) -> bool,

    /// Enable or disable a viewport filter.
    pub set_filter_enabled: extern "C" fn(filter_id: RString, enabled: bool),
}

/// FFI-safe GPU filter context passed from the host to the plugin.
///
/// Contains raw Vulkan handles for zero-copy GPU memory access. The plugin
/// creates its own Vulkan pipeline on the shared device and submits commands
/// that operate on `vk_image`. The plugin **must** signal `vk_done_fence`
/// when its GPU work is complete.
#[repr(C)]
#[derive(StableAbi, Clone, Debug)]
pub struct GpuFilterContextFFI {
    /// `VkDevice` handle (as `u64`).
    pub vk_device: u64,
    /// `VkPhysicalDevice` handle (as `u64`).
    pub vk_physical_device: u64,
    /// `VkQueue` handle the plugin may submit to.
    pub vk_queue: u64,
    /// Queue family index for `vk_queue`.
    pub queue_family_index: u32,
    /// `VkImage` handle for the RGBA8 frame.
    pub vk_image: u64,
    /// Image width in pixels.
    pub width: u32,
    /// Image height in pixels.
    pub height: u32,
    /// `VkFormat` value (e.g. `VK_FORMAT_R8G8B8A8_UNORM` = 37).
    pub vk_format: u32,
    /// `VkFence` handle — the plugin **must** signal this when its GPU
    /// commands have finished executing on `vk_image`.
    pub vk_done_fence: u64,
}

/// FFI-safe viewport filter descriptor.
#[repr(C)]
#[derive(StableAbi, Clone, Debug)]
pub struct ViewportFilterFFI {
    pub filter_id: RString,
    pub name: RString,
    pub supports_cpu: bool,
    pub supports_gpu: bool,
    pub enabled: bool,
}

/// The type of the factory function each plugin exports.
pub type GetPluginVTableFn = unsafe extern "C" fn() -> PluginVTable;

/// Symbol name the host looks for in plugin shared libraries.
pub const PLUGIN_VTABLE_SYMBOL: &[u8] = b"eov_get_plugin_vtable\0";

/// Returns the expected shared library filename for a plugin on the current
/// platform, derived from the plugin id.
pub fn plugin_library_filename(id: &str) -> String {
    if cfg!(target_os = "macos") {
        format!("lib{id}.dylib")
    } else if cfg!(target_os = "windows") {
        format!("{id}.dll")
    } else {
        format!("lib{id}.so")
    }
}