diff options
Diffstat (limited to 'native')
-rw-r--r-- | native/src/image.rs | 31 | ||||
-rw-r--r-- | native/src/svg.rs | 9 | ||||
-rw-r--r-- | native/src/widget/button.rs | 9 | ||||
-rw-r--r-- | native/src/widget/container.rs | 8 | ||||
-rw-r--r-- | native/src/widget/image.rs | 4 | ||||
-rw-r--r-- | native/src/widget/image/viewer.rs | 4 | ||||
-rw-r--r-- | native/src/widget/svg.rs | 4 | ||||
-rw-r--r-- | native/src/widget/text_input.rs | 12 | ||||
-rw-r--r-- | native/src/window/action.rs | 20 |
9 files changed, 66 insertions, 35 deletions
diff --git a/native/src/image.rs b/native/src/image.rs index 516eb2db..06fd7ae6 100644 --- a/native/src/image.rs +++ b/native/src/image.rs @@ -1,6 +1,7 @@ //! Load and draw raster graphics. -use crate::{Hasher, Rectangle}; +use crate::{Hasher, Rectangle, Size}; +use std::borrow::Cow; use std::hash::{Hash, Hasher as _}; use std::path::PathBuf; use std::sync::Arc; @@ -21,15 +22,19 @@ impl Handle { } /// Creates an image [`Handle`] containing the image pixels directly. This - /// function expects the input data to be provided as a `Vec<u8>` of BGRA + /// function expects the input data to be provided as a `Vec<u8>` of RGBA /// pixels. /// /// This is useful if you have already decoded your image. - pub fn from_pixels(width: u32, height: u32, pixels: Vec<u8>) -> Handle { - Self::from_data(Data::Pixels { + pub fn from_pixels( + width: u32, + height: u32, + pixels: impl Into<Cow<'static, [u8]>>, + ) -> Handle { + Self::from_data(Data::Rgba { width, height, - pixels, + pixels: pixels.into(), }) } @@ -39,8 +44,8 @@ impl Handle { /// /// This is useful if you already have your image loaded in-memory, maybe /// because you downloaded or generated it procedurally. - pub fn from_memory(bytes: Vec<u8>) -> Handle { - Self::from_data(Data::Bytes(bytes)) + pub fn from_memory(bytes: impl Into<Cow<'static, [u8]>>) -> Handle { + Self::from_data(Data::Bytes(bytes.into())) } fn from_data(data: Data) -> Handle { @@ -86,16 +91,16 @@ pub enum Data { Path(PathBuf), /// In-memory data - Bytes(Vec<u8>), + Bytes(Cow<'static, [u8]>), - /// Decoded image pixels in BGRA format. - Pixels { + /// Decoded image pixels in RGBA format. + Rgba { /// The width of the image. width: u32, /// The height of the image. height: u32, /// The pixels. - pixels: Vec<u8>, + pixels: Cow<'static, [u8]>, }, } @@ -104,7 +109,7 @@ impl std::fmt::Debug for Data { match self { Data::Path(path) => write!(f, "Path({:?})", path), Data::Bytes(_) => write!(f, "Bytes(...)"), - Data::Pixels { width, height, .. } => { + Data::Rgba { width, height, .. } => { write!(f, "Pixels({} * {})", width, height) } } @@ -121,7 +126,7 @@ pub trait Renderer: crate::Renderer { type Handle: Clone + Hash; /// Returns the dimensions of an image for the given [`Handle`]. - fn dimensions(&self, handle: &Self::Handle) -> (u32, u32); + fn dimensions(&self, handle: &Self::Handle) -> Size<u32>; /// Draws an image with the given [`Handle`] and inside the provided /// `bounds`. diff --git a/native/src/svg.rs b/native/src/svg.rs index f86fec5b..a8e481d2 100644 --- a/native/src/svg.rs +++ b/native/src/svg.rs @@ -1,6 +1,7 @@ //! Load and draw vector graphics. -use crate::{Hasher, Rectangle}; +use crate::{Hasher, Rectangle, Size}; +use std::borrow::Cow; use std::hash::{Hash, Hasher as _}; use std::path::PathBuf; use std::sync::Arc; @@ -24,7 +25,7 @@ impl Handle { /// /// This is useful if you already have your SVG data in-memory, maybe /// because you downloaded or generated it procedurally. - pub fn from_memory(bytes: impl Into<Vec<u8>>) -> Handle { + pub fn from_memory(bytes: impl Into<Cow<'static, [u8]>>) -> Handle { Self::from_data(Data::Bytes(bytes.into())) } @@ -64,7 +65,7 @@ pub enum Data { /// In-memory data /// /// Can contain an SVG string or a gzip compressed data. - Bytes(Vec<u8>), + Bytes(Cow<'static, [u8]>), } impl std::fmt::Debug for Data { @@ -81,7 +82,7 @@ impl std::fmt::Debug for Data { /// [renderer]: crate::renderer pub trait Renderer: crate::Renderer { /// Returns the default dimensions of an SVG for the given [`Handle`]. - fn dimensions(&self, handle: &Handle) -> (u32, u32); + fn dimensions(&self, handle: &Handle) -> Size<u32>; /// Draws an SVG with the given [`Handle`] and inside the provided `bounds`. fn draw(&mut self, handle: Handle, bounds: Rectangle); diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index 6c0b8f6e..1582188b 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -426,12 +426,13 @@ pub fn layout<Renderer>( padding: Padding, layout_content: impl FnOnce(&Renderer, &layout::Limits) -> layout::Node, ) -> layout::Node { - let limits = limits.width(width).height(height).pad(padding); + let limits = limits.width(width).height(height); - let mut content = layout_content(renderer, &limits); - content.move_to(Point::new(padding.left.into(), padding.top.into())); + let mut content = layout_content(renderer, &limits.pad(padding)); + let padding = padding.fit(content.size(), limits.max()); + let size = limits.pad(padding).resolve(content.size()).pad(padding); - let size = limits.resolve(content.size()).pad(padding); + content.move_to(Point::new(padding.left.into(), padding.top.into())); layout::Node::with_children(size, vec![content]) } diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index 2afad3f2..10a80b58 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -293,11 +293,11 @@ pub fn layout<Renderer>( .max_width(max_width) .max_height(max_height) .width(width) - .height(height) - .pad(padding); + .height(height); - let mut content = layout_content(renderer, &limits.loose()); - let size = limits.resolve(content.size()); + let mut content = layout_content(renderer, &limits.pad(padding).loose()); + let padding = padding.fit(content.size(), limits.max()); + let size = limits.pad(padding).resolve(content.size()); content.move_to(Point::new(padding.left.into(), padding.top.into())); content.align( diff --git a/native/src/widget/image.rs b/native/src/widget/image.rs index 91d68e34..8bd8ca1e 100644 --- a/native/src/widget/image.rs +++ b/native/src/widget/image.rs @@ -85,7 +85,7 @@ where { // The raw w/h of the underlying image let image_size = { - let (width, height) = renderer.dimensions(handle); + let Size { width, height } = renderer.dimensions(handle); Size::new(width as f32, height as f32) }; @@ -149,7 +149,7 @@ where _cursor_position: Point, _viewport: &Rectangle, ) { - let (width, height) = renderer.dimensions(&self.handle); + let Size { width, height } = renderer.dimensions(&self.handle); let image_size = Size::new(width as f32, height as f32); let bounds = layout.bounds(); diff --git a/native/src/widget/image/viewer.rs b/native/src/widget/image/viewer.rs index b1fe596c..9c83287e 100644 --- a/native/src/widget/image/viewer.rs +++ b/native/src/widget/image/viewer.rs @@ -108,7 +108,7 @@ where renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let (width, height) = renderer.dimensions(&self.handle); + let Size { width, height } = renderer.dimensions(&self.handle); let mut size = limits .width(self.width) @@ -409,7 +409,7 @@ pub fn image_size<Renderer>( where Renderer: image::Renderer, { - let (width, height) = renderer.dimensions(handle); + let Size { width, height } = renderer.dimensions(handle); let (width, height) = { let dimensions = (width as f32, height as f32); diff --git a/native/src/widget/svg.rs b/native/src/widget/svg.rs index aa68bfb8..1015ed0a 100644 --- a/native/src/widget/svg.rs +++ b/native/src/widget/svg.rs @@ -83,7 +83,7 @@ where limits: &layout::Limits, ) -> layout::Node { // The raw w/h of the underlying image - let (width, height) = renderer.dimensions(&self.handle); + let Size { width, height } = renderer.dimensions(&self.handle); let image_size = Size::new(width as f32, height as f32); // The size to be available to the widget prior to `Shrink`ing @@ -120,7 +120,7 @@ where _cursor_position: Point, _viewport: &Rectangle, ) { - let (width, height) = renderer.dimensions(&self.handle); + let Size { width, height } = renderer.dimensions(&self.handle); let image_size = Size::new(width as f32, height as f32); let bounds = layout.bounds(); diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index e5213cbe..25a8690e 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -92,7 +92,7 @@ where is_secure: false, font: Default::default(), width: Length::Fill, - padding: Padding::ZERO, + padding: Padding::new(5), size: None, on_change: Box::new(on_change), on_paste: None, @@ -350,15 +350,19 @@ where { let text_size = size.unwrap_or_else(|| renderer.default_size()); - let limits = limits + let text_limits = limits .pad(padding) .width(width) .height(Length::Units(text_size)); + let limits = limits.width(width).height(Length::Shrink); + + let mut text = layout::Node::new(text_limits.resolve(Size::ZERO)); + let padding = padding.fit(text.size(), limits.max()); + let size = limits.pad(padding).resolve(text.size()).pad(padding); - let mut text = layout::Node::new(limits.resolve(Size::ZERO)); text.move_to(Point::new(padding.left.into(), padding.top.into())); - layout::Node::with_children(text.size().pad(padding), vec![text]) + layout::Node::with_children(size, vec![text]) } /// Processes an [`Event`] and updates the [`State`] of a [`TextInput`] diff --git a/native/src/window/action.rs b/native/src/window/action.rs index 73338e22..009dcc27 100644 --- a/native/src/window/action.rs +++ b/native/src/window/action.rs @@ -5,6 +5,12 @@ use std::fmt; /// An operation to be performed on some window. pub enum Action<T> { + /// Moves the window with the left mouse button until the button is + /// released. + /// + /// There’s no guarantee that this will work unless the left mouse + /// button was pressed immediately before this function is called. + Drag, /// Resize the window. Resize { /// The new logical width of the window @@ -12,6 +18,10 @@ pub enum Action<T> { /// The new logical height of the window height: u32, }, + /// Sets the window to maximized or back + Maximize(bool), + /// Set the window to minimized or back + Minimize(bool), /// Move the window. /// /// Unsupported on Wayland. @@ -23,6 +33,8 @@ pub enum Action<T> { }, /// Set the [`Mode`] of the window. SetMode(Mode), + /// Sets the window to maximized or back + ToggleMaximize, /// Fetch the current [`Mode`] of the window. FetchMode(Box<dyn FnOnce(Mode) -> T + 'static>), } @@ -37,9 +49,13 @@ impl<T> Action<T> { T: 'static, { match self { + Self::Drag => Action::Drag, Self::Resize { width, height } => Action::Resize { width, height }, + Self::Maximize(bool) => Action::Maximize(bool), + Self::Minimize(bool) => Action::Minimize(bool), Self::Move { x, y } => Action::Move { x, y }, Self::SetMode(mode) => Action::SetMode(mode), + Self::ToggleMaximize => Action::ToggleMaximize, Self::FetchMode(o) => Action::FetchMode(Box::new(move |s| f(o(s)))), } } @@ -48,15 +64,19 @@ impl<T> Action<T> { impl<T> fmt::Debug for Action<T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { + Self::Drag => write!(f, "Action::Drag"), Self::Resize { width, height } => write!( f, "Action::Resize {{ widget: {}, height: {} }}", width, height ), + Self::Maximize(value) => write!(f, "Action::Maximize({})", value), + Self::Minimize(value) => write!(f, "Action::Minimize({}", value), Self::Move { x, y } => { write!(f, "Action::Move {{ x: {}, y: {} }}", x, y) } Self::SetMode(mode) => write!(f, "Action::SetMode({:?})", mode), + Self::ToggleMaximize => write!(f, "Action::ToggleMaximize"), Self::FetchMode(_) => write!(f, "Action::FetchMode"), } } |