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