summaryrefslogtreecommitdiffstats
path: root/widget/src/rule.rs
diff options
context:
space:
mode:
authorLibravatar Bingus <shankern@protonmail.com>2023-07-12 12:23:18 -0700
committerLibravatar Bingus <shankern@protonmail.com>2023-07-12 12:23:18 -0700
commit633f405f3f78bc7f82d2b2061491b0e011137451 (patch)
tree5ebfc1f45d216a5c14a90492563599e6969eab4d /widget/src/rule.rs
parent41836dd80d0534608e7aedfbf2319c540a23de1a (diff)
parent21bd51426d900e271206f314e0c915dd41065521 (diff)
downloadiced-633f405f3f78bc7f82d2b2061491b0e011137451.tar.gz
iced-633f405f3f78bc7f82d2b2061491b0e011137451.tar.bz2
iced-633f405f3f78bc7f82d2b2061491b0e011137451.zip
Merge remote-tracking branch 'origin/master' into feat/multi-window-support
# Conflicts: # Cargo.toml # core/src/window/icon.rs # core/src/window/id.rs # core/src/window/position.rs # core/src/window/settings.rs # examples/integration/src/main.rs # examples/integration_opengl/src/main.rs # glutin/src/application.rs # native/src/subscription.rs # native/src/window.rs # runtime/src/window/action.rs # src/lib.rs # src/window.rs # winit/Cargo.toml # winit/src/application.rs # winit/src/icon.rs # winit/src/settings.rs # winit/src/window.rs
Diffstat (limited to 'widget/src/rule.rs')
-rw-r--r--widget/src/rule.rs148
1 files changed, 148 insertions, 0 deletions
diff --git a/widget/src/rule.rs b/widget/src/rule.rs
new file mode 100644
index 00000000..d703e6ae
--- /dev/null
+++ b/widget/src/rule.rs
@@ -0,0 +1,148 @@
+//! Display a horizontal or vertical rule for dividing content.
+use crate::core::layout;
+use crate::core::mouse;
+use crate::core::renderer;
+use crate::core::widget::Tree;
+use crate::core::{
+ Color, Element, Layout, Length, Pixels, Rectangle, Size, Widget,
+};
+
+pub use crate::style::rule::{Appearance, FillMode, StyleSheet};
+
+/// Display a horizontal or vertical rule for dividing content.
+#[allow(missing_debug_implementations)]
+pub struct Rule<Renderer = crate::Renderer>
+where
+ Renderer: crate::core::Renderer,
+ Renderer::Theme: StyleSheet,
+{
+ width: Length,
+ height: Length,
+ is_horizontal: bool,
+ style: <Renderer::Theme as StyleSheet>::Style,
+}
+
+impl<Renderer> Rule<Renderer>
+where
+ Renderer: crate::core::Renderer,
+ Renderer::Theme: StyleSheet,
+{
+ /// Creates a horizontal [`Rule`] with the given height.
+ pub fn horizontal(height: impl Into<Pixels>) -> Self {
+ Rule {
+ width: Length::Fill,
+ height: Length::Fixed(height.into().0),
+ is_horizontal: true,
+ style: Default::default(),
+ }
+ }
+
+ /// Creates a vertical [`Rule`] with the given width.
+ pub fn vertical(width: impl Into<Pixels>) -> Self {
+ Rule {
+ width: Length::Fixed(width.into().0),
+ height: Length::Fill,
+ is_horizontal: false,
+ style: Default::default(),
+ }
+ }
+
+ /// Sets the style of the [`Rule`].
+ pub fn style(
+ mut self,
+ style: impl Into<<Renderer::Theme as StyleSheet>::Style>,
+ ) -> Self {
+ self.style = style.into();
+ self
+ }
+}
+
+impl<Message, Renderer> Widget<Message, Renderer> for Rule<Renderer>
+where
+ Renderer: crate::core::Renderer,
+ Renderer::Theme: StyleSheet,
+{
+ fn width(&self) -> Length {
+ self.width
+ }
+
+ fn height(&self) -> Length {
+ self.height
+ }
+
+ fn layout(
+ &self,
+ _renderer: &Renderer,
+ limits: &layout::Limits,
+ ) -> layout::Node {
+ let limits = limits.width(self.width).height(self.height);
+
+ layout::Node::new(limits.resolve(Size::ZERO))
+ }
+
+ fn draw(
+ &self,
+ _state: &Tree,
+ renderer: &mut Renderer,
+ theme: &Renderer::Theme,
+ _style: &renderer::Style,
+ layout: Layout<'_>,
+ _cursor: mouse::Cursor,
+ _viewport: &Rectangle,
+ ) {
+ let bounds = layout.bounds();
+ let style = theme.appearance(&self.style);
+
+ let bounds = if self.is_horizontal {
+ let line_y = (bounds.y + (bounds.height / 2.0)
+ - (style.width as f32 / 2.0))
+ .round();
+
+ let (offset, line_width) = style.fill_mode.fill(bounds.width);
+ let line_x = bounds.x + offset;
+
+ Rectangle {
+ x: line_x,
+ y: line_y,
+ width: line_width,
+ height: style.width as f32,
+ }
+ } else {
+ let line_x = (bounds.x + (bounds.width / 2.0)
+ - (style.width as f32 / 2.0))
+ .round();
+
+ let (offset, line_height) = style.fill_mode.fill(bounds.height);
+ let line_y = bounds.y + offset;
+
+ Rectangle {
+ x: line_x,
+ y: line_y,
+ width: style.width as f32,
+ height: line_height,
+ }
+ };
+
+ renderer.fill_quad(
+ renderer::Quad {
+ bounds,
+ border_radius: style.radius,
+ border_width: 0.0,
+ border_color: Color::TRANSPARENT,
+ },
+ style.color,
+ );
+ }
+}
+
+impl<'a, Message, Renderer> From<Rule<Renderer>>
+ for Element<'a, Message, Renderer>
+where
+ Message: 'a,
+ Renderer: 'a + crate::core::Renderer,
+ Renderer::Theme: StyleSheet,
+{
+ fn from(rule: Rule<Renderer>) -> Element<'a, Message, Renderer> {
+ Element::new(rule)
+ }
+}