Skip to content

Commit

Permalink
Separate the Widget trait into a new Update trait
Browse files Browse the repository at this point in the history
  • Loading branch information
antoyo committed Aug 15, 2017
1 parent a370729 commit 9b83e1e
Show file tree
Hide file tree
Showing 27 changed files with 630 additions and 426 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Expand Up @@ -39,6 +39,10 @@ git = "https://github.com/antoyo/futures-glib-rs"
path = "relm-core" path = "relm-core"
version = "^0.1.1" version = "^0.1.1"


[dependencies.relm-state]
path = "relm-state"
version = "^0.1.0"

[dev-dependencies] [dev-dependencies]
base64 = "^0.4.0" base64 = "^0.4.0"
blake2 = "^0.4.0" blake2 = "^0.4.0"
Expand Down
21 changes: 12 additions & 9 deletions examples/async-callback.rs
Expand Up @@ -40,7 +40,7 @@ use gtk::{
WindowType, WindowType,
}; };
use gtk::Orientation::Vertical; use gtk::Orientation::Vertical;
use relm::{Relm, Resolver, Widget}; use relm::{Relm, Resolver, Update, Widget};


use Msg::*; use Msg::*;


Expand All @@ -62,27 +62,20 @@ struct Win {
window: Window, window: Window,
} }


impl Widget for Win { impl Update for Win {
// Specify the model used for this widget. // Specify the model used for this widget.
type Model = Model; type Model = Model;
// Specify the model parameter used to init the model. // Specify the model parameter used to init the model.
type ModelParam = (); type ModelParam = ();
// Specify the type of the messages sent to the update function. // Specify the type of the messages sent to the update function.
type Msg = Msg; type Msg = Msg;
// Specify the type of the root widget.
type Root = Window;


fn model(_: &Relm<Self>, _: ()) -> Model { fn model(_: &Relm<Self>, _: ()) -> Model {
Model { Model {
counter: 0, counter: 0,
} }
} }


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

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


Expand All @@ -99,6 +92,16 @@ impl Widget for Win {
}, },
} }
} }
}

