Skip to content

Commit 29a8568

Browse files
committed
Move the model inside the Widget
1 parent fd2e5a5 commit 29a8568

50 files changed

Lines changed: 793 additions & 748 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/buttons-attribute.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ impl Widget for Win {
6464
}
6565

6666
// Update the model according to the message received.
67-
fn update(&mut self, event: Msg, model: &mut Model) {
67+
fn update(&mut self, event: Msg) {
6868
match event {
69-
Decrement => model.counter -= 1,
70-
Increment => model.counter += 1,
69+
Decrement => self.model.counter -= 1,
70+
Increment => self.model.counter += 1,
7171
Quit => gtk::main_quit(),
7272
}
7373
}
@@ -86,7 +86,7 @@ impl Widget for Win {
8686
},
8787
gtk::Label {
8888
// Bind the text property of the label to the counter attribute of the model.
89-
text: &model.counter.to_string(),
89+
text: &self.model.counter.to_string(),
9090
},
9191
gtk::Button {
9292
clicked => Decrement,

examples/buttons-child-attribute.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ impl Widget for Win {
6363
}
6464
}
6565

66-
fn update(&mut self, event: Msg, model: &mut Model) {
66+
fn update(&mut self, event: Msg) {
6767
match event {
68-
Decrement => model.counter -= 1,
69-
Increment => model.counter += 1,
68+
Decrement => self.model.counter -= 1,
69+
Increment => self.model.counter += 1,
7070
Quit => gtk::main_quit(),
7171
}
7272
}
@@ -76,7 +76,7 @@ impl Widget for Win {
7676
gtk::Box {
7777
orientation: Vertical,
7878
gtk::Label {
79-
text: &model.counter.to_string(),
79+
text: &self.model.counter.to_string(),
8080
},
8181
gtk::Button {
8282
clicked => Decrement,

examples/buttons.relm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ view! {
1111
},
1212
gtk::Label {
1313
// Bind the text property of the label to the counter attribute of the model.
14-
text: &model.counter.to_string(),
14+
text: &self.model.counter.to_string(),
1515
},
1616
gtk::Button {
1717
clicked => Decrement,

examples/buttons.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ extern crate relm;
2525
#[macro_use]
2626
extern crate relm_derive;
2727

28+
use std::cell::RefCell;
29+
use std::rc::Rc;
30+
2831
use gtk::{
2932
Button,
3033
ButtonExt,
@@ -38,7 +41,6 @@ use gtk::{
3841
use gtk::Orientation::Vertical;
3942
use relm::{Relm, Widget};
4043

41-
#[derive(Clone)]
4244
struct Model {
4345
counter: i32,
4446
}
@@ -51,9 +53,9 @@ enum Msg {
5153
}
5254

5355
// Create the structure that holds the widgets used in the view.
54-
#[derive(Clone)]
5556
struct Win {
5657
counter_label: Label,
58+
model: Model,
5759
window: Window,
5860
}
5961

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

7678
// Return the root widget.
77-
fn root(&self) -> &Self::Root {
78-
&self.window
79+
fn root(&self) -> Self::Root {
80+
self.window.clone()
7981
}
8082

81-
fn update(&mut self, event: Msg, model: &mut Model) {
83+
fn update(&mut self, event: Msg) {
8284
let label = &self.counter_label;
8385

8486
match event {
8587
Msg::Decrement => {
86-
model.counter -= 1;
88+
self.model.counter -= 1;
8789
// Manually update the view.
88-
label.set_text(&model.counter.to_string());
90+
label.set_text(&self.model.counter.to_string());
8991
},
9092
Msg::Increment => {
91-
model.counter += 1;
92-
label.set_text(&model.counter.to_string());
93+
self.model.counter += 1;
94+
label.set_text(&self.model.counter.to_string());
9395
},
9496
Msg::Quit => gtk::main_quit(),
9597
}
9698
}
9799

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

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

122-
Win {
124+
Rc::new(RefCell::new(Win {
123125
counter_label: counter_label,
126+
model,
124127
window: window,
125-
}
128+
}))
126129
}
127130
}
128131

examples/child-prop-attribute.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl Widget for Button {
5151
fn model() -> () {
5252
}
5353

54-
fn update(&mut self, _msg: ButtonMsg, _model: &mut ()) {
54+
fn update(&mut self, _msg: ButtonMsg) {
5555
}
5656

5757
view! {
@@ -88,10 +88,10 @@ impl Widget for Win {
8888
}
8989
}
9090

91-
fn update(&mut self, event: Msg, model: &mut Model) {
91+
fn update(&mut self, event: Msg) {
9292
match event {
93-
Decrement => model.counter -= 1,
94-
Increment => model.counter += 1,
93+
Decrement => self.model.counter -= 1,
94+
Increment => self.model.counter += 1,
9595
Quit => gtk::main_quit(),
9696
}
9797
}
@@ -101,7 +101,7 @@ impl Widget for Win {
101101
gtk::Box {
102102
orientation: Vertical,
103103
gtk::Label {
104-
text: &model.counter.to_string(),
104+
text: &self.model.counter.to_string(),
105105
},
106106
gtk::Button {
107107
clicked => Decrement,

examples/child-prop.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ extern crate relm;
2727
#[macro_use]
2828
extern crate relm_derive;
2929

30+
use std::cell::RefCell;
31+
use std::rc::Rc;
32+
3033
use gtk::{
3134
BoxExt,
3235
Cast,
@@ -61,8 +64,8 @@ impl Widget for Button {
6164
fn model(_: ()) -> () {
6265
}
6366

64-
fn root(&self) -> &Self::Root {
65-
&self.button
67+
fn root(&self) -> Self::Root {
68+
self.button.clone()
6669
}
6770

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

80-
fn update(&mut self, _msg: ButtonMsg, _model: &mut ()) {
83+
fn update(&mut self, _msg: ButtonMsg) {
8184
}
8285

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

86-
Button {
89+
Rc::new(RefCell::new(Button {
8790
button: button,
88-
}
91+
}))
8992
}
9093
}
9194

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

112-
fn root(&self) -> &Self::Root {
113-
&self.window
115+
fn root(&self) -> Self::Root {
116+
self.window.clone()
114117
}
115118

116-
fn update(&mut self, event: Msg, _model: &mut ()) {
119+
fn update(&mut self, event: Msg) {
117120
match event {
118121
Quit => gtk::main_quit(),
119122
}
120123
}
121124

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

134-
Win {
137+
Rc::new(RefCell::new(Win {
135138
button: relm_button,
136139
window: window,
137-
}
140+
}))
138141
}
139142
}
140143

examples/clock-attribute.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,17 @@ impl Widget for Win {
6767
relm.connect_exec_ignore_err(stream, Tick);
6868
}
6969

70-
fn update(&mut self, event: Msg, model: &mut Model) {
70+
fn update(&mut self, event: Msg) {
7171
match event {
72-
Tick => model.time = Local::now(),
72+
Tick => self.model.time = Local::now(),
7373
Quit => gtk::main_quit(),
7474
}
7575
}
7676

7777
view! {
7878
gtk::Window {
7979
gtk::Label {
80-
text: &model.time.format("%H:%M:%S").to_string(),
80+
text: &self.model.time.format("%H:%M:%S").to_string(),
8181
},
8282
delete_event(_, _) => (Quit, Inhibit(false)),
8383
}

examples/clock-nightly.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ extern crate relm;
2929
#[macro_use]
3030
extern crate relm_derive;
3131

32+
use std::cell::RefCell;
33+
use std::rc::Rc;
3234
use std::time::Duration;
3335

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

63-
fn root(&self) -> &Self::Root {
64-
&self.window
65+
fn root(&self) -> Self::Root {
66+
self.window.clone()
6567
}
6668

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

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

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

8587
let window = Window::new(WindowType::Toplevel);
@@ -94,8 +96,8 @@ impl Widget for Win {
9496
label: label,
9597
window: window,
9698
};
97-
win.update(Tick, &mut ());
98-
win
99+
win.update(Tick);
100+
Rc::new(RefCell::new(win))
99101
}
100102
}
101103

examples/clock.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ extern crate relm;
2727
#[macro_use]
2828
extern crate relm_derive;
2929

30+
use std::cell::RefCell;
31+
use std::rc::Rc;
3032
use std::time::Duration;
3133

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

61-
fn root(&self) -> &Self::Root {
62-
&self.window
63+
fn root(&self) -> Self::Root {
64+
self.window.clone()
6365
}
6466

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

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

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

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

96-
win.update(Tick(()), &mut ());
97-
win
98+
win.update(Tick(()));
99+
Rc::new(RefCell::new(win))
98100
}
99101
}
100102

0 commit comments

Comments
 (0)