Skip to main content

nexus\api/
quick_access.rs

1//! Quick access creation.
2
3use crate::{
4    AddonApi, QuickAccessApi,
5    ffi::{OptionCStrExt, str_to_c},
6    gui::RawGuiRender,
7    revertible::Revertible,
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).expect("failed to convert shortcut identifier");
45    let texture_identifier =
46        str_to_c(texture_identifier).expect("failed to convert shortcut texture identifier");
47    let texture_hover_identifier = str_to_c(texture_hover_identifier)
48        .expect("failed to convert shortcut hover texture identifier");
49    let keybind_identifier =
50        str_to_c(keybind_identifier).expect("failed to convert shortcut keybind identifier");
51    let tooltip_text = str_to_c(tooltip_text).expect("failed to convert shortcut tooltip text");
52    unsafe {
53        add(
54            identifier.as_ptr(),
55            texture_identifier.as_ptr(),
56            texture_hover_identifier.as_ptr(),
57            keybind_identifier.as_ptr(),
58            tooltip_text.as_ptr(),
59        )
60    };
61    let revert = move || unsafe { remove(identifier.as_ptr()) };
62    revert.into()
63}
64
65/// Removes a previously registered shortcut from the quick access.
66pub fn remove_quick_access(identifier: impl AsRef<str>) {
67    let QuickAccessApi { remove, .. } = AddonApi::get().quick_access;
68    let identifier = str_to_c(identifier).expect("failed to convert shortcut identifier");
69    unsafe { remove(identifier.as_ptr()) }
70}
71
72/// Sends a notification to the given quick access shortcut.
73pub fn notify_quick_access(identifier: impl AsRef<str>) {
74    let QuickAccessApi { notify, .. } = AddonApi::get().quick_access;
75    let identifier = str_to_c(identifier).expect("failed to convert shortcut identifier");
76    unsafe { notify(identifier.as_ptr()) }
77}
78
79/// Adds a new [`RawGuiRender`] callback for the shortcut context menu.
80///
81/// Returns a [`Revertible`] to remove the context menu.
82pub fn add_quick_access_context_menu(
83    identifier: impl AsRef<str>,
84    target_identifier: Option<impl AsRef<str>>,
85    render_callback: RawGuiRender,
86) -> Revertible<impl Fn() + Send + Sync + Clone + 'static> {
87    let QuickAccessApi {
88        add_context_menu,
89        remove_context_menu,
90        ..
91    } = AddonApi::get().quick_access;
92    let identifier = str_to_c(identifier).expect("failed to convert shortcut identifier");
93    let target_identifier = target_identifier
94        .map(|string| str_to_c(string).expect("failed to convert shortcut target identifier"));
95    unsafe {
96        add_context_menu(
97            identifier.as_ptr(),
98            target_identifier.as_ptr_opt(),
99            render_callback,
100        )
101    };
102    let revert = move || unsafe { remove_context_menu(identifier.as_ptr()) };
103    revert.into()
104}
105
106/// Removes a previously registered shortcut context menu callback.
107pub fn remove_quick_access_context_menu(identifier: impl AsRef<str>) {
108    let QuickAccessApi {
109        remove_context_menu,
110        ..
111    } = AddonApi::get().quick_access;
112    let identifier = str_to_c(identifier).expect("failed to convert shortcut identifier");
113    unsafe { remove_context_menu(identifier.as_ptr()) }
114}
115
116/// Adds a new shortcut icon to the quick access with the given texture identifiers.
117/// When clicked the given keybind identifier is triggered.
118///
119/// Returns a [`Revertible`] to remove the shortcut.
120#[deprecated = "use add_quick_access"]
121pub fn add_shortcut(
122    identifier: impl AsRef<str>,
123    texture_identifier: impl AsRef<str>,
124    texture_hover_identifier: impl AsRef<str>,
125    keybind_identifier: impl AsRef<str>,
126    tooltip_text: impl AsRef<str>,
127) -> Revertible<impl Fn() + Send + Sync + Clone + 'static> {
128    add_quick_access(
129        identifier,
130        texture_identifier,
131        texture_hover_identifier,
132        keybind_identifier,
133        tooltip_text,
134    )
135}
136
137/// Removes a previously registered shortcut from the quick access.
138#[deprecated = "use remove_quick_access"]
139pub fn remove_shortcut(identifier: impl AsRef<str>) {
140    remove_quick_access(identifier)
141}
142
143/// Adds a new [`RawGuiRender`] callback fired when the quick access is right-clicked.
144///
145/// Returns a [`Revertible`] to remove the shortcut.
146#[deprecated = "use add_quick_access_context_menu"]
147pub fn add_simple_shortcut(
148    identifier: impl AsRef<str>,
149    render_callback: RawGuiRender,
150) -> Revertible<impl Fn() + Send + Sync + Clone + 'static> {
151    add_quick_access_context_menu(identifier, None::<&str>, render_callback)
152}
153
154/// Removes a previously registered simple shortcut callback.
155#[deprecated = "use remove_quick_access_context_menu"]
156pub fn remove_simple_shortcut(identifier: impl AsRef<str>) {
157    remove_quick_access_context_menu(identifier)
158}