From 233196eb14b40f8bd5201ea0262571f82136ad53 Mon Sep 17 00:00:00 2001 From: Bingus Date: Sat, 25 Mar 2023 10:45:39 -0700 Subject: Added offscreen rendering support for wgpu & tiny-skia exposed with the window::screenshot command. --- examples/screenshot/src/main.rs | 305 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 305 insertions(+) create mode 100644 examples/screenshot/src/main.rs (limited to 'examples/screenshot/src/main.rs') diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs new file mode 100644 index 00000000..29d961d9 --- /dev/null +++ b/examples/screenshot/src/main.rs @@ -0,0 +1,305 @@ +use iced::alignment::{Horizontal, Vertical}; +use iced::keyboard::KeyCode; +use iced::theme::{Button, Container}; +use iced::widget::runtime::{CropError, Screenshot}; +use iced::widget::{ + button, column as col, container, image as iced_image, row, text, + text_input, +}; +use iced::{ + event, executor, keyboard, subscription, Alignment, Application, Command, + ContentFit, Element, Event, Length, Rectangle, Renderer, Subscription, + Theme, +}; +use image as img; +use image::ColorType; + +fn main() -> iced::Result { + env_logger::builder().format_timestamp(None).init(); + + Example::run(iced::Settings::default()) +} + +struct Example { + screenshot: Option, + saved_png_path: Option>, + png_saving: bool, + crop_error: Option, + x_input_value: u32, + y_input_value: u32, + width_input_value: u32, + height_input_value: u32, +} + +#[derive(Clone, Debug)] +enum Message { + Crop, + Screenshot, + ScreenshotData(Screenshot), + Png, + PngSaved(Result), + XInputChanged(String), + YInputChanged(String), + WidthInputChanged(String), + HeightInputChanged(String), +} + +impl Application for Example { + type Executor = executor::Default; + type Message = Message; + type Theme = Theme; + type Flags = (); + + fn new(_flags: Self::Flags) -> (Self, Command) { + ( + Example { + screenshot: None, + saved_png_path: None, + png_saving: false, + crop_error: None, + x_input_value: 0, + y_input_value: 0, + width_input_value: 0, + height_input_value: 0, + }, + Command::none(), + ) + } + + fn title(&self) -> String { + "Screenshot".to_string() + } + + fn update(&mut self, message: Self::Message) -> Command { + match message { + Message::Screenshot => { + return iced::window::screenshot(Message::ScreenshotData); + } + Message::ScreenshotData(screenshot) => { + self.screenshot = Some(screenshot); + } + Message::Png => { + if let Some(screenshot) = &self.screenshot { + return Command::perform( + save_to_png(screenshot.clone()), + Message::PngSaved, + ); + } + self.png_saving = true; + } + Message::PngSaved(res) => { + self.png_saving = false; + self.saved_png_path = Some(res); + } + Message::XInputChanged(new) => { + if let Ok(value) = new.parse::() { + self.x_input_value = value; + } + } + Message::YInputChanged(new) => { + if let Ok(value) = new.parse::() { + self.y_input_value = value; + } + } + Message::WidthInputChanged(new) => { + if let Ok(value) = new.parse::() { + self.width_input_value = value; + } + } + Message::HeightInputChanged(new) => { + if let Ok(value) = new.parse::() { + self.height_input_value = value; + } + } + Message::Crop => { + if let Some(screenshot) = &self.screenshot { + let cropped = screenshot.crop(Rectangle:: { + x: self.x_input_value, + y: self.y_input_value, + width: self.width_input_value, + height: self.height_input_value, + }); + + match cropped { + Ok(screenshot) => { + self.screenshot = Some(screenshot); + self.crop_error = None; + } + Err(crop_error) => { + self.crop_error = Some(crop_error); + } + } + } + } + } + + Command::none() + } + + fn view(&self) -> Element<'_, Self::Message, Renderer> { + let image: Element = if let Some(screenshot) = &self.screenshot + { + iced_image(iced_image::Handle::from_pixels( + screenshot.size.width, + screenshot.size.height, + screenshot.bytes.clone(), + )) + .content_fit(ContentFit::ScaleDown) + .width(Length::Fill) + .height(Length::Fill) + .into() + } else { + text("Press the button to take a screenshot!").into() + }; + + let image = container(image) + .padding(10) + .style(Container::Custom(Box::new(ScreenshotDisplayContainer))) + .width(Length::FillPortion(2)) + .height(Length::Fill) + .center_x() + .center_y(); + + let crop_origin_controls = row![ + text("X:").vertical_alignment(Vertical::Center).width(14), + text_input("0", &format!("{}", self.x_input_value),) + .on_input(Message::XInputChanged) + .width(40), + text("Y:").vertical_alignment(Vertical::Center).width(14), + text_input("0", &format!("{}", self.y_input_value),) + .on_input(Message::YInputChanged) + .width(40), + ] + .spacing(10) + .align_items(Alignment::Center); + + let crop_dimension_controls = row![ + text("W:").vertical_alignment(Vertical::Center).width(14), + text_input("0", &format!("{}", self.width_input_value),) + .on_input(Message::WidthInputChanged) + .width(40), + text("H:").vertical_alignment(Vertical::Center).width(14), + text_input("0", &format!("{}", self.height_input_value),) + .on_input(Message::HeightInputChanged) + .width(40), + ] + .spacing(10) + .align_items(Alignment::Center); + + let mut crop_controls = + col![crop_origin_controls, crop_dimension_controls] + .spacing(10) + .align_items(Alignment::Center); + + if let Some(crop_error) = &self.crop_error { + crop_controls = crop_controls + .push(text(format!("Crop error! \n{}", crop_error))); + } + + let png_button = if !self.png_saving { + button("Save to png.") + .style(Button::Secondary) + .padding([10, 20, 10, 20]) + .on_press(Message::Png) + } else { + button("Saving..") + .style(Button::Secondary) + .padding([10, 20, 10, 20]) + }; + + let mut controls = col![ + button("Screenshot!") + .padding([10, 20, 10, 20]) + .on_press(Message::Screenshot), + button("Crop") + .style(Button::Destructive) + .padding([10, 20, 10, 20]) + .on_press(Message::Crop), + crop_controls, + png_button, + ] + .spacing(40) + .align_items(Alignment::Center); + + if let Some(png_result) = &self.saved_png_path { + let msg = match png_result { + Ok(path) => format!("Png saved as: {:?}!", path), + Err(msg) => { + format!("Png could not be saved due to:\n{:?}", msg) + } + }; + + controls = controls.push(text(msg)); + } + + let side_content = container(controls) + .align_x(Horizontal::Center) + .width(Length::FillPortion(1)) + .height(Length::Fill) + .center_y() + .center_x(); + + let content = row![side_content, image] + .width(Length::Fill) + .height(Length::Fill) + .align_items(Alignment::Center); + + container(content) + .padding(10) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .into() + } + + fn subscription(&self) -> Subscription { + subscription::events_with(|event, status| { + if let event::Status::Captured = status { + return None; + } + + if let Event::Keyboard(keyboard::Event::KeyPressed { + key_code: KeyCode::F5, + .. + }) = event + { + Some(Message::Screenshot) + } else { + None + } + }) + } +} + +struct ScreenshotDisplayContainer; + +impl container::StyleSheet for ScreenshotDisplayContainer { + type Style = Theme; + + fn appearance(&self, style: &Self::Style) -> container::Appearance { + container::Appearance { + text_color: None, + background: None, + border_radius: 5.0, + border_width: 4.0, + border_color: style.palette().primary, + } + } +} + +async fn save_to_png(screenshot: Screenshot) -> Result { + let path = "screenshot.png".to_string(); + img::save_buffer( + &path, + &screenshot.bytes, + screenshot.size.width, + screenshot.size.height, + ColorType::Rgba8, + ) + .map(|_| path) + .map_err(|err| PngError(format!("{:?}", err))) +} + +#[derive(Clone, Debug)] +struct PngError(String); -- cgit From 5ed945287745f5c40ca5cc8013458eee89e76f35 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 6 Jun 2023 15:39:29 +0200 Subject: Use `Container::Box` in `screenshot` example --- examples/screenshot/src/main.rs | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) (limited to 'examples/screenshot/src/main.rs') diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index 29d961d9..cfdddd72 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -154,7 +154,7 @@ impl Application for Example { let image = container(image) .padding(10) - .style(Container::Custom(Box::new(ScreenshotDisplayContainer))) + .style(Container::Box) .width(Length::FillPortion(2)) .height(Length::Fill) .center_x() @@ -272,22 +272,6 @@ impl Application for Example { } } -struct ScreenshotDisplayContainer; - -impl container::StyleSheet for ScreenshotDisplayContainer { - type Style = Theme; - - fn appearance(&self, style: &Self::Style) -> container::Appearance { - container::Appearance { - text_color: None, - background: None, - border_radius: 5.0, - border_width: 4.0, - border_color: style.palette().primary, - } - } -} - async fn save_to_png(screenshot: Screenshot) -> Result { let path = "screenshot.png".to_string(); img::save_buffer( -- cgit From 8820583cc04f468b403e0660118760389f9e4370 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 6 Jun 2023 15:51:32 +0200 Subject: Create `numeric_input` helper in `screenshot` example --- examples/screenshot/src/main.rs | 98 +++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 44 deletions(-) (limited to 'examples/screenshot/src/main.rs') diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index cfdddd72..8e3bcaec 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -25,10 +25,10 @@ struct Example { saved_png_path: Option>, png_saving: bool, crop_error: Option, - x_input_value: u32, - y_input_value: u32, - width_input_value: u32, - height_input_value: u32, + x_input_value: Option, + y_input_value: Option, + width_input_value: Option, + height_input_value: Option, } #[derive(Clone, Debug)] @@ -38,10 +38,10 @@ enum Message { ScreenshotData(Screenshot), Png, PngSaved(Result), - XInputChanged(String), - YInputChanged(String), - WidthInputChanged(String), - HeightInputChanged(String), + XInputChanged(Option), + YInputChanged(Option), + WidthInputChanged(Option), + HeightInputChanged(Option), } impl Application for Example { @@ -57,10 +57,10 @@ impl Application for Example { saved_png_path: None, png_saving: false, crop_error: None, - x_input_value: 0, - y_input_value: 0, - width_input_value: 0, - height_input_value: 0, + x_input_value: None, + y_input_value: None, + width_input_value: None, + height_input_value: None, }, Command::none(), ) @@ -91,33 +91,25 @@ impl Application for Example { self.png_saving = false; self.saved_png_path = Some(res); } - Message::XInputChanged(new) => { - if let Ok(value) = new.parse::() { - self.x_input_value = value; - } + Message::XInputChanged(new_value) => { + self.x_input_value = new_value; } - Message::YInputChanged(new) => { - if let Ok(value) = new.parse::() { - self.y_input_value = value; - } + Message::YInputChanged(new_value) => { + self.y_input_value = new_value; } - Message::WidthInputChanged(new) => { - if let Ok(value) = new.parse::() { - self.width_input_value = value; - } + Message::WidthInputChanged(new_value) => { + self.width_input_value = new_value; } - Message::HeightInputChanged(new) => { - if let Ok(value) = new.parse::() { - self.height_input_value = value; - } + Message::HeightInputChanged(new_value) => { + self.height_input_value = new_value; } Message::Crop => { if let Some(screenshot) = &self.screenshot { let cropped = screenshot.crop(Rectangle:: { - x: self.x_input_value, - y: self.y_input_value, - width: self.width_input_value, - height: self.height_input_value, + x: self.x_input_value.unwrap_or(0), + y: self.y_input_value.unwrap_or(0), + width: self.width_input_value.unwrap_or(0), + height: self.height_input_value.unwrap_or(0), }); match cropped { @@ -162,26 +154,20 @@ impl Application for Example { let crop_origin_controls = row![ text("X:").vertical_alignment(Vertical::Center).width(14), - text_input("0", &format!("{}", self.x_input_value),) - .on_input(Message::XInputChanged) - .width(40), + numeric_input("0", self.x_input_value).map(Message::XInputChanged), text("Y:").vertical_alignment(Vertical::Center).width(14), - text_input("0", &format!("{}", self.y_input_value),) - .on_input(Message::YInputChanged) - .width(40), + numeric_input("0", self.y_input_value).map(Message::YInputChanged) ] .spacing(10) .align_items(Alignment::Center); let crop_dimension_controls = row![ text("W:").vertical_alignment(Vertical::Center).width(14), - text_input("0", &format!("{}", self.width_input_value),) - .on_input(Message::WidthInputChanged) - .width(40), + numeric_input("0", self.width_input_value) + .map(Message::WidthInputChanged), text("H:").vertical_alignment(Vertical::Center).width(14), - text_input("0", &format!("{}", self.height_input_value),) - .on_input(Message::HeightInputChanged) - .width(40), + numeric_input("0", self.height_input_value) + .map(Message::HeightInputChanged) ] .spacing(10) .align_items(Alignment::Center); @@ -287,3 +273,27 @@ async fn save_to_png(screenshot: Screenshot) -> Result { #[derive(Clone, Debug)] struct PngError(String); + +fn numeric_input( + placeholder: &str, + value: Option, +) -> Element<'_, Option> { + text_input( + placeholder, + &value + .as_ref() + .map(ToString::to_string) + .unwrap_or_else(String::new), + ) + .on_input(move |text| { + if text.is_empty() { + None + } else if let Ok(new_value) = text.parse() { + Some(new_value) + } else { + value + } + }) + .width(40) + .into() +} -- cgit From cd15f8305ab1094ff9887f8d8822e385a381ab1c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 6 Jun 2023 15:59:36 +0200 Subject: Fix width of crop labels in `screenshot` example --- examples/screenshot/src/main.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'examples/screenshot/src/main.rs') diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index 8e3bcaec..0d77d316 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -153,19 +153,19 @@ impl Application for Example { .center_y(); let crop_origin_controls = row![ - text("X:").vertical_alignment(Vertical::Center).width(14), + text("X:").vertical_alignment(Vertical::Center).width(20), numeric_input("0", self.x_input_value).map(Message::XInputChanged), - text("Y:").vertical_alignment(Vertical::Center).width(14), + text("Y:").vertical_alignment(Vertical::Center).width(20), numeric_input("0", self.y_input_value).map(Message::YInputChanged) ] .spacing(10) .align_items(Alignment::Center); let crop_dimension_controls = row![ - text("W:").vertical_alignment(Vertical::Center).width(14), + text("W:").vertical_alignment(Vertical::Center).width(20), numeric_input("0", self.width_input_value) .map(Message::WidthInputChanged), - text("H:").vertical_alignment(Vertical::Center).width(14), + text("H:").vertical_alignment(Vertical::Center).width(20), numeric_input("0", self.height_input_value) .map(Message::HeightInputChanged) ] -- cgit From c1021c71758d0c850c7b3fea26075bb83830cb7d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 6 Jun 2023 15:59:56 +0200 Subject: Fix punctuation in `screenshot` example --- examples/screenshot/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'examples/screenshot/src/main.rs') diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index 0d77d316..0f1d8dce 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -183,12 +183,12 @@ impl Application for Example { } let png_button = if !self.png_saving { - button("Save to png.") + button("Save to png") .style(Button::Secondary) .padding([10, 20, 10, 20]) .on_press(Message::Png) } else { - button("Saving..") + button("Saving...") .style(Button::Secondary) .padding([10, 20, 10, 20]) }; -- cgit From 7adfaa88a68e1accfaddf13e82b8bd7a11ee8786 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 6 Jun 2023 16:05:46 +0200 Subject: Avoid `iced_image` import in `screenshot` example --- examples/screenshot/src/main.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'examples/screenshot/src/main.rs') diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index 0f1d8dce..66cbcd8c 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -1,18 +1,17 @@ +use ::image as img; +use ::image::ColorType; use iced::alignment::{Horizontal, Vertical}; use iced::keyboard::KeyCode; use iced::theme::{Button, Container}; use iced::widget::runtime::{CropError, Screenshot}; use iced::widget::{ - button, column as col, container, image as iced_image, row, text, - text_input, + button, column as col, container, image, row, text, text_input, }; use iced::{ event, executor, keyboard, subscription, Alignment, Application, Command, ContentFit, Element, Event, Length, Rectangle, Renderer, Subscription, Theme, }; -use image as img; -use image::ColorType; fn main() -> iced::Result { env_logger::builder().format_timestamp(None).init(); @@ -131,7 +130,7 @@ impl Application for Example { fn view(&self) -> Element<'_, Self::Message, Renderer> { let image: Element = if let Some(screenshot) = &self.screenshot { - iced_image(iced_image::Handle::from_pixels( + image(image::Handle::from_pixels( screenshot.size.width, screenshot.size.height, screenshot.bytes.clone(), -- cgit From 5324928044cba800454b1861eb9999038bc28c2e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 6 Jun 2023 16:14:42 +0200 Subject: Wrap `Screenshot::bytes` in an `Arc` and implement `AsRef<[u8]>` --- examples/screenshot/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/screenshot/src/main.rs') diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index 66cbcd8c..7d53b3b2 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -133,7 +133,7 @@ impl Application for Example { image(image::Handle::from_pixels( screenshot.size.width, screenshot.size.height, - screenshot.bytes.clone(), + screenshot.clone(), )) .content_fit(ContentFit::ScaleDown) .width(Length::Fill) -- cgit From 5b5000e3ae9789cf1d83cecd08f4d0cedc16d788 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 6 Jun 2023 16:18:20 +0200 Subject: Introduce `on_press_maybe` helper for `Button` --- examples/screenshot/src/main.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'examples/screenshot/src/main.rs') diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index 7d53b3b2..be39f829 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -79,12 +79,13 @@ impl Application for Example { } Message::Png => { if let Some(screenshot) = &self.screenshot { + self.png_saving = true; + return Command::perform( save_to_png(screenshot.clone()), Message::PngSaved, ); } - self.png_saving = true; } Message::PngSaved(res) => { self.png_saving = false; @@ -185,7 +186,7 @@ impl Application for Example { button("Save to png") .style(Button::Secondary) .padding([10, 20, 10, 20]) - .on_press(Message::Png) + .on_press_maybe(self.screenshot.is_some().then(|| Message::Png)) } else { button("Saving...") .style(Button::Secondary) -- cgit From 38582873b7d64174f11a7c4d74ed8f654320f7f8 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 6 Jun 2023 16:32:14 +0200 Subject: Rearrange controls of the `screenshot` example --- examples/screenshot/src/main.rs | 84 +++++++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 32 deletions(-) (limited to 'examples/screenshot/src/main.rs') diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index be39f829..7b9ac4e0 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -1,12 +1,10 @@ use ::image as img; use ::image::ColorType; -use iced::alignment::{Horizontal, Vertical}; +use iced::alignment; use iced::keyboard::KeyCode; use iced::theme::{Button, Container}; use iced::widget::runtime::{CropError, Screenshot}; -use iced::widget::{ - button, column as col, container, image, row, text, text_input, -}; +use iced::widget::{button, column, container, image, row, text, text_input}; use iced::{ event, executor, keyboard, subscription, Alignment, Application, Command, ContentFit, Element, Event, Length, Rectangle, Renderer, Subscription, @@ -153,19 +151,27 @@ impl Application for Example { .center_y(); let crop_origin_controls = row![ - text("X:").vertical_alignment(Vertical::Center).width(20), + text("X:") + .vertical_alignment(alignment::Vertical::Center) + .width(20), numeric_input("0", self.x_input_value).map(Message::XInputChanged), - text("Y:").vertical_alignment(Vertical::Center).width(20), + text("Y:") + .vertical_alignment(alignment::Vertical::Center) + .width(20), numeric_input("0", self.y_input_value).map(Message::YInputChanged) ] .spacing(10) .align_items(Alignment::Center); let crop_dimension_controls = row![ - text("W:").vertical_alignment(Vertical::Center).width(20), + text("W:") + .vertical_alignment(alignment::Vertical::Center) + .width(20), numeric_input("0", self.width_input_value) .map(Message::WidthInputChanged), - text("H:").vertical_alignment(Vertical::Center).width(20), + text("H:") + .vertical_alignment(alignment::Vertical::Center) + .width(20), numeric_input("0", self.height_input_value) .map(Message::HeightInputChanged) ] @@ -173,7 +179,7 @@ impl Application for Example { .align_items(Alignment::Center); let mut crop_controls = - col![crop_origin_controls, crop_dimension_controls] + column![crop_origin_controls, crop_dimension_controls] .spacing(10) .align_items(Alignment::Center); @@ -182,30 +188,36 @@ impl Application for Example { .push(text(format!("Crop error! \n{}", crop_error))); } - let png_button = if !self.png_saving { - button("Save to png") - .style(Button::Secondary) - .padding([10, 20, 10, 20]) - .on_press_maybe(self.screenshot.is_some().then(|| Message::Png)) - } else { - button("Saving...") + let mut controls = column![ + column![ + button(centered_text("Screenshot!")) + .padding([10, 20, 10, 20]) + .width(Length::Fill) + .on_press(Message::Screenshot), + if !self.png_saving { + button(centered_text("Save as png")).on_press_maybe( + self.screenshot.is_some().then(|| Message::Png), + ) + } else { + button(centered_text("Saving...")).style(Button::Secondary) + } .style(Button::Secondary) .padding([10, 20, 10, 20]) - }; - - let mut controls = col![ - button("Screenshot!") - .padding([10, 20, 10, 20]) - .on_press(Message::Screenshot), - button("Crop") - .style(Button::Destructive) - .padding([10, 20, 10, 20]) - .on_press(Message::Crop), - crop_controls, - png_button, + .width(Length::Fill) + ] + .spacing(10), + column![ + crop_controls, + button(centered_text("Crop")) + .on_press(Message::Crop) + .style(Button::Destructive) + .padding([10, 20, 10, 20]) + .width(Length::Fill), + ] + .spacing(10) + .align_items(Alignment::Center), ] - .spacing(40) - .align_items(Alignment::Center); + .spacing(40); if let Some(png_result) = &self.saved_png_path { let msg = match png_result { @@ -219,21 +231,22 @@ impl Application for Example { } let side_content = container(controls) - .align_x(Horizontal::Center) + .align_x(alignment::Horizontal::Center) .width(Length::FillPortion(1)) .height(Length::Fill) .center_y() .center_x(); let content = row![side_content, image] + .spacing(10) .width(Length::Fill) .height(Length::Fill) .align_items(Alignment::Center); container(content) - .padding(10) .width(Length::Fill) .height(Length::Fill) + .padding(10) .center_x() .center_y() .into() @@ -297,3 +310,10 @@ fn numeric_input( .width(40) .into() } + +fn centered_text(content: &str) -> Element<'_, Message> { + text(content) + .width(Length::Fill) + .horizontal_alignment(alignment::Horizontal::Center) + .into() +} -- cgit From 78c0189824bbae2ba679c8f8b5ae9552debcb0fd Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 6 Jun 2023 16:36:20 +0200 Subject: Fix width of crop labels in `screenshot` example (again) --- examples/screenshot/src/main.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'examples/screenshot/src/main.rs') diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index 7b9ac4e0..59dfdf14 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -153,11 +153,11 @@ impl Application for Example { let crop_origin_controls = row![ text("X:") .vertical_alignment(alignment::Vertical::Center) - .width(20), + .width(30), numeric_input("0", self.x_input_value).map(Message::XInputChanged), text("Y:") .vertical_alignment(alignment::Vertical::Center) - .width(20), + .width(30), numeric_input("0", self.y_input_value).map(Message::YInputChanged) ] .spacing(10) @@ -166,12 +166,12 @@ impl Application for Example { let crop_dimension_controls = row![ text("W:") .vertical_alignment(alignment::Vertical::Center) - .width(20), + .width(30), numeric_input("0", self.width_input_value) .map(Message::WidthInputChanged), text("H:") .vertical_alignment(alignment::Vertical::Center) - .width(20), + .width(30), numeric_input("0", self.height_input_value) .map(Message::HeightInputChanged) ] -- cgit From 05e238e9ed5f0c6cade87228f8f3044ee26df756 Mon Sep 17 00:00:00 2001 From: Bingus Date: Thu, 8 Jun 2023 10:10:26 -0700 Subject: Adjusted offscreen pass to be a render pass vs compute for compat with web-colors flag. --- examples/screenshot/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/screenshot/src/main.rs') diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index 59dfdf14..ef2be4fe 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -134,7 +134,7 @@ impl Application for Example { screenshot.size.height, screenshot.clone(), )) - .content_fit(ContentFit::ScaleDown) + .content_fit(ContentFit::Contain) .width(Length::Fill) .height(Length::Fill) .into() -- cgit From 5ae726e02c4d6c9889ef7335d9bc80ef1992e34f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 27 Jun 2023 19:41:03 +0200 Subject: Move `Screenshot` inside `window` module --- examples/screenshot/src/main.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'examples/screenshot/src/main.rs') diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index ef2be4fe..83824535 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -1,16 +1,17 @@ -use ::image as img; -use ::image::ColorType; use iced::alignment; use iced::keyboard::KeyCode; use iced::theme::{Button, Container}; -use iced::widget::runtime::{CropError, Screenshot}; use iced::widget::{button, column, container, image, row, text, text_input}; +use iced::window::screenshot::{self, Screenshot}; use iced::{ event, executor, keyboard, subscription, Alignment, Application, Command, ContentFit, Element, Event, Length, Rectangle, Renderer, Subscription, Theme, }; +use ::image as img; +use ::image::ColorType; + fn main() -> iced::Result { env_logger::builder().format_timestamp(None).init(); @@ -21,7 +22,7 @@ struct Example { screenshot: Option, saved_png_path: Option>, png_saving: bool, - crop_error: Option, + crop_error: Option, x_input_value: Option, y_input_value: Option, width_input_value: Option, -- cgit