~boringcactus/survey-of-rust-gui-libraries

db58977a26df8b3bb2b634387062749cc702ee19 — Melody Horn 2 years ago 97fc46e
finish iced test
2 files changed, 103 insertions(+), 37 deletions(-)

A iced-test/index.html
M iced-test/src/main.rs
A iced-test/index.html => iced-test/index.html +25 -0
@@ 0,0 1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
    <title>iced-test</title>
    <style>
        html,
        body,
        canvas {
            margin: 0px;
            padding: 0px;
            width: 100%;
            height: 100%;
            overflow: hidden;
        }
    </style>
</head>
<body>
<canvas id="canvas"></canvas>
<script src="pkg/iced-test.js"></script>
<script>
    wasm_bindgen('./pkg/iced-test_bg.wasm');
</script>
</body>
</html>

M iced-test/src/main.rs => iced-test/src/main.rs +78 -37
@@ 1,56 1,97 @@
use iced::button;
use iced::{button, text_input};

struct Counter {
    // The counter value
    value: i32,
struct TodoItem {
    name: String,
    done: bool,
}

impl TodoItem {
    fn new<S: Into<String>>(name: S, done: bool) -> Self {
        TodoItem {
            name: name.into(),
            done
        }
    }
}

    // The local state of the two buttons
    increment_button: button::State,
    decrement_button: button::State,
struct Todos {
    input: text_input::State,
    input_value: String,
    add_button: button::State,
    tasks: Vec<TodoItem>,
}

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone)]
pub enum Message {
    IncrementPressed,
    DecrementPressed,
    EditTaskText(String),
    CreateTask,
    SetTaskDone(usize, bool),
}

use iced::{Button, Column, Text};
use iced::{Button, Checkbox, Column, Element, Row, Sandbox, Settings, Text, TextInput};

impl Counter {
    pub fn view(&mut self) -> Column<Message> {
        // We use a column: a simple vertical layout
        Column::new()
            .push(
                // The increment button. We tell it to produce an
                // `IncrementPressed` message when pressed
                Button::new(&mut self.increment_button, Text::new("+"))
                    .on_press(Message::IncrementPressed),
            )
            .push(
                // We show the value of the counter here
                Text::new(&self.value.to_string()).size(50),
            )
            .push(
                // The decrement button. We tell it to produce a
                // `DecrementPressed` message when pressed
                Button::new(&mut self.decrement_button, Text::new("-"))
                    .on_press(Message::DecrementPressed),
            )
impl Sandbox for Todos {
    type Message = Message;

    fn new() -> Self {
        Todos {
            input: Default::default(),
            input_value: String::new(),
            add_button: Default::default(),
            tasks: vec![
                TodoItem::new("Write example", false),
                TodoItem::new("Be cool", true),
            ],
        }
    }

    fn title(&self) -> String {
        String::from("iced todo list")
    }

    pub fn update(&mut self, message: Message) {
    fn update(&mut self, message: Message) {
        match message {
            Message::IncrementPressed => {
                self.value += 1;
            Message::EditTaskText(val) => {
                self.input_value = val.clone();
            }
            Message::DecrementPressed => {
                self.value -= 1;
            Message::CreateTask => {
                let task_text = std::mem::replace(&mut self.input_value, String::new());
                self.tasks.push(TodoItem::new(task_text, false));
            }
            Message::SetTaskDone(index, done) => {
                self.tasks[index].done = done;
            }
        }
    }

    fn view(&mut self) -> Element<Message> {
        let new_item_row = Row::new()
            .push(
                TextInput::new(&mut self.input, "New todo", &self.input_value, Message::EditTaskText)
                    .on_submit(Message::CreateTask)
            )
            .push(
                Button::new(&mut self.add_button, Text::new("Add"))
                    .on_press(Message::CreateTask)
            );
        let mut result = Column::new()
            .push(
                new_item_row
            );
        for (i, task) in self.tasks.iter().enumerate() {
            let task_row = Row::new()
                .push(
                    Text::new(&task.name)
                )
                .push(
                    Checkbox::new(task.done, "", move |done| Message::SetTaskDone(i, done))
                );
            result = result.push(task_row);
        }
        result.into()
    }
}

fn main() {
    println!("Hello, world!");
    Todos::run(Settings::default());
}