@@ -37,6 +37,7 @@ mod imp {
37
37
<arg type='u' name='delay' direction='in'/>
38
38
<arg type='s' name='greet' direction='out'/>
39
39
</method>
40
+ <method name='GoodBye'></method>
40
41
</interface>
41
42
</node>
42
43
"# ;
@@ -56,6 +57,7 @@ mod imp {
56
57
enum HelloMethod {
57
58
Hello ( Hello ) ,
58
59
SlowHello ( SlowHello ) ,
60
+ GoodBye ,
59
61
}
60
62
61
63
impl DBusMethodCall for HelloMethod {
@@ -68,6 +70,7 @@ mod imp {
68
70
match method {
69
71
"Hello" => Ok ( params. get :: < Hello > ( ) . map ( Self :: Hello ) ) ,
70
72
"SlowHello" => Ok ( params. get :: < SlowHello > ( ) . map ( Self :: SlowHello ) ) ,
73
+ "GoodBye" => Ok ( Some ( Self :: GoodBye ) ) ,
71
74
_ => Err ( glib:: Error :: new ( IOErrorEnum :: Failed , "No such method" ) ) ,
72
75
}
73
76
. and_then ( |p| {
@@ -94,24 +97,35 @@ mod imp {
94
97
connection
95
98
. register_object ( "/com/github/gtk_rs/examples/HelloWorld" , & example)
96
99
. typed_method_call :: < HelloMethod > ( )
97
- . invoke_and_return_future_local ( |_, sender, call| {
98
- println ! ( "Method call from {sender:?}" ) ;
99
- async {
100
- match call {
101
- HelloMethod :: Hello ( Hello { name } ) => {
102
- let greet = format ! ( "Hello {name}!" ) ;
103
- println ! ( "{greet}" ) ;
104
- Ok ( Some ( greet. to_variant ( ) ) )
105
- }
106
- HelloMethod :: SlowHello ( SlowHello { name, delay } ) => {
107
- glib:: timeout_future ( Duration :: from_secs ( delay as u64 ) ) . await ;
108
- let greet = format ! ( "Hello {name} after {delay} seconds!" ) ;
109
- println ! ( "{greet}" ) ;
110
- Ok ( Some ( greet. to_variant ( ) ) )
100
+ . invoke_and_return_future_local ( glib:: clone!(
101
+ #[ weak_allow_none( rename_to = app) ]
102
+ self . obj( ) ,
103
+ move |_, sender, call| {
104
+ println!( "Method call from {sender:?}" ) ;
105
+ let app = app. clone( ) ;
106
+ async move {
107
+ match call {
108
+ HelloMethod :: Hello ( Hello { name } ) => {
109
+ let greet = format!( "Hello {name}!" ) ;
110
+ println!( "{greet}" ) ;
111
+ Ok ( Some ( greet. to_variant( ) ) )
112
+ }
113
+ HelloMethod :: SlowHello ( SlowHello { name, delay } ) => {
114
+ glib:: timeout_future( Duration :: from_secs( delay as u64 ) ) . await ;
115
+ let greet = format!( "Hello {name} after {delay} seconds!" ) ;
116
+ println!( "{greet}" ) ;
117
+ Ok ( Some ( greet. to_variant( ) ) )
118
+ }
119
+ HelloMethod :: GoodBye => {
120
+ if let Some ( app) = app {
121
+ app. quit( ) ;
122
+ }
123
+ Ok ( None )
124
+ }
111
125
}
112
126
}
113
127
}
114
- } )
128
+ ) )
115
129
. build ( )
116
130
}
117
131
}
@@ -140,9 +154,9 @@ mod imp {
140
154
Ok ( ( ) )
141
155
}
142
156
143
- fn shutdown ( & self ) {
157
+ fn dbus_unregister ( & self , connection : & DBusConnection , object_path : & str ) {
158
+ self . parent_dbus_unregister ( connection, object_path) ;
144
159
if let Some ( id) = self . registration_id . take ( ) {
145
- let connection = self . obj ( ) . dbus_connection ( ) . expect ( "connection" ) ;
146
160
if connection. unregister_object ( id) . is_ok ( ) {
147
161
println ! ( "Unregistered object" ) ;
148
162
} else {
@@ -151,9 +165,16 @@ mod imp {
151
165
}
152
166
}
153
167
168
+ fn shutdown ( & self ) {
169
+ self . parent_shutdown ( ) ;
170
+ println ! ( "Good bye!" ) ;
171
+ }
172
+
154
173
fn activate ( & self ) {
155
174
println ! ( "Waiting for DBus Hello method to be called. Call the following command from another terminal:" ) ;
156
175
println ! ( "dbus-send --print-reply --dest=com.github.gtk-rs.examples.RegisterDBusObject /com/github/gtk_rs/examples/HelloWorld com.github.gtk_rs.examples.HelloWorld.Hello string:YourName" ) ;
176
+ println ! ( "Quit with the following command:" ) ;
177
+ println ! ( "dbus-send --print-reply --dest=com.github.gtk-rs.examples.RegisterDBusObject /com/github/gtk_rs/examples/HelloWorld com.github.gtk_rs.examples.HelloWorld.GoodBye" ) ;
157
178
}
158
179
}
159
180
}
0 commit comments