@@ -34,17 +34,12 @@ mod model {
34
34
const NAME : & ' static str = "Model" ;
35
35
type Type = super :: Model ;
36
36
type ParentType = glib:: Object ;
37
+ type Interfaces = ( gio:: ListModel , ) ;
37
38
type Instance = subclass:: simple:: InstanceStruct < Self > ;
38
39
type Class = subclass:: simple:: ClassStruct < Self > ;
39
40
40
41
glib:: object_subclass!( ) ;
41
42
42
- // Called right before class_init and allows a GObject to specify
43
- // which interfaces it implement, in this case gio::ListModel
44
- fn type_init ( type_ : & mut subclass:: InitializingType < Self > ) {
45
- type_. add_interface :: < gio:: ListModel > ( ) ;
46
- }
47
-
48
43
// Called once at the very beginning of instantiation
49
44
fn new ( ) -> Self {
50
45
Self ( RefCell :: new ( Vec :: new ( ) ) )
@@ -306,48 +301,17 @@ mod row_data {
306
301
count : RefCell < u32 > ,
307
302
}
308
303
309
- // GObject property definitions for our two values
310
- static PROPERTIES : [ subclass:: Property ; 2 ] = [
311
- subclass:: Property ( "name" , |name| {
312
- glib:: ParamSpec :: string (
313
- name,
314
- "Name" ,
315
- "Name" ,
316
- None , // Default value
317
- glib:: ParamFlags :: READWRITE ,
318
- )
319
- } ) ,
320
- subclass:: Property ( "count" , |name| {
321
- glib:: ParamSpec :: uint (
322
- name,
323
- "Count" ,
324
- "Count" ,
325
- 0 ,
326
- 100 ,
327
- 0 , // Allowed range and default value
328
- glib:: ParamFlags :: READWRITE ,
329
- )
330
- } ) ,
331
- ] ;
332
-
333
304
// Basic declaration of our type for the GObject type system
334
305
impl ObjectSubclass for RowData {
335
306
const NAME : & ' static str = "RowData" ;
336
307
type Type = super :: RowData ;
337
308
type ParentType = glib:: Object ;
309
+ type Interfaces = ( ) ;
338
310
type Instance = subclass:: simple:: InstanceStruct < Self > ;
339
311
type Class = subclass:: simple:: ClassStruct < Self > ;
340
312
341
313
glib:: object_subclass!( ) ;
342
314
343
- // Called exactly once before the first instantiation of an instance. This
344
- // sets up any type-specific things, in this specific case it installs the
345
- // properties so that GObject knows about their existence and they can be
346
- // used on instances of our type
347
- fn class_init ( klass : & mut Self :: Class ) {
348
- klass. install_properties ( & PROPERTIES ) ;
349
- }
350
-
351
315
// Called once at the very beginning of instantiation of each instance and
352
316
// creates the data structure that contains all our state
353
317
fn new ( ) -> Self {
@@ -365,17 +329,47 @@ mod row_data {
365
329
// This maps between the GObject properties and our internal storage of the
366
330
// corresponding values of the properties.
367
331
impl ObjectImpl for RowData {
368
- fn set_property ( & self , _obj : & Self :: Type , id : usize , value : & glib:: Value ) {
369
- let prop = & PROPERTIES [ id] ;
332
+ fn properties ( ) -> & ' static [ glib:: ParamSpec ] {
333
+ use once_cell:: sync:: Lazy ;
334
+ static PROPERTIES : Lazy < Vec < glib:: ParamSpec > > = Lazy :: new ( || {
335
+ vec ! [
336
+ glib:: ParamSpec :: string(
337
+ "name" ,
338
+ "Name" ,
339
+ "Name" ,
340
+ None , // Default value
341
+ glib:: ParamFlags :: READWRITE ,
342
+ ) ,
343
+ glib:: ParamSpec :: uint(
344
+ "count" ,
345
+ "Count" ,
346
+ "Count" ,
347
+ 0 ,
348
+ 100 ,
349
+ 0 , // Allowed range and default value
350
+ glib:: ParamFlags :: READWRITE ,
351
+ ) ,
352
+ ]
353
+ } ) ;
354
+
355
+ PROPERTIES . as_ref ( )
356
+ }
370
357
371
- match * prop {
372
- subclass:: Property ( "name" , ..) => {
358
+ fn set_property (
359
+ & self ,
360
+ _obj : & Self :: Type ,
361
+ _id : usize ,
362
+ value : & glib:: Value ,
363
+ pspec : & glib:: ParamSpec ,
364
+ ) {
365
+ match pspec. get_name ( ) {
366
+ "name" => {
373
367
let name = value
374
368
. get ( )
375
369
. expect ( "type conformity checked by `Object::set_property`" ) ;
376
370
self . name . replace ( name) ;
377
371
}
378
- subclass :: Property ( "count" , .. ) => {
372
+ "count" => {
379
373
let count = value
380
374
. get_some ( )
381
375
. expect ( "type conformity checked by `Object::set_property`" ) ;
@@ -385,12 +379,15 @@ mod row_data {
385
379
}
386
380
}
387
381
388
- fn get_property ( & self , _obj : & Self :: Type , id : usize ) -> glib:: Value {
389
- let prop = & PROPERTIES [ id] ;
390
-
391
- match * prop {
392
- subclass:: Property ( "name" , ..) => self . name . borrow ( ) . to_value ( ) ,
393
- subclass:: Property ( "count" , ..) => self . count . borrow ( ) . to_value ( ) ,
382
+ fn get_property (
383
+ & self ,
384
+ _obj : & Self :: Type ,
385
+ _id : usize ,
386
+ pspec : & glib:: ParamSpec ,
387
+ ) -> glib:: Value {
388
+ match pspec. get_name ( ) {
389
+ "name" => self . name . borrow ( ) . to_value ( ) ,
390
+ "count" => self . count . borrow ( ) . to_value ( ) ,
394
391
_ => unimplemented ! ( ) ,
395
392
}
396
393
}
0 commit comments