summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector@hecrj.dev>2024-02-15 01:48:36 +0100
committerLibravatar GitHub <noreply@github.com>2024-02-15 01:48:36 +0100
commit5827023ccc3f80012b17dbfe778fbd8b63186c99 (patch)
treeaa0095d5c246c0fb5cee826d8da42ea8c0d4425b
parent75b19646d3d17b9b44bb495e239cbbc2d139c0e9 (diff)
parenta73386f68e01943a28f9286f6d5a1bd017e1d7c3 (diff)
downloadiced-5827023ccc3f80012b17dbfe778fbd8b63186c99.tar.gz
iced-5827023ccc3f80012b17dbfe778fbd8b63186c99.tar.bz2
iced-5827023ccc3f80012b17dbfe778fbd8b63186c99.zip
Merge pull request #2252 from iced-rs/feature/clip-property
Add `clip` property to `Container`, `Column`, and `Row`
-rw-r--r--CHANGELOG.md1
-rw-r--r--widget/src/button.rs19
-rw-r--r--widget/src/column.rs23
-rw-r--r--widget/src/container.rs17
-rw-r--r--widget/src/row.rs23
5 files changed, 75 insertions, 8 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 14e72d07..2b5dc776 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -47,6 +47,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Support for generic `Element` in `Tooltip`. [#2228](https://github.com/iced-rs/iced/pull/2228)
- Container and `gap` styling for `Scrollable`. [#2239](https://github.com/iced-rs/iced/pull/2239)
- Use `Borrow` for both `options` and `selected` in PickList. [#2251](https://github.com/iced-rs/iced/pull/2251)
+- `clip` property for `Container`, `Column`, `Row`, and `Button`. #[2252](https://github.com/iced-rs/iced/pull/2252)
### Changed
- Enable WebGPU backend in `wgpu` by default instead of WebGL. [#2068](https://github.com/iced-rs/iced/pull/2068)
diff --git a/widget/src/button.rs b/widget/src/button.rs
index d16e8c67..867fbfaf 100644
--- a/widget/src/button.rs
+++ b/widget/src/button.rs
@@ -61,6 +61,7 @@ where
width: Length,
height: Length,
padding: Padding,
+ clip: bool,
style: Theme::Style,
}
@@ -82,6 +83,7 @@ where
width: size.width.fluid(),
height: size.height.fluid(),
padding: Padding::new(5.0),
+ clip: false,
style: Theme::Style::default(),
}
}
@@ -126,6 +128,13 @@ where
self.style = style.into();
self
}
+
+ /// Sets whether the contents of the [`Button`] should be clipped on
+ /// overflow.
+ pub fn clip(mut self, clip: bool) -> Self {
+ self.clip = clip;
+ self
+ }
}
impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
@@ -227,7 +236,7 @@ where
_style: &renderer::Style,
layout: Layout<'_>,
cursor: mouse::Cursor,
- _viewport: &Rectangle,
+ viewport: &Rectangle,
) {
let bounds = layout.bounds();
let content_layout = layout.children().next().unwrap();
@@ -242,6 +251,12 @@ where
|| tree.state.downcast_ref::<State>(),
);
+ let viewport = if self.clip {
+ bounds.intersection(viewport).unwrap_or(*viewport)
+ } else {
+ *viewport
+ };
+
self.content.as_widget().draw(
&tree.children[0],
renderer,
@@ -251,7 +266,7 @@ where
},
content_layout,
cursor,
- &bounds,
+ &viewport,
);
}
diff --git a/widget/src/column.rs b/widget/src/column.rs
index 1e9841ee..8154ad85 100644
--- a/widget/src/column.rs
+++ b/widget/src/column.rs
@@ -20,6 +20,7 @@ pub struct Column<'a, Message, Theme = crate::Theme, Renderer = crate::Renderer>
height: Length,
max_width: f32,
align_items: Alignment,
+ clip: bool,
children: Vec<Element<'a, Message, Theme, Renderer>>,
}
@@ -36,6 +37,7 @@ where
height: Length::Shrink,
max_width: f32::INFINITY,
align_items: Alignment::Start,
+ clip: false,
children: Vec::new(),
}
}
@@ -87,6 +89,13 @@ where
self
}
+ /// Sets whether the contents of the [`Column`] should be clipped on
+ /// overflow.
+ pub fn clip(mut self, clip: bool) -> Self {
+ self.clip = clip;
+ self
+ }
+
/// Adds an element to the [`Column`].
pub fn push(
mut self,
@@ -240,7 +249,7 @@ where
cursor: mouse::Cursor,
viewport: &Rectangle,
) {
- if let Some(viewport) = layout.bounds().intersection(viewport) {
+ if let Some(clipped_viewport) = layout.bounds().intersection(viewport) {
for ((child, state), layout) in self
.children
.iter()
@@ -248,7 +257,17 @@ where
.zip(layout.children())
{
child.as_widget().draw(
- state, renderer, theme, style, layout, cursor, &viewport,
+ state,
+ renderer,
+ theme,
+ style,
+ layout,
+ cursor,
+ if self.clip {
+ &clipped_viewport
+ } else {
+ viewport
+ },
);
}
}
diff --git a/widget/src/container.rs b/widget/src/container.rs
index 4eb4a5d9..e0174177 100644
--- a/widget/src/container.rs
+++ b/widget/src/container.rs
@@ -37,6 +37,7 @@ pub struct Container<
horizontal_alignment: alignment::Horizontal,
vertical_alignment: alignment::Vertical,
style: Theme::Style,
+ clip: bool,
content: Element<'a, Message, Theme, Renderer>,
}
@@ -63,6 +64,7 @@ where
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top,
style: Default::default(),
+ clip: false,
content,
}
}
@@ -132,6 +134,13 @@ where
self.style = style.into();
self
}
+
+ /// Sets whether the contents of the [`Container`] should be clipped on
+ /// overflow.
+ pub fn clip(mut self, clip: bool) -> Self {
+ self.clip = clip;
+ self
+ }
}
impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
@@ -255,7 +264,7 @@ where
) {
let style = theme.appearance(&self.style);
- if let Some(viewport) = layout.bounds().intersection(viewport) {
+ if let Some(clipped_viewport) = layout.bounds().intersection(viewport) {
draw_background(renderer, &style, layout.bounds());
self.content.as_widget().draw(
@@ -269,7 +278,11 @@ where
},
layout.children().next().unwrap(),
cursor,
- &viewport,
+ if self.clip {
+ &clipped_viewport
+ } else {
+ viewport
+ },
);
}
}
diff --git a/widget/src/row.rs b/widget/src/row.rs
index 7f8c3354..735fbbc0 100644
--- a/widget/src/row.rs
+++ b/widget/src/row.rs
@@ -18,6 +18,7 @@ pub struct Row<'a, Message, Theme = crate::Theme, Renderer = crate::Renderer> {
width: Length,
height: Length,
align_items: Alignment,
+ clip: bool,
children: Vec<Element<'a, Message, Theme, Renderer>>,
}
@@ -33,6 +34,7 @@ where
width: Length::Shrink,
height: Length::Shrink,
align_items: Alignment::Start,
+ clip: false,
children: Vec::new(),
}
}
@@ -78,6 +80,13 @@ where
self
}
+ /// Sets whether the contents of the [`Row`] should be clipped on
+ /// overflow.
+ pub fn clip(mut self, clip: bool) -> Self {
+ self.clip = clip;
+ self
+ }
+
/// Adds an [`Element`] to the [`Row`].
pub fn push(
mut self,
@@ -229,7 +238,7 @@ where
cursor: mouse::Cursor,
viewport: &Rectangle,
) {
- if let Some(viewport) = layout.bounds().intersection(viewport) {
+ if let Some(clipped_viewport) = layout.bounds().intersection(viewport) {
for ((child, state), layout) in self
.children
.iter()
@@ -237,7 +246,17 @@ where
.zip(layout.children())
{
child.as_widget().draw(
- state, renderer, theme, style, layout, cursor, &viewport,
+ state,
+ renderer,
+ theme,
+ style,
+ layout,
+ cursor,
+ if self.clip {
+ &clipped_viewport
+ } else {
+ viewport
+ },
);
}
}