21
21
// Imports
22
22
use adwaita:: { gio, glib, prelude:: * , subclass:: prelude:: * , ActionRow } ;
23
23
use gio:: Settings ;
24
- use glib:: { once_cell:: sync:: OnceCell , signal:: Inhibit , subclass:: InitializingObject } ;
24
+ use glib:: { once_cell:: sync:: OnceCell , signal:: Inhibit , subclass:: InitializingObject , FromVariant , once_cell :: sync :: Lazy , ParamSpec , Value } ;
25
25
use gtk:: { subclass:: prelude:: * , CompositeTemplate , ListBox , TemplateChild } ;
26
26
use std:: { cell:: Cell , cell:: RefCell , rc:: Rc } ;
27
27
@@ -40,7 +40,6 @@ pub struct SettingsWindowContainer {
40
40
#[ template( resource = "/main-window.ui" ) ]
41
41
pub struct MainWindow {
42
42
pub settings : OnceCell < Settings > ,
43
- pub app_id : Cell < String > ,
44
43
pub settings_window : Rc < RefCell < SettingsWindowContainer > > ,
45
44
pub provider : Cell < Option < Provider > > ,
46
45
@@ -89,6 +88,61 @@ impl ObjectSubclass for MainWindow {
89
88
*/
90
89
#[ gtk:: template_callbacks]
91
90
impl MainWindow {
91
+ /*
92
+ * Name:
93
+ * update_setting
94
+ *
95
+ * Description:
96
+ * Generic function for updating setting values
97
+ *
98
+ * Made:
99
+ * 30/10/2022
100
+ *
101
+ * Made by:
102
+ * Deren Vural
103
+ *
104
+ * Notes:
105
+ *
106
+ */
107
+ pub fn update_setting < T : ToVariant > ( & self , name : & str , value : T ) {
108
+ // Fetch settings
109
+ match self . settings . get ( ) {
110
+ Some ( settings) => {
111
+ match settings. set ( name, & value) {
112
+ Ok ( _) => println ! ( "..Setting `{}` updated!" , name) ,
113
+ Err ( err) => panic ! ( "..Cannot update `{}` setting: `{}`" , name, err) ,
114
+ }
115
+ } ,
116
+ None => panic ! ( "..Cannot retrieve settings" )
117
+ }
118
+ }
119
+
120
+ /*
121
+ * Name:
122
+ * get_setting
123
+ *
124
+ * Description:
125
+ * Generic function for getting setting value
126
+ *
127
+ * Made:
128
+ * 30/10/2022
129
+ *
130
+ * Made by:
131
+ * Deren Vural
132
+ *
133
+ * Notes:
134
+ *
135
+ */
136
+ pub fn get_setting < T : FromVariant > ( & self , name : & str ) -> T {
137
+ // Return the value of the property
138
+ match self . settings . get ( ) {
139
+ Some ( settings) => {
140
+ settings. get :: < T > ( name)
141
+ } ,
142
+ None => panic ! ( "`settings` should be set in `setup_settings`." )
143
+ }
144
+ }
145
+
92
146
/*
93
147
* Name:
94
148
* card_selected
@@ -379,6 +433,8 @@ impl MainWindow {
379
433
}
380
434
}
381
435
436
+
437
+
382
438
/*
383
439
* Name:
384
440
* refresh_cards
@@ -397,9 +453,11 @@ impl MainWindow {
397
453
*/
398
454
#[ template_callback]
399
455
fn refresh_cards ( & self , button : & CustomButton ) {
400
- //TEST: Grab button label
401
- let label_val = button. label ( ) . expect ( "cannot grab label of refresh button" ) ;
402
- println ! ( "Button Pressed: {}" , label_val) ;
456
+ //TEST
457
+ match button. label ( ) {
458
+ Some ( label_val) => println ! ( "Button Pressed: {}" , label_val) ,
459
+ None => panic ! ( "..Cannot grab label of refresh button" )
460
+ }
403
461
404
462
// Clear current ActionRow objects from GtkListBox
405
463
let mut done: bool = false ;
@@ -434,41 +492,37 @@ impl MainWindow {
434
492
435
493
// Update each property
436
494
match existing_provider. update_property_value :: < i32 > ( "gpu-count" , gpu_count) {
437
- Ok ( _result) => { }
495
+ Ok ( _) => {
496
+ // Construct a row for each GPU
497
+ for uuid in gpu_uuids {
498
+ // Get GPU data
499
+ let gpu_name = "GPU NAME XXQ" ; //existing_provider.get_gpu_data(uuid, "name");
500
+
501
+ // Create new ActionRow object
502
+ let current_row: ActionRow =
503
+ ActionRow :: builder ( )
504
+ . title ( gpu_name)
505
+ . subtitle ( & uuid)
506
+ . activatable ( true )
507
+ . selectable ( true )
508
+ . build ( ) ;
509
+
510
+ // Append new ActionRow object to GtkListBox
511
+ self . cards_list . append ( & current_row) ;
512
+ }
513
+ }
438
514
Err ( err) => println ! ( "..Attempt to read GPU data failed, returning: {}" , err) ,
439
515
}
440
-
441
- // Construct a row for each GPU
442
- for uuid in gpu_uuids {
443
- // Get GPU data
444
- let gpu_name = "GPU NAME XXQ" ; //existing_provider.get_gpu_data(uuid, "name");
445
-
446
- // Create new ActionRow object
447
- let current_row: ActionRow =
448
- ActionRow :: builder ( )
449
- . title ( gpu_name)
450
- . subtitle ( & uuid)
451
- . activatable ( true )
452
- . selectable ( true )
453
- . build ( ) ;
454
-
455
- // Append new ActionRow object to GtkListBox
456
- self . cards_list . append ( & current_row) ;
457
- }
458
516
}
459
- Err ( err) => println ! ( "..Attempt to read GPU data failed, returning: {}" , err) ,
517
+ Err ( err) => println ! ( "..Attempt to update GPU list failed, returning: {}" , err) ,
460
518
}
461
519
462
520
// Re-Store provider
463
521
self . provider . set ( Some ( existing_provider) ) ;
464
522
}
465
523
None => {
466
524
// Check provider type
467
- let provider_type: i32 = self
468
- . settings
469
- . get ( )
470
- . expect ( "..cannot fetch settings" )
471
- . int ( "provider" ) ;
525
+ let provider_type: i32 = self . get_setting :: < i32 > ( "provider" ) ;
472
526
473
527
let new_provider: Provider = MainWindow :: create_provider ( provider_type) ;
474
528
@@ -480,29 +534,29 @@ impl MainWindow {
480
534
481
535
// Update each property
482
536
match new_provider. update_property_value :: < i32 > ( "gpu-count" , gpu_count) {
483
- Ok ( _result) => { }
537
+ Ok ( _) => {
538
+ // Construct a row for each GPU
539
+ for uuid in gpu_uuids {
540
+ // Get GPU data
541
+ let gpu_name = "GPU NAME QXX" ; //existing_provider.get_gpu_data(uuid, "name");
542
+
543
+ // Create new ActionRow object
544
+ let current_row: ActionRow =
545
+ ActionRow :: builder ( )
546
+ . title ( gpu_name)
547
+ . subtitle ( & uuid)
548
+ . activatable ( true )
549
+ . selectable ( true )
550
+ . build ( ) ;
551
+
552
+ // Append new ActionRow object to GtkListBox
553
+ self . cards_list . append ( & current_row) ;
554
+ }
555
+ }
484
556
Err ( err) => println ! ( "..Attempt to read GPU data failed, returning: {}" , err) ,
485
557
}
486
-
487
- // Construct a row for each GPU
488
- for uuid in gpu_uuids {
489
- // Get GPU data
490
- let gpu_name = "GPU NAME QXX" ; //existing_provider.get_gpu_data(uuid, "name");
491
-
492
- // Create new ActionRow object
493
- let current_row: ActionRow =
494
- ActionRow :: builder ( )
495
- . title ( gpu_name)
496
- . subtitle ( & uuid)
497
- . activatable ( true )
498
- . selectable ( true )
499
- . build ( ) ;
500
-
501
- // Append new ActionRow object to GtkListBox
502
- self . cards_list . append ( & current_row) ;
503
- }
504
558
}
505
- Err ( err) => println ! ( "..Attempt to read GPU data failed, returning: {}" , err) ,
559
+ Err ( err) => println ! ( "..Attempt to update GPU list failed, returning: {}" , err) ,
506
560
}
507
561
508
562
// Store new provider
@@ -539,6 +593,105 @@ impl ObjectImpl for MainWindow {
539
593
obj. setup_callbacks ( ) ;
540
594
obj. setup_actions ( ) ;
541
595
}
596
+
597
+ /*
598
+ * Name:
599
+ * properties
600
+ *
601
+ * Description:
602
+ * Create list of custom properties for our GObject
603
+ *
604
+ * Made:
605
+ * 06/10/2022
606
+ *
607
+ * Made by:
608
+ * Deren Vural
609
+ *
610
+ * Notes:
611
+ * beware that you need to use kebab-case (https://en.wikipedia.org/wiki/Letter_case#Kebab_case)
612
+ *
613
+ * ParamSpec Examples:
614
+ * glib::ParamSpecString::builder("icon").build(),
615
+ * glib::ParamSpecUInt::builder("gpu_count").build(),
616
+ * glib::ParamSpecString::builder("call_extension").build(),
617
+ * TODO: these are from property class
618
+ * glib::ParamSpecBoxed::builder("processor").build(),
619
+ * glib::ParamSpecObject::builder("formatter").build(),
620
+ */
621
+ fn properties ( ) -> & ' static [ ParamSpec ] {
622
+ static PROPERTIES : Lazy < Vec < ParamSpec > > = Lazy :: new ( || {
623
+ vec ! [
624
+ glib:: ParamSpecObject :: builder( "provider" , glib:: Type :: OBJECT ) . build( ) ,
625
+ ]
626
+ } ) ;
627
+
628
+ //println!("PROPERTIES: {:?}", PROPERTIES);//TEST
629
+ //println!("trying to add `base_call`: {:?}", glib::ParamSpecString::builder("base_call").build());//TEST
630
+
631
+ PROPERTIES . as_ref ( )
632
+ }
633
+
634
+ /*
635
+ * Name:
636
+ * set_property
637
+ *
638
+ * Description:
639
+ * Mutator for custom GObject properties
640
+ *
641
+ * Made:
642
+ * 06/10/2022
643
+ *
644
+ * Made by:
645
+ * Deren Vural
646
+ *
647
+ * Notes:
648
+ *
649
+ */
650
+ fn set_property ( & self , _obj : & Self :: Type , _id : usize , value : & Value , pspec : & ParamSpec ) {
651
+ //println!("setting: {:?}", pspec.name());//TEST
652
+
653
+ match pspec. name ( ) {
654
+ "provider" => {
655
+ let input_provider_property: Option < Provider > = value
656
+ . get ( )
657
+ . expect ( "The value needs to be of type `Provider`." ) ;
658
+ self . provider . replace ( input_provider_property) ;
659
+ }
660
+ _ => panic ! ( "Property `{}` does not exist.." , pspec. name( ) )
661
+ }
662
+ }
663
+
664
+ /*
665
+ * Name:
666
+ * property
667
+ *
668
+ * Description:
669
+ * Accessor for custom GObject properties
670
+ *
671
+ * Made:
672
+ * 06/10/2022
673
+ *
674
+ * Made by:
675
+ * Deren Vural
676
+ *
677
+ * Notes:
678
+ *
679
+ */
680
+ fn property ( & self , _obj : & Self :: Type , _id : usize , pspec : & ParamSpec ) -> Value {
681
+ //println!("getting: {:?}", pspec.name());//TEST
682
+
683
+ match pspec. name ( ) {
684
+ "provider" => {
685
+ //TODO: this seems ridiculous..
686
+ let value: Option < Provider > = self . provider . take ( ) ;
687
+
688
+ self . provider . set ( value. clone ( ) ) ;
689
+
690
+ value. to_value ( )
691
+ }
692
+ _ => panic ! ( "Property `{}` does not exist.." , pspec. name( ) )
693
+ }
694
+ }
542
695
}
543
696
544
697
/*
@@ -594,13 +747,8 @@ impl WindowImpl for MainWindow {
594
747
595
748
*/
596
749
// Set state in settings
597
- let settings: & Settings = window. settings ( ) ;
598
- settings
599
- . set_boolean ( "app-settings-open" , false )
600
- . expect ( "Could not set setting." ) ;
601
- settings
602
- . set_boolean ( "nvidia-settings-open" , false )
603
- . expect ( "Could not set setting." ) ;
750
+ self . update_setting ( "app-settings-open" , false ) ;
751
+ self . update_setting ( "nvidia-settings-open" , false ) ;
604
752
605
753
// Pass close request on to the parent
606
754
self . parent_close_request ( window)
0 commit comments