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 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