Change the exaple to used named struct for models

This commit is contained in:
Olivier Goffart 2020-09-17 14:00:50 +02:00
parent bfe2bf2478
commit 221bb853d7
5 changed files with 26 additions and 19 deletions

View file

@ -9,15 +9,13 @@
LICENSE END */
#include "printerdemo.h"
// FIXME: Ideally it should be a better type in the generated code
using InkData = std::tuple<sixtyfps::Color, float>;
struct InkLevelModel : sixtyfps::Model<InkData>
struct InkLevelModel : sixtyfps::Model<InkLevel>
{
int row_count() const override { return m_data.size(); }
InkData row_data(int i) const override { return m_data[i]; }
InkLevel row_data(int i) const override { return m_data[i]; }
std::vector<InkData> m_data = { { sixtyfps::Color::from_rgb_uint8(255, 255, 0), 0.9 },
std::vector<InkLevel> m_data = { { sixtyfps::Color::from_rgb_uint8(255, 255, 0), 0.9 },
{ sixtyfps::Color::from_rgb_uint8(0, 255, 255), 0.5 },
{ sixtyfps::Color::from_rgb_uint8(255, 0, 255), 0.8 },
{ sixtyfps::Color::from_rgb_uint8(0, 0, 0), 0.1 } };

View file

@ -22,10 +22,10 @@ pub fn main() {
let main_window = MainWindow::new();
// FIXME: better represtation of the models
main_window.set_ink_levels(sixtyfps::VecModel::from_slice(&[
(sixtyfps::Color::from_rgb_u8(0, 255, 255), 0.40),
(sixtyfps::Color::from_rgb_u8(255, 0, 255), 0.20),
(sixtyfps::Color::from_rgb_u8(255, 255, 0), 0.50),
(sixtyfps::Color::from_rgb_u8(0, 0, 0), 0.80),
InkLevel { color: sixtyfps::Color::from_rgb_u8(0, 255, 255), level: 0.40 },
InkLevel { color: sixtyfps::Color::from_rgb_u8(255, 0, 255), level: 0.20 },
InkLevel { color: sixtyfps::Color::from_rgb_u8(255, 255, 0), level: 0.50 },
InkLevel { color: sixtyfps::Color::from_rgb_u8(0, 0, 0), level: 0.80 },
]));
main_window.run();

View file

@ -258,6 +258,11 @@ TopPanel := Rectangle {
}
}
export InkLevel := {
property<color> color;
property<float> level;
}
MainWindow := Window {
width: 800lx;
@ -265,7 +270,7 @@ MainWindow := Window {
/// Note that this property is overwriten in the .cpp and .rs code.
// The data is only in this file so it looks good in the viewer
property <[{color: color, level: float}]> ink_levels: [
property <[InkLevel]> ink_levels: [
{color: #0ff, level: 60%},
{color: #ff0, level: 80%},
{color: #f0f, level: 70%},

View file

@ -22,19 +22,18 @@ pub fn main() {
#[cfg(all(debug_assertions, target_arch = "wasm32"))]
console_error_panic_hook::set_once();
type TodoModelData = (bool, sixtyfps::SharedString);
let todo_model = Rc::new(sixtyfps::VecModel::<TodoModelData>::from(vec![
(true, "Implement the .60 file".into()),
(true, "Do the rust part".into()),
(false, "Make the C++ code".into()),
(false, "???".into()),
(false, "Profit".into()),
let todo_model = Rc::new(sixtyfps::VecModel::<TodoItem>::from(vec![
TodoItem { checked: true, title: "Implement the .60 file".into() },
TodoItem { checked: true, title: "Do the rust part".into() },
TodoItem { checked: false, title: "Make the C++ code".into() },
TodoItem { checked: false, title: "???".into() },
TodoItem { checked: false, title: "Profit".into() },
]));
let main_window = MainWindow::new();
main_window.as_ref().on_todo_added({
let todo_model = todo_model.clone();
move |text| todo_model.push((true, text))
move |text| todo_model.push(TodoItem { checked: true, title: text })
});
main_window.set_todo_model(Some(todo_model));

View file

@ -10,12 +10,17 @@ LICENSE END */
import { SpinBox, Button, CheckBox, Slider } from "sixtyfps_widgets.60";
export TodoItem := {
property <string> title;
property <bool> checked;
}
MainWindow := Window {
width: 400lx;
height: 600lx;
signal todo_added(string);
property <[{title: string, checked: bool}]> todo_model: [
property <[TodoItem]> todo_model: [
{ title: "Implement the .60 file", checked: true },
{ title: "Do the rust part", checked: false },
{ title: "Make the C++ code", checked: false },