Last active
August 13, 2020 19:42
-
-
Save Nnubes256/0f51b766da74df17782acf062501f212 to your computer and use it in GitHub Desktop.
Rust GUI with iced: expanded counter demo, for personal testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "iced-playground" | |
version = "0.1.0" | |
authors = ["Ignacio <nnubes256@gmail.com>"] | |
edition = "2018" | |
[dependencies] | |
iced = "0.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use iced::{self, Sandbox, Element, Column, Row, Container, Align, Button, Text, Settings, Length, | |
HorizontalAlignment}; | |
type ButtonState = iced::button::State; | |
#[derive(Default)] | |
struct CounterExample { | |
// The value of our counter | |
value: i64, | |
// Button states | |
increment_one_button: ButtonState, | |
increment_ten_button: ButtonState, | |
decrement_one_button: ButtonState, | |
decrement_ten_button: ButtonState, | |
} | |
#[derive(Debug, Copy, Clone)] | |
enum Message { | |
IncrementPressed { amount: i64 }, | |
DecrementPressed { amount: i64 } | |
} | |
impl Sandbox for CounterExample { | |
type Message = Message; | |
fn new() -> Self { | |
CounterExample::default() | |
} | |
fn title(&self) -> String { | |
String::from("Cool Gamer Counter App") | |
} | |
fn view(&mut self) -> Element<Self::Message> { | |
let counter = Row::new() | |
.padding(20) | |
.spacing(10) | |
.align_items(Align::Center) | |
.push( | |
Button::new(&mut self.decrement_ten_button, Text::new("-10").horizontal_alignment(HorizontalAlignment::Center)) | |
.width(Length::FillPortion(1)) | |
.padding(10) | |
.on_press(Message::DecrementPressed { amount: 10 }) | |
) | |
.push( | |
Button::new(&mut self.decrement_one_button, Text::new("-1").horizontal_alignment(HorizontalAlignment::Center)) | |
.width(Length::FillPortion(1)) | |
.padding(10) | |
.on_press(Message::DecrementPressed { amount: 1 }) | |
) | |
.push(Row::new() | |
.width(Length::FillPortion(4)) | |
.push( | |
Text::new(self.value.to_string()) | |
.size(50) | |
.width(Length::Fill) | |
.horizontal_alignment(HorizontalAlignment::Center) | |
) | |
) | |
.push( | |
Button::new(&mut self.increment_one_button, Text::new("+1").horizontal_alignment(HorizontalAlignment::Center)) | |
.width(Length::FillPortion(1)) | |
.padding(10) | |
.on_press(Message::IncrementPressed { amount: 1 }) | |
) | |
.push( | |
Button::new(&mut self.increment_ten_button, Text::new("+10").horizontal_alignment(HorizontalAlignment::Center)) | |
.width(Length::FillPortion(1)) | |
.padding(10) | |
.on_press(Message::IncrementPressed { amount: 10 }) | |
); | |
let mut content: Column<Self::Message> = Column::new() | |
.padding(40) | |
.align_items(Align::Start) | |
.push(counter) | |
.into(); | |
if self.value % 3 == 0 || self.value % 5 == 0 { | |
let fizzbuzz_text = { | |
let mut txt = String::with_capacity(8); | |
if self.value % 3 == 0 { | |
txt.push_str("Fizz"); | |
} | |
if self.value % 5 == 0 { | |
txt.push_str("Buzz"); | |
} | |
txt | |
}; | |
content = content.push( | |
Row::new() | |
.padding(20) | |
.push(Text::new(fizzbuzz_text).size(50)) | |
); | |
} | |
Container::new(content) | |
.height(Length::Fill) | |
.width(Length::Fill) | |
.center_x() | |
.into() | |
} | |
fn update(&mut self, message: Self::Message) { | |
match message { | |
Message::IncrementPressed { amount } => { | |
self.value += amount; | |
}, | |
Message::DecrementPressed { amount } => { | |
self.value -= amount; | |
} | |
} | |
} | |
} | |
fn main() { | |
CounterExample::run(Settings::default()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment