gdk4/toplevel_size.rs
1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::{ffi, prelude::*};
4use glib::translate::*;
5
6/// Contains information that is useful to compute the size of a toplevel.
7#[repr(transparent)]
8#[doc(alias = "GdkToplevelSize")]
9pub struct ToplevelSize(std::ptr::NonNull<ffi::GdkToplevelSize>);
10
11impl StaticType for ToplevelSize {
12 fn static_type() -> glib::Type {
13 unsafe { from_glib(ffi::gdk_toplevel_size_get_type()) }
14 }
15}
16
17impl ToplevelSize {
18 /// Retrieves the bounds the toplevel is placed within.
19 ///
20 /// The bounds represent the largest size a toplevel may have while still being
21 /// able to fit within some type of boundary. Depending on the backend, this may
22 /// be equivalent to the dimensions of the work area or the monitor on which the
23 /// window is being presented on, or something else that limits the way a
24 /// toplevel can be presented.
25 ///
26 /// # Returns
27 ///
28 ///
29 /// ## `bounds_width`
30 /// return location for width
31 ///
32 /// ## `bounds_height`
33 /// return location for height
34 #[doc(alias = "gdk_toplevel_size_get_bounds")]
35 #[doc(alias = "get_bounds")]
36 pub fn bounds(&self) -> (i32, i32) {
37 unsafe {
38 let mut bounds_width = std::mem::MaybeUninit::uninit();
39 let mut bounds_height = std::mem::MaybeUninit::uninit();
40
41 ffi::gdk_toplevel_size_get_bounds(
42 self.0.as_ptr(),
43 bounds_width.as_mut_ptr(),
44 bounds_height.as_mut_ptr(),
45 );
46 (bounds_width.assume_init(), bounds_height.assume_init())
47 }
48 }
49
50 /// Sets the minimum size of the toplevel.
51 ///
52 /// The minimum size corresponds to the limitations the toplevel can be shrunk
53 /// to, without resulting in incorrect painting. A user of a [`Toplevel`][crate::Toplevel] should
54 /// calculate these given both the existing size, and the bounds retrieved from
55 /// the [`ToplevelSize`][crate::ToplevelSize] object.
56 ///
57 /// The minimum size should be within the bounds (see
58 /// [`bounds()`][Self::bounds()]).
59 /// ## `min_width`
60 /// the minimum width
61 /// ## `min_height`
62 /// the minimum height
63 #[doc(alias = "gdk_toplevel_size_set_min_size")]
64 pub fn set_min_size(&mut self, min_width: i32, min_height: i32) {
65 unsafe {
66 ffi::gdk_toplevel_size_set_min_size(self.0.as_mut(), min_width, min_height);
67 }
68 }
69
70 /// Sets the shadows size of the toplevel.
71 ///
72 /// The shadow width corresponds to the part of the computed surface size
73 /// that would consist of the shadow margin surrounding the window, would
74 /// there be any.
75 ///
76 /// Shadow width should only be set if
77 /// `Gtk::Display::supports_shadow_width()` is [`true`].
78 /// ## `left`
79 /// width of the left part of the shadow
80 /// ## `right`
81 /// width of the right part of the shadow
82 /// ## `top`
83 /// height of the top part of the shadow
84 /// ## `bottom`
85 /// height of the bottom part of the shadow
86 #[doc(alias = "gdk_toplevel_size_set_shadow_width")]
87 pub fn set_shadow_width(&mut self, left: i32, right: i32, top: i32, bottom: i32) {
88 unsafe {
89 ffi::gdk_toplevel_size_set_shadow_width(self.0.as_mut(), left, right, top, bottom);
90 }
91 }
92
93 /// Sets the size the toplevel prefers to be resized to.
94 ///
95 /// The size should be within the bounds (see
96 /// [`bounds()`][Self::bounds()]). The set size should
97 /// be considered as a hint, and should not be assumed to be
98 /// respected by the windowing system, or backend.
99 /// ## `width`
100 /// the width
101 /// ## `height`
102 /// the height
103 #[doc(alias = "gdk_toplevel_size_set_size")]
104 pub fn set_size(&mut self, width: i32, height: i32) {
105 unsafe {
106 ffi::gdk_toplevel_size_set_size(self.0.as_mut(), width, height);
107 }
108 }
109}
110
111impl std::fmt::Debug for ToplevelSize {
112 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
113 f.debug_struct("ToplevelSize")
114 .field("bounds", &self.bounds())
115 .finish()
116 }
117}