Skip to main content

azul_core/
macros.rs

1#![allow(unused_macros)]
2
3/// Implements functions for `CallbackInfo` and `Info`,
4/// to prevent duplicating the functions
5#[macro_export]
6macro_rules! impl_task_api {
7    () => {
8        /// Insert a timer into the list of active timers.
9        /// Replaces the existing timer if called with the same TimerId.
10        pub fn add_timer(&mut self, id: TimerId, timer: Timer) {
11            self.timers.insert(id, timer);
12        }
13
14        /// Returns if a timer with the given ID is currently running
15        pub fn has_timer(&self, timer_id: &TimerId) -> bool {
16            self.get_timer(timer_id).is_some()
17        }
18
19        /// Returns a reference to an existing timer (if the `TimerId` is valid)
20        pub fn get_timer(&self, timer_id: &TimerId) -> Option<&Timer> {
21            self.timers.get(&timer_id)
22        }
23
24        /// Deletes a timer and returns it (if the `TimerId` is valid)
25        pub fn delete_timer(&mut self, timer_id: &TimerId) -> Option<Timer> {
26            self.timers.remove(timer_id)
27        }
28
29        /// Adds a (thread-safe) `Task` to the app that runs on a different thread
30        pub fn add_task(&mut self, task: Task) {
31            self.tasks.push(task);
32        }
33    };
34}
35
36/// Implement the `From` trait for any type.
37#[macro_export]
38macro_rules! impl_from {
39    // From a type with a lifetime to a type which also has a lifetime
40    ($a:ident < $c:lifetime > , $b:ident:: $enum_type:ident) => {
41        impl<$c> From<$a<$c>> for $b<$c> {
42            fn from(e: $a<$c>) -> Self {
43                $b::$enum_type(e)
44            }
45        }
46    };
47
48    // From a type without a lifetime to a type which also does not have a lifetime
49    ($a:ident, $b:ident:: $enum_type:ident) => {
50        impl From<$a> for $b {
51            fn from(e: $a) -> Self {
52                $b::$enum_type(e)
53            }
54        }
55    };
56}
57
58/// Implement `Display` for an enum.
59#[macro_export]
60macro_rules! impl_display {
61    // For a type with a lifetime
62    ($enum:ident<$lt:lifetime>, {$($variant:pat => $fmt_string:expr),+$(,)* }) => {
63
64        impl<$lt> ::core::fmt::Display for $enum<$lt> {
65            fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
66                use self::$enum::*;
67                match &self {
68                    $(
69                        $variant => write!(f, "{}", $fmt_string),
70                    )+
71                }
72            }
73        }
74
75    };
76
77    // For a type without a lifetime
78    ($enum:ident, {$($variant:pat => $fmt_string:expr),+$(,)* }) => {
79
80        impl ::core::fmt::Display for $enum {
81            fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
82                use self::$enum::*;
83                match &self {
84                    $(
85                        $variant => write!(f, "{}", $fmt_string),
86                    )+
87                }
88            }
89        }
90
91    };
92}
93
94/// Implements `Display, Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Hash`
95/// for a Callback with a `.cb` and `.ctx` field.
96///
97/// This is necessary to work around for https://github.com/rust-lang/rust/issues/54508
98#[macro_export]
99macro_rules! impl_callback {
100    // Version with callable field (for UI callbacks that need FFI support)
101    ($callback_value:ident, $callback_ty:ty) => {
102        impl ::core::fmt::Display for $callback_value {
103            fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
104                write!(f, "{:?}", self)
105            }
106        }
107
108        impl ::core::fmt::Debug for $callback_value {
109            fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
110                let callback = stringify!($callback_value);
111                write!(f, "{} @ 0x{:x}", callback, self.cb as usize)
112            }
113        }
114
115        impl Clone for $callback_value {
116            fn clone(&self) -> Self {
117                $callback_value {
118                    cb: self.cb.clone(),
119                    ctx: self.ctx.clone(),
120                }
121            }
122        }
123
124        impl ::core::hash::Hash for $callback_value {
125            fn hash<H>(&self, state: &mut H)
126            where
127                H: ::core::hash::Hasher,
128            {
129                state.write_usize(self.cb as usize);
130            }
131        }
132
133        impl PartialEq for $callback_value {
134            fn eq(&self, rhs: &Self) -> bool {
135                self.cb as usize == rhs.cb as usize
136            }
137        }
138
139        impl PartialOrd for $callback_value {
140            fn partial_cmp(&self, other: &Self) -> Option<::core::cmp::Ordering> {
141                Some((self.cb as usize).cmp(&(other.cb as usize)))
142            }
143        }
144
145        impl Ord for $callback_value {
146            fn cmp(&self, other: &Self) -> ::core::cmp::Ordering {
147                (self.cb as usize).cmp(&(other.cb as usize))
148            }
149        }
150
151        impl Eq for $callback_value {}
152
153        /// Allow creating callback from a raw function pointer
154        /// Sets callable to None (for native Rust/C usage)
155        impl From<$callback_ty> for $callback_value {
156            fn from(cb: $callback_ty) -> Self {
157                $callback_value {
158                    cb,
159                    ctx: $crate::refany::OptionRefAny::None,
160                }
161            }
162        }
163    };
164}
165
166/// Macro to implement callback traits for simple system callbacks (no callable field)
167///
168/// Use this for destructor callbacks, system callbacks, and other internal callbacks
169/// that don't need FFI callable support.
170#[macro_export]
171macro_rules! impl_callback_simple {
172    ($callback_value:ident) => {
173        impl ::core::fmt::Display for $callback_value {
174            fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
175                write!(f, "{:?}", self)
176            }
177        }
178
179        impl ::core::fmt::Debug for $callback_value {
180            fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
181                let callback = stringify!($callback_value);
182                write!(f, "{} @ 0x{:x}", callback, self.cb as usize)
183            }
184        }
185
186        impl Clone for $callback_value {
187            fn clone(&self) -> Self {
188                $callback_value {
189                    cb: self.cb.clone(),
190                }
191            }
192        }
193
194        impl ::core::hash::Hash for $callback_value {
195            fn hash<H>(&self, state: &mut H)
196            where
197                H: ::core::hash::Hasher,
198            {
199                state.write_usize(self.cb as usize);
200            }
201        }
202
203        impl PartialEq for $callback_value {
204            fn eq(&self, rhs: &Self) -> bool {
205                self.cb as usize == rhs.cb as usize
206            }
207        }
208
209        impl PartialOrd for $callback_value {
210            fn partial_cmp(&self, other: &Self) -> Option<::core::cmp::Ordering> {
211                Some((self.cb as usize).cmp(&(other.cb as usize)))
212            }
213        }
214
215        impl Ord for $callback_value {
216            fn cmp(&self, other: &Self) -> ::core::cmp::Ordering {
217                (self.cb as usize).cmp(&(other.cb as usize))
218            }
219        }
220
221        impl Eq for $callback_value {}
222
223        impl Copy for $callback_value {}
224    };
225}
226
227#[allow(unused_macros)]
228macro_rules! impl_get_gl_context {
229    () => {
230        /// Returns a reference-counted pointer to the OpenGL context
231        pub fn get_gl_context(&self) -> OptionGlContextPtr {
232            Some(self.gl_context.clone())
233        }
234    };
235}