1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::prelude::*;
use glib::translate::*;

/// The [`ToplevelSize`][crate::ToplevelSize] struct contains information that is useful
/// to compute the size of a toplevel.
#[repr(transparent)]
#[doc(alias = "GdkToplevelSize")]
pub struct ToplevelSize(std::ptr::NonNull<ffi::GdkToplevelSize>);

impl StaticType for ToplevelSize {
    fn static_type() -> glib::Type {
        unsafe { from_glib(ffi::gdk_toplevel_size_get_type()) }
    }
}

impl ToplevelSize {
    /// Retrieves the bounds the toplevel is placed within.
    ///
    /// The bounds represent the largest size a toplevel may have while still being
    /// able to fit within some type of boundary. Depending on the backend, this may
    /// be equivalent to the dimensions of the work area or the monitor on which the
    /// window is being presented on, or something else that limits the way a
    /// toplevel can be presented.
    ///
    /// # Returns
    ///
    ///
    /// ## `bounds_width`
    /// return location for width
    ///
    /// ## `bounds_height`
    /// return location for height
    #[doc(alias = "gdk_toplevel_size_get_bounds")]
    #[doc(alias = "get_bounds")]
    pub fn bounds(&self) -> (i32, i32) {
        unsafe {
            let mut bounds_width = std::mem::MaybeUninit::uninit();
            let mut bounds_height = std::mem::MaybeUninit::uninit();

            ffi::gdk_toplevel_size_get_bounds(
                self.0.as_ptr(),
                bounds_width.as_mut_ptr(),
                bounds_height.as_mut_ptr(),
            );
            (bounds_width.assume_init(), bounds_height.assume_init())
        }
    }

    /// Sets the minimum size of the toplevel.
    ///
    /// The minimum size corresponds to the limitations the toplevel can be shrunk
    /// to, without resulting in incorrect painting. A user of a [`Toplevel`][crate::Toplevel] should
    /// calculate these given both the existing size, and the bounds retrieved from
    /// the [`ToplevelSize`][crate::ToplevelSize] object.
    ///
    /// The minimum size should be within the bounds (see
    /// [`bounds()`][Self::bounds()]).
    /// ## `min_width`
    /// the minimum width
    /// ## `min_height`
    /// the minimum height
    #[doc(alias = "gdk_toplevel_size_set_min_size")]
    pub fn set_min_size(&mut self, min_width: i32, min_height: i32) {
        unsafe {
            ffi::gdk_toplevel_size_set_min_size(self.0.as_mut(), min_width, min_height);
        }
    }

    /// Sets the shadows size of the toplevel.
    ///
    /// The shadow width corresponds to the part of the computed surface size
    /// that would consist of the shadow margin surrounding the window, would
    /// there be any.
    ///
    /// Shadow width should only be set if
    /// `Gtk::Display::supports_shadow_width()` is [`true`].
    /// ## `left`
    /// width of the left part of the shadow
    /// ## `right`
    /// width of the right part of the shadow
    /// ## `top`
    /// height of the top part of the shadow
    /// ## `bottom`
    /// height of the bottom part of the shadow
    #[doc(alias = "gdk_toplevel_size_set_shadow_width")]
    pub fn set_shadow_width(&mut self, left: i32, right: i32, top: i32, bottom: i32) {
        unsafe {
            ffi::gdk_toplevel_size_set_shadow_width(self.0.as_mut(), left, right, top, bottom);
        }
    }

    /// Sets the size the toplevel prefers to be resized to.
    ///
    /// The size should be within the bounds (see
    /// [`bounds()`][Self::bounds()]). The set size should
    /// be considered as a hint, and should not be assumed to be
    /// respected by the windowing system, or backend.
    /// ## `width`
    /// the width
    /// ## `height`
    /// the height
    #[doc(alias = "gdk_toplevel_size_set_size")]
    pub fn set_size(&mut self, width: i32, height: i32) {
        unsafe {
            ffi::gdk_toplevel_size_set_size(self.0.as_mut(), width, height);
        }
    }
}

impl std::fmt::Debug for ToplevelSize {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.debug_struct("ToplevelSize")
            .field("bounds", &self.bounds())
            .finish()
    }
}