Skip to main content

App

Trait App 

Source
pub trait App: 'static {
    type Msg: Send + 'static;

    // Required methods
    fn update(&mut self, msg: Self::Msg) -> Command<Self::Msg>;
    fn view(&self, ui: &mut View<'_, Self::Msg>);

    // Provided methods
    fn action(&self, _name: &str) -> Option<Self::Msg> { ... }
    fn clipboard(&self, _event: &ClipboardEvent) -> Option<Self::Msg> { ... }
}
Expand description

An application built with quvyta-framework: data, a function that draws it and a function that changes it.

An application implements this trait and runs in a Runtime, or in a Harness for tests.

use qframe::prelude::*;

struct Counter {
    value: i32,
}

#[derive(Clone)]
enum Msg {
    Increment,
}

impl App for Counter {
    type Msg = Msg;

    fn update(&mut self, msg: Msg) -> Command<Msg> {
        match msg {
            Msg::Increment => self.value += 1,
        }
        Command::none()
    }

    fn view(&self, ui: &mut View<'_, Msg>) {
        ui.column(|ui| {
            ui.add(Text::new(format!("Value: {}", self.value)));
            ui.add(Button::new("Increment").on_press(Msg::Increment));
        });
    }
}

let mut app = Harness::new(Counter { value: 0 }, 30, 4);
app.press("tab").press("enter");
assert!(app.screen().contains("Value: 1"));

Required Associated Types§

Source

type Msg: Send + 'static

Everything that can happen in the application.

Required Methods§

Source

fn update(&mut self, msg: Self::Msg) -> Command<Self::Msg>

Applies a message and returns work for the runtime to do.

Source

fn view(&self, ui: &mut View<'_, Self::Msg>)

Describes the screen. Runs after every change; must not do I/O.

Provided Methods§

Source

fn action(&self, _name: &str) -> Option<Self::Msg>

Turns an [app] keymap action into a message, e.g. "save" into Msg::Save.

Source

fn clipboard(&self, _event: &ClipboardEvent) -> Option<Self::Msg>

Hears about the clipboard: text a widget or a mouse selection copied, and pasted text that no focused widget took. Copies the application asked for with Command::copy are not reported, so answering a copy with a copy cannot loop.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§