From fb478a4014021d200a76c93d7f93f57371a843d8 Mon Sep 17 00:00:00 2001 From: Clark Moody Date: Tue, 8 Dec 2020 18:47:47 -0600 Subject: Update PaneGrid example with more complex TitleBar --- examples/pane_grid/src/main.rs | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) (limited to 'examples') diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs index 3c3256cf..c44d24fd 100644 --- a/examples/pane_grid/src/main.rs +++ b/examples/pane_grid/src/main.rs @@ -1,7 +1,7 @@ use iced::{ button, executor, keyboard, pane_grid, scrollable, Align, Application, - Button, Column, Command, Container, Element, HorizontalAlignment, Length, - PaneGrid, Scrollable, Settings, Subscription, Text, + Button, Color, Column, Command, Container, Element, HorizontalAlignment, + Length, PaneGrid, Row, Scrollable, Settings, Subscription, Text, }; use iced_native::{event, subscription, Event}; @@ -141,10 +141,21 @@ impl Application for Example { let pane_grid = PaneGrid::new(&mut self.panes, |pane, content| { let is_focused = focus == Some(pane); - let title_bar = - pane_grid::TitleBar::new(format!("Pane {}", content.id)) - .padding(10) - .style(style::TitleBar { is_focused }); + let title = Row::with_children(vec![ + Text::new("Pane").into(), + Text::new(content.id.to_string()) + .color(if is_focused { + PANE_ID_COLOR_FOCUSED + } else { + PANE_ID_COLOR_UNFOCUSED + }) + .into(), + ]) + .spacing(5); + + let title_bar = pane_grid::TitleBar::new(title) + .padding(10) + .style(style::TitleBar { is_focused }); pane_grid::Content::new(content.view(pane, total_panes)) .title_bar(title_bar) @@ -165,6 +176,17 @@ impl Application for Example { } } +const PANE_ID_COLOR_UNFOCUSED: Color = Color::from_rgb( + 0xFF as f32 / 255.0, + 0xC7 as f32 / 255.0, + 0xC7 as f32 / 255.0, +); +const PANE_ID_COLOR_FOCUSED: Color = Color::from_rgb( + 0xFF as f32 / 255.0, + 0x47 as f32 / 255.0, + 0x47 as f32 / 255.0, +); + fn handle_hotkey(key_code: keyboard::KeyCode) -> Option { use keyboard::KeyCode; use pane_grid::{Axis, Direction}; -- cgit From 07b570036aad3f42578876e1a299a0577e6761ae Mon Sep 17 00:00:00 2001 From: Nils Martel Date: Tue, 15 Dec 2020 11:25:15 +0100 Subject: Mention birth year in `README`s --- examples/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/README.md b/examples/README.md index 32ccf724..10c28cf5 100644 --- a/examples/README.md +++ b/examples/README.md @@ -118,7 +118,7 @@ cargo run --package [Ghostscript Tiger]: https://commons.wikimedia.org/wiki/File:Ghostscript_Tiger.svg ## [Coffee] -Since [Iced was born in May], it has been powering the user interfaces in +Since [Iced was born in May 2019], it has been powering the user interfaces in [Coffee], an experimental 2D game engine. @@ -128,6 +128,6 @@ Since [Iced was born in May], it has been powering the user interfaces in -[Iced was born in May]: https://github.com/hecrj/coffee/pull/35 +[Iced was born in May 2019]: https://github.com/hecrj/coffee/pull/35 [`ui` module]: https://docs.rs/coffee/0.3.2/coffee/ui/index.html [Coffee]: https://github.com/hecrj/coffee -- cgit From 0cdf8d56ee515fb56b08ee149683a2216a535950 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 18 Dec 2020 10:44:06 +0100 Subject: Use `image::Viewer` in `pokedex` example --- examples/pokedex/src/main.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/pokedex/src/main.rs b/examples/pokedex/src/main.rs index 187e5dee..f432f0fc 100644 --- a/examples/pokedex/src/main.rs +++ b/examples/pokedex/src/main.rs @@ -1,6 +1,6 @@ use iced::{ button, futures, image, Align, Application, Button, Column, Command, - Container, Element, Image, Length, Row, Settings, Text, + Container, Element, Length, Row, Settings, Text, }; pub fn main() -> iced::Result { @@ -112,16 +112,20 @@ struct Pokemon { name: String, description: String, image: image::Handle, + image_viewer: image::viewer::State, } impl Pokemon { const TOTAL: u16 = 807; - fn view(&self) -> Element { + fn view(&mut self) -> Element { Row::new() .spacing(20) .align_items(Align::Center) - .push(Image::new(self.image.clone())) + .push(image::Viewer::new( + &mut self.image_viewer, + self.image.clone(), + )) .push( Column::new() .spacing(20) @@ -200,6 +204,7 @@ impl Pokemon { .map(|c| if c.is_control() { ' ' } else { c }) .collect(), image, + image_viewer: image::viewer::State::new(), }) } -- cgit From 50452e62b4df458d676fc95361b04ef2dd83aebf Mon Sep 17 00:00:00 2001 From: Yusuf Bera Ertan Date: Mon, 4 Jan 2021 22:58:39 +0300 Subject: Update `tokio` to `1.0` --- examples/game_of_life/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/game_of_life/Cargo.toml b/examples/game_of_life/Cargo.toml index 9c4172c4..ffd2f19e 100644 --- a/examples/game_of_life/Cargo.toml +++ b/examples/game_of_life/Cargo.toml @@ -7,6 +7,6 @@ publish = false [dependencies] iced = { path = "../..", features = ["canvas", "tokio", "debug"] } -tokio = { version = "0.3", features = ["sync"] } +tokio = { version = "1.0", features = ["sync"] } itertools = "0.9" rustc-hash = "1.1" -- cgit From 31522e30aa1436f634a2675ba69d12495ff672bc Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 4 Jan 2021 23:38:07 +0100 Subject: Remove unnecessary `SaveError` variant in `todos` example --- examples/todos/src/main.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index ccee2703..b47df5df 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -489,7 +489,6 @@ enum LoadError { #[derive(Debug, Clone)] enum SaveError { - DirectoryError, FileError, WriteError, FormatError, @@ -538,7 +537,7 @@ impl SavedState { if let Some(dir) = path.parent() { async_std::fs::create_dir_all(dir) .await - .map_err(|_| SaveError::DirectoryError)?; + .map_err(|_| SaveError::FileError)?; } { -- cgit From fd2c96c8e36eb37ea4a53aafe0986b569a4e3753 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 15 Jan 2021 18:52:12 +0100 Subject: Fix `time::Every` implementation for `smol` runtime --- examples/stopwatch/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/stopwatch/Cargo.toml b/examples/stopwatch/Cargo.toml index 075aa111..9f935951 100644 --- a/examples/stopwatch/Cargo.toml +++ b/examples/stopwatch/Cargo.toml @@ -6,4 +6,4 @@ edition = "2018" publish = false [dependencies] -iced = { path = "../..", features = ["tokio"] } +iced = { path = "../..", features = ["smol"] } -- cgit From 09a5348740803329c98f6422700569595c2be8c5 Mon Sep 17 00:00:00 2001 From: Poly Date: Wed, 3 Feb 2021 22:38:45 +0100 Subject: [wgpu 0.7] Update integration example --- examples/integration/src/main.rs | 8 ++++---- examples/integration/src/scene.rs | 40 +++++++++++++++++++-------------------- 2 files changed, 24 insertions(+), 24 deletions(-) (limited to 'examples') diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs index 9b52f3a5..ec01c4e6 100644 --- a/examples/integration/src/main.rs +++ b/examples/integration/src/main.rs @@ -36,7 +36,7 @@ pub fn main() { let (mut device, queue) = futures::executor::block_on(async { let adapter = instance .request_adapter(&wgpu::RequestAdapterOptions { - power_preference: wgpu::PowerPreference::Default, + power_preference: wgpu::PowerPreference::HighPerformance, compatible_surface: Some(&surface), }) .await @@ -45,9 +45,9 @@ pub fn main() { adapter .request_device( &wgpu::DeviceDescriptor { + label: None, features: wgpu::Features::empty(), limits: wgpu::Limits::default(), - shader_validation: false, }, None, ) @@ -63,7 +63,7 @@ pub fn main() { device.create_swap_chain( &surface, &wgpu::SwapChainDescriptor { - usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT, + usage: wgpu::TextureUsage::RENDER_ATTACHMENT, format: format, width: size.width, height: size.height, @@ -157,7 +157,7 @@ pub fn main() { swap_chain = device.create_swap_chain( &surface, &wgpu::SwapChainDescriptor { - usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT, + usage: wgpu::TextureUsage::RENDER_ATTACHMENT, format: format, width: size.width, height: size.height, diff --git a/examples/integration/src/scene.rs b/examples/integration/src/scene.rs index 03a338c6..36c0a41d 100644 --- a/examples/integration/src/scene.rs +++ b/examples/integration/src/scene.rs @@ -19,6 +19,7 @@ impl Scene { background_color: Color, ) -> wgpu::RenderPass<'a> { encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: None, color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { attachment: target, resolve_target: None, @@ -48,10 +49,10 @@ impl Scene { fn build_pipeline(device: &wgpu::Device) -> wgpu::RenderPipeline { let vs_module = - device.create_shader_module(wgpu::include_spirv!("shader/vert.spv")); + device.create_shader_module(&wgpu::include_spirv!("shader/vert.spv")); let fs_module = - device.create_shader_module(wgpu::include_spirv!("shader/frag.spv")); + device.create_shader_module(&wgpu::include_spirv!("shader/frag.spv")); let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { @@ -64,34 +65,33 @@ fn build_pipeline(device: &wgpu::Device) -> wgpu::RenderPipeline { device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { label: None, layout: Some(&pipeline_layout), - vertex_stage: wgpu::ProgrammableStageDescriptor { + vertex: wgpu::VertexState { module: &vs_module, entry_point: "main", + buffers: &[], }, - fragment_stage: Some(wgpu::ProgrammableStageDescriptor { + fragment: Some(wgpu::FragmentState { module: &fs_module, entry_point: "main", + targets: &[wgpu::ColorTargetState { + format: wgpu::TextureFormat::Bgra8UnormSrgb, + color_blend: wgpu::BlendState::REPLACE, + alpha_blend: wgpu::BlendState::REPLACE, + write_mask: wgpu::ColorWrite::ALL, + }], }), - rasterization_state: Some(wgpu::RasterizationStateDescriptor { + primitive: wgpu::PrimitiveState { + topology: wgpu::PrimitiveTopology::TriangleList, front_face: wgpu::FrontFace::Ccw, cull_mode: wgpu::CullMode::None, ..Default::default() - }), - primitive_topology: wgpu::PrimitiveTopology::TriangleList, - color_states: &[wgpu::ColorStateDescriptor { - format: wgpu::TextureFormat::Bgra8UnormSrgb, - color_blend: wgpu::BlendDescriptor::REPLACE, - alpha_blend: wgpu::BlendDescriptor::REPLACE, - write_mask: wgpu::ColorWrite::ALL, - }], - depth_stencil_state: None, - vertex_state: wgpu::VertexStateDescriptor { - index_format: wgpu::IndexFormat::Uint16, - vertex_buffers: &[], }, - sample_count: 1, - sample_mask: !0, - alpha_to_coverage_enabled: false, + depth_stencil: None, + multisample: wgpu::MultisampleState { + count: 1, + mask: !0, + alpha_to_coverage_enabled: false, + }, }); pipeline -- cgit From 9f5c2eb0c43daa61b19769322cf3692b29e0ac0f Mon Sep 17 00:00:00 2001 From: Folyd Date: Sat, 13 Feb 2021 05:00:52 +0800 Subject: Improve download_progress example (#283) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add advanced download example * Rename to task fields and variables * Cargo fmt advanced_download/src/download.rs * Add progress bar for advanced download example * Merge two download examples to single one * Apply great review suggestions * Change to url::Url instead of plain String * Simplify `download_progress` example * Update `README` of `download_progress` example Co-authored-by: Héctor Ramón Jiménez --- examples/download_progress/Cargo.toml | 6 +- examples/download_progress/README.md | 2 +- examples/download_progress/src/download.rs | 46 ++++--- examples/download_progress/src/main.rs | 195 ++++++++++++++++++++--------- 4 files changed, 168 insertions(+), 81 deletions(-) (limited to 'examples') diff --git a/examples/download_progress/Cargo.toml b/examples/download_progress/Cargo.toml index 4b05e7dc..d3c578b1 100644 --- a/examples/download_progress/Cargo.toml +++ b/examples/download_progress/Cargo.toml @@ -1,12 +1,12 @@ [package] name = "download_progress" version = "0.1.0" -authors = ["Songtronix "] +authors = ["Songtronix ", "Folyd "] edition = "2018" publish = false [dependencies] -iced = { path = "../..", features = ["tokio_old"] } +iced = { path = "../..", features = ["tokio"] } iced_native = { path = "../../native" } iced_futures = { path = "../../futures" } -reqwest = "0.10" +reqwest = "0.11" diff --git a/examples/download_progress/README.md b/examples/download_progress/README.md index c606c5f9..7999ce94 100644 --- a/examples/download_progress/README.md +++ b/examples/download_progress/README.md @@ -1,6 +1,6 @@ ## Download progress -A basic application that asynchronously downloads a dummy file of 100 MB and tracks the download progress. +A basic application that asynchronously downloads multiple dummy files of 100 MB and tracks the download progress. The example implements a custom `Subscription` in the __[`download`](src/download.rs)__ module. This subscription downloads and produces messages that can be used to keep track of its progress. diff --git a/examples/download_progress/src/download.rs b/examples/download_progress/src/download.rs index f46a01f7..08805f13 100644 --- a/examples/download_progress/src/download.rs +++ b/examples/download_progress/src/download.rs @@ -1,37 +1,46 @@ use iced_futures::futures; +use std::hash::{Hash, Hasher}; // Just a little utility function -pub fn file(url: T) -> iced::Subscription { +pub fn file( + id: I, + url: T, +) -> iced::Subscription<(I, Progress)> { iced::Subscription::from_recipe(Download { + id, url: url.to_string(), }) } -pub struct Download { +pub struct Download { + id: I, url: String, } // Make sure iced can use our download stream -impl iced_native::subscription::Recipe for Download +impl iced_native::subscription::Recipe for Download where - H: std::hash::Hasher, + T: 'static + Hash + Copy + Send, + H: Hasher, { - type Output = Progress; + type Output = (T, Progress); fn hash(&self, state: &mut H) { - use std::hash::Hash; + struct Marker; + std::any::TypeId::of::().hash(state); - std::any::TypeId::of::().hash(state); - self.url.hash(state); + self.id.hash(state); } fn stream( self: Box, _input: futures::stream::BoxStream<'static, I>, ) -> futures::stream::BoxStream<'static, Self::Output> { + let id = self.id; + Box::pin(futures::stream::unfold( State::Ready(self.url), - |state| async move { + move |state| async move { match state { State::Ready(url) => { let response = reqwest::get(&url).await; @@ -40,7 +49,7 @@ where Ok(response) => { if let Some(total) = response.content_length() { Some(( - Progress::Started, + (id, Progress::Started), State::Downloading { response, total, @@ -48,11 +57,14 @@ where }, )) } else { - Some((Progress::Errored, State::Finished)) + Some(( + (id, Progress::Errored), + State::Finished, + )) } } Err(_) => { - Some((Progress::Errored, State::Finished)) + Some(((id, Progress::Errored), State::Finished)) } } } @@ -68,7 +80,7 @@ where (downloaded as f32 / total as f32) * 100.0; Some(( - Progress::Advanced(percentage), + (id, Progress::Advanced(percentage)), State::Downloading { response, total, @@ -76,8 +88,12 @@ where }, )) } - Ok(None) => Some((Progress::Finished, State::Finished)), - Err(_) => Some((Progress::Errored, State::Finished)), + Ok(None) => { + Some(((id, Progress::Finished), State::Finished)) + } + Err(_) => { + Some(((id, Progress::Errored), State::Finished)) + } }, State::Finished => { // We do not let the stream die, as it would start a diff --git a/examples/download_progress/src/main.rs b/examples/download_progress/src/main.rs index 77b01354..cd024926 100644 --- a/examples/download_progress/src/main.rs +++ b/examples/download_progress/src/main.rs @@ -10,17 +10,17 @@ pub fn main() -> iced::Result { } #[derive(Debug)] -enum Example { - Idle { button: button::State }, - Downloading { progress: f32 }, - Finished { button: button::State }, - Errored { button: button::State }, +struct Example { + downloads: Vec, + last_id: usize, + add: button::State, } #[derive(Debug, Clone)] pub enum Message { - Download, - DownloadProgressed(download::Progress), + Add, + Download(usize), + DownloadProgressed((usize, download::Progress)), } impl Application for Example { @@ -30,8 +30,10 @@ impl Application for Example { fn new(_flags: ()) -> (Example, Command) { ( - Example::Idle { - button: button::State::new(), + Example { + downloads: vec![Download::new(0)], + last_id: 0, + add: button::State::new(), }, Command::none(), ) @@ -43,102 +45,171 @@ impl Application for Example { fn update(&mut self, message: Message) -> Command { match message { - Message::Download => match self { - Example::Idle { .. } - | Example::Finished { .. } - | Example::Errored { .. } => { - *self = Example::Downloading { progress: 0.0 }; + Message::Add => { + self.last_id = self.last_id + 1; + + self.downloads.push(Download::new(self.last_id)); + } + Message::Download(index) => { + if let Some(download) = self.downloads.get_mut(index) { + download.start(); } - _ => {} - }, - Message::DownloadProgressed(message) => match self { - Example::Downloading { progress } => match message { - download::Progress::Started => { - *progress = 0.0; - } - download::Progress::Advanced(percentage) => { - *progress = percentage; - } - download::Progress::Finished => { - *self = Example::Finished { - button: button::State::new(), - } - } - download::Progress::Errored => { - *self = Example::Errored { - button: button::State::new(), - }; - } - }, - _ => {} - }, + } + Message::DownloadProgressed((id, progress)) => { + if let Some(download) = + self.downloads.iter_mut().find(|download| download.id == id) + { + download.progress(progress); + } + } }; Command::none() } fn subscription(&self) -> Subscription { - match self { - Example::Downloading { .. } => { - download::file("https://speed.hetzner.de/100MB.bin") + Subscription::batch(self.downloads.iter().map(Download::subscription)) + } + + fn view(&mut self) -> Element { + let downloads = self + .downloads + .iter_mut() + .fold(Column::new().spacing(20), |column, download| { + column.push(download.view()) + }) + .push( + Button::new(&mut self.add, Text::new("Add another download")) + .on_press(Message::Add) + .padding(10), + ) + .align_items(Align::End); + + Container::new(downloads) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .padding(20) + .into() + } +} + +#[derive(Debug)] +struct Download { + id: usize, + state: State, +} + +#[derive(Debug)] +enum State { + Idle { button: button::State }, + Downloading { progress: f32 }, + Finished { button: button::State }, + Errored { button: button::State }, +} + +impl Download { + pub fn new(id: usize) -> Self { + Download { + id, + state: State::Idle { + button: button::State::new(), + }, + } + } + + pub fn start(&mut self) { + match self.state { + State::Idle { .. } + | State::Finished { .. } + | State::Errored { .. } => { + self.state = State::Downloading { progress: 0.0 }; + } + _ => {} + } + } + + pub fn progress(&mut self, new_progress: download::Progress) { + match &mut self.state { + State::Downloading { progress } => match new_progress { + download::Progress::Started => { + *progress = 0.0; + } + download::Progress::Advanced(percentage) => { + *progress = percentage; + } + download::Progress::Finished => { + self.state = State::Finished { + button: button::State::new(), + } + } + download::Progress::Errored => { + self.state = State::Errored { + button: button::State::new(), + }; + } + }, + _ => {} + } + } + + pub fn subscription(&self) -> Subscription { + match self.state { + State::Downloading { .. } => { + download::file(self.id, "https://speed.hetzner.de/100MB.bin?") .map(Message::DownloadProgressed) } _ => Subscription::none(), } } - fn view(&mut self) -> Element { - let current_progress = match self { - Example::Idle { .. } => 0.0, - Example::Downloading { progress } => *progress, - Example::Finished { .. } => 100.0, - Example::Errored { .. } => 0.0, + pub fn view(&mut self) -> Element { + let current_progress = match &self.state { + State::Idle { .. } => 0.0, + State::Downloading { progress } => *progress, + State::Finished { .. } => 100.0, + State::Errored { .. } => 0.0, }; let progress_bar = ProgressBar::new(0.0..=100.0, current_progress); - let control: Element<_> = match self { - Example::Idle { button } => { + let control: Element<_> = match &mut self.state { + State::Idle { button } => { Button::new(button, Text::new("Start the download!")) - .on_press(Message::Download) + .on_press(Message::Download(self.id)) .into() } - Example::Finished { button } => Column::new() + State::Finished { button } => Column::new() .spacing(10) .align_items(Align::Center) .push(Text::new("Download finished!")) .push( Button::new(button, Text::new("Start again")) - .on_press(Message::Download), + .on_press(Message::Download(self.id)), ) .into(), - Example::Downloading { .. } => { + State::Downloading { .. } => { Text::new(format!("Downloading... {:.2}%", current_progress)) .into() } - Example::Errored { button } => Column::new() + State::Errored { button } => Column::new() .spacing(10) .align_items(Align::Center) .push(Text::new("Something went wrong :(")) .push( Button::new(button, Text::new("Try again")) - .on_press(Message::Download), + .on_press(Message::Download(self.id)), ) .into(), }; - let content = Column::new() + Column::new() .spacing(10) .padding(10) .align_items(Align::Center) .push(progress_bar) - .push(control); - - Container::new(content) - .width(Length::Fill) - .height(Length::Fill) - .center_x() - .center_y() + .push(control) .into() } } -- cgit From a19f89d3a6af2804f2ac4e30f6d639b56a9bebfd Mon Sep 17 00:00:00 2001 From: Yusuf Bera Ertan Date: Tue, 28 Jul 2020 18:07:46 +0300 Subject: feat(native): add Tooltip widget --- examples/tooltip/Cargo.toml | 9 ++++ examples/tooltip/README.md | 14 +++++ examples/tooltip/src/main.rs | 123 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 examples/tooltip/Cargo.toml create mode 100644 examples/tooltip/README.md create mode 100644 examples/tooltip/src/main.rs (limited to 'examples') diff --git a/examples/tooltip/Cargo.toml b/examples/tooltip/Cargo.toml new file mode 100644 index 00000000..1171de00 --- /dev/null +++ b/examples/tooltip/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "tooltip" +version = "0.1.0" +authors = ["Yusuf Bera Ertan "] +edition = "2018" +publish = false + +[dependencies] +iced = { path = "../..", features = ["debug"] } diff --git a/examples/tooltip/README.md b/examples/tooltip/README.md new file mode 100644 index 00000000..4ccf6578 --- /dev/null +++ b/examples/tooltip/README.md @@ -0,0 +1,14 @@ +## Tooltip + +A tooltip. + +It displays and positions a widget on another based on cursor position. + +The __[`main`]__ file contains all the code of the example. + +You can run it with `cargo run`: +``` +cargo run --package tooltip +``` + +[`main`]: src/main.rs diff --git a/examples/tooltip/src/main.rs b/examples/tooltip/src/main.rs new file mode 100644 index 00000000..6e2c4dd4 --- /dev/null +++ b/examples/tooltip/src/main.rs @@ -0,0 +1,123 @@ +use iced::{ + button, tooltip::TooltipPosition, Button, Column, Container, Element, + Length, Row, Sandbox, Settings, Text, Tooltip, +}; + +pub fn main() { + Example::run(Settings::default()).unwrap() +} + +#[derive(Default)] +struct Example { + tooltip_top_button_state: button::State, + tooltip_bottom_button_state: button::State, + tooltip_right_button_state: button::State, + tooltip_left_button_state: button::State, + tooltip_cursor_button_state: button::State, +} + +#[derive(Debug, Clone, Copy)] +struct Message; + +impl Sandbox for Example { + type Message = Message; + + fn new() -> Self { + Self::default() + } + + fn title(&self) -> String { + String::from("Tooltip - Iced") + } + + fn update(&mut self, _message: Message) {} + + fn view(&mut self) -> Element { + let tooltip_top = tooltip_builder( + "Tooltip at top", + &mut self.tooltip_top_button_state, + TooltipPosition::Top, + ); + let tooltip_bottom = tooltip_builder( + "Tooltip at bottom", + &mut self.tooltip_bottom_button_state, + TooltipPosition::Bottom, + ); + let tooltip_right = tooltip_builder( + "Tooltip at right", + &mut self.tooltip_right_button_state, + TooltipPosition::Right, + ); + let tooltip_left = tooltip_builder( + "Tooltip at left", + &mut self.tooltip_left_button_state, + TooltipPosition::Left, + ); + + let fixed_tooltips = Row::with_children(vec![ + tooltip_top.into(), + tooltip_bottom.into(), + tooltip_left.into(), + tooltip_right.into(), + ]) + .width(Length::Fill) + .height(Length::Fill) + .align_items(iced::Align::Center) + .spacing(120); + + let cursor_tooltip_area = Tooltip::new( + Button::new( + &mut self.tooltip_cursor_button_state, + Container::new(Text::new("Tooltip follows cursor").size(40)) + .center_y() + .center_x() + .width(Length::Fill) + .height(Length::Fill), + ) + .on_press(Message) + .width(Length::Fill) + .height(Length::Fill), + tooltip(), + TooltipPosition::FollowCursor, + ); + + let content = Column::with_children(vec![ + Container::new(fixed_tooltips) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .into(), + cursor_tooltip_area.into(), + ]) + .width(Length::Fill) + .height(Length::Fill); + + Container::new(content) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .into() + } +} + +fn tooltip_builder<'a>( + label: &str, + button_state: &'a mut button::State, + position: TooltipPosition, +) -> Container<'a, Message> { + Container::new(Tooltip::new( + Button::new(button_state, Text::new(label).size(40)).on_press(Message), + tooltip(), + position, + )) + .center_x() + .center_y() + .width(Length::Fill) + .height(Length::Fill) +} + +fn tooltip() -> Text { + Text::new("Tooltip").size(20) +} -- cgit From 81c75c15249b608dd8a6d47e25f96feb10ca68da Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 23 Feb 2021 03:09:16 +0100 Subject: Change `Tooltip` to support `Text` only for now --- examples/tooltip/src/main.rs | 114 ++++++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 60 deletions(-) (limited to 'examples') diff --git a/examples/tooltip/src/main.rs b/examples/tooltip/src/main.rs index 6e2c4dd4..a49caa70 100644 --- a/examples/tooltip/src/main.rs +++ b/examples/tooltip/src/main.rs @@ -1,6 +1,7 @@ +use iced::tooltip::{self, Tooltip}; use iced::{ - button, tooltip::TooltipPosition, Button, Column, Container, Element, - Length, Row, Sandbox, Settings, Text, Tooltip, + button, Button, Column, Container, Element, HorizontalAlignment, Length, + Row, Sandbox, Settings, Text, VerticalAlignment, }; pub fn main() { @@ -9,11 +10,11 @@ pub fn main() { #[derive(Default)] struct Example { - tooltip_top_button_state: button::State, - tooltip_bottom_button_state: button::State, - tooltip_right_button_state: button::State, - tooltip_left_button_state: button::State, - tooltip_cursor_button_state: button::State, + top: button::State, + bottom: button::State, + right: button::State, + left: button::State, + follow_cursor: button::State, } #[derive(Debug, Clone, Copy)] @@ -33,52 +34,39 @@ impl Sandbox for Example { fn update(&mut self, _message: Message) {} fn view(&mut self) -> Element { - let tooltip_top = tooltip_builder( - "Tooltip at top", - &mut self.tooltip_top_button_state, - TooltipPosition::Top, - ); - let tooltip_bottom = tooltip_builder( + let top = + tooltip("Tooltip at top", &mut self.top, tooltip::Position::Top); + + let bottom = tooltip( "Tooltip at bottom", - &mut self.tooltip_bottom_button_state, - TooltipPosition::Bottom, + &mut self.bottom, + tooltip::Position::Bottom, ); - let tooltip_right = tooltip_builder( + + let left = + tooltip("Tooltip at left", &mut self.left, tooltip::Position::Left); + + let right = tooltip( "Tooltip at right", - &mut self.tooltip_right_button_state, - TooltipPosition::Right, - ); - let tooltip_left = tooltip_builder( - "Tooltip at left", - &mut self.tooltip_left_button_state, - TooltipPosition::Left, + &mut self.right, + tooltip::Position::Right, ); let fixed_tooltips = Row::with_children(vec![ - tooltip_top.into(), - tooltip_bottom.into(), - tooltip_left.into(), - tooltip_right.into(), + top.into(), + bottom.into(), + left.into(), + right.into(), ]) .width(Length::Fill) .height(Length::Fill) .align_items(iced::Align::Center) - .spacing(120); - - let cursor_tooltip_area = Tooltip::new( - Button::new( - &mut self.tooltip_cursor_button_state, - Container::new(Text::new("Tooltip follows cursor").size(40)) - .center_y() - .center_x() - .width(Length::Fill) - .height(Length::Fill), - ) - .on_press(Message) - .width(Length::Fill) - .height(Length::Fill), - tooltip(), - TooltipPosition::FollowCursor, + .spacing(50); + + let follow_cursor = tooltip( + "Tooltip follows cursor", + &mut self.follow_cursor, + tooltip::Position::FollowCursor, ); let content = Column::with_children(vec![ @@ -88,36 +76,42 @@ impl Sandbox for Example { .center_x() .center_y() .into(), - cursor_tooltip_area.into(), + follow_cursor.into(), ]) .width(Length::Fill) - .height(Length::Fill); + .height(Length::Fill) + .spacing(50); Container::new(content) .width(Length::Fill) .height(Length::Fill) .center_x() .center_y() + .padding(50) .into() } } -fn tooltip_builder<'a>( +fn tooltip<'a>( label: &str, button_state: &'a mut button::State, - position: TooltipPosition, -) -> Container<'a, Message> { - Container::new(Tooltip::new( - Button::new(button_state, Text::new(label).size(40)).on_press(Message), - tooltip(), + position: tooltip::Position, +) -> Element<'a, Message> { + Tooltip::new( + Button::new( + button_state, + Text::new(label) + .size(40) + .width(Length::Fill) + .height(Length::Fill) + .horizontal_alignment(HorizontalAlignment::Center) + .vertical_alignment(VerticalAlignment::Center), + ) + .on_press(Message) + .width(Length::Fill) + .height(Length::Fill), + Text::new("Tooltip"), position, - )) - .center_x() - .center_y() - .width(Length::Fill) - .height(Length::Fill) -} - -fn tooltip() -> Text { - Text::new("Tooltip").size(20) + ) + .into() } -- cgit From 2f766b73413fe60cd881e139fa0e84a0f0134d91 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 23 Feb 2021 03:16:37 +0100 Subject: Introduce `Tooltip::gap` to control spacing --- examples/tooltip/src/main.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'examples') diff --git a/examples/tooltip/src/main.rs b/examples/tooltip/src/main.rs index a49caa70..3677dc75 100644 --- a/examples/tooltip/src/main.rs +++ b/examples/tooltip/src/main.rs @@ -113,5 +113,6 @@ fn tooltip<'a>( Text::new("Tooltip"), position, ) + .gap(10) .into() } -- cgit From 4e923290ccb38dc9cee05592554f98f1f0f12966 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 23 Feb 2021 04:00:35 +0100 Subject: Add `style` and `padding` to `Tooltip` --- examples/tooltip/src/main.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'examples') diff --git a/examples/tooltip/src/main.rs b/examples/tooltip/src/main.rs index 3677dc75..b10b8b50 100644 --- a/examples/tooltip/src/main.rs +++ b/examples/tooltip/src/main.rs @@ -114,5 +114,25 @@ fn tooltip<'a>( position, ) .gap(10) + .padding(20) + .style(style::Tooltip) .into() } + +mod style { + use iced::container; + use iced::Color; + + pub struct Tooltip; + + impl container::StyleSheet for Tooltip { + fn style(&self) -> container::Style { + container::Style { + text_color: Some(Color::from_rgb8(0xEE, 0xEE, 0xEE)), + background: Some(Color::from_rgb(0.11, 0.42, 0.87).into()), + border_radius: 12.0, + ..container::Style::default() + } + } + } +} -- cgit From 2736e4ca35f17a92768f0be682acf6da3b574cb6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 24 Feb 2021 00:59:29 +0100 Subject: Hide `Text` as an implementation detail of `Tooltip` --- examples/tooltip/src/main.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/tooltip/src/main.rs b/examples/tooltip/src/main.rs index b10b8b50..d6c8b8e1 100644 --- a/examples/tooltip/src/main.rs +++ b/examples/tooltip/src/main.rs @@ -110,11 +110,11 @@ fn tooltip<'a>( .on_press(Message) .width(Length::Fill) .height(Length::Fill), - Text::new("Tooltip"), + "Tooltip", position, ) - .gap(10) - .padding(20) + .gap(5) + .padding(10) .style(style::Tooltip) .into() } -- cgit From 5f27ed47207645bbb9fe2837675531584d88d394 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 3 Mar 2021 01:23:18 +0100 Subject: Update: rand --- examples/solar_system/Cargo.toml | 2 +- examples/solar_system/src/main.rs | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) (limited to 'examples') diff --git a/examples/solar_system/Cargo.toml b/examples/solar_system/Cargo.toml index 44ced729..327fe0aa 100644 --- a/examples/solar_system/Cargo.toml +++ b/examples/solar_system/Cargo.toml @@ -7,4 +7,4 @@ publish = false [dependencies] iced = { path = "../..", features = ["canvas", "tokio", "debug"] } -rand = "0.7" +rand = "0.8.3" diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 6a2de736..c8f74978 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -117,15 +117,13 @@ impl State { ( Point::new( rng.gen_range( - -(width as f32) / 2.0, - width as f32 / 2.0, + (-(width as f32) / 2.0)..(width as f32 / 2.0), ), rng.gen_range( - -(height as f32) / 2.0, - height as f32 / 2.0, + (-(height as f32) / 2.0)..(height as f32 / 2.0), ), ), - rng.gen_range(0.5, 1.0), + rng.gen_range(0.5..1.0), ) }) .collect() -- cgit From 21971e0037c2ddcb96fd48ea96332445de4137bb Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 10 Mar 2021 01:59:02 +0100 Subject: Make `Clipboard` argument in `Widget` trait mutable --- examples/integration/src/main.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs index ec01c4e6..b57c73d8 100644 --- a/examples/integration/src/main.rs +++ b/examples/integration/src/main.rs @@ -5,7 +5,7 @@ use controls::Controls; use scene::Scene; use iced_wgpu::{wgpu, Backend, Renderer, Settings, Viewport}; -use iced_winit::{conversion, futures, program, winit, Debug, Size}; +use iced_winit::{conversion, futures, program, winit, Clipboard, Debug, Size}; use futures::task::SpawnExt; use winit::{ @@ -28,6 +28,7 @@ pub fn main() { ); let mut cursor_position = PhysicalPosition::new(-1.0, -1.0); let mut modifiers = ModifiersState::default(); + let mut clipboard = Clipboard::connect(&window); // Initialize wgpu let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY); @@ -141,8 +142,8 @@ pub fn main() { cursor_position, viewport.scale_factor(), ), - None, &mut renderer, + &mut clipboard, &mut debug, ); -- cgit From ae517b9fa033ba75df5fc6ce766698fab22504fa Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 11 Mar 2021 03:38:20 +0100 Subject: Add `clipboard` argument to `Application::update` --- examples/clock/src/main.rs | 10 +++++++--- examples/download_progress/src/main.rs | 10 +++++++--- examples/events/src/main.rs | 10 +++++++--- examples/game_of_life/src/main.rs | 10 +++++++--- examples/integration/src/controls.rs | 11 ++++++++--- examples/pane_grid/src/main.rs | 11 ++++++++--- examples/pokedex/src/main.rs | 10 +++++++--- examples/solar_system/src/main.rs | 10 +++++++--- examples/stopwatch/src/main.rs | 10 +++++++--- examples/todos/src/main.rs | 10 +++++++--- 10 files changed, 72 insertions(+), 30 deletions(-) (limited to 'examples') diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index b317ac00..9bcc827b 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -1,7 +1,7 @@ use iced::{ canvas::{self, Cache, Canvas, Cursor, Geometry, LineCap, Path, Stroke}, - executor, time, Application, Color, Command, Container, Element, Length, - Point, Rectangle, Settings, Subscription, Vector, + executor, time, Application, Clipboard, Color, Command, Container, Element, + Length, Point, Rectangle, Settings, Subscription, Vector, }; pub fn main() -> iced::Result { @@ -40,7 +40,11 @@ impl Application for Clock { String::from("Clock - Iced") } - fn update(&mut self, message: Message) -> Command { + fn update( + &mut self, + message: Message, + _clipboard: &mut Clipboard, + ) -> Command { match message { Message::Tick(local_time) => { let now = local_time; diff --git a/examples/download_progress/src/main.rs b/examples/download_progress/src/main.rs index cd024926..6f844e66 100644 --- a/examples/download_progress/src/main.rs +++ b/examples/download_progress/src/main.rs @@ -1,6 +1,6 @@ use iced::{ - button, executor, Align, Application, Button, Column, Command, Container, - Element, Length, ProgressBar, Settings, Subscription, Text, + button, executor, Align, Application, Button, Clipboard, Column, Command, + Container, Element, Length, ProgressBar, Settings, Subscription, Text, }; mod download; @@ -43,7 +43,11 @@ impl Application for Example { String::from("Download progress - Iced") } - fn update(&mut self, message: Message) -> Command { + fn update( + &mut self, + message: Message, + _clipboard: &mut Clipboard, + ) -> Command { match message { Message::Add => { self.last_id = self.last_id + 1; diff --git a/examples/events/src/main.rs b/examples/events/src/main.rs index 6eba6aad..18e6364b 100644 --- a/examples/events/src/main.rs +++ b/examples/events/src/main.rs @@ -1,6 +1,6 @@ use iced::{ - executor, Align, Application, Checkbox, Column, Command, Container, - Element, Length, Settings, Subscription, Text, + executor, Align, Application, Checkbox, Clipboard, Column, Command, + Container, Element, Length, Settings, Subscription, Text, }; pub fn main() -> iced::Result { @@ -32,7 +32,11 @@ impl Application for Events { String::from("Events - Iced") } - fn update(&mut self, message: Message) -> Command { + fn update( + &mut self, + message: Message, + _clipboard: &mut Clipboard, + ) -> Command { match message { Message::EventOccurred(event) => { self.last.push(event); diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index e18bd6e0..64599163 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -10,8 +10,8 @@ use iced::pick_list::{self, PickList}; use iced::slider::{self, Slider}; use iced::time; use iced::{ - Align, Application, Checkbox, Column, Command, Container, Element, Length, - Row, Settings, Subscription, Text, + Align, Application, Checkbox, Clipboard, Column, Command, Container, + Element, Length, Row, Settings, Subscription, Text, }; use preset::Preset; use std::time::{Duration, Instant}; @@ -65,7 +65,11 @@ impl Application for GameOfLife { String::from("Game of Life - Iced") } - fn update(&mut self, message: Message) -> Command { + fn update( + &mut self, + message: Message, + _clipboard: &mut Clipboard, + ) -> Command { match message { Message::Grid(message, version) => { if version == self.version { diff --git a/examples/integration/src/controls.rs b/examples/integration/src/controls.rs index 824f9f53..36ee9b7e 100644 --- a/examples/integration/src/controls.rs +++ b/examples/integration/src/controls.rs @@ -1,7 +1,7 @@ use iced_wgpu::Renderer; use iced_winit::{ - slider, Align, Color, Column, Command, Element, Length, Program, Row, - Slider, Text, + slider, Align, Clipboard, Color, Column, Command, Element, Length, Program, + Row, Slider, Text, }; pub struct Controls { @@ -30,8 +30,13 @@ impl Controls { impl Program for Controls { type Renderer = Renderer; type Message = Message; + type Clipboard = Clipboard; - fn update(&mut self, message: Message) -> Command { + fn update( + &mut self, + message: Message, + _clipboard: &mut Clipboard, + ) -> Command { match message { Message::BackgroundColorChanged(color) => { self.background_color = color; diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs index c44d24fd..4b87a568 100644 --- a/examples/pane_grid/src/main.rs +++ b/examples/pane_grid/src/main.rs @@ -1,7 +1,8 @@ use iced::{ button, executor, keyboard, pane_grid, scrollable, Align, Application, - Button, Color, Column, Command, Container, Element, HorizontalAlignment, - Length, PaneGrid, Row, Scrollable, Settings, Subscription, Text, + Button, Clipboard, Color, Column, Command, Container, Element, + HorizontalAlignment, Length, PaneGrid, Row, Scrollable, Settings, + Subscription, Text, }; use iced_native::{event, subscription, Event}; @@ -49,7 +50,11 @@ impl Application for Example { String::from("Pane grid - Iced") } - fn update(&mut self, message: Message) -> Command { + fn update( + &mut self, + message: Message, + _clipboard: &mut Clipboard, + ) -> Command { match message { Message::Split(axis, pane) => { let result = self.panes.split( diff --git a/examples/pokedex/src/main.rs b/examples/pokedex/src/main.rs index f432f0fc..d2f1bb62 100644 --- a/examples/pokedex/src/main.rs +++ b/examples/pokedex/src/main.rs @@ -1,6 +1,6 @@ use iced::{ - button, futures, image, Align, Application, Button, Column, Command, - Container, Element, Length, Row, Settings, Text, + button, futures, image, Align, Application, Button, Clipboard, Column, + Command, Container, Element, Length, Row, Settings, Text, }; pub fn main() -> iced::Result { @@ -48,7 +48,11 @@ impl Application for Pokedex { format!("{} - Pokédex", subtitle) } - fn update(&mut self, message: Message) -> Command { + fn update( + &mut self, + message: Message, + _clipboard: &mut Clipboard, + ) -> Command { match message { Message::PokemonFound(Ok(pokemon)) => { *self = Pokedex::Loaded { diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index c8f74978..8f844828 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -8,8 +8,8 @@ //! [1]: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Basic_animations#An_animated_solar_system use iced::{ canvas::{self, Cursor, Path, Stroke}, - executor, time, window, Application, Canvas, Color, Command, Element, - Length, Point, Rectangle, Settings, Size, Subscription, Vector, + executor, time, window, Application, Canvas, Clipboard, Color, Command, + Element, Length, Point, Rectangle, Settings, Size, Subscription, Vector, }; use std::time::Instant; @@ -48,7 +48,11 @@ impl Application for SolarSystem { String::from("Solar system - Iced") } - fn update(&mut self, message: Message) -> Command { + fn update( + &mut self, + message: Message, + _clipboard: &mut Clipboard, + ) -> Command { match message { Message::Tick(instant) => { self.state.update(instant); diff --git a/examples/stopwatch/src/main.rs b/examples/stopwatch/src/main.rs index 983cf3e6..51972e01 100644 --- a/examples/stopwatch/src/main.rs +++ b/examples/stopwatch/src/main.rs @@ -1,6 +1,6 @@ use iced::{ - button, executor, time, Align, Application, Button, Column, Command, - Container, Element, HorizontalAlignment, Length, Row, Settings, + button, executor, time, Align, Application, Button, Clipboard, Column, + Command, Container, Element, HorizontalAlignment, Length, Row, Settings, Subscription, Text, }; use std::time::{Duration, Instant}; @@ -49,7 +49,11 @@ impl Application for Stopwatch { String::from("Stopwatch - Iced") } - fn update(&mut self, message: Message) -> Command { + fn update( + &mut self, + message: Message, + _clipboard: &mut Clipboard, + ) -> Command { match message { Message::Toggle => match self.state { State::Idle => { diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index b47df5df..7186b950 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -1,7 +1,7 @@ use iced::{ button, scrollable, text_input, Align, Application, Button, Checkbox, - Column, Command, Container, Element, Font, HorizontalAlignment, Length, - Row, Scrollable, Settings, Text, TextInput, + Clipboard, Column, Command, Container, Element, Font, HorizontalAlignment, + Length, Row, Scrollable, Settings, Text, TextInput, }; use serde::{Deserialize, Serialize}; @@ -58,7 +58,11 @@ impl Application for Todos { format!("Todos{} - Iced", if dirty { "*" } else { "" }) } - fn update(&mut self, message: Message) -> Command { + fn update( + &mut self, + message: Message, + _clipboard: &mut Clipboard, + ) -> Command { match self { Todos::Loading => { match message { -- cgit From 57247106b95acb120381305cbe1ab4077b17651f Mon Sep 17 00:00:00 2001 From: Aldo Fregoso Date: Sat, 13 Mar 2021 14:50:59 -0600 Subject: Added `select_all` method to `TextInput`. --- examples/todos/src/main.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 7186b950..ee0022d5 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -265,8 +265,10 @@ impl Task { self.completed = completed; } TaskMessage::Edit => { + let mut text_input = text_input::State::focused(); + text_input.select_all(); self.state = TaskState::Editing { - text_input: text_input::State::focused(), + text_input, delete_button: button::State::new(), }; } -- cgit From 8f952452ce3d61203856bcebae7016372556be31 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 30 Mar 2021 21:45:49 +0200 Subject: Showcase graceful exiting in `events` example --- examples/events/src/main.rs | 47 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) (limited to 'examples') diff --git a/examples/events/src/main.rs b/examples/events/src/main.rs index 18e6364b..446c190b 100644 --- a/examples/events/src/main.rs +++ b/examples/events/src/main.rs @@ -1,22 +1,30 @@ use iced::{ - executor, Align, Application, Checkbox, Clipboard, Column, Command, - Container, Element, Length, Settings, Subscription, Text, + button, executor, Align, Application, Button, Checkbox, Clipboard, Column, + Command, Container, Element, HorizontalAlignment, Length, Settings, + Subscription, Text, }; +use iced_native::{window, Event}; pub fn main() -> iced::Result { - Events::run(Settings::default()) + Events::run(Settings { + exit_on_close_request: false, + ..Settings::default() + }) } #[derive(Debug, Default)] struct Events { last: Vec, enabled: bool, + exit: button::State, + should_exit: bool, } #[derive(Debug, Clone)] enum Message { EventOccurred(iced_native::Event), Toggled(bool), + Exit, } impl Application for Events { @@ -38,27 +46,35 @@ impl Application for Events { _clipboard: &mut Clipboard, ) -> Command { match message { - Message::EventOccurred(event) => { + Message::EventOccurred(event) if self.enabled => { self.last.push(event); if self.last.len() > 5 { let _ = self.last.remove(0); } } + Message::EventOccurred(event) => { + if let Event::Window(window::Event::CloseRequested) = event { + self.should_exit = true; + } + } Message::Toggled(enabled) => { self.enabled = enabled; } + Message::Exit => { + self.should_exit = true; + } }; Command::none() } fn subscription(&self) -> Subscription { - if self.enabled { - iced_native::subscription::events().map(Message::EventOccurred) - } else { - Subscription::none() - } + iced_native::subscription::events().map(Message::EventOccurred) + } + + fn should_exit(&self) -> bool { + self.should_exit } fn view(&mut self) -> Element { @@ -75,11 +91,22 @@ impl Application for Events { Message::Toggled, ); + let exit = Button::new( + &mut self.exit, + Text::new("Exit") + .width(Length::Fill) + .horizontal_alignment(HorizontalAlignment::Center), + ) + .width(Length::Units(100)) + .padding(10) + .on_press(Message::Exit); + let content = Column::new() .align_items(Align::Center) .spacing(20) .push(events) - .push(toggle); + .push(toggle) + .push(exit); Container::new(content) .width(Length::Fill) -- cgit From 9a2c78c4059d2be37d10adda397fb6e64f38ac02 Mon Sep 17 00:00:00 2001 From: Dispersia Date: Sun, 11 Apr 2021 18:55:57 -0700 Subject: Upgrade wgpu --- examples/integration/src/scene.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'examples') diff --git a/examples/integration/src/scene.rs b/examples/integration/src/scene.rs index 36c0a41d..3e8277c8 100644 --- a/examples/integration/src/scene.rs +++ b/examples/integration/src/scene.rs @@ -20,8 +20,8 @@ impl Scene { ) -> wgpu::RenderPass<'a> { encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: None, - color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { - attachment: target, + color_attachments: &[wgpu::RenderPassColorAttachment { + view: target, resolve_target: None, ops: wgpu::Operations { load: wgpu::LoadOp::Clear({ @@ -75,15 +75,16 @@ fn build_pipeline(device: &wgpu::Device) -> wgpu::RenderPipeline { entry_point: "main", targets: &[wgpu::ColorTargetState { format: wgpu::TextureFormat::Bgra8UnormSrgb, - color_blend: wgpu::BlendState::REPLACE, - alpha_blend: wgpu::BlendState::REPLACE, + blend: Some(wgpu::BlendState { + color: wgpu::BlendComponent::REPLACE, + alpha: wgpu::BlendComponent::REPLACE, + }), write_mask: wgpu::ColorWrite::ALL, }], }), primitive: wgpu::PrimitiveState { topology: wgpu::PrimitiveTopology::TriangleList, front_face: wgpu::FrontFace::Ccw, - cull_mode: wgpu::CullMode::None, ..Default::default() }, depth_stencil: None, -- cgit From 983aa1b3665c6b546700767d21d73de72372ddad Mon Sep 17 00:00:00 2001 From: Dispersia Date: Mon, 12 Apr 2021 23:23:47 -0700 Subject: Run cargo fmt --- examples/pokedex/src/main.rs | 5 ++++- examples/styling/src/main.rs | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/pokedex/src/main.rs b/examples/pokedex/src/main.rs index d2f1bb62..da1d5d5d 100644 --- a/examples/pokedex/src/main.rs +++ b/examples/pokedex/src/main.rs @@ -213,7 +213,10 @@ impl Pokemon { } async fn fetch_image(id: u16) -> Result { - let url = format!("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/{}.png", id); + let url = format!( + "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/{}.png", + id + ); #[cfg(not(target_arch = "wasm32"))] { diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index 8975fd9a..4d7dfc48 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -44,7 +44,7 @@ impl Sandbox for Styling { match message { Message::ThemeChanged(theme) => self.theme = theme, Message::InputChanged(value) => self.input_value = value, - Message::ButtonPressed => (), + Message::ButtonPressed => {} Message::SliderChanged(value) => self.slider_value = value, Message::CheckboxToggled(value) => self.toggle_value = value, } -- cgit From 59c2500c55430ac715b846017f79ad93a58fc3a5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Date: Mon, 3 May 2021 15:42:02 +0700 Subject: Fix `Scrollable` scrollbar being rendered behind contents ... by issuing a new clip layer just for the scrollbar itself. --- examples/scrollable/Cargo.toml | 2 +- examples/scrollable/src/main.rs | 41 ++++++++++++++++++++++++++++------------- 2 files changed, 29 insertions(+), 14 deletions(-) (limited to 'examples') diff --git a/examples/scrollable/Cargo.toml b/examples/scrollable/Cargo.toml index 12753fb6..08502458 100644 --- a/examples/scrollable/Cargo.toml +++ b/examples/scrollable/Cargo.toml @@ -6,4 +6,4 @@ edition = "2018" publish = false [dependencies] -iced = { path = "../.." } +iced = { path = "../..", features = ["debug"] } diff --git a/examples/scrollable/src/main.rs b/examples/scrollable/src/main.rs index 8dd2e20c..a570f0f6 100644 --- a/examples/scrollable/src/main.rs +++ b/examples/scrollable/src/main.rs @@ -1,8 +1,8 @@ mod style; use iced::{ - scrollable, Column, Container, Element, Length, Radio, Row, Rule, Sandbox, - Scrollable, Settings, Space, Text, + button, scrollable, Button, Column, Container, Element, Length, Radio, Row, + Rule, Sandbox, Scrollable, Settings, Space, Text, }; pub fn main() -> iced::Result { @@ -63,12 +63,14 @@ impl Sandbox for ScrollableDemo { variants .iter_mut() .map(|variant| { - let mut scrollable = Scrollable::new(&mut variant.state) - .padding(10) - .width(Length::Fill) - .height(Length::Fill) - .style(*theme) - .push(Text::new(variant.title)); + let mut scrollable = + Scrollable::new(&mut variant.scrollable) + .padding(10) + .spacing(10) + .width(Length::Fill) + .height(Length::Fill) + .style(*theme) + .push(Text::new(variant.title)); if let Some(scrollbar_width) = variant.scrollbar_width { scrollable = scrollable @@ -108,6 +110,14 @@ impl Sandbox for ScrollableDemo { .push(Space::with_height(Length::Units(1200))) .push(Text::new("Middle")) .push(Space::with_height(Length::Units(1200))) + .push( + Button::new( + &mut variant.button, + Text::new("I am a button"), + ) + .width(Length::Fill) + .padding(10), + ) .push(Text::new("The End.")); Container::new(scrollable) @@ -142,7 +152,8 @@ impl Sandbox for ScrollableDemo { /// A version of a scrollable struct Variant { title: &'static str, - state: scrollable::State, + scrollable: scrollable::State, + button: button::State, scrollbar_width: Option, scrollbar_margin: Option, scroller_width: Option, @@ -153,28 +164,32 @@ impl Variant { vec![ Self { title: "Default Scrollbar", - state: scrollable::State::new(), + scrollable: scrollable::State::new(), + button: button::State::new(), scrollbar_width: None, scrollbar_margin: None, scroller_width: None, }, Self { title: "Slimmed & Margin", - state: scrollable::State::new(), + scrollable: scrollable::State::new(), + button: button::State::new(), scrollbar_width: Some(4), scrollbar_margin: Some(3), scroller_width: Some(4), }, Self { title: "Wide Scroller", - state: scrollable::State::new(), + scrollable: scrollable::State::new(), + button: button::State::new(), scrollbar_width: Some(4), scrollbar_margin: None, scroller_width: Some(10), }, Self { title: "Narrow Scroller", - state: scrollable::State::new(), + scrollable: scrollable::State::new(), + button: button::State::new(), scrollbar_width: Some(10), scrollbar_margin: None, scroller_width: Some(4), -- cgit From e6f8b32583ae00eb77d6e10e3a43c899ecadc88f Mon Sep 17 00:00:00 2001 From: Clark Moody Date: Mon, 24 May 2021 14:28:52 -0500 Subject: Example: Add close button to pane grid controls Refactors the state data structure to hold content and controls in separate fields. Adds a new button style for the control button. --- examples/pane_grid/src/main.rs | 62 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 8 deletions(-) (limited to 'examples') diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs index 4b87a568..7c0d6582 100644 --- a/examples/pane_grid/src/main.rs +++ b/examples/pane_grid/src/main.rs @@ -11,7 +11,7 @@ pub fn main() -> iced::Result { } struct Example { - panes: pane_grid::State, + panes: pane_grid::State, panes_created: usize, focus: Option, } @@ -34,7 +34,7 @@ impl Application for Example { type Flags = (); fn new(_flags: ()) -> (Self, Command) { - let (panes, _) = pane_grid::State::new(Content::new(0)); + let (panes, _) = pane_grid::State::new(Pane::new(0)); ( Example { @@ -60,7 +60,7 @@ impl Application for Example { let result = self.panes.split( axis, &pane, - Content::new(self.panes_created), + Pane::new(self.panes_created), ); if let Some((pane, _)) = result { @@ -74,7 +74,7 @@ impl Application for Example { let result = self.panes.split( axis, &pane, - Content::new(self.panes_created), + Pane::new(self.panes_created), ); if let Some((pane, _)) = result { @@ -143,12 +143,12 @@ impl Application for Example { let focus = self.focus; let total_panes = self.panes.len(); - let pane_grid = PaneGrid::new(&mut self.panes, |pane, content| { - let is_focused = focus == Some(pane); + let pane_grid = PaneGrid::new(&mut self.panes, |id, pane| { + let is_focused = focus == Some(id); let title = Row::with_children(vec![ Text::new("Pane").into(), - Text::new(content.id.to_string()) + Text::new(pane.content.id.to_string()) .color(if is_focused { PANE_ID_COLOR_FOCUSED } else { @@ -159,10 +159,11 @@ impl Application for Example { .spacing(5); let title_bar = pane_grid::TitleBar::new(title) + .controls(pane.controls.view(id, total_panes)) .padding(10) .style(style::TitleBar { is_focused }); - pane_grid::Content::new(content.view(pane, total_panes)) + pane_grid::Content::new(pane.content.view(id, total_panes)) .title_bar(title_bar) .style(style::Pane { is_focused }) }) @@ -212,6 +213,11 @@ fn handle_hotkey(key_code: keyboard::KeyCode) -> Option { } } +struct Pane { + pub content: Content, + pub controls: Controls, +} + struct Content { id: usize, scroll: scrollable::State, @@ -220,6 +226,19 @@ struct Content { close: button::State, } +struct Controls { + close: button::State, +} + +impl Pane { + fn new(id: usize) -> Self { + Self { + content: Content::new(id), + controls: Controls::new(), + } + } +} + impl Content { fn new(id: usize) -> Self { Content { @@ -297,7 +316,31 @@ impl Content { } } +impl Controls { + fn new() -> Self { + Self { + close: button::State::new(), + } + } + + pub fn view( + &mut self, + pane: pane_grid::Pane, + total_panes: usize, + ) -> Element { + let mut button = + Button::new(&mut self.close, Text::new("Close").size(14)) + .style(style::Button::Control) + .padding(3); + if total_panes > 1 { + button = button.on_press(Message::Close(pane)); + } + button.into() + } +} + mod style { + use crate::PANE_ID_COLOR_FOCUSED; use iced::{button, container, Background, Color, Vector}; const SURFACE: Color = Color::from_rgb( @@ -359,6 +402,7 @@ mod style { pub enum Button { Primary, Destructive, + Control, } impl button::StyleSheet for Button { @@ -368,6 +412,7 @@ mod style { Button::Destructive => { (None, Color::from_rgb8(0xFF, 0x47, 0x47)) } + Button::Control => (Some(PANE_ID_COLOR_FOCUSED), Color::WHITE), }; button::Style { @@ -388,6 +433,7 @@ mod style { a: 0.2, ..active.text_color }), + Button::Control => Some(PANE_ID_COLOR_FOCUSED), }; button::Style { -- cgit From 1a2fd4e743fe2b5237a4b51fa0ab2ddc660dfd5a Mon Sep 17 00:00:00 2001 From: Clark Moody Date: Mon, 24 May 2021 15:53:20 -0500 Subject: Example: Add Pin button to prevent closing a pane Functionality will not work until PaneGrid implementation is updated to support events within the title area. --- examples/pane_grid/src/main.rs | 50 +++++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 8 deletions(-) (limited to 'examples') diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs index 7c0d6582..81cf1770 100644 --- a/examples/pane_grid/src/main.rs +++ b/examples/pane_grid/src/main.rs @@ -24,6 +24,7 @@ enum Message { Clicked(pane_grid::Pane), Dragged(pane_grid::DragEvent), Resized(pane_grid::ResizeEvent), + TogglePin(pane_grid::Pane), Close(pane_grid::Pane), CloseFocused, } @@ -106,6 +107,12 @@ impl Application for Example { self.panes.swap(&pane, &target); } Message::Dragged(_) => {} + Message::TogglePin(pane) => { + if let Some(Pane { is_pinned, .. }) = self.panes.get_mut(&pane) + { + *is_pinned = !*is_pinned; + } + } Message::Close(pane) => { if let Some((_, sibling)) = self.panes.close(&pane) { self.focus = Some(sibling); @@ -113,8 +120,14 @@ impl Application for Example { } Message::CloseFocused => { if let Some(pane) = self.focus { - if let Some((_, sibling)) = self.panes.close(&pane) { - self.focus = Some(sibling); + if let Some(Pane { is_pinned, .. }) = self.panes.get(&pane) + { + if !is_pinned { + if let Some((_, sibling)) = self.panes.close(&pane) + { + self.focus = Some(sibling); + } + } } } } @@ -146,7 +159,15 @@ impl Application for Example { let pane_grid = PaneGrid::new(&mut self.panes, |id, pane| { let is_focused = focus == Some(id); + let text = if pane.is_pinned { "Unpin" } else { "Pin" }; + let pin_button = + Button::new(&mut pane.pin_button, Text::new(text).size(14)) + .on_press(Message::TogglePin(id)) + .style(style::Button::Pin) + .padding(3); + let title = Row::with_children(vec![ + pin_button.into(), Text::new("Pane").into(), Text::new(pane.content.id.to_string()) .color(if is_focused { @@ -159,13 +180,17 @@ impl Application for Example { .spacing(5); let title_bar = pane_grid::TitleBar::new(title) - .controls(pane.controls.view(id, total_panes)) + .controls(pane.controls.view(id, total_panes, pane.is_pinned)) .padding(10) .style(style::TitleBar { is_focused }); - pane_grid::Content::new(pane.content.view(id, total_panes)) - .title_bar(title_bar) - .style(style::Pane { is_focused }) + pane_grid::Content::new(pane.content.view( + id, + total_panes, + pane.is_pinned, + )) + .title_bar(title_bar) + .style(style::Pane { is_focused }) }) .width(Length::Fill) .height(Length::Fill) @@ -214,6 +239,8 @@ fn handle_hotkey(key_code: keyboard::KeyCode) -> Option { } struct Pane { + pub is_pinned: bool, + pub pin_button: button::State, pub content: Content, pub controls: Controls, } @@ -233,6 +260,8 @@ struct Controls { impl Pane { fn new(id: usize) -> Self { Self { + is_pinned: false, + pin_button: button::State::new(), content: Content::new(id), controls: Controls::new(), } @@ -253,6 +282,7 @@ impl Content { &mut self, pane: pane_grid::Pane, total_panes: usize, + is_pinned: bool, ) -> Element { let Content { scroll, @@ -292,7 +322,7 @@ impl Content { style::Button::Primary, )); - if total_panes > 1 { + if total_panes > 1 && !is_pinned { controls = controls.push(button( close, "Close", @@ -327,12 +357,13 @@ impl Controls { &mut self, pane: pane_grid::Pane, total_panes: usize, + is_pinned: bool, ) -> Element { let mut button = Button::new(&mut self.close, Text::new("Close").size(14)) .style(style::Button::Control) .padding(3); - if total_panes > 1 { + if total_panes > 1 && !is_pinned { button = button.on_press(Message::Close(pane)); } button.into() @@ -403,6 +434,7 @@ mod style { Primary, Destructive, Control, + Pin, } impl button::StyleSheet for Button { @@ -413,6 +445,7 @@ mod style { (None, Color::from_rgb8(0xFF, 0x47, 0x47)) } Button::Control => (Some(PANE_ID_COLOR_FOCUSED), Color::WHITE), + Button::Pin => (Some(ACTIVE), Color::WHITE), }; button::Style { @@ -434,6 +467,7 @@ mod style { ..active.text_color }), Button::Control => Some(PANE_ID_COLOR_FOCUSED), + Button::Pin => Some(HOVERED), }; button::Style { -- cgit From 1ef38cc207ad5a393cb54ea779ae87eec2cd5ad9 Mon Sep 17 00:00:00 2001 From: Kaiden42 Date: Fri, 25 Sep 2020 15:45:46 +0200 Subject: Add `Toggler` to styling example --- examples/styling/src/main.rs | 73 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 64 insertions(+), 9 deletions(-) (limited to 'examples') diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index 4d7dfc48..7bc49281 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -1,7 +1,7 @@ use iced::{ button, scrollable, slider, text_input, Align, Button, Checkbox, Column, Container, Element, Length, ProgressBar, Radio, Row, Rule, Sandbox, - Scrollable, Settings, Slider, Space, Text, TextInput, + Scrollable, Settings, Slider, Space, Text, TextInput, Toggler, }; pub fn main() -> iced::Result { @@ -17,7 +17,8 @@ struct Styling { button: button::State, slider: slider::State, slider_value: f32, - toggle_value: bool, + checkbox_value: bool, + toggler_value: bool, } #[derive(Debug, Clone)] @@ -27,6 +28,7 @@ enum Message { ButtonPressed, SliderChanged(f32), CheckboxToggled(bool), + TogglerToggled(bool), } impl Sandbox for Styling { @@ -46,7 +48,8 @@ impl Sandbox for Styling { Message::InputChanged(value) => self.input_value = value, Message::ButtonPressed => {} Message::SliderChanged(value) => self.slider_value = value, - Message::CheckboxToggled(value) => self.toggle_value = value, + Message::CheckboxToggled(value) => self.checkbox_value = value, + Message::TogglerToggled(value) => self.toggler_value = value, } } @@ -101,11 +104,19 @@ impl Sandbox for Styling { .push(Text::new("You did it!")); let checkbox = Checkbox::new( - self.toggle_value, - "Toggle me!", + self.checkbox_value, + "Check me!", Message::CheckboxToggled, ) - .width(Length::Fill) + .style(self.theme); + + let toggler = Toggler::new( + self.toggler_value, + String::from("Toggle me!"), + Message::TogglerToggled, + ) + .width(Length::Shrink) + .spacing(10) .style(self.theme); let content = Column::new() @@ -124,7 +135,13 @@ impl Sandbox for Styling { .align_items(Align::Center) .push(scrollable) .push(Rule::vertical(38).style(self.theme)) - .push(checkbox), + .push( + Column::new() + .width(Length::Shrink) + .spacing(20) + .push(checkbox) + .push(toggler), + ), ); Container::new(content) @@ -140,7 +157,7 @@ impl Sandbox for Styling { mod style { use iced::{ button, checkbox, container, progress_bar, radio, rule, scrollable, - slider, text_input, + slider, text_input, toggler, }; #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -231,6 +248,15 @@ mod style { } } + impl From for Box { + fn from(theme: Theme) -> Self { + match theme { + Theme::Light => Default::default(), + Theme::Dark => dark::Toggler.into(), + } + } + } + impl From for Box { fn from(theme: Theme) -> Self { match theme { @@ -269,7 +295,7 @@ mod style { mod dark { use iced::{ button, checkbox, container, progress_bar, radio, rule, scrollable, - slider, text_input, Color, + slider, text_input, toggler, Color, }; const SURFACE: Color = Color::from_rgb( @@ -520,6 +546,35 @@ mod style { } } + pub struct Toggler; + + impl toggler::StyleSheet for Toggler { + fn active(&self, is_active: bool) -> toggler::Style { + toggler::Style { + background: if is_active { ACTIVE } else { SURFACE }, + background_border: None, + foreground: if is_active { Color::WHITE } else { ACTIVE }, + foreground_border: None, + } + } + + fn hovered(&self, is_active: bool) -> toggler::Style { + toggler::Style { + background: if is_active { ACTIVE } else { SURFACE }, + background_border: None, + foreground: if is_active { + Color { + a: 0.5, + ..Color::WHITE + } + } else { + Color { a: 0.5, ..ACTIVE } + }, + foreground_border: None, + } + } + } + pub struct Rule; impl rule::StyleSheet for Rule { -- cgit From be3ee9adf1659cf8eaa7a75cbb2aa1ecc33e4c42 Mon Sep 17 00:00:00 2001 From: Kaiden42 Date: Fri, 25 Sep 2020 16:35:58 +0200 Subject: Add `Toggler` to tour example --- examples/tour/src/main.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index e8755d39..de935444 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -1,7 +1,7 @@ use iced::{ button, scrollable, slider, text_input, Button, Checkbox, Color, Column, Container, Element, HorizontalAlignment, Image, Length, Radio, Row, - Sandbox, Scrollable, Settings, Slider, Space, Text, TextInput, + Sandbox, Scrollable, Settings, Slider, Space, Text, TextInput, Toggler, }; pub fn main() -> iced::Result { @@ -135,6 +135,9 @@ impl Steps { color: Color::BLACK, }, Step::Radio { selection: None }, + Step::Toggler { + can_continue: false, + }, Step::Image { width: 300, slider: slider::State::new(), @@ -206,6 +209,9 @@ enum Step { Radio { selection: Option, }, + Toggler { + can_continue: bool, + }, Image { width: u16, slider: slider::State, @@ -232,6 +238,7 @@ pub enum StepMessage { InputChanged(String), ToggleSecureInput(bool), DebugToggled(bool), + TogglerChanged(bool), } impl<'a> Step { @@ -287,6 +294,11 @@ impl<'a> Step { *is_secure = toggle; } } + StepMessage::TogglerChanged(value) => { + if let Step::Toggler { can_continue, .. } = self { + *can_continue = value; + } + } }; } @@ -294,6 +306,7 @@ impl<'a> Step { match self { Step::Welcome => "Welcome", Step::Radio { .. } => "Radio button", + Step::Toggler { .. } => "Toggler", Step::Slider { .. } => "Slider", Step::Text { .. } => "Text", Step::Image { .. } => "Image", @@ -309,6 +322,7 @@ impl<'a> Step { match self { Step::Welcome => true, Step::Radio { selection } => *selection == Some(Language::Rust), + Step::Toggler { can_continue } => *can_continue, Step::Slider { .. } => true, Step::Text { .. } => true, Step::Image { .. } => true, @@ -324,6 +338,7 @@ impl<'a> Step { match self { Step::Welcome => Self::welcome(), Step::Radio { selection } => Self::radio(*selection), + Step::Toggler { can_continue } => Self::toggler(*can_continue), Step::Slider { state, value } => Self::slider(state, *value), Step::Text { size_slider, @@ -545,6 +560,18 @@ impl<'a> Step { )) } + fn toggler(can_continue: bool) -> Column<'a, StepMessage> { + Self::container("Toggler") + .push(Text::new( + "A toggler is mostly used to enable or disable something.", + )) + .push(Toggler::new( + can_continue, + String::from("Toggle me to continue..."), + StepMessage::TogglerChanged, + )) + } + fn image( width: u16, slider: &'a mut slider::State, -- cgit From d3d6f3efb33f601ff3fca4a6496cfeef052501ee Mon Sep 17 00:00:00 2001 From: Héctor Ramón Date: Thu, 3 Jun 2021 20:35:26 +0700 Subject: Add some horizontal padding to `toggler` section in `tour` example --- examples/tour/src/main.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'examples') diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index de935444..1215f83d 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -565,11 +565,14 @@ impl<'a> Step { .push(Text::new( "A toggler is mostly used to enable or disable something.", )) - .push(Toggler::new( - can_continue, - String::from("Toggle me to continue..."), - StepMessage::TogglerChanged, - )) + .push( + Container::new(Toggler::new( + can_continue, + String::from("Toggle me to continue..."), + StepMessage::TogglerChanged, + )) + .padding([0, 40]), + ) } fn image( -- cgit From 57510c43c853c7332890f8e7f36c6ba1f2a7f252 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Date: Fri, 4 Jun 2021 20:15:06 +0700 Subject: Add buttons to control scrolling in `scrollable` example --- examples/scrollable/src/main.rs | 52 +++++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 12 deletions(-) (limited to 'examples') diff --git a/examples/scrollable/src/main.rs b/examples/scrollable/src/main.rs index a570f0f6..32c44df8 100644 --- a/examples/scrollable/src/main.rs +++ b/examples/scrollable/src/main.rs @@ -17,6 +17,8 @@ struct ScrollableDemo { #[derive(Debug, Clone)] enum Message { ThemeChanged(style::Theme), + ScrollToTop(usize), + ScrollToBottom(usize), } impl Sandbox for ScrollableDemo { @@ -36,6 +38,16 @@ impl Sandbox for ScrollableDemo { fn update(&mut self, message: Message) { match message { Message::ThemeChanged(theme) => self.theme = theme, + Message::ScrollToTop(i) => { + if let Some(variant) = self.variants.get_mut(i) { + variant.scrollable.snap_to(0.0); + } + } + Message::ScrollToBottom(i) => { + if let Some(variant) = self.variants.get_mut(i) { + variant.scrollable.snap_to(1.0); + } + } } } @@ -62,7 +74,8 @@ impl Sandbox for ScrollableDemo { let scrollable_row = Row::with_children( variants .iter_mut() - .map(|variant| { + .enumerate() + .map(|(i, variant)| { let mut scrollable = Scrollable::new(&mut variant.scrollable) .padding(10) @@ -70,7 +83,16 @@ impl Sandbox for ScrollableDemo { .width(Length::Fill) .height(Length::Fill) .style(*theme) - .push(Text::new(variant.title)); + .push(Text::new(variant.title)) + .push( + Button::new( + &mut variant.scroll_to_bottom, + Text::new("Scroll to bottom"), + ) + .width(Length::Fill) + .padding(10) + .on_press(Message::ScrollToBottom(i)), + ); if let Some(scrollbar_width) = variant.scrollbar_width { scrollable = scrollable @@ -110,15 +132,16 @@ impl Sandbox for ScrollableDemo { .push(Space::with_height(Length::Units(1200))) .push(Text::new("Middle")) .push(Space::with_height(Length::Units(1200))) + .push(Text::new("The End.")) .push( Button::new( - &mut variant.button, - Text::new("I am a button"), + &mut variant.scroll_to_top, + Text::new("Scroll to top"), ) .width(Length::Fill) - .padding(10), - ) - .push(Text::new("The End.")); + .padding(10) + .on_press(Message::ScrollToTop(i)), + ); Container::new(scrollable) .width(Length::Fill) @@ -153,7 +176,8 @@ impl Sandbox for ScrollableDemo { struct Variant { title: &'static str, scrollable: scrollable::State, - button: button::State, + scroll_to_top: button::State, + scroll_to_bottom: button::State, scrollbar_width: Option, scrollbar_margin: Option, scroller_width: Option, @@ -165,7 +189,8 @@ impl Variant { Self { title: "Default Scrollbar", scrollable: scrollable::State::new(), - button: button::State::new(), + scroll_to_top: button::State::new(), + scroll_to_bottom: button::State::new(), scrollbar_width: None, scrollbar_margin: None, scroller_width: None, @@ -173,7 +198,8 @@ impl Variant { Self { title: "Slimmed & Margin", scrollable: scrollable::State::new(), - button: button::State::new(), + scroll_to_top: button::State::new(), + scroll_to_bottom: button::State::new(), scrollbar_width: Some(4), scrollbar_margin: Some(3), scroller_width: Some(4), @@ -181,7 +207,8 @@ impl Variant { Self { title: "Wide Scroller", scrollable: scrollable::State::new(), - button: button::State::new(), + scroll_to_top: button::State::new(), + scroll_to_bottom: button::State::new(), scrollbar_width: Some(4), scrollbar_margin: None, scroller_width: Some(10), @@ -189,7 +216,8 @@ impl Variant { Self { title: "Narrow Scroller", scrollable: scrollable::State::new(), - button: button::State::new(), + scroll_to_top: button::State::new(), + scroll_to_bottom: button::State::new(), scrollbar_width: Some(10), scrollbar_margin: None, scroller_width: Some(4), -- cgit From ce3a5f19b92889d03f564133a90d328d430137af Mon Sep 17 00:00:00 2001 From: Héctor Ramón Date: Fri, 4 Jun 2021 20:46:47 +0700 Subject: Add scrolling progress indicators to `scrollable` example --- examples/scrollable/src/main.rs | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) (limited to 'examples') diff --git a/examples/scrollable/src/main.rs b/examples/scrollable/src/main.rs index 32c44df8..3416b83d 100644 --- a/examples/scrollable/src/main.rs +++ b/examples/scrollable/src/main.rs @@ -1,8 +1,8 @@ mod style; use iced::{ - button, scrollable, Button, Column, Container, Element, Length, Radio, Row, - Rule, Sandbox, Scrollable, Settings, Space, Text, + button, scrollable, Button, Column, Container, Element, Length, + ProgressBar, Radio, Row, Rule, Sandbox, Scrollable, Settings, Space, Text, }; pub fn main() -> iced::Result { @@ -19,6 +19,7 @@ enum Message { ThemeChanged(style::Theme), ScrollToTop(usize), ScrollToBottom(usize), + Scrolled(usize, f32), } impl Sandbox for ScrollableDemo { @@ -41,11 +42,20 @@ impl Sandbox for ScrollableDemo { Message::ScrollToTop(i) => { if let Some(variant) = self.variants.get_mut(i) { variant.scrollable.snap_to(0.0); + + variant.latest_offset = 0.0; } } Message::ScrollToBottom(i) => { if let Some(variant) = self.variants.get_mut(i) { variant.scrollable.snap_to(1.0); + + variant.latest_offset = 1.0; + } + } + Message::Scrolled(i, offset) => { + if let Some(variant) = self.variants.get_mut(i) { + variant.latest_offset = offset; } } } @@ -82,6 +92,9 @@ impl Sandbox for ScrollableDemo { .spacing(10) .width(Length::Fill) .height(Length::Fill) + .on_scroll(move |offset| { + Message::Scrolled(i, offset) + }) .style(*theme) .push(Text::new(variant.title)) .push( @@ -143,10 +156,20 @@ impl Sandbox for ScrollableDemo { .on_press(Message::ScrollToTop(i)), ); - Container::new(scrollable) + Column::new() .width(Length::Fill) .height(Length::Fill) - .style(*theme) + .spacing(10) + .push( + Container::new(scrollable) + .width(Length::Fill) + .height(Length::Fill) + .style(*theme), + ) + .push(ProgressBar::new( + 0.0..=1.0, + variant.latest_offset, + )) .into() }) .collect(), @@ -181,6 +204,7 @@ struct Variant { scrollbar_width: Option, scrollbar_margin: Option, scroller_width: Option, + latest_offset: f32, } impl Variant { @@ -194,6 +218,7 @@ impl Variant { scrollbar_width: None, scrollbar_margin: None, scroller_width: None, + latest_offset: 0.0, }, Self { title: "Slimmed & Margin", @@ -203,6 +228,7 @@ impl Variant { scrollbar_width: Some(4), scrollbar_margin: Some(3), scroller_width: Some(4), + latest_offset: 0.0, }, Self { title: "Wide Scroller", @@ -212,6 +238,7 @@ impl Variant { scrollbar_width: Some(4), scrollbar_margin: None, scroller_width: Some(10), + latest_offset: 0.0, }, Self { title: "Narrow Scroller", @@ -221,6 +248,7 @@ impl Variant { scrollbar_width: Some(10), scrollbar_margin: None, scroller_width: Some(4), + latest_offset: 0.0, }, ] } -- cgit From 9ae22b58d843d9a39212028478598c19a49bc2e6 Mon Sep 17 00:00:00 2001 From: Richard Date: Wed, 21 Apr 2021 17:52:31 -0300 Subject: Added events for url handling and create example --- examples/url_handler/Cargo.toml | 12 +++++++ examples/url_handler/src/main.rs | 67 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 examples/url_handler/Cargo.toml create mode 100644 examples/url_handler/src/main.rs (limited to 'examples') diff --git a/examples/url_handler/Cargo.toml b/examples/url_handler/Cargo.toml new file mode 100644 index 00000000..595bdac0 --- /dev/null +++ b/examples/url_handler/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "url_handler" +version = "0.1.0" +authors = ["Héctor Ramón Jiménez "] +edition = "2018" +publish = false + +[dependencies] +iced = { path = "../.." } +iced_native = { path = "../../native" } +syslog="4.0" +log="0.4" \ No newline at end of file diff --git a/examples/url_handler/src/main.rs b/examples/url_handler/src/main.rs new file mode 100644 index 00000000..56d81031 --- /dev/null +++ b/examples/url_handler/src/main.rs @@ -0,0 +1,67 @@ +use iced::{ + executor, Application, Command, Clipboard, + Container, Element, Length, Settings, Subscription, Text, +}; +use iced_native::Event; + +pub fn main() -> iced::Result { + App::run(Settings::default()) +} + +#[derive(Debug, Default)] +struct App { + url: Option, +} + +#[derive(Debug, Clone)] +enum Message { + EventOccurred(iced_native::Event), +} + +impl Application for App { + type Executor = executor::Default; + type Message = Message; + type Flags = (); + + fn new(_flags: ()) -> (App, Command) { + (App::default(), Command::none()) + } + + fn title(&self) -> String { + String::from("Url - Iced") + } + + fn update( + &mut self, + message: Message, + _clipboard: &mut Clipboard, + ) -> Command { + match message { + Message::EventOccurred(event) => { + if let Event::UrlReceived(url) = event{ + self.url = Some(url); + } + } + }; + + Command::none() + } + + fn subscription(&self) -> Subscription { + iced_native::subscription::events().map(Message::EventOccurred) + } + + fn view(&mut self) -> Element { + let content = match &self.url{ + Some(url) => Text::new(format!("{}", url)), + None => Text::new("No URL received yet!") + }; + + Container::new(content.size(48)) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .into() + } +} -- cgit From 96a462d2f2cb608ad14c93cc55896108a2dccb2b Mon Sep 17 00:00:00 2001 From: Richard Date: Wed, 9 Jun 2021 15:00:01 -0300 Subject: Use new enum variant and new winit repo --- examples/url_handler/Cargo.toml | 4 +--- examples/url_handler/src/main.rs | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 9 deletions(-) (limited to 'examples') diff --git a/examples/url_handler/Cargo.toml b/examples/url_handler/Cargo.toml index 595bdac0..911b2f25 100644 --- a/examples/url_handler/Cargo.toml +++ b/examples/url_handler/Cargo.toml @@ -7,6 +7,4 @@ publish = false [dependencies] iced = { path = "../.." } -iced_native = { path = "../../native" } -syslog="4.0" -log="0.4" \ No newline at end of file +iced_native = { path = "../../native" } \ No newline at end of file diff --git a/examples/url_handler/src/main.rs b/examples/url_handler/src/main.rs index 56d81031..f14e5227 100644 --- a/examples/url_handler/src/main.rs +++ b/examples/url_handler/src/main.rs @@ -1,8 +1,11 @@ use iced::{ - executor, Application, Command, Clipboard, - Container, Element, Length, Settings, Subscription, Text, + executor, Application, Clipboard, Command, Container, Element, Length, + Settings, Subscription, Text, +}; +use iced_native::{ + event::{MacOS, PlatformSpecific}, + Event, }; -use iced_native::Event; pub fn main() -> iced::Result { App::run(Settings::default()) @@ -38,7 +41,10 @@ impl Application for App { ) -> Command { match message { Message::EventOccurred(event) => { - if let Event::UrlReceived(url) = event{ + if let Event::PlatformSpecific(PlatformSpecific::MacOS( + MacOS::ReceivedUrl(url), + )) = event + { self.url = Some(url); } } @@ -52,9 +58,9 @@ impl Application for App { } fn view(&mut self) -> Element { - let content = match &self.url{ + let content = match &self.url { Some(url) => Text::new(format!("{}", url)), - None => Text::new("No URL received yet!") + None => Text::new("No URL received yet!"), }; Container::new(content.size(48)) -- cgit From 52d44769a332d1d96a5e9292805a5884073b9185 Mon Sep 17 00:00:00 2001 From: TimUntersberger Date: Fri, 25 Jun 2021 15:38:30 +0200 Subject: add initial attempt at adding winit example --- examples/winit/Cargo.toml | 9 +++++++ examples/winit/README.md | 18 ++++++++++++++ examples/winit/src/main.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 examples/winit/Cargo.toml create mode 100644 examples/winit/README.md create mode 100644 examples/winit/src/main.rs (limited to 'examples') diff --git a/examples/winit/Cargo.toml b/examples/winit/Cargo.toml new file mode 100644 index 00000000..6d16a7a9 --- /dev/null +++ b/examples/winit/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "counter_winit" +version = "0.1.0" +authors = ["Héctor Ramón Jiménez "] +edition = "2018" +publish = false + +[dependencies] +iced_winit = { path = "../../winit" } diff --git a/examples/winit/README.md b/examples/winit/README.md new file mode 100644 index 00000000..4d9fc5b9 --- /dev/null +++ b/examples/winit/README.md @@ -0,0 +1,18 @@ +## Counter + +The classic counter example explained in the [`README`](../../README.md). + +The __[`main`]__ file contains all the code of the example. + + + +You can run it with `cargo run`: +``` +cargo run --package counter +``` + +[`main`]: src/main.rs diff --git a/examples/winit/src/main.rs b/examples/winit/src/main.rs new file mode 100644 index 00000000..890ded79 --- /dev/null +++ b/examples/winit/src/main.rs @@ -0,0 +1,62 @@ +use iced_winit::{button, Align, Button, Column, Element, Application, Settings, Text, Renderer, Program, Command}; + +pub fn main() { + Counter::run(Settings::default()).unwrap() +} + +#[derive(Default)] +struct Counter { + value: i32, + increment_button: button::State, + decrement_button: button::State, +} + +#[derive(Debug, Clone, Copy)] +enum Message { + IncrementPressed, + DecrementPressed, +} + +impl Application for Counter { + type Flags = (); + + fn new(flags: Self::Flags) -> (Self, Command) { + (Self::default(), Command::none()) + } + + fn title(&self) -> String { + String::from("Counter with winit - Iced") + } +} + +impl Program for Counter { + type Renderer = Renderer; + type Message = Message; + + fn update(&mut self, message: Message) { + match message { + Message::IncrementPressed => { + self.value += 1; + } + Message::DecrementPressed => { + self.value -= 1; + } + } + } + + fn view(&mut self) -> Element { + Column::new() + .padding(20) + .align_items(Align::Center) + .push( + Button::new(&mut self.increment_button, Text::new("Increment")) + .on_press(Message::IncrementPressed), + ) + .push(Text::new(self.value.to_string()).size(50)) + .push( + Button::new(&mut self.decrement_button, Text::new("Decrement")) + .on_press(Message::DecrementPressed), + ) + .into() + } +} -- cgit From 5c45d36d1a8cfd92cd1a454a7f4deedcd4d13fe7 Mon Sep 17 00:00:00 2001 From: TimUntersberger Date: Fri, 25 Jun 2021 17:16:44 +0200 Subject: wip --- examples/winit/Cargo.toml | 2 +- examples/winit/src/main.rs | 27 +++++++++++++++------------ 2 files changed, 16 insertions(+), 13 deletions(-) (limited to 'examples') diff --git a/examples/winit/Cargo.toml b/examples/winit/Cargo.toml index 6d16a7a9..2d58f031 100644 --- a/examples/winit/Cargo.toml +++ b/examples/winit/Cargo.toml @@ -6,4 +6,4 @@ edition = "2018" publish = false [dependencies] -iced_winit = { path = "../../winit" } +iced = { path = "../.." } diff --git a/examples/winit/src/main.rs b/examples/winit/src/main.rs index 890ded79..202d9b5a 100644 --- a/examples/winit/src/main.rs +++ b/examples/winit/src/main.rs @@ -1,7 +1,15 @@ -use iced_winit::{button, Align, Button, Column, Element, Application, Settings, Text, Renderer, Program, Command}; +use iced::{button, Align, Button, Column, Element, Sandbox, Settings, window::Settings as WindowSettings, Text}; pub fn main() { - Counter::run(Settings::default()).unwrap() + let settings = Settings { + window: WindowSettings { + size: (400, 200), + position: (100, 100), + ..Default::default() + }, + ..Default::default() + }; + Counter::run(settings).unwrap() } #[derive(Default)] @@ -17,21 +25,16 @@ enum Message { DecrementPressed, } -impl Application for Counter { - type Flags = (); +impl Sandbox for Counter { + type Message = Message; - fn new(flags: Self::Flags) -> (Self, Command) { - (Self::default(), Command::none()) + fn new() -> Self { + Self::default() } fn title(&self) -> String { String::from("Counter with winit - Iced") } -} - -impl Program for Counter { - type Renderer = Renderer; - type Message = Message; fn update(&mut self, message: Message) { match message { @@ -44,7 +47,7 @@ impl Program for Counter { } } - fn view(&mut self) -> Element { + fn view(&mut self) -> Element { Column::new() .padding(20) .align_items(Align::Center) -- cgit From de79a01b88e1610d374ed06077ac78f3e10b9c3d Mon Sep 17 00:00:00 2001 From: TimUntersberger Date: Fri, 25 Jun 2021 18:03:18 +0200 Subject: done --- examples/winit/src/main.rs | 63 ++++++++++++++++------------------------------ 1 file changed, 22 insertions(+), 41 deletions(-) (limited to 'examples') diff --git a/examples/winit/src/main.rs b/examples/winit/src/main.rs index 202d9b5a..a1364ea6 100644 --- a/examples/winit/src/main.rs +++ b/examples/winit/src/main.rs @@ -1,65 +1,46 @@ -use iced::{button, Align, Button, Column, Element, Sandbox, Settings, window::Settings as WindowSettings, Text}; +use iced::{Column, Element, Sandbox, Settings, window::Settings as WindowSettings}; + +const WINDOW_WIDTH: i32 = 200; +const WINDOW_HEIGHT: i32 = 200; +const DISPLAY_WIDTH: i32 = 1920; +const DISPLAY_HEIGHT: i32 = 1080; +// These numbers are specific to a 1920x1080 monitor +const BORDER_X: i32 = 8; +const BORDER_Y: i32 = 2; +const CAPTION_HEIGHT: i32 = 4; pub fn main() { + let x = DISPLAY_WIDTH / 2 - WINDOW_WIDTH / 2 - BORDER_X; + let y = DISPLAY_HEIGHT / 2 - WINDOW_HEIGHT / 2 - BORDER_Y - CAPTION_HEIGHT; let settings = Settings { window: WindowSettings { - size: (400, 200), - position: (100, 100), + size: (WINDOW_WIDTH as u32, WINDOW_HEIGHT as u32), + position: (x, y), ..Default::default() }, ..Default::default() }; - Counter::run(settings).unwrap() + Winit::run(settings).unwrap() } #[derive(Default)] -struct Counter { - value: i32, - increment_button: button::State, - decrement_button: button::State, -} - -#[derive(Debug, Clone, Copy)] -enum Message { - IncrementPressed, - DecrementPressed, -} +struct Winit; -impl Sandbox for Counter { - type Message = Message; +impl Sandbox for Winit { + type Message = (); fn new() -> Self { Self::default() } fn title(&self) -> String { - String::from("Counter with winit - Iced") + String::from("winit - Iced") } - fn update(&mut self, message: Message) { - match message { - Message::IncrementPressed => { - self.value += 1; - } - Message::DecrementPressed => { - self.value -= 1; - } - } + fn update(&mut self, _message: Self::Message) { } - fn view(&mut self) -> Element { - Column::new() - .padding(20) - .align_items(Align::Center) - .push( - Button::new(&mut self.increment_button, Text::new("Increment")) - .on_press(Message::IncrementPressed), - ) - .push(Text::new(self.value.to_string()).size(50)) - .push( - Button::new(&mut self.decrement_button, Text::new("Decrement")) - .on_press(Message::DecrementPressed), - ) - .into() + fn view(&mut self) -> Element { + Column::new().into() } } -- cgit From 552b7a1307ceaf83d00cbee88dc33ae5d92ee53e Mon Sep 17 00:00:00 2001 From: TimUntersberger Date: Fri, 25 Jun 2021 18:07:03 +0200 Subject: update cargo.toml of example --- examples/winit/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/winit/Cargo.toml b/examples/winit/Cargo.toml index 2d58f031..2286dfc9 100644 --- a/examples/winit/Cargo.toml +++ b/examples/winit/Cargo.toml @@ -1,7 +1,7 @@ [package] -name = "counter_winit" +name = "winit" version = "0.1.0" -authors = ["Héctor Ramón Jiménez "] +authors = ["Tim Untersberger "] edition = "2018" publish = false -- cgit From 9fc5ad23edca93553137100d167de7b69e88f785 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 5 Jul 2021 16:23:44 -0300 Subject: Initial menu implementation --- examples/menu/Cargo.toml | 10 ++++ examples/menu/src/main.rs | 123 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 examples/menu/Cargo.toml create mode 100644 examples/menu/src/main.rs (limited to 'examples') diff --git a/examples/menu/Cargo.toml b/examples/menu/Cargo.toml new file mode 100644 index 00000000..44597734 --- /dev/null +++ b/examples/menu/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "menu" +version = "0.1.0" +authors = ["Héctor Ramón Jiménez "] +edition = "2018" +publish = false + +[dependencies] +iced = { path = "../.." } +iced_native = { path = "../../native" } \ No newline at end of file diff --git a/examples/menu/src/main.rs b/examples/menu/src/main.rs new file mode 100644 index 00000000..f8f70903 --- /dev/null +++ b/examples/menu/src/main.rs @@ -0,0 +1,123 @@ +use iced::{ + executor, Application, Clipboard, Command, Container, Element, Length, + Menu, Settings, Text, +}; +use iced_native::keyboard::{Hotkey, KeyCode, Modifiers}; + +pub fn main() -> iced::Result { + App::run(Settings::default()) +} + +#[derive(Debug, Default)] +struct App { + selected: Option, +} + +#[derive(Debug, Clone)] +enum Entry { + One, + Two, + Three, + A, + B, + C, +} + +#[derive(Debug, Clone)] +enum Message { + MenuActivated(Entry), +} + +impl Application for App { + type Executor = executor::Default; + type Message = Message; + type Flags = (); + + fn new(_flags: ()) -> (App, Command) { + (App::default(), Command::none()) + } + + fn title(&self) -> String { + String::from("Menu - Iced") + } + + fn menu(&self) -> Menu { + let alt = Modifiers { + alt: true, + control: false, + logo: false, + shift: false, + }; + let ctrl_shift = Modifiers { + control: true, + shift: true, + logo: false, + alt: false, + }; + + Menu::new() + .dropdown( + "First", + Menu::new() + .item( + "One", + Hotkey::new(alt, KeyCode::F1), + Message::MenuActivated(Entry::One), + ) + .item( + "Two", + Hotkey::new(alt, KeyCode::F2), + Message::MenuActivated(Entry::Two), + ) + .separator() + .item( + "Three", + Hotkey::new(alt, KeyCode::F3), + Message::MenuActivated(Entry::Three), + ), + ) + .dropdown( + "Second", + Menu::new() + .item( + "A", + Hotkey::new(ctrl_shift, KeyCode::A), + Message::MenuActivated(Entry::A), + ) + .item( + "B", + Hotkey::new(ctrl_shift, KeyCode::B), + Message::MenuActivated(Entry::B), + ) + .separator() + .item( + "C", + Hotkey::new(ctrl_shift, KeyCode::C), + Message::MenuActivated(Entry::C), + ), + ) + } + + fn update( + &mut self, + message: Message, + _clipboard: &mut Clipboard, + ) -> Command { + match message { + Message::MenuActivated(entry) => self.selected = Some(entry), + } + + Command::none() + } + + fn view(&mut self) -> Element { + Container::new( + Text::new(format!("Selected {:?}", self.selected)).size(48), + ) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .into() + } +} -- cgit From 1428e9180ae9f4edbf22514bb74c5c7e9df9c712 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 12 Jul 2021 21:38:54 +0200 Subject: Make `Menu` API a bit more functional --- examples/menu/src/main.rs | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) (limited to 'examples') diff --git a/examples/menu/src/main.rs b/examples/menu/src/main.rs index f8f70903..9d4ed71b 100644 --- a/examples/menu/src/main.rs +++ b/examples/menu/src/main.rs @@ -1,6 +1,7 @@ +use iced::menu::{self, Menu}; use iced::{ executor, Application, Clipboard, Command, Container, Element, Length, - Menu, Settings, Text, + Settings, Text, }; use iced_native::keyboard::{Hotkey, KeyCode, Modifiers}; @@ -55,47 +56,50 @@ impl Application for App { alt: false, }; - Menu::new() - .dropdown( + Menu::with_entries(vec![ + menu::Entry::dropdown( "First", - Menu::new() - .item( + Menu::with_entries(vec![ + menu::Entry::item( "One", Hotkey::new(alt, KeyCode::F1), Message::MenuActivated(Entry::One), - ) - .item( + ), + menu::Entry::item( "Two", Hotkey::new(alt, KeyCode::F2), Message::MenuActivated(Entry::Two), - ) - .separator() - .item( + ), + menu::Entry::Separator, + menu::Entry::item( "Three", Hotkey::new(alt, KeyCode::F3), Message::MenuActivated(Entry::Three), ), - ) - .dropdown( + ]), + ), + menu::Entry::dropdown( "Second", - Menu::new() - .item( + Menu::with_entries(vec![ + menu::Entry::item( "A", Hotkey::new(ctrl_shift, KeyCode::A), Message::MenuActivated(Entry::A), - ) - .item( + ), + menu::Entry::item( "B", Hotkey::new(ctrl_shift, KeyCode::B), Message::MenuActivated(Entry::B), - ) - .separator() - .item( + ), + menu::Entry::Separator, + menu::Entry::item( "C", Hotkey::new(ctrl_shift, KeyCode::C), Message::MenuActivated(Entry::C), ), - ) + ]), + ), + ]) } fn update( -- cgit From b57d567981bb7ef5f9ff397c210778f211d2de5b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 12 Jul 2021 22:01:57 +0200 Subject: Use `bitflags` for `keyboard::Modifiers` --- examples/menu/src/main.rs | 14 ++------------ examples/pane_grid/src/main.rs | 2 +- 2 files changed, 3 insertions(+), 13 deletions(-) (limited to 'examples') diff --git a/examples/menu/src/main.rs b/examples/menu/src/main.rs index 9d4ed71b..7403713c 100644 --- a/examples/menu/src/main.rs +++ b/examples/menu/src/main.rs @@ -43,18 +43,8 @@ impl Application for App { } fn menu(&self) -> Menu { - let alt = Modifiers { - alt: true, - control: false, - logo: false, - shift: false, - }; - let ctrl_shift = Modifiers { - control: true, - shift: true, - logo: false, - alt: false, - }; + let alt = Modifiers::ALT; + let ctrl_shift = Modifiers::CTRL | Modifiers::SHIFT; Menu::with_entries(vec![ menu::Entry::dropdown( diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs index 81cf1770..3bd8aa25 100644 --- a/examples/pane_grid/src/main.rs +++ b/examples/pane_grid/src/main.rs @@ -146,7 +146,7 @@ impl Application for Example { Event::Keyboard(keyboard::Event::KeyPressed { modifiers, key_code, - }) if modifiers.is_command_pressed() => handle_hotkey(key_code), + }) if modifiers.command() => handle_hotkey(key_code), _ => None, } }) -- cgit From b97954a1ee3ec7bc85d1d41b397e994752ff1831 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 19 Jul 2021 21:18:54 +0700 Subject: Add a presets `Menu` to the `game_of_life` example --- examples/game_of_life/src/main.rs | 8 ++++++++ examples/game_of_life/src/preset.rs | 13 +++++++++++++ 2 files changed, 21 insertions(+) (limited to 'examples') diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index 64599163..877aa2d2 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -6,6 +6,7 @@ mod style; use grid::Grid; use iced::button::{self, Button}; use iced::executor; +use iced::menu::{self, Menu}; use iced::pick_list::{self, PickList}; use iced::slider::{self, Slider}; use iced::time; @@ -128,6 +129,13 @@ impl Application for GameOfLife { } } + fn menu(&self) -> Menu { + Menu::with_entries(vec![menu::Entry::dropdown( + "Presets", + Preset::menu().map(Message::PresetPicked), + )]) + } + fn view(&mut self) -> Element { let version = self.version; let selected_speed = self.next_speed.unwrap_or(self.speed); diff --git a/examples/game_of_life/src/preset.rs b/examples/game_of_life/src/preset.rs index 05157b6a..1c199a72 100644 --- a/examples/game_of_life/src/preset.rs +++ b/examples/game_of_life/src/preset.rs @@ -1,3 +1,5 @@ +use iced::menu::{self, Menu}; + #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Preset { Custom, @@ -26,6 +28,17 @@ pub static ALL: &[Preset] = &[ ]; impl Preset { + pub fn menu() -> Menu { + Menu::with_entries( + ALL.iter() + .copied() + .map(|preset| { + menu::Entry::item(preset.to_string(), None, preset) + }) + .collect(), + ) + } + pub fn life(self) -> Vec<(isize, isize)> { #[rustfmt::skip] let cells = match self { -- cgit From 4de27142133b2dd6f780f0f955f620787102e581 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 21 Jul 2021 18:11:31 +0700 Subject: Remove `winit` example ... we can extend an existing example instead! --- examples/winit/Cargo.toml | 9 --------- examples/winit/README.md | 18 ------------------ examples/winit/src/main.rs | 46 ---------------------------------------------- 3 files changed, 73 deletions(-) delete mode 100644 examples/winit/Cargo.toml delete mode 100644 examples/winit/README.md delete mode 100644 examples/winit/src/main.rs (limited to 'examples') diff --git a/examples/winit/Cargo.toml b/examples/winit/Cargo.toml deleted file mode 100644 index 2286dfc9..00000000 --- a/examples/winit/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[package] -name = "winit" -version = "0.1.0" -authors = ["Tim Untersberger "] -edition = "2018" -publish = false - -[dependencies] -iced = { path = "../.." } diff --git a/examples/winit/README.md b/examples/winit/README.md deleted file mode 100644 index 4d9fc5b9..00000000 --- a/examples/winit/README.md +++ /dev/null @@ -1,18 +0,0 @@ -## Counter - -The classic counter example explained in the [`README`](../../README.md). - -The __[`main`]__ file contains all the code of the example. - - - -You can run it with `cargo run`: -``` -cargo run --package counter -``` - -[`main`]: src/main.rs diff --git a/examples/winit/src/main.rs b/examples/winit/src/main.rs deleted file mode 100644 index a1364ea6..00000000 --- a/examples/winit/src/main.rs +++ /dev/null @@ -1,46 +0,0 @@ -use iced::{Column, Element, Sandbox, Settings, window::Settings as WindowSettings}; - -const WINDOW_WIDTH: i32 = 200; -const WINDOW_HEIGHT: i32 = 200; -const DISPLAY_WIDTH: i32 = 1920; -const DISPLAY_HEIGHT: i32 = 1080; -// These numbers are specific to a 1920x1080 monitor -const BORDER_X: i32 = 8; -const BORDER_Y: i32 = 2; -const CAPTION_HEIGHT: i32 = 4; - -pub fn main() { - let x = DISPLAY_WIDTH / 2 - WINDOW_WIDTH / 2 - BORDER_X; - let y = DISPLAY_HEIGHT / 2 - WINDOW_HEIGHT / 2 - BORDER_Y - CAPTION_HEIGHT; - let settings = Settings { - window: WindowSettings { - size: (WINDOW_WIDTH as u32, WINDOW_HEIGHT as u32), - position: (x, y), - ..Default::default() - }, - ..Default::default() - }; - Winit::run(settings).unwrap() -} - -#[derive(Default)] -struct Winit; - -impl Sandbox for Winit { - type Message = (); - - fn new() -> Self { - Self::default() - } - - fn title(&self) -> String { - String::from("winit - Iced") - } - - fn update(&mut self, _message: Self::Message) { - } - - fn view(&mut self) -> Element { - Column::new().into() - } -} -- cgit From 39b8f7de50ca53c67214c4ec2af2d3a0944f006a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 21 Jul 2021 19:10:11 +0700 Subject: Center window in `game_of_life` example --- examples/game_of_life/src/main.rs | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'examples') diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index 64599163..642262b0 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -9,6 +9,7 @@ use iced::executor; use iced::pick_list::{self, PickList}; use iced::slider::{self, Slider}; use iced::time; +use iced::window; use iced::{ Align, Application, Checkbox, Clipboard, Column, Command, Container, Element, Length, Row, Settings, Subscription, Text, @@ -19,6 +20,10 @@ use std::time::{Duration, Instant}; pub fn main() -> iced::Result { GameOfLife::run(Settings { antialiasing: true, + window: window::Settings { + position: window::Position::Centered, + ..window::Settings::default() + }, ..Settings::default() }) } -- cgit From 9c75ddcb3370c0e2caefdb0a3c1f30ea877faf02 Mon Sep 17 00:00:00 2001 From: Jon Pacheco Date: Sat, 22 May 2021 19:28:27 +0100 Subject: feat: add placeholder to pick_list example --- examples/pick_list/src/main.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'examples') diff --git a/examples/pick_list/src/main.rs b/examples/pick_list/src/main.rs index 68662602..1eec9791 100644 --- a/examples/pick_list/src/main.rs +++ b/examples/pick_list/src/main.rs @@ -11,7 +11,7 @@ pub fn main() -> iced::Result { struct Example { scroll: scrollable::State, pick_list: pick_list::State, - selected_language: Language, + selected_language: Option, } #[derive(Debug, Clone, Copy)] @@ -33,7 +33,7 @@ impl Sandbox for Example { fn update(&mut self, message: Message) { match message { Message::LanguageSelected(language) => { - self.selected_language = language; + self.selected_language = Some(language); } } } @@ -42,9 +42,10 @@ impl Sandbox for Example { let pick_list = PickList::new( &mut self.pick_list, &Language::ALL[..], - Some(self.selected_language), + self.selected_language, Message::LanguageSelected, - ); + ) + .placeholder("Choose a language..."); let mut content = Scrollable::new(&mut self.scroll) .width(Length::Fill) -- cgit From 1b3606884747374f1e5599e3c783f36a2f2cac6f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 22 Jul 2021 20:13:14 +0700 Subject: Introduce `placeholder_color` to `pick_list::Style` --- examples/game_of_life/src/style.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'examples') diff --git a/examples/game_of_life/src/style.rs b/examples/game_of_life/src/style.rs index 6605826f..be9a0e96 100644 --- a/examples/game_of_life/src/style.rs +++ b/examples/game_of_life/src/style.rs @@ -171,6 +171,7 @@ impl pick_list::StyleSheet for PickList { }, border_radius: 2.0, icon_size: 0.5, + ..pick_list::Style::default() } } -- cgit From 72d5d9b4bd03f6596369360018ee101a4a3053ae Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 22 Jul 2021 20:37:12 +0700 Subject: Add newline to `todos` example ... just for readability --- examples/todos/src/main.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'examples') diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index ee0022d5..97415475 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -267,6 +267,7 @@ impl Task { TaskMessage::Edit => { let mut text_input = text_input::State::focused(); text_input.select_all(); + self.state = TaskState::Editing { text_input, delete_button: button::State::new(), -- cgit