@@ -25,6 +25,9 @@ extern crate relm;
2525#[ macro_use]
2626extern crate relm_derive;
2727
28+ use std:: cell:: RefCell ;
29+ use std:: rc:: Rc ;
30+
2831use gtk:: {
2932 Button ,
3033 ButtonExt ,
@@ -38,7 +41,6 @@ use gtk::{
3841use gtk:: Orientation :: Vertical ;
3942use relm:: { Relm , Widget } ;
4043
41- #[ derive( Clone ) ]
4244struct 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 ) ]
5556struct 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
0 commit comments