Skip to content
Permalink
Browse files
Move the model inside the Widget
  • Loading branch information
antoyo committed Aug 15, 2017
1 parent fd2e5a5 commit 29a85685dad3a5fb70fc989c7be2be50eab8b954
Show file tree
Hide file tree
Showing 50 changed files with 793 additions and 748 deletions.
@@ -64,10 +64,10 @@ impl Widget for Win {
}

// Update the model according to the message received.
fn update(&mut self, event: Msg, model: &mut Model) {
fn update(&mut self, event: Msg) {
match event {
Decrement => model.counter -= 1,
Increment => model.counter += 1,
Decrement => self.model.counter -= 1,
Increment => self.model.counter += 1,
Quit => gtk::main_quit(),
}
}
@@ -86,7 +86,7 @@ impl Widget for Win {
},
gtk::Label {
// Bind the text property of the label to the counter attribute of the model.
text: &model.counter.to_string(),
text: &self.model.counter.to_string(),
},
gtk::Button {
clicked => Decrement,
@@ -63,10 +63,10 @@ impl Widget for Win {
}
}

fn update(&mut self, event: Msg, model: &mut Model) {
fn update(&mut self, event: Msg) {
match event {
Decrement => model.counter -= 1,
Increment => model.counter += 1,
Decrement => self.model.counter -= 1,
Increment => self.model.counter += 1,
Quit => gtk::main_quit(),
}
}
@@ -76,7 +76,7 @@ impl Widget for Win {
gtk::Box {
orientation: Vertical,
gtk::Label {
text: &model.counter.to_string(),
text: &self.model.counter.to_string(),
},
gtk::Button {
clicked => Decrement,
@@ -11,7 +11,7 @@ view! {
},
gtk::Label {
// Bind the text property of the label to the counter attribute of the model.
text: &model.counter.to_string(),
text: &self.model.counter.to_string(),
},
gtk::Button {
clicked => Decrement,
@@ -25,6 +25,9 @@ extern crate relm;
#[macro_use]
extern crate relm_derive;

use std::cell::RefCell;
use std::rc::Rc;

use gtk::{
Button,
ButtonExt,
@@ -38,7 +41,6 @@ use gtk::{
use gtk::Orientation::Vertical;
use relm::{Relm, Widget};

#[derive(Clone)]
struct Model {
counter: i32,
}
@@ -51,9 +53,9 @@ enum Msg {
}

// Create the structure that holds the widgets used in the view.
#[derive(Clone)]
struct Win {
counter_label: Label,
model: Model,
window: Window,
}

@@ -74,28 +76,28 @@ impl Widget for Win {
}

// Return the root widget.
fn root(&self) -> &Self::Root {
&self.window
fn root(&self) -> Self::Root {
self.window.clone()
}

fn update(&mut self, event: Msg, model: &mut Model) {
fn update(&mut self, event: Msg) {
let label = &self.counter_label;

match event {
Msg::Decrement => {
model.counter -= 1;
self.model.counter -= 1;
// Manually update the view.
label.set_text(&model.counter.to_string());
label.set_text(&self.model.counter.to_string());
},
Msg::Increment => {
model.counter += 1;
label.set_text(&model.counter.to_string());
self.model.counter += 1;
label.set_text(&self.model.counter.to_string());
},
Msg::Quit => gtk::main_quit(),
}
}

fn view(relm: Relm<Msg>, _model: &Self::Model) -> Self {
fn view(relm: &Relm<Self>, model: Self::Model) -> Rc<RefCell<Self>> {
// Create the view using the normal GTK+ method calls.
let vbox = gtk::Box::new(Vertical, 0);

@@ -119,10 +121,11 @@ impl Widget for Win {
connect!(relm, minus_button, connect_clicked(_), Msg::Decrement);
connect!(relm, window, connect_delete_event(_, _) (Some(Msg::Quit), Inhibit(false)));

Win {
Rc::new(RefCell::new(Win {
counter_label: counter_label,
model,
window: window,
}
}))
}
}

@@ -51,7 +51,7 @@ impl Widget for Button {
fn model() -> () {
}

fn update(&mut self, _msg: ButtonMsg, _model: &mut ()) {
fn update(&mut self, _msg: ButtonMsg) {
}

view! {
@@ -88,10 +88,10 @@ impl Widget for Win {
}
}

fn update(&mut self, event: Msg, model: &mut Model) {
fn update(&mut self, event: Msg) {
match event {
Decrement => model.counter -= 1,
Increment => model.counter += 1,
Decrement => self.model.counter -= 1,
Increment => self.model.counter += 1,
Quit => gtk::main_quit(),
}
}
@@ -101,7 +101,7 @@ impl Widget for Win {
gtk::Box {
orientation: Vertical,
gtk::Label {
text: &model.counter.to_string(),
text: &self.model.counter.to_string(),
},
gtk::Button {
clicked => Decrement,
@@ -27,6 +27,9 @@ extern crate relm;
#[macro_use]
extern crate relm_derive;

use std::cell::RefCell;
use std::rc::Rc;

use gtk::{
BoxExt,
Cast,
@@ -61,8 +64,8 @@ impl Widget for Button {
fn model(_: ()) -> () {
}

fn root(&self) -> &Self::Root {
&self.button
fn root(&self) -> Self::Root {
self.button.clone()
}

fn on_add<W: IsA<gtk::Widget> + IsA<Object>>(&self, parent: W) {
@@ -77,15 +80,15 @@ impl Widget for Button {
parent.set_child_position(&self.button, 0);
}

fn update(&mut self, _msg: ButtonMsg, _model: &mut ()) {
fn update(&mut self, _msg: ButtonMsg) {
}

fn view(_relm: Relm<ButtonMsg>, _model: &Self::Model) -> Self {
fn view(_relm: &Relm<Self>, _model: Self::Model) -> Rc<RefCell<Self>> {
let button = gtk::Button::new_with_label("+");

Button {
Rc::new(RefCell::new(Button {
button: button,
}
}))
}
}

@@ -109,17 +112,17 @@ impl Widget for Win {
fn model(_: ()) -> () {
}

fn root(&self) -> &Self::Root {
&self.window
fn root(&self) -> Self::Root {
self.window.clone()
}

fn update(&mut self, event: Msg, _model: &mut ()) {
fn update(&mut self, event: Msg) {
match event {
Quit => gtk::main_quit(),
}
}

fn view(relm: Relm<Msg>, _model: &Self::Model) -> Self {
fn view(relm: &Relm<Self>, _model: Self::Model) -> Rc<RefCell<Self>> {
let window = gtk::Window::new(Toplevel);
let vbox = gtk::Box::new(Vertical, 0);
window.add(&vbox);
@@ -131,10 +134,10 @@ impl Widget for Win {
connect!(relm, window, connect_delete_event(_, _) (Some(Msg::Quit), Inhibit(false)));
window.show_all();

Win {
Rc::new(RefCell::new(Win {
button: relm_button,
window: window,
}
}))
}
}

@@ -67,17 +67,17 @@ impl Widget for Win {
relm.connect_exec_ignore_err(stream, Tick);
}

fn update(&mut self, event: Msg, model: &mut Model) {
fn update(&mut self, event: Msg) {
match event {
Tick => model.time = Local::now(),
Tick => self.model.time = Local::now(),
Quit => gtk::main_quit(),
}
}

view! {
gtk::Window {
gtk::Label {
text: &model.time.format("%H:%M:%S").to_string(),
text: &self.model.time.format("%H:%M:%S").to_string(),
},
delete_event(_, _) => (Quit, Inhibit(false)),
}
@@ -29,6 +29,8 @@ extern crate relm;
#[macro_use]
extern crate relm_derive;

use std::cell::RefCell;
use std::rc::Rc;
use std::time::Duration;

use chrono::Local;
@@ -60,16 +62,16 @@ impl Widget for Win {
()
}

fn root(&self) -> &Self::Root {
&self.window
fn root(&self) -> Self::Root {
self.window.clone()
}

fn subscriptions(relm: &Relm<Msg>) {
let stream = Interval::new(Duration::from_secs(1));
relm.connect_exec_ignore_err(stream, Tick);
}

fn update(&mut self, event: Msg, _model: &mut ()) {
fn update(&mut self, event: Msg) {
match event {
Tick => {
let time = Local::now();
@@ -79,7 +81,7 @@ impl Widget for Win {
}
}

fn view(relm: Relm<Msg>, _model: &Self::Model) -> Self {
fn view(relm: &Relm<Self>, _model: Self::Model) -> Rc<RefCell<Self>> {
let label = Label::new(None);

let window = Window::new(WindowType::Toplevel);
@@ -94,8 +96,8 @@ impl Widget for Win {
label: label,
window: window,
};
win.update(Tick, &mut ());
win
win.update(Tick);
Rc::new(RefCell::new(win))
}
}

@@ -27,6 +27,8 @@ extern crate relm;
#[macro_use]
extern crate relm_derive;

use std::cell::RefCell;
use std::rc::Rc;
use std::time::Duration;

use chrono::Local;
@@ -58,16 +60,16 @@ impl Widget for Win {
()
}

fn root(&self) -> &Self::Root {
&self.window
fn root(&self) -> Self::Root {
self.window.clone()
}

fn subscriptions(relm: &Relm<Msg>) {
let stream = Interval::new(Duration::from_secs(1));
relm.connect_exec_ignore_err(stream, Tick);
}

fn update(&mut self, event: Msg, _model: &mut ()) {
fn update(&mut self, event: Msg) {
match event {
Tick(()) => {
let time = Local::now();
@@ -77,7 +79,7 @@ impl Widget for Win {
}
}

fn view(relm: Relm<Msg>, _model: &Self::Model) -> Self {
fn view(relm: &Relm<Self>, _model: Self::Model) -> Rc<RefCell<Self>> {
let label = Label::new(None);

let window = Window::new(WindowType::Toplevel);
@@ -93,8 +95,8 @@ impl Widget for Win {
window: window,
};

win.update(Tick(()), &mut ());
win
win.update(Tick(()));
Rc::new(RefCell::new(win))
}
}

0 comments on commit 29a8568

Please sign in to comment.