diff options
| author | 2025-03-20 11:57:26 +0000 | |
|---|---|---|
| committer | 2025-03-20 11:57:26 +0000 | |
| commit | 27b099c895825ee03555fd7bdaa8cefdb2125ead (patch) | |
| tree | 4b77fc19db7785884c4a67e85e452bb140e13688 /core/src | |
| parent | bae25b74f68078e5ff74cdae717273cf315d4e90 (diff) | |
| download | iced-master.tar.gz iced-master.tar.bz2 iced-master.zip | |
WIP: background image supportmaster
Diffstat (limited to 'core/src')
| -rw-r--r-- | core/src/background.rs | 23 | ||||
| -rw-r--r-- | core/src/background/background_image.rs | 60 | ||||
| -rw-r--r-- | core/src/text.rs | 4 | 
3 files changed, 82 insertions, 5 deletions
| diff --git a/core/src/background.rs b/core/src/background.rs index 1f665ef4..98430020 100644 --- a/core/src/background.rs +++ b/core/src/background.rs @@ -1,14 +1,22 @@ -use crate::Color; +mod background_image; + +use std::sync::Arc; + +use background_image::BackgroundImage; +  use crate::gradient::{self, Gradient}; +use crate::image::{FilterMethod, Handle}; +use crate::{Color, Image, Point, Rectangle, Size, image};  /// The background of some element. -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, PartialEq)]  pub enum Background {      /// A solid color.      Color(Color),      /// Linearly interpolate between several colors.      Gradient(Gradient), -    // TODO: Add image variant +    /// A background image. +    Image(BackgroundImage),  }  impl Background { @@ -20,6 +28,9 @@ impl Background {              Self::Gradient(gradient) => {                  Self::Gradient(gradient.scale_alpha(factor))              } +            Self::Image(background_image) => { +                Self::Image(background_image.scale_opacity(factor)) +            }          }      }  } @@ -41,3 +52,9 @@ impl From<gradient::Linear> for Background {          Background::Gradient(Gradient::Linear(gradient))      }  } + +impl From<BackgroundImage> for Background { +    fn from(background_image: BackgroundImage) -> Self { +        Background::Image(background_image) +    } +} diff --git a/core/src/background/background_image.rs b/core/src/background/background_image.rs new file mode 100644 index 00000000..75d63dfc --- /dev/null +++ b/core/src/background/background_image.rs @@ -0,0 +1,60 @@ +use crate::{Image, Point, Size}; + +/// A background image. +#[derive(Debug, Clone, PartialEq)] +pub struct BackgroundImage { +    // TODO: svg support +    image: Image, +    attachment: Attachment, +    clip: Sizing, +    origin: Sizing, +    /// (x, y) +    repeat: (Repeat, Repeat), +    size: Size, +    // filter_method: FilterMethod, +    // opacity: f32, +    // TODO: blend mode +} + +impl BackgroundImage { +    /// Scales the opacity  of the [`BackgroundImage`] by the given factor. +    pub fn scale_opacity(mut self, factor: f32) -> Self { +        let opacity = self.image.opacity * factor; +        self.image = self.image.opacity(opacity); +        self +    } +} + +/// An attachment property. +#[derive(Debug, Clone, Copy, PartialEq, Default)] +pub enum Attachment { +    /// Fixed relative to the viewport. +    Fixed, +    /// Fixed relative to self. +    #[default] +    Local, +} + +/// A sizing property. +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum Sizing { +    /// At outside edge of border. +    Border, +    /// At outside edge of padding. +    Padding, +    /// At outside edge of content. +    Content, +} + +/// A repetition property. +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum Repeat { +    /// Repeat beginning at offset. Will be clipped if necessary. +    Repeat(Point), +    /// Repeated as much as possible without clipping. Whitespace is evenly distributed between each instance. +    Space, +    /// Repeated as much as possible without clipping. Stretched until there is room for another to be added. +    Round, +    /// No repetition. Position defined by point. Will be clipped if necessary. +    NoRepeat(Point), +} diff --git a/core/src/text.rs b/core/src/text.rs index a7e1f281..8a6ff140 100644 --- a/core/src/text.rs +++ b/core/src/text.rs @@ -16,7 +16,7 @@ use std::borrow::Cow;  use std::hash::{Hash, Hasher};  /// A paragraph. -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone)]  pub struct Text<Content = String, Font = crate::Font> {      /// The content of the paragraph.      pub content: Content, @@ -271,7 +271,7 @@ pub struct Span<'a, Link = (), Font = crate::Font> {  }  /// A text highlight. -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, PartialEq)]  pub struct Highlight {      /// The [`Background`] of the highlight.      pub background: Background, | 
