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