Skip to main content

kas_widgets/adapt/
mod.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License in the LICENSE-APACHE file or at:
4//     https://www.apache.org/licenses/LICENSE-2.0
5
6//! Adapter widgets (wrappers)
7
8mod adapt;
9mod adapt_cx;
10mod adapt_events;
11mod adapt_widget;
12mod reserve;
13mod with_label;
14
15pub use adapt::{Adapt, Map};
16pub use adapt_cx::{AdaptConfigCx, AdaptEventCx};
17pub use adapt_events::AdaptEvents;
18pub use adapt_widget::*;
19#[doc(inline)] pub use kas::widgets::adapt::*;
20pub use reserve::{Reserve, WithMarginStyle};
21pub use with_label::{WithHiddenLabel, WithLabel};
22
23#[allow(unused)] use kas::event::{ConfigCx, EventCx};
24use kas::layout::{AxisInfo, SizeRules, Stretch};
25use kas::theme::SizeCx;
26use kas::{Layout, Widget, impl_self};
27
28#[impl_self]
29mod WithStretch {
30    /// Adjust stretch rules
31    ///
32    /// Usually, this type will be constructed through one of the methods on
33    /// trait [`AdaptWidget`].
34    #[derive_widget]
35    pub struct WithStretch<W: Widget> {
36        #[widget]
37        pub inner: W,
38        /// Horizontal stretch
39        ///
40        /// Use [`ConfigCx::resize`] to apply changes.
41        pub horiz: Option<Stretch>,
42        /// Vertical stretch
43        ///
44        /// Use [`ConfigCx::resize`] to apply changes.
45        pub vert: Option<Stretch>,
46    }
47
48    impl Self {
49        /// Construct
50        pub fn new(
51            inner: W,
52            horiz: impl Into<Option<Stretch>>,
53            vert: impl Into<Option<Stretch>>,
54        ) -> Self {
55            WithStretch {
56                inner,
57                horiz: horiz.into(),
58                vert: vert.into(),
59            }
60        }
61    }
62
63    impl Layout for Self {
64        fn size_rules(&mut self, cx: &mut SizeCx, axis: AxisInfo) -> SizeRules {
65            let mut rules = self.inner.size_rules(cx, axis);
66            if axis.is_horizontal()
67                && let Some(stretch) = self.horiz
68            {
69                rules.set_stretch(stretch);
70            } else if axis.is_vertical()
71                && let Some(stretch) = self.vert
72            {
73                rules.set_stretch(stretch);
74            }
75            rules
76        }
77    }
78}