Working state, all from crates.io

This commit is contained in:
Imbus 2024-06-10 10:05:25 +02:00
parent 33713184b4
commit 4da836c535
2 changed files with 15 additions and 7 deletions

View file

@ -4,7 +4,6 @@ version = "0.1.0"
edition = "2021" edition = "2021"
description = "A simple demonstration of the Iced GUI library." description = "A simple demonstration of the Iced GUI library."
license = "MIT" license = "MIT"
#license-file = "LICENSE.txt"
repository = "no" repository = "no"
# This application was created and tested with Rust version 1.81.0-nightly # This application was created and tested with Rust version 1.81.0-nightly

View file

@ -3,6 +3,7 @@ use race::Race;
use iced::widget::{button, column, combo_box, row, text, Column, ComboBox, Text}; use iced::widget::{button, column, combo_box, row, text, Column, ComboBox, Text};
use iced::{Alignment, Padding}; use iced::{Alignment, Padding};
use iced::{Element, Sandbox};
/// Represents the state of our application and handles /// Represents the state of our application and handles
/// message matching and updating the state accordingly. /// message matching and updating the state accordingly.
@ -36,7 +37,9 @@ enum Command {
Closed, Closed,
} }
impl State { impl Sandbox for State {
type Message = Command;
// Constructor for the state, nothing special here. // Constructor for the state, nothing special here.
fn new() -> Self { fn new() -> Self {
Self { Self {
@ -47,6 +50,10 @@ impl State {
} }
} }
fn title(&self) -> String {
String::from("Test Example")
}
// On each update, we recieve a command and update the state accordingly. // On each update, we recieve a command and update the state accordingly.
fn update(&mut self, command: Command) { fn update(&mut self, command: Command) {
match command { match command {
@ -60,7 +67,7 @@ impl State {
// Called every frame to render the user interface. // Called every frame to render the user interface.
// Changes to the state are reflected here. // Changes to the state are reflected here.
fn view(&self) -> Column<Command> { fn view(&self) -> Element<Command> {
// Our combo box // Our combo box
let combo_box: ComboBox<Race, Command> = combo_box::ComboBox::new( let combo_box: ComboBox<Race, Command> = combo_box::ComboBox::new(
&self.races, &self.races,
@ -93,14 +100,16 @@ impl State {
.align_items(Alignment::Start) .align_items(Alignment::Start)
.spacing(10) .spacing(10)
.width(iced::Length::Fill) .width(iced::Length::Fill)
.into()
} }
} }
fn main() { fn main() {
// Iced::run is an entry point for the application. // Iced::run is an entry point for the application.
// It may fail so we match on the result. // It may fail so we match on the result.
match iced::run("Test Example", State::update, State::view) { // match iced::run("Test Example", State::update, State::view) {
Ok(_) => {} // Ok(_) => {}
Err(error) => eprintln!("Error: {}", error), // Err(error) => eprintln!("Error: {}", error),
} // }
State::run(iced::Settings::default());
} }