1use crate::{ffi, ViewStack, ViewSwitcherPolicy};
7use glib::{
8 prelude::*,
9 signal::{connect_raw, SignalHandlerId},
10 translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15 #[doc(alias = "AdwViewSwitcher")]
16 pub struct ViewSwitcher(Object<ffi::AdwViewSwitcher, ffi::AdwViewSwitcherClass>) @extends gtk::Widget, @implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget;
17
18 match fn {
19 type_ => || ffi::adw_view_switcher_get_type(),
20 }
21}
22
23impl ViewSwitcher {
24 #[doc(alias = "adw_view_switcher_new")]
25 pub fn new() -> ViewSwitcher {
26 assert_initialized_main_thread!();
27 unsafe { gtk::Widget::from_glib_none(ffi::adw_view_switcher_new()).unsafe_cast() }
28 }
29
30 pub fn builder() -> ViewSwitcherBuilder {
35 ViewSwitcherBuilder::new()
36 }
37
38 #[doc(alias = "adw_view_switcher_get_policy")]
39 #[doc(alias = "get_policy")]
40 pub fn policy(&self) -> ViewSwitcherPolicy {
41 unsafe { from_glib(ffi::adw_view_switcher_get_policy(self.to_glib_none().0)) }
42 }
43
44 #[doc(alias = "adw_view_switcher_get_stack")]
45 #[doc(alias = "get_stack")]
46 pub fn stack(&self) -> Option<ViewStack> {
47 unsafe { from_glib_none(ffi::adw_view_switcher_get_stack(self.to_glib_none().0)) }
48 }
49
50 #[doc(alias = "adw_view_switcher_set_policy")]
51 #[doc(alias = "policy")]
52 pub fn set_policy(&self, policy: ViewSwitcherPolicy) {
53 unsafe {
54 ffi::adw_view_switcher_set_policy(self.to_glib_none().0, policy.into_glib());
55 }
56 }
57
58 #[doc(alias = "adw_view_switcher_set_stack")]
59 #[doc(alias = "stack")]
60 pub fn set_stack(&self, stack: Option<&ViewStack>) {
61 unsafe {
62 ffi::adw_view_switcher_set_stack(self.to_glib_none().0, stack.to_glib_none().0);
63 }
64 }
65
66 #[doc(alias = "policy")]
67 pub fn connect_policy_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
68 unsafe extern "C" fn notify_policy_trampoline<F: Fn(&ViewSwitcher) + 'static>(
69 this: *mut ffi::AdwViewSwitcher,
70 _param_spec: glib::ffi::gpointer,
71 f: glib::ffi::gpointer,
72 ) {
73 let f: &F = &*(f as *const F);
74 f(&from_glib_borrow(this))
75 }
76 unsafe {
77 let f: Box_<F> = Box_::new(f);
78 connect_raw(
79 self.as_ptr() as *mut _,
80 c"notify::policy".as_ptr() as *const _,
81 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
82 notify_policy_trampoline::<F> as *const (),
83 )),
84 Box_::into_raw(f),
85 )
86 }
87 }
88
89 #[doc(alias = "stack")]
90 pub fn connect_stack_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
91 unsafe extern "C" fn notify_stack_trampoline<F: Fn(&ViewSwitcher) + 'static>(
92 this: *mut ffi::AdwViewSwitcher,
93 _param_spec: glib::ffi::gpointer,
94 f: glib::ffi::gpointer,
95 ) {
96 let f: &F = &*(f as *const F);
97 f(&from_glib_borrow(this))
98 }
99 unsafe {
100 let f: Box_<F> = Box_::new(f);
101 connect_raw(
102 self.as_ptr() as *mut _,
103 c"notify::stack".as_ptr() as *const _,
104 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
105 notify_stack_trampoline::<F> as *const (),
106 )),
107 Box_::into_raw(f),
108 )
109 }
110 }
111}
112
113impl Default for ViewSwitcher {
114 fn default() -> Self {
115 Self::new()
116 }
117}
118
119#[must_use = "The builder must be built to be used"]
124pub struct ViewSwitcherBuilder {
125 builder: glib::object::ObjectBuilder<'static, ViewSwitcher>,
126}
127
128impl ViewSwitcherBuilder {
129 fn new() -> Self {
130 Self {
131 builder: glib::object::Object::builder(),
132 }
133 }
134
135 pub fn policy(self, policy: ViewSwitcherPolicy) -> Self {
136 Self {
137 builder: self.builder.property("policy", policy),
138 }
139 }
140
141 pub fn stack(self, stack: &ViewStack) -> Self {
142 Self {
143 builder: self.builder.property("stack", stack.clone()),
144 }
145 }
146
147 pub fn can_focus(self, can_focus: bool) -> Self {
148 Self {
149 builder: self.builder.property("can-focus", can_focus),
150 }
151 }
152
153 pub fn can_target(self, can_target: bool) -> Self {
154 Self {
155 builder: self.builder.property("can-target", can_target),
156 }
157 }
158
159 pub fn css_classes(self, css_classes: impl Into<glib::StrV>) -> Self {
160 Self {
161 builder: self.builder.property("css-classes", css_classes.into()),
162 }
163 }
164
165 pub fn css_name(self, css_name: impl Into<glib::GString>) -> Self {
166 Self {
167 builder: self.builder.property("css-name", css_name.into()),
168 }
169 }
170
171 pub fn cursor(self, cursor: &gdk::Cursor) -> Self {
172 Self {
173 builder: self.builder.property("cursor", cursor.clone()),
174 }
175 }
176
177 pub fn focus_on_click(self, focus_on_click: bool) -> Self {
178 Self {
179 builder: self.builder.property("focus-on-click", focus_on_click),
180 }
181 }
182
183 pub fn focusable(self, focusable: bool) -> Self {
184 Self {
185 builder: self.builder.property("focusable", focusable),
186 }
187 }
188
189 pub fn halign(self, halign: gtk::Align) -> Self {
190 Self {
191 builder: self.builder.property("halign", halign),
192 }
193 }
194
195 pub fn has_tooltip(self, has_tooltip: bool) -> Self {
196 Self {
197 builder: self.builder.property("has-tooltip", has_tooltip),
198 }
199 }
200
201 pub fn height_request(self, height_request: i32) -> Self {
202 Self {
203 builder: self.builder.property("height-request", height_request),
204 }
205 }
206
207 pub fn hexpand(self, hexpand: bool) -> Self {
208 Self {
209 builder: self.builder.property("hexpand", hexpand),
210 }
211 }
212
213 pub fn hexpand_set(self, hexpand_set: bool) -> Self {
214 Self {
215 builder: self.builder.property("hexpand-set", hexpand_set),
216 }
217 }
218
219 pub fn layout_manager(self, layout_manager: &impl IsA<gtk::LayoutManager>) -> Self {
220 Self {
221 builder: self
222 .builder
223 .property("layout-manager", layout_manager.clone().upcast()),
224 }
225 }
226
227 #[cfg(feature = "gtk_v4_18")]
228 #[cfg_attr(docsrs, doc(cfg(feature = "gtk_v4_18")))]
229 pub fn limit_events(self, limit_events: bool) -> Self {
230 Self {
231 builder: self.builder.property("limit-events", limit_events),
232 }
233 }
234
235 pub fn margin_bottom(self, margin_bottom: i32) -> Self {
236 Self {
237 builder: self.builder.property("margin-bottom", margin_bottom),
238 }
239 }
240
241 pub fn margin_end(self, margin_end: i32) -> Self {
242 Self {
243 builder: self.builder.property("margin-end", margin_end),
244 }
245 }
246
247 pub fn margin_start(self, margin_start: i32) -> Self {
248 Self {
249 builder: self.builder.property("margin-start", margin_start),
250 }
251 }
252
253 pub fn margin_top(self, margin_top: i32) -> Self {
254 Self {
255 builder: self.builder.property("margin-top", margin_top),
256 }
257 }
258
259 pub fn name(self, name: impl Into<glib::GString>) -> Self {
260 Self {
261 builder: self.builder.property("name", name.into()),
262 }
263 }
264
265 pub fn opacity(self, opacity: f64) -> Self {
266 Self {
267 builder: self.builder.property("opacity", opacity),
268 }
269 }
270
271 pub fn overflow(self, overflow: gtk::Overflow) -> Self {
272 Self {
273 builder: self.builder.property("overflow", overflow),
274 }
275 }
276
277 pub fn receives_default(self, receives_default: bool) -> Self {
278 Self {
279 builder: self.builder.property("receives-default", receives_default),
280 }
281 }
282
283 pub fn sensitive(self, sensitive: bool) -> Self {
284 Self {
285 builder: self.builder.property("sensitive", sensitive),
286 }
287 }
288
289 pub fn tooltip_markup(self, tooltip_markup: impl Into<glib::GString>) -> Self {
290 Self {
291 builder: self
292 .builder
293 .property("tooltip-markup", tooltip_markup.into()),
294 }
295 }
296
297 pub fn tooltip_text(self, tooltip_text: impl Into<glib::GString>) -> Self {
298 Self {
299 builder: self.builder.property("tooltip-text", tooltip_text.into()),
300 }
301 }
302
303 pub fn valign(self, valign: gtk::Align) -> Self {
304 Self {
305 builder: self.builder.property("valign", valign),
306 }
307 }
308
309 pub fn vexpand(self, vexpand: bool) -> Self {
310 Self {
311 builder: self.builder.property("vexpand", vexpand),
312 }
313 }
314
315 pub fn vexpand_set(self, vexpand_set: bool) -> Self {
316 Self {
317 builder: self.builder.property("vexpand-set", vexpand_set),
318 }
319 }
320
321 pub fn visible(self, visible: bool) -> Self {
322 Self {
323 builder: self.builder.property("visible", visible),
324 }
325 }
326
327 pub fn width_request(self, width_request: i32) -> Self {
328 Self {
329 builder: self.builder.property("width-request", width_request),
330 }
331 }
332
333 pub fn accessible_role(self, accessible_role: gtk::AccessibleRole) -> Self {
334 Self {
335 builder: self.builder.property("accessible-role", accessible_role),
336 }
337 }
338
339 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
342 pub fn build(self) -> ViewSwitcher {
343 assert_initialized_main_thread!();
344 self.builder.build()
345 }
346}