Skip to main content

gtk/
image.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::Container;
4use crate::IconSize;
5use crate::Image;
6use crate::{Align, ffi};
7use glib::object::Cast;
8use glib::object::{IsA, ObjectExt};
9use glib::signal::{SignalHandlerId, connect_raw};
10use glib::translate::*;
11use std::boxed::Box as Box_;
12use std::mem::transmute;
13
14mod sealed {
15    pub trait Sealed {}
16    impl<T: glib::object::IsA<crate::Image>> Sealed for T {}
17}
18
19pub trait ImageExtManual: IsA<Image> + sealed::Sealed + 'static {
20    #[doc(alias = "icon-size")]
21    fn icon_size(&self) -> IconSize {
22        unsafe { from_glib(self.as_ref().property::<i32>("icon-size")) }
23    }
24
25    #[doc(alias = "icon-size")]
26    fn set_icon_size(&self, icon_size: IconSize) {
27        self.as_ref()
28            .set_property("icon-size", icon_size.into_glib());
29    }
30
31    #[doc(alias = "icon-size")]
32    fn connect_icon_size_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
33        unsafe extern "C" fn notify_icon_size_trampoline<P: IsA<Image>, F: Fn(&P) + 'static>(
34            this: *mut ffi::GtkImage,
35            _param_spec: glib::ffi::gpointer,
36            f: glib::ffi::gpointer,
37        ) {
38            unsafe {
39                let f: &F = &*(f as *const F);
40                f(Image::from_glib_borrow(this).unsafe_cast_ref())
41            }
42        }
43        unsafe {
44            let f: Box_<F> = Box_::new(f);
45            connect_raw(
46                self.as_ptr() as *mut _,
47                c"notify::icon-size".as_ptr() as *const _,
48                Some(transmute::<*const (), unsafe extern "C" fn()>(
49                    notify_icon_size_trampoline::<Self, F> as *const (),
50                )),
51                Box_::into_raw(f),
52            )
53        }
54    }
55}
56
57impl<O: IsA<Image>> ImageExtManual for O {}
58
59impl Image {
60    // rustdoc-stripper-ignore-next
61    /// Creates a new builder-style object to construct a [`Image`].
62    ///
63    /// This method returns an instance of [`ImageBuilder`] which can be used to create a [`Image`].
64    pub fn builder() -> ImageBuilder {
65        ImageBuilder::new()
66    }
67}
68
69// rustdoc-stripper-ignore-next
70/// A builder for generating a [`Image`].
71#[must_use = "The builder must be built to be used"]
72pub struct ImageBuilder {
73    builder: glib::object::ObjectBuilder<'static, Image>,
74}
75
76impl ImageBuilder {
77    // rustdoc-stripper-ignore-next
78    /// Create a new [`ImageBuilder`].
79    pub fn new() -> Self {
80        Self {
81            builder: glib::object::Object::builder(),
82        }
83    }
84
85    pub fn file(self, file: &str) -> Self {
86        Self {
87            builder: self.builder.property("file", file),
88        }
89    }
90
91    pub fn gicon<P: IsA<gio::Icon>>(self, gicon: &P) -> Self {
92        Self {
93            builder: self.builder.property("gicon", gicon),
94        }
95    }
96
97    pub fn icon_name(self, icon_name: &str) -> Self {
98        Self {
99            builder: self.builder.property("icon-name", icon_name),
100        }
101    }
102
103    pub fn icon_size(self, icon_size: IconSize) -> Self {
104        Self {
105            builder: self.builder.property("icon-size", icon_size),
106        }
107    }
108
109    pub fn pixbuf(self, pixbuf: &gdk_pixbuf::Pixbuf) -> Self {
110        Self {
111            builder: self.builder.property("pixbuf", pixbuf),
112        }
113    }
114
115    pub fn pixbuf_animation<P: IsA<gdk_pixbuf::PixbufAnimation>>(
116        self,
117        pixbuf_animation: &P,
118    ) -> Self {
119        Self {
120            builder: self.builder.property("pixbuf-animation", pixbuf_animation),
121        }
122    }
123
124    pub fn pixel_size(self, pixel_size: i32) -> Self {
125        Self {
126            builder: self.builder.property("pixel-size", pixel_size),
127        }
128    }
129
130    pub fn resource(self, resource: &str) -> Self {
131        Self {
132            builder: self.builder.property("resource", resource),
133        }
134    }
135
136    pub fn surface(self, surface: &cairo::Surface) -> Self {
137        Self {
138            builder: self.builder.property("surface", surface),
139        }
140    }
141
142    pub fn use_fallback(self, use_fallback: bool) -> Self {
143        Self {
144            builder: self.builder.property("use-fallback", use_fallback),
145        }
146    }
147
148    pub fn app_paintable(self, app_paintable: bool) -> Self {
149        Self {
150            builder: self.builder.property("app-paintable", app_paintable),
151        }
152    }
153
154    pub fn can_default(self, can_default: bool) -> Self {
155        Self {
156            builder: self.builder.property("can-default", can_default),
157        }
158    }
159
160    pub fn can_focus(self, can_focus: bool) -> Self {
161        Self {
162            builder: self.builder.property("can-focus", can_focus),
163        }
164    }
165
166    pub fn events(self, events: gdk::EventMask) -> Self {
167        Self {
168            builder: self.builder.property("events", events),
169        }
170    }
171
172    pub fn expand(self, expand: bool) -> Self {
173        Self {
174            builder: self.builder.property("expand", expand),
175        }
176    }
177
178    pub fn focus_on_click(self, focus_on_click: bool) -> Self {
179        Self {
180            builder: self.builder.property("focus-on-click", focus_on_click),
181        }
182    }
183
184    pub fn halign(self, halign: Align) -> Self {
185        Self {
186            builder: self.builder.property("halign", halign),
187        }
188    }
189
190    pub fn has_default(self, has_default: bool) -> Self {
191        Self {
192            builder: self.builder.property("has-default", has_default),
193        }
194    }
195
196    pub fn has_focus(self, has_focus: bool) -> Self {
197        Self {
198            builder: self.builder.property("has-focus", has_focus),
199        }
200    }
201
202    pub fn has_tooltip(self, has_tooltip: bool) -> Self {
203        Self {
204            builder: self.builder.property("has-tooltip", has_tooltip),
205        }
206    }
207
208    pub fn height_request(self, height_request: i32) -> Self {
209        Self {
210            builder: self.builder.property("height-request", height_request),
211        }
212    }
213
214    pub fn hexpand(self, hexpand: bool) -> Self {
215        Self {
216            builder: self.builder.property("hexpand", hexpand),
217        }
218    }
219
220    pub fn hexpand_set(self, hexpand_set: bool) -> Self {
221        Self {
222            builder: self.builder.property("hexpand-set", hexpand_set),
223        }
224    }
225
226    pub fn is_focus(self, is_focus: bool) -> Self {
227        Self {
228            builder: self.builder.property("is-focus", is_focus),
229        }
230    }
231
232    pub fn margin(self, margin: i32) -> Self {
233        Self {
234            builder: self.builder.property("margin", margin),
235        }
236    }
237
238    pub fn margin_bottom(self, margin_bottom: i32) -> Self {
239        Self {
240            builder: self.builder.property("margin-bottom", margin_bottom),
241        }
242    }
243
244    pub fn margin_end(self, margin_end: i32) -> Self {
245        Self {
246            builder: self.builder.property("margin-end", margin_end),
247        }
248    }
249
250    pub fn margin_start(self, margin_start: i32) -> Self {
251        Self {
252            builder: self.builder.property("margin-start", margin_start),
253        }
254    }
255
256    pub fn margin_top(self, margin_top: i32) -> Self {
257        Self {
258            builder: self.builder.property("margin-top", margin_top),
259        }
260    }
261
262    pub fn name(self, name: &str) -> Self {
263        Self {
264            builder: self.builder.property("name", name),
265        }
266    }
267
268    pub fn no_show_all(self, no_show_all: bool) -> Self {
269        Self {
270            builder: self.builder.property("no-show-all", no_show_all),
271        }
272    }
273
274    pub fn opacity(self, opacity: f64) -> Self {
275        Self {
276            builder: self.builder.property("opacity", opacity),
277        }
278    }
279
280    pub fn parent<P: IsA<Container>>(self, parent: &P) -> Self {
281        Self {
282            builder: self.builder.property("parent", parent),
283        }
284    }
285
286    pub fn receives_default(self, receives_default: bool) -> Self {
287        Self {
288            builder: self.builder.property("receives-default", receives_default),
289        }
290    }
291
292    pub fn sensitive(self, sensitive: bool) -> Self {
293        Self {
294            builder: self.builder.property("sensitive", sensitive),
295        }
296    }
297
298    pub fn tooltip_markup(self, tooltip_markup: &str) -> Self {
299        Self {
300            builder: self.builder.property("tooltip-markup", tooltip_markup),
301        }
302    }
303
304    pub fn tooltip_text(self, tooltip_text: &str) -> Self {
305        Self {
306            builder: self.builder.property("tooltip-text", tooltip_text),
307        }
308    }
309
310    pub fn valign(self, valign: Align) -> Self {
311        Self {
312            builder: self.builder.property("valign", valign),
313        }
314    }
315
316    pub fn vexpand(self, vexpand: bool) -> Self {
317        Self {
318            builder: self.builder.property("vexpand", vexpand),
319        }
320    }
321
322    pub fn vexpand_set(self, vexpand_set: bool) -> Self {
323        Self {
324            builder: self.builder.property("vexpandset", vexpand_set),
325        }
326    }
327
328    pub fn visible(self, visible: bool) -> Self {
329        Self {
330            builder: self.builder.property("visible", visible),
331        }
332    }
333
334    pub fn width_request(self, width_request: i32) -> Self {
335        Self {
336            builder: self.builder.property("width-request", width_request),
337        }
338    }
339
340    // rustdoc-stripper-ignore-next
341    /// Build the [`Image`].
342    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
343    pub fn build(self) -> Image {
344        self.builder.build()
345    }
346}