Permalink
Browse files

Move the model inside the Widget

  • Loading branch information...
antoyo committed May 1, 2017
1 parent fd2e5a5 commit 29a85685dad3a5fb70fc989c7be2be50eab8b954
Showing with 793 additions and 748 deletions.
  1. +4 −4 examples/buttons-attribute.rs
  2. +4 −4 examples/buttons-child-attribute.rs
  3. +1 −1 examples/buttons.relm
  4. +15 −12 examples/buttons.rs
  5. +5 −5 examples/child-prop-attribute.rs
  6. +15 −12 examples/child-prop.rs
  7. +3 −3 examples/clock-attribute.rs
  8. +8 −6 examples/clock-nightly.rs
  9. +8 −6 examples/clock.rs
  10. +12 −12 examples/communication-attribute.rs
  11. +56 −49 examples/communication.rs
  12. +6 −6 examples/container-attribute.rs
  13. +21 −18 examples/container.rs
  14. +5 −6 examples/generic-widget-attribute.rs
  15. +21 −19 examples/generic-widget.rs
  16. +3 −3 examples/include.rs
  17. +5 −5 examples/key-events-attribute.rs
  18. +25 −18 examples/key-events.rs
  19. +4 −4 examples/model-param-attribute.rs
  20. +6 −6 examples/model-param-widget-attribute.rs
  21. +17 −12 examples/model-param.rs
  22. +4 −4 examples/model-params-attribute.rs
  23. +8 −8 examples/multi-container-attribute.rs
  24. +36 −33 examples/multi-container.rs
  25. +5 −5 examples/multi-generic-widget-attribute.rs
  26. +20 −16 examples/multi-generic-widget.rs
  27. +8 −8 examples/multiple-widgets-attribute.rs
  28. +36 −29 examples/multiple-widgets.rs
  29. +8 −6 examples/nested-loop.rs
  30. +4 −4 examples/relm-root-attribute.rs
  31. +28 −25 examples/relm-root.rs
  32. +4 −4 examples/simple.rs
  33. +9 −6 examples/sync-callback.rs
  34. +3 −3 examples/text-fields-attribute.rs
  35. +6 −6 examples/text-fields-prop-attribute.rs
  36. +16 −11 examples/text-fields.rs
  37. +21 −16 examples/widget-list.rs
  38. +0 −11 relm-derive/src/lib.rs
  39. +8 −6 relm-gen-widget/src/adder.rs
  40. +63 −81 relm-gen-widget/src/gen.rs
  41. +15 −60 relm-gen-widget/src/lib.rs
  42. +50 −39 relm-gen-widget/src/parser.rs
  43. +60 −0 relm-gen-widget/src/remover.rs
  44. +8 −3 relm-gen-widget/src/walker.rs
  45. +26 −28 src/component.rs
  46. +36 −35 src/container.rs
  47. +36 −61 src/lib.rs
  48. +6 −7 src/macros.rs
  49. +8 −6 src/widget.rs
  50. +17 −16 tests/buttons-test.rs
@@ -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,
View
@@ -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,
View
@@ -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,
View
@@ -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)),
}
View
@@ -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))
}
}
View
@@ -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))
}
}
Oops, something went wrong.

0 comments on commit 29a8568

Please sign in to comment.