Skip to main content

azul_layout/widgets/
mod.rs

1//! Built-in widgets for the Azul GUI system
2
3/// Implements `Display, Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Hash`
4/// for a Callback with a `.cb` field.
5///
6/// This is necessary to work around for https://github.com/rust-lang/rust/issues/54508
7#[macro_export]
8macro_rules! impl_widget_callback {
9    (
10        $callback_wrapper:ident,
11        $option_callback_wrapper:ident,
12        $callback_value:ident,
13        $callback_ty:ident
14    ) => {
15        #[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
16        #[repr(C)]
17        pub struct $callback_wrapper {
18            pub refany: RefAny,
19            pub callback: $callback_value,
20        }
21
22        #[repr(C)]
23        pub struct $callback_value {
24            pub cb: $callback_ty,
25            /// For FFI: stores the foreign callable (e.g., PyFunction)
26            /// Native Rust code sets this to None
27            pub ctx: azul_core::refany::OptionRefAny,
28        }
29
30        azul_css::impl_option!(
31            $callback_wrapper,
32            $option_callback_wrapper,
33            copy = false,
34            [Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash]
35        );
36
37        impl $callback_value {
38            /// Create a new callback with just a function pointer (for native Rust code)
39            pub fn create<I: Into<$callback_value>>(cb: I) -> $callback_value {
40                cb.into()
41            }
42        }
43
44        impl ::core::fmt::Display for $callback_value {
45            fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
46                write!(f, "{:?}", self)
47            }
48        }
49
50        impl ::core::fmt::Debug for $callback_value {
51            fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
52                let callback = stringify!($callback_value);
53                write!(f, "{} @ 0x{:x}", callback, self.cb as usize)
54            }
55        }
56
57        impl Clone for $callback_value {
58            fn clone(&self) -> Self {
59                $callback_value {
60                    cb: self.cb.clone(),
61                    ctx: self.ctx.clone(),
62                }
63            }
64        }
65
66        impl core::hash::Hash for $callback_value {
67            fn hash<H>(&self, state: &mut H)
68            where
69                H: ::core::hash::Hasher,
70            {
71                state.write_usize(self.cb as usize);
72            }
73        }
74
75        impl PartialEq for $callback_value {
76            fn eq(&self, rhs: &Self) -> bool {
77                self.cb as usize == rhs.cb as usize
78            }
79        }
80
81        impl PartialOrd for $callback_value {
82            fn partial_cmp(&self, other: &Self) -> Option<::core::cmp::Ordering> {
83                Some((self.cb as usize).cmp(&(other.cb as usize)))
84            }
85        }
86
87        impl Ord for $callback_value {
88            fn cmp(&self, other: &Self) -> ::core::cmp::Ordering {
89                (self.cb as usize).cmp(&(other.cb as usize))
90            }
91        }
92
93        impl Eq for $callback_value {}
94
95        /// Allow creating callback from a raw function pointer
96        /// Sets callable to None (for native Rust/C usage)
97        impl From<$callback_ty> for $callback_value {
98            fn from(cb: $callback_ty) -> $callback_value {
99                $callback_value {
100                    cb,
101                    ctx: azul_core::refany::OptionRefAny::None,
102                }
103            }
104        }
105
106        /// Allow creating widget callback from a generic Callback
107        /// This enables Python/FFI code to pass generic callbacks to widget methods
108        impl From<crate::callbacks::Callback> for $callback_value {
109            fn from(cb: crate::callbacks::Callback) -> $callback_value {
110                $callback_value {
111                    cb: unsafe { core::mem::transmute(cb.cb) },
112                    ctx: cb.ctx,
113                }
114            }
115        }
116    };
117}
118
119/// Button widget
120pub mod button;
121/// Checkbox widget
122pub mod check_box;
123/// Box displaying a color which opens a color picker dialog on being clicked
124pub mod color_input;
125/// File input widget
126pub mod file_input;
127/// Label widget (centered text)
128pub mod label;
129// /// Single line text input widget
130/// Drop-down select widget
131pub mod drop_down;
132/// Frame container widget
133pub mod frame;
134/// List view widget
135pub mod list_view;
136/// Node graph widget
137pub mod node_graph;
138/// Same as text input, but only allows numeric input
139pub mod number_input;
140/// Progress bar widget
141pub mod progressbar;
142/// Ribbon widget
143pub mod ribbon;
144/// Tab container widgets
145pub mod tabs;
146pub mod text_input;
147/// Tree view widget
148pub mod tree_view;
149// /// Spreadsheet (iframe) widget
150// pub mod spreadsheet;
151// /// Slider widget
152// pub mod slider;
153// /// Multi-line text input
154// pub mod text_edit;