19
19
*/
20
20
21
21
// Imports
22
- use adwaita:: { gio, glib, prelude:: * , subclass:: prelude:: * } ;
22
+ use adwaita:: { gio, glib, prelude:: * , subclass:: prelude:: * , ActionRow } ;
23
23
use gio:: Settings ;
24
24
use glib:: { once_cell:: sync:: OnceCell , signal:: Inhibit , subclass:: InitializingObject } ;
25
- use gtk:: { subclass:: prelude:: * , CompositeTemplate } ; //, Entry , ListBox, TemplateChild };
25
+ use gtk:: { subclass:: prelude:: * , CompositeTemplate , TemplateChild , ListBox } ;
26
26
use std:: { cell:: Cell , cell:: RefCell , rc:: Rc } ;
27
27
28
+ use crate :: formatter:: Formatter ;
29
+ use crate :: processor:: Processor ;
30
+ use crate :: property:: Property ;
31
+ use crate :: provider:: Provider ;
28
32
// Modules
29
33
//use crate::utils::data_path;
30
34
use crate :: settingswindow:: SettingsWindow ;
35
+ use crate :: custom_button:: CustomButton ;
31
36
32
37
// Structure for storing SettingsWindow and info
33
38
pub struct SettingsWindowContainer {
34
39
pub window : Option < SettingsWindow > ,
35
40
pub open : bool ,
36
- //... other values associated to that window
37
41
}
38
42
impl Default for SettingsWindowContainer {
39
43
fn default ( ) -> Self {
@@ -51,6 +55,13 @@ pub struct MainWindow {
51
55
pub settings : OnceCell < Settings > ,
52
56
pub app_id : Cell < String > ,
53
57
pub settings_window : Rc < RefCell < SettingsWindowContainer > > ,
58
+ pub provider : Cell < Option < Provider > > ,
59
+
60
+ #[ template_child]
61
+ pub cards_list : TemplateChild < ListBox > ,
62
+
63
+ #[ template_child]
64
+ pub refresh_button : TemplateChild < CustomButton > ,
54
65
}
55
66
56
67
// The central trait for subclassing a GObject
@@ -63,13 +74,201 @@ impl ObjectSubclass for MainWindow {
63
74
64
75
fn class_init ( klass : & mut Self :: Class ) {
65
76
klass. bind_template ( ) ;
77
+ klass. bind_template_callbacks ( ) ;
66
78
}
67
79
68
80
fn instance_init ( obj : & InitializingObject < Self > ) {
69
81
obj. init_template ( ) ;
70
82
}
71
83
}
72
84
85
+ /*
86
+ * Name:
87
+ * MainWindow
88
+ *
89
+ * Description:
90
+ * Trait shared by all MainWindow objects
91
+ *
92
+ * Made:
93
+ * 13/10/2022
94
+ *
95
+ * Made by:
96
+ * Deren Vural
97
+ *
98
+ * Notes:
99
+ *
100
+ */
101
+ #[ gtk:: template_callbacks]
102
+ impl MainWindow {
103
+ #[ template_callback]
104
+ fn card_selected ( & self , row : & ActionRow ) {
105
+ println ! ( "CARD CHOSEN: {}" , row. title( ) ) ;
106
+ }
107
+
108
+ pub fn create_provider ( provider_type : i32 ) -> Provider {
109
+ match provider_type {
110
+ 0 => {
111
+ // Nvidia Settings and Nvidia SMI
112
+ // Create new provider
113
+ Provider :: new ( || {
114
+ vec ! [
115
+ Property :: new( & Processor :: new( "nvidia-settings" , "-q GpuUUID -t" ) , "utilization.gpu" , "" , & Formatter :: new( ) , & 1 ) ,
116
+ Property :: new( & Processor :: new( "nvidia-settings" , "-q GpuUUID -t" ) , "temperature.gpu" , "" , & Formatter :: new( ) , & 1 ) ,
117
+ Property :: new( & Processor :: new( "nvidia-settings" , "-q GpuUUID -t" ) , "memory.used,memory.total" , "" , & Formatter :: new( ) , & 1 ) ,
118
+ Property :: new( & Processor :: new( "nvidia-settings" , "-q GpuUUID -t" ) , "fan.speed" , "" , & Formatter :: new( ) , & 1 ) ,
119
+ Property :: new( & Processor :: new( "nvidia-smi" , "--query-gpu=gpu_name --format=csv,noheader" ) , "power.draw" , "" , & Formatter :: new( ) , & 1 ) ,
120
+ ]
121
+ } , 0 )
122
+ }
123
+ 1 => {
124
+ // Nvidia Settings
125
+ // Create new provider
126
+ Provider :: new ( || {
127
+ vec ! [
128
+ Property :: new( & Processor :: new( "nvidia-settings" , "-q GpuUUID -t" ) , "utilization.gpu" , "" , & Formatter :: new( ) , & 1 ) ,
129
+ Property :: new( & Processor :: new( "nvidia-settings" , "-q GpuUUID -t" ) , "temperature.gpu" , "" , & Formatter :: new( ) , & 1 ) ,
130
+ Property :: new( & Processor :: new( "nvidia-settings" , "-q GpuUUID -t" ) , "memory.used,memory.total" , "" , & Formatter :: new( ) , & 1 ) ,
131
+ Property :: new( & Processor :: new( "nvidia-settings" , "-q GpuUUID -t" ) , "fan.speed" , "" , & Formatter :: new( ) , & 1 ) ,
132
+ ]
133
+ } , 1 )
134
+ }
135
+ 2 => {
136
+ // Nvidia SMI
137
+ // Create new provider
138
+ Provider :: new ( || {
139
+ vec ! [
140
+ Property :: new( & Processor :: new( "nvidia-smi" , "--query-gpu=gpu_name --format=csv,noheader" ) , "utilization.gpu" , "" , & Formatter :: new( ) , & 1 ) ,
141
+ Property :: new( & Processor :: new( "nvidia-smi" , "--query-gpu=gpu_name --format=csv,noheader" ) , "temperature.gpu" , "" , & Formatter :: new( ) , & 1 ) ,
142
+ Property :: new( & Processor :: new( "nvidia-smi" , "--query-gpu=gpu_name --format=csv,noheader" ) , "memory.used,memory.total" , "" , & Formatter :: new( ) , & 1 ) ,
143
+ Property :: new( & Processor :: new( "nvidia-smi" , "--query-gpu=gpu_name --format=csv,noheader" ) , "fan.speed" , "" , & Formatter :: new( ) , & 1 ) ,
144
+ Property :: new( & Processor :: new( "nvidia-smi" , "--query-gpu=gpu_name --format=csv,noheader" ) , "power.draw" , "" , & Formatter :: new( ) , & 1 ) ,
145
+ ]
146
+ } , 2 )
147
+ }
148
+ 3 => {
149
+ // Nvidia Optimus
150
+ // Create new provider
151
+ Provider :: new ( || {
152
+ vec ! [
153
+ Property :: new( & Processor :: new( "optirun" , "nvidia-smi --query-gpu=gpu_name --format=csv,noheader" ) , "utilization.gpu" , "" , & Formatter :: new( ) , & 1 ) ,
154
+ Property :: new( & Processor :: new( "optirun" , "nvidia-smi --query-gpu=gpu_name --format=csv,noheader" ) , "temperature.gpu" , "" , & Formatter :: new( ) , & 1 ) ,
155
+ Property :: new( & Processor :: new( "optirun" , "nvidia-smi --query-gpu=gpu_name --format=csv,noheader" ) , "memory.used,memory.total" , "" , & Formatter :: new( ) , & 1 ) ,
156
+ Property :: new( & Processor :: new( "optirun" , "nvidia-smi --query-gpu=gpu_name --format=csv,noheader" ) , "fan.speed" , "" , & Formatter :: new( ) , & 1 ) ,
157
+ Property :: new( & Processor :: new( "optirun" , "nvidia-smi --query-gpu=gpu_name --format=csv,noheader" ) , "power.draw" , "" , & Formatter :: new( ) , & 1 ) ,
158
+ ]
159
+ } , 3 )
160
+ } ,
161
+ _ => {
162
+ // Assume Default (Nvidia Settings and Nvidia SMI)
163
+ // Create new provider
164
+ Provider :: new ( || {
165
+ vec ! [
166
+ Property :: new( & Processor :: new( "nvidia-settings" , "-q GpuUUID -t" ) , "utilization.gpu" , "" , & Formatter :: new( ) , & 1 ) ,
167
+ Property :: new( & Processor :: new( "nvidia-settings" , "-q GpuUUID -t" ) , "temperature.gpu" , "" , & Formatter :: new( ) , & 1 ) ,
168
+ Property :: new( & Processor :: new( "nvidia-settings" , "-q GpuUUID -t" ) , "memory.used,memory.total" , "" , & Formatter :: new( ) , & 1 ) ,
169
+ Property :: new( & Processor :: new( "nvidia-settings" , "-q GpuUUID -t" ) , "fan.speed" , "" , & Formatter :: new( ) , & 1 ) ,
170
+ Property :: new( & Processor :: new( "nvidia-smi" , "--query-gpu=gpu_name --format=csv,noheader" ) , "power.draw" , "" , & Formatter :: new( ) , & 1 ) ,
171
+ ]
172
+ } , 0 )
173
+ }
174
+ }
175
+ }
176
+
177
+ #[ template_callback]
178
+ fn refresh_cards ( & self , button : & CustomButton ) {
179
+ //TEST: Grab button label
180
+ let label_val = button. label ( ) . expect ( "cannot grab label of refresh button" ) ;
181
+ println ! ( "Button Pressed: {}" , label_val) ;
182
+
183
+ // Clear current ActionRow objects from GtkListBox
184
+ let mut done: bool = false ;
185
+ while done == false {
186
+ // Try to grab a child
187
+ let current_child: Option < gtk:: Widget > = self . cards_list . get ( ) . first_child ( ) ;
188
+
189
+ // Check if there are any children left
190
+ match current_child {
191
+ Some ( valid_child) => {
192
+ // Remove child
193
+ self . cards_list . get ( ) . remove ( & valid_child) ;
194
+ }
195
+ None => {
196
+ // End loop
197
+ done = true ;
198
+ }
199
+ }
200
+ }
201
+
202
+ // Grab settings
203
+ let settings: & Settings = self . settings . get ( ) . expect ( "..Cannot retrieve settings" ) ;
204
+
205
+ // Grab provider
206
+ let provider: Option < Provider > = self . provider . take ( ) ;
207
+
208
+ // If provider does not exist
209
+ match provider {
210
+ Some ( existing_provider) => {
211
+ // Update GPU list
212
+ //let gpu_uuids = existing_provider.gpus;
213
+ let gpu_uuids = [ "" , "" ] ;
214
+
215
+ // Store GPU list in settings
216
+ settings
217
+ . set_strv ( "gpus" , & gpu_uuids)
218
+ . expect ( "..Cannot store list of GPU UUID in `gpus` setting" ) ;
219
+
220
+ // Re-Store provider
221
+ self . provider . set ( Some ( existing_provider) ) ;
222
+ }
223
+ None => {
224
+ // Check provider type
225
+ let provider_type: i32 = self . settings . get ( ) . expect ( "..cannot fetch settings" ) . int ( "provider" ) ;
226
+
227
+ let new_provider: Provider = MainWindow :: create_provider ( provider_type) ;
228
+
229
+ // Update GPU list
230
+ //let gpu_uuids = new_provider.gpus;
231
+ let gpu_uuids = [ "" , "" ] ;
232
+
233
+ // Store new provider
234
+ self . provider . set ( Some ( new_provider) ) ;
235
+
236
+ // Store GPU list in settings
237
+ settings
238
+ . set_strv ( "gpus" , & gpu_uuids)
239
+ . expect ( "..Cannot store list of GPU UUID in `gpus` setting" ) ;
240
+ }
241
+ }
242
+
243
+ // Grab provider
244
+ let provider: Option < Provider > = self . provider . take ( ) ;
245
+
246
+ // Fetch updated GPU list
247
+ //let gpus: Vec<GString> = settings.strv("gpus");
248
+
249
+ // For each scanned GPU
250
+ let gpus = vec ! [ ( "title 1" , "subtitle 1" ) , ( "title 2" , "subtitle 2" ) , ( "title 3" , "subtitle 3" ) ] ; //TEST
251
+ for i in 0 ..gpus. len ( ) {
252
+ // Get GPU data
253
+ let title: & str = gpus[ i] . 0 ; //TEST
254
+ let subtitle: & str = gpus[ i] . 1 ; //TEST
255
+ //let gpu_name = provider.get_gpu_data(&gpus[i], "name");
256
+ //let gpu_uuid = provider.get_gpu_data(&gpus[i], "UUID");
257
+
258
+ // Create new ActionRow object
259
+ let current_row: ActionRow = ActionRow :: builder ( )
260
+ . title ( title)
261
+ . subtitle ( subtitle)
262
+ . build ( ) ;
263
+
264
+ // Append new ActionRow object to GtkListBox
265
+ self . cards_list . append ( & current_row) ;
266
+ }
267
+
268
+ // Put provider back
269
+ self . provider . set ( provider) ;
270
+ }
271
+ }
73
272
/*
74
273
* Trait Name:
75
274
* ObjectImpl
@@ -93,7 +292,7 @@ impl ObjectImpl for MainWindow {
93
292
94
293
// Setup
95
294
obj. setup_settings ( ) ;
96
- // obj.setup_tasks ();
295
+ obj. setup_widgets ( ) ;
97
296
obj. restore_data ( ) ;
98
297
obj. setup_callbacks ( ) ;
99
298
obj. setup_actions ( ) ;
0 commit comments