diff options
Diffstat (limited to 'native/src')
-rw-r--r-- | native/src/image.rs | 31 | ||||
-rw-r--r-- | native/src/svg.rs | 9 | ||||
-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/pick_list.rs | 6 | ||||
-rw-r--r-- | native/src/widget/svg.rs | 4 | ||||
-rw-r--r-- | native/src/widget/text_input.rs | 8 | ||||
-rw-r--r-- | native/src/window/action.rs | 20 |
8 files changed, 56 insertions, 30 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/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/pick_list.rs b/native/src/widget/pick_list.rs index c334804e..896f5b35 100644 --- a/native/src/widget/pick_list.rs +++ b/native/src/widget/pick_list.rs @@ -348,9 +348,9 @@ where let state = state(); let event_status = if state.is_open { - // TODO: Encode cursor availability in the type system - state.is_open = - cursor_position.x < 0.0 || cursor_position.y < 0.0; + // Event wasn't processed by overlay, so cursor was clicked either outside it's + // bounds or on the drop-down, either way we close the overlay. + state.is_open = false; event::Status::Captured } else if layout.bounds().contains(cursor_position) { 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 dfc49a8d..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, @@ -716,14 +716,14 @@ where } return event::Status::Captured; + } else { + state.is_pasting = None; } } Event::Keyboard(keyboard::Event::ModifiersChanged(modifiers)) => { let state = state(); - if state.is_focused { - state.keyboard_modifiers = modifiers; - } + state.keyboard_modifiers = modifiers; } _ => {} } 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"), } } |