impl Widget for Win {
// Specify the type of the root widget.
type Root = Window;

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


fn view(relm: &Relm<Self>, model: Self::Model) -> Rc<RefCell<Self>> { fn view(relm: &Relm<Self>, model: Self::Model) -> Rc<RefCell<Self>> {
// Create the view using the normal GTK+ method calls. // Create the view using the normal GTK+ method calls.
Expand Down
21 changes: 12 additions & 9 deletions examples/buttons.rs
Expand Up @@ -39,7 +39,7 @@ use gtk::{
WindowType, WindowType,
}; };
use gtk::Orientation::Vertical; use gtk::Orientation::Vertical;
use relm::{Relm, Widget}; use relm::{Relm, Update, Widget};


struct Model { struct Model {
counter: i32, counter: i32,
Expand All @@ -59,27 +59,20 @@ struct Win {
window: Window, window: Window,
} }


impl Widget for Win { impl Update for Win {
// Specify the model used for this widget. // Specify the model used for this widget.
type Model = Model; type Model = Model;
// Specify the model parameter used to init the model. // Specify the model parameter used to init the model.
type ModelParam = (); type ModelParam = ();
// Specify the type of the messages sent to the update function. // Specify the type of the messages sent to the update function.
type Msg = Msg; type Msg = Msg;
// Specify the type of the root widget.
type Root = Window;


fn model(_: &Relm<Self>, _: ()) -> Model { fn model(_: &Relm<Self>, _: ()) -> Model {
Model { Model {
counter: 0, counter: 0,
} }
} }


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

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


Expand All @@ -96,6 +89,16 @@ impl Widget for Win {
Msg::Quit => gtk::main_quit(), Msg::Quit => gtk::main_quit(),
} }
} }
}

impl Widget for Win {
// Specify the type of the root widget.
type Root = Window;

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


fn view(relm: &Relm<Self>, model: Self::Model) -> Rc<RefCell<Self>> { fn view(relm: &Relm<Self>, model: Self::Model) -> Rc<RefCell<Self>> {
// Create the view using the normal GTK+ method calls. // Create the view using the normal GTK+ method calls.
Expand Down
32 changes: 19 additions & 13 deletions examples/checkboxes.rs
Expand Up @@ -38,7 +38,7 @@ use gtk::{
WindowType, WindowType,
}; };
use gtk::Orientation::Vertical; use gtk::Orientation::Vertical;
use relm::{Component, ContainerWidget, Relm, Widget}; use relm::{Component, ContainerWidget, Relm, Update, Widget};


use self::CheckMsg::*; use self::CheckMsg::*;
use self::Msg::*; use self::Msg::*;
Expand All @@ -61,11 +61,10 @@ struct CheckButton {
relm: Relm<CheckButton>, relm: Relm<CheckButton>,
} }


impl Widget for CheckButton { impl Update for CheckButton {
type Model = CheckModel; type Model = CheckModel;
type ModelParam = &'static str; type ModelParam = &'static str;
type Msg = CheckMsg; type Msg = CheckMsg;
type Root = gtk::CheckButton;


fn model(_: &Relm<Self>, label: &'static str) -> CheckModel { fn model(_: &Relm<Self>, label: &'static str) -> CheckModel {
CheckModel { CheckModel {
Expand All @@ -74,10 +73,6 @@ impl Widget for CheckButton {
} }
} }


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

fn update(&mut self, event: CheckMsg) { fn update(&mut self, event: CheckMsg) {
match event { match event {
Check => { Check => {
Expand All @@ -99,6 +94,14 @@ impl Widget for CheckButton {
}, },
} }
} }
}

impl Widget for CheckButton {
type Root = gtk::CheckButton;

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


fn view(relm: &Relm<Self>, model: Self::Model) -> Rc<RefCell<Self>> { fn view(relm: &Relm<Self>, model: Self::Model) -> Rc<RefCell<Self>> {
let button = gtk::CheckButton::new_with_label(model.label); let button = gtk::CheckButton::new_with_label(model.label);
Expand Down Expand Up @@ -126,19 +129,14 @@ struct Win {
window: Window, window: Window,
} }


impl Widget for Win { impl Update for Win {
type Model = (); type Model = ();
type ModelParam = (); type ModelParam = ();
type Msg = Msg; type Msg = Msg;
type Root = Window;


fn model(_: &Relm<Self>, _: ()) -> () { fn model(_: &Relm<Self>, _: ()) -> () {
} }


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

fn update(&mut self, event: Msg) { fn update(&mut self, event: Msg) {
match event { match event {
Quit => gtk::main_quit(), Quit => gtk::main_quit(),
Expand All @@ -160,6 +158,14 @@ impl Widget for Win {
}, },
} }
} }
}

impl Widget for Win {
type Root = Window;

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


fn view(relm: &Relm<Self>, _model: Self::Model) -> Rc<RefCell<Self>> { fn view(relm: &Relm<Self>, _model: Self::Model) -> Rc<RefCell<Self>> {
let vbox = gtk::Box::new(Vertical, 0); let vbox = gtk::Box::new(Vertical, 0);
Expand Down
30 changes: 18 additions & 12 deletions examples/child-prop.rs
Expand Up @@ -42,7 +42,7 @@ use gtk::{
}; };
use gtk::Orientation::Vertical; use gtk::Orientation::Vertical;
use gtk::WindowType::Toplevel; use gtk::WindowType::Toplevel;
use relm::{Component, ContainerWidget, Relm, Widget}; use relm::{Component, ContainerWidget, Relm, Update, Widget};


use self::Msg::*; use self::Msg::*;


Expand All @@ -54,15 +54,21 @@ struct Button {
button: gtk::Button, button: gtk::Button,
} }


impl Widget for Button { impl Update for Button {
type Model = (); type Model = ();
type ModelParam = (); type ModelParam = ();
type Msg = ButtonMsg; type Msg = ButtonMsg;
type Root = gtk::Button;


fn model(_: &Relm<Self>, _: ()) -> () { fn model(_: &Relm<Self>, _: ()) -> () {
} }


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

impl Widget for Button {
type Root = gtk::Button;

fn root(&self) -> Self::Root { fn root(&self) -> Self::Root {
self.button.clone() self.button.clone()
} }
Expand All @@ -79,9 +85,6 @@ impl Widget for Button {
parent.set_child_position(&self.button, 0); parent.set_child_position(&self.button, 0);
} }


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

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


Expand All @@ -101,24 +104,27 @@ struct Win {
window: gtk::Window, window: gtk::Window,
} }


impl Widget for Win { impl Update for Win {
type Model = (); type Model = ();
type ModelParam = (); type ModelParam = ();
type Msg = Msg; type Msg = Msg;
type Root = gtk::Window;


fn model(_: &Relm<Self>, _: ()) -> () { fn model(_: &Relm<Self>, _: ()) -> () {
} }


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

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

impl Widget for Win {
type Root = gtk::Window;

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


fn view(relm: &Relm<Self>, _model: Self::Model) -> Rc<RefCell<Self>> { fn view(relm: &Relm<Self>, _model: Self::Model) -> Rc<RefCell<Self>> {
let window = gtk::Window::new(Toplevel); let window = gtk::Window::new(Toplevel);
Expand Down
19 changes: 11 additions & 8 deletions examples/clock-nightly.rs
Expand Up @@ -36,7 +36,7 @@ use std::time::Duration;
use chrono::Local; use chrono::Local;
use futures_glib::Interval; use futures_glib::Interval;
use gtk::{ContainerExt, Inhibit, Label, WidgetExt, Window, WindowType}; use gtk::{ContainerExt, Inhibit, Label, WidgetExt, Window, WindowType};
use relm::{Relm, Widget}; use relm::{Relm, Update, Widget};


use self::Msg::*; use self::Msg::*;


Expand All @@ -51,21 +51,16 @@ struct Win {
window: Window, window: Window,
} }


impl Widget for Win { impl Update for Win {
type Model = (); type Model = ();
type ModelParam = (); type ModelParam = ();
type Msg = Msg; type Msg = Msg;
type Root = Window;


fn model(_: &Relm<Self>, _: ()) -> () { fn model(_: &Relm<Self>, _: ()) -> () {
() ()
} }


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

fn subscriptions(relm: &Relm<Msg>) {
let stream = Interval::new(Duration::from_secs(1)); let stream = Interval::new(Duration::from_secs(1));
relm.connect_exec_ignore_err(stream, Tick); relm.connect_exec_ignore_err(stream, Tick);
} }
Expand All @@ -79,6 +74,14 @@ impl Widget for Win {
Quit => gtk::main_quit(), Quit => gtk::main_quit(),
} }
} }
}

impl Widget for Win {
type Root = Window;

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


fn view(relm: &Relm<Self>, _model: Self::Model) -> Rc<RefCell<Self>> { fn view(relm: &Relm<Self>, _model: Self::Model) -> Rc<RefCell<Self>> {
let label = Label::new(None); let label = Label::new(None);
Expand Down
19 changes: 11 additions & 8 deletions examples/clock.rs
Expand Up @@ -34,7 +34,7 @@ use std::time::Duration;
use chrono::Local; use chrono::Local;
use futures_glib::Interval; use futures_glib::Interval;
use gtk::{ContainerExt, Inhibit, Label, WidgetExt, Window, WindowType}; use gtk::{ContainerExt, Inhibit, Label, WidgetExt, Window, WindowType};
use relm::{Relm, Widget}; use relm::{Relm, Update, Widget};


use self::Msg::*; use self::Msg::*;


Expand All @@ -49,21 +49,16 @@ struct Win {
window: Window, window: Window,
} }


impl Widget for Win { impl Update for Win {
type Model = (); type Model = ();
type ModelParam = (); type ModelParam = ();
type Msg = Msg; type Msg = Msg;
type Root = Window;


fn model(_: &Relm<Self>, _: ()) -> () { fn model(_: &Relm<Self>, _: ()) -> () {
() ()
} }


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

fn subscriptions(relm: &Relm<Msg>) {
let stream = Interval::new(Duration::from_secs(1)); let stream = Interval::new(Duration::from_secs(1));
relm.connect_exec_ignore_err(stream, Tick); relm.connect_exec_ignore_err(stream, Tick);
} }
Expand All @@ -77,6 +72,14 @@ impl Widget for Win {
Quit => gtk::main_quit(), Quit => gtk::main_quit(),
} }
} }
}

impl Widget for Win {
type Root = Window;

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


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

0 comments on commit 9b83e1e

Please sign in to comment.