nexus\api/
quick_access.rs

1//! Quick access creation.
2
3use crate::{gui::RawGuiRender, revertible::Revertible, util::str_to_c, AddonApi, QuickAccessApi};
4use std::{ffi::c_char, ptr};
5
6pub type RawQuickAccessAddShortcut = unsafe extern "C-unwind" fn(
7    identifier: *const c_char,
8    texture_identifier: *const c_char,
9    texture_hover_identifier: *const c_char,
10    keybind_identifier: *const c_char,
11    tooltip_text: *const c_char,
12);
13
14pub type RawQuickAccessAddContextMenu =
15    unsafe extern "C-unwind" fn(identifier: *const c_char, shortcut_render_callback: RawGuiRender);
16
17pub type RawQuickAccessAddContextMenu2 = unsafe extern "C-unwind" fn(
18    identifier: *const c_char,
19    target_identifier: *const c_char,
20    shortcut_render_callback: RawGuiRender,
21);
22
23pub type RawQuickAccessGeneric = unsafe extern "C-unwind" fn(identifier: *const c_char);
24
25// TODO: combination with texture & keybind calls
26
27/// Adds a new shortcut icon to the quick access with the given texture identifiers.
28/// When clicked the given keybind identifier is triggered.
29///
30/// Returns a [`Revertible`] to remove the shortcut.
31pub fn add_quick_access(
32    identifier: impl AsRef<str>,
33    texture_identifier: impl AsRef<str>,
34    texture_hover_identifier: impl AsRef<str>,
35    keybind_identifier: impl AsRef<str>,
36    tooltip_text: impl AsRef<str>,
37) -> Revertible<impl Fn() + Send + Sync + Clone + 'static> {
38    let QuickAccessApi { add, remove, .. } = AddonApi::get().quick_access;
39    let identifier = str_to_c(identifier, "failed to convert shortcut identifier");
40    let texture_identifier = str_to_c(
41        texture_identifier,
42        "failed to convert shortcut texture identifier",
43    );
44    let texture_hover_identifier = str_to_c(
45        texture_hover_identifier,
46        "failed to convert shortcut hover texture identifier",
47    );
48    let keybind_identifier = str_to_c(
49        keybind_identifier,
50        "failed to convert shortcut keybind identifier",
51    );
52    let tooltip_text = str_to_c(tooltip_text, "failed to convert shortcut tooltip text");
53    unsafe {
54        add(
55            identifier.as_ptr(),
56            texture_identifier.as_ptr(),
57            texture_hover_identifier.as_ptr(),
58            keybind_identifier.as_ptr(),
59            tooltip_text.as_ptr(),
60        )
61    };
62    let revert = move || unsafe { remove(identifier.as_ptr()) };
63    revert.into()
64}
65
66/// Removes a previously registered shortcut from the quick access.
67pub fn remove_quick_access(identifier: impl AsRef<str>) {
68    let QuickAccessApi { remove, .. } = AddonApi::get().quick_access;
69    let identifier = str_to_c(identifier, "failed to convert shortcut identifier");
70    unsafe { remove(identifier.as_ptr()) }
71}
72
73/// Sends a notification to the given quick access shortcut.
74pub fn notify_quick_access(identifier: impl AsRef<str>) {
75    let QuickAccessApi { notify, .. } = AddonApi::get().quick_access;
76    let identifier = str_to_c(identifier, "failed to convert shortcut identifier");
77    unsafe { notify(identifier.as_ptr()) }
78}
79
80/// Adds a new [`RawGuiRender`] callback for the shortcut context menu.
81///
82/// Returns a [`Revertible`] to remove the context menu.
83pub fn add_quick_access_context_menu(
84    identifier: impl AsRef<str>,
85    target_identifier: Option<impl AsRef<str>>,
86    render_callback: RawGuiRender,
87) -> Revertible<impl Fn() + Send + Sync + Clone + 'static> {
88    let QuickAccessApi {
89        add_context_menu,
90        remove_context_menu,
91        ..
92    } = AddonApi::get().quick_access;
93    let identifier = str_to_c(identifier, "failed to convert shortcut identifier");
94    let target_identifier = target_identifier
95        .map(|string| str_to_c(string, "failed to convert shortcut target identifier"));
96    unsafe {
97        add_context_menu(
98            identifier.as_ptr(),
99            target_identifier
100                .as_ref()
101                .map(|string| string.as_ptr())
102                .unwrap_or(ptr::null()),
103            render_callback,
104        )
105    };
106    let revert = move || unsafe { remove_context_menu(identifier.as_ptr()) };
107    revert.into()
108}
109
110/// Removes a previously registered shortcut context menu callback.
111pub fn remove_quick_access_context_menu(identifier: impl AsRef<str>) {
112    let QuickAccessApi {
113        remove_context_menu,
114        ..
115    } = AddonApi::get().quick_access;
116    let identifier = str_to_c(identifier, "failed to convert shortcut identifier");
117    unsafe { remove_context_menu(identifier.as_ptr()) }
118}
119
120/// Adds a new shortcut icon to the quick access with the given texture identifiers.
121/// When clicked the given keybind identifier is triggered.
122///
123/// Returns a [`Revertible`] to remove the shortcut.
124#[deprecated = "use add_quick_access"]
125pub fn add_shortcut(
126    identifier: impl AsRef<str>,
127    texture_identifier: impl AsRef<str>,
128    texture_hover_identifier: impl AsRef<str>,
129    keybind_identifier: impl AsRef<str>,
130    tooltip_text: impl AsRef<str>,
131) -> Revertible<impl Fn() + Send + Sync + Clone + 'static> {
132    add_quick_access(
133        identifier,
134        texture_identifier,
135        texture_hover_identifier,
136        keybind_identifier,
137        tooltip_text,
138    )
139}
140
141/// Removes a previously registered shortcut from the quick access.
142#[deprecated = "use remove_quick_access"]
143pub fn remove_shortcut(identifier: impl AsRef<str>) {
144    remove_quick_access(identifier)
145}
146
147/// Adds a new [`RawGuiRender`] callback fired when the quick access is right-clicked.
148///
149/// Returns a [`Revertible`] to remove the shortcut.
150#[deprecated = "use add_quick_access_context_menu"]
151pub fn add_simple_shortcut(
152    identifier: impl AsRef<str>,
153    render_callback: RawGuiRender,
154) -> Revertible<impl Fn() + Send + Sync + Clone + 'static> {
155    add_quick_access_context_menu(identifier, None::<&str>, render_callback)
156}
157
158/// Removes a previously registered simple shortcut callback.
159#[deprecated = "use remove_quick_access_context_menu"]
160pub fn remove_simple_shortcut(identifier: impl AsRef<str>) {
161    remove_quick_access_context_menu(identifier)
162}