nexus\api/
quick_access.rs

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