Skip to content

Commit

Permalink
Move the model inside the Widget
Browse files Browse the repository at this point in the history
  • Loading branch information
antoyo committed Aug 15, 2017
1 parent fd2e5a5 commit 29a8568
Show file tree
Hide file tree
Showing 50 changed files with 793 additions and 748 deletions.
8 changes: 4 additions & 4 deletions examples/buttons-attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions examples/buttons-child-attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion examples/buttons.relm
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
27 changes: 15 additions & 12 deletions examples/buttons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -38,7 +41,6 @@ use gtk::{
use gtk::Orientation::Vertical;
use relm::{Relm, Widget};

#[derive(Clone)]
struct Model {
counter: i32,
}
Expand All @@ -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,
}

Expand All @@ -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);

Expand All @@ -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,
}
}))
}
}

Expand Down
10 changes: 5 additions & 5 deletions examples/child-prop-attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down Expand Up @@ -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(),
}
}
Expand All @@ -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,
Expand Down
27 changes: 15 additions & 12 deletions examples/child-prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
}
}))
}
}

Expand All @@ -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);
Expand All @@ -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,
}
}))
}
}

Expand Down
6 changes: 3 additions & 3 deletions examples/clock-attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}
Expand Down
14 changes: 8 additions & 6 deletions examples/clock-nightly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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))
}
}

Expand Down
14 changes: 8 additions & 6 deletions examples/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -93,8 +95,8 @@ impl Widget for Win {
window: window,
};

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

Expand Down
Loading

0 comments on commit 29a8568

Please sign in to comment.