Skip to content

Commit b74e650

Browse files
author
Deren Vural
committed
Updated MainWindow class
- Cleaned up crate imports into single line - Changed default gpu_count to 0 for obvious reasons - Cleaned up create_provider function - Added function comment headers - Cleaned up refresh_cards function (removed unecessary settings fetch, mostly finished row creation, removed uneeded tests) - Removed most uneeded actions from setup_actions function Updated Processor class - Switched to "&str" instead of "&'static str" in constructor, all stored as "String" anyway - Removed old TODO statement Updated Property class - Removed old TODO statement - Fixed incorrect property name in constructor ("gpu_count" -> "gpu-count") - Added function comment headers - Cleaned up some comments - Added "update_value" function Updated Provider class - Fixed incorrect NAME value - Added missing "provider_type" (i32) property - Renamed properties to include "-property" if using Property class - Added check to constructor in case of default or empty Vec input - Refactored "get_gpu_uuids" function - Changed "open_settings" return type to use String instead of &str (easier for passing) - Added "update_property_value" function, for updating Property class objects internal fields Updated MainWindow UI file - Removed uneeded menu options - Moved app and nvidia settings buttons into a "Preferences" submenu - Added "show-separators" property to the left pane list box - Added "row_activated" signal to the left pane list box, calling "card_selected" function - Commented out default cards, need to have text here if at 0 cards! Signed-off-by: Deren Vural <[email protected]>
1 parent bda99c4 commit b74e650

File tree

7 files changed

+419
-390
lines changed

7 files changed

+419
-390
lines changed

src/mainwindow/imp.rs

Lines changed: 169 additions & 121 deletions
Large diffs are not rendered by default.

src/mainwindow/mod.rs

Lines changed: 1 addition & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ use glib::{clone, Object};
2929
use std::cell::RefMut;
3030

3131
// Modules
32-
use crate::{provider, settingswindow, APP_ID};
33-
use provider::Provider;
34-
use settingswindow::SettingsWindow;
32+
use crate::{APP_ID, provider::Provider, settingswindow::SettingsWindow};
3533

3634
// GObject wrapper for Property
3735
glib::wrapper! {
@@ -243,15 +241,6 @@ impl MainWindow {
243241
* have a single globally accessible action instead, we call add_action on our application instead.
244242
*/
245243
fn setup_actions(&self) {
246-
/*
247-
let action_close = SimpleAction::new("close", None);
248-
action_close.connect_activate(clone!(@weak self as window => move |_, _| {
249-
window.close();
250-
}));
251-
self.add_action(&action_close);
252-
actions.add_action(&action_close);
253-
*/
254-
255244
// Create action from key "open_nvidia_settings" and add to action group "win"
256245
let open_nvidia_settings: SimpleAction = SimpleAction::new("open_nvidia_settings", None);
257246
open_nvidia_settings.connect_activate(clone!(@weak self as window => move |_, _| {
@@ -311,70 +300,6 @@ impl MainWindow {
311300
self.add_action(&open_nvidia_settings);
312301

313302
/*
314-
// Create action from key "subprocess" and add to action group "win"
315-
let subprocess: SimpleAction = SimpleAction::new("subprocess", None);
316-
subprocess.connect_activate(clone!(@weak self as window => move |_, _| {
317-
match subprocess::exec_communicate(
318-
&[
319-
OsStr::new("nvidia-settings"),
320-
OsStr::new("-q"),
321-
OsStr::new("GpuUUID"),
322-
OsStr::new("-t"),
323-
],
324-
None::<&gio::Cancellable>,
325-
) {
326-
Ok(return_val) => match return_val {
327-
(None, None) => println!("no stdout or stderr, something went really wrong..."),
328-
(None, Some(stderr_buffer)) => match std::str::from_utf8(&stderr_buffer) {
329-
Ok(stderr_buffer_contents) => {
330-
println!("Process failed with error: {}", stderr_buffer_contents)
331-
}
332-
Err(err) => panic!("{}", err),
333-
},
334-
(Some(stdout_buffer), None) => match std::str::from_utf8(&stdout_buffer) {
335-
Ok(stdout_buffer_contents) => {
336-
println!("Process suceeded, returning: `{}`", stdout_buffer_contents)
337-
}
338-
Err(err) => panic!("{}", err),
339-
},
340-
(Some(stdout_buffer), Some(stderr_buffer)) => {
341-
match std::str::from_utf8(&stdout_buffer) {
342-
Ok(stdout_buffer_contents) => match std::str::from_utf8(&stderr_buffer) {
343-
Ok(stderr_buffer_contents) => println!(
344-
"Process suceeded, returning: `{}` but with error: `{}`",
345-
stdout_buffer_contents, stderr_buffer_contents
346-
),
347-
Err(err) => panic!("{}", err),
348-
},
349-
Err(err) => panic!("{}", err),
350-
}
351-
}
352-
},
353-
Err(err) => println!("something went wrong: {}", err),
354-
};
355-
}));
356-
self.add_action(&subprocess);
357-
358-
// Create action from key "processor" and add to action group "win"
359-
let processor: SimpleAction = SimpleAction::new("processor", None);
360-
processor.connect_activate(clone!(@weak self as window => move |_, _| {
361-
let p: Processor = Processor::new("nvidia-settings", "-q GpuUUID -t");
362-
363-
//NOTE: Leaving this here for future use..
364-
//p.set_property("base-call", "nvidia-settings");
365-
//p.set_property("call", "nvidia-settings");
366-
//p.set_property("tail-call", "t");
367-
368-
match p.process() {
369-
Ok(output) => match output {
370-
Some(valid_output) => println!("Process suceeded, returning: `{}`", valid_output),
371-
None => println!("Process encountered an unknown error.."),
372-
},
373-
Err(err) => println!("Process encountered an error, returning: `{}`", err),
374-
}
375-
}));
376-
self.add_action(&processor);
377-
378303
// Create action from key "formatter_and_property" and add to action group "win"
379304
let formatter_and_property: SimpleAction =
380305
SimpleAction::new("formatter_and_property", None);
@@ -597,58 +522,6 @@ impl MainWindow {
597522
}
598523
}));
599524
self.add_action(&formatter_and_property);
600-
601-
// Create action from key "providers" and add to action group "win"
602-
let providers: SimpleAction = SimpleAction::new("providers", None);
603-
providers.connect_activate(clone!(@weak self as window => move |_, _| {
604-
let _gpu_count: i32 = 1;
605-
606-
// SETTINGS
607-
let _settings_prov: Provider = Provider::new(|| {
608-
vec![
609-
Property::new(&Processor::new("nvidia-settings", "-q GpuUUID -t"), "utilization.gpu", "", &Formatter::new(), &1),
610-
Property::new(&Processor::new("nvidia-settings", "-q GpuUUID -t"), "temperature.gpu", "", &Formatter::new(), &1),
611-
Property::new(&Processor::new("nvidia-settings", "-q GpuUUID -t"), "memory.used,memory.total", "", &Formatter::new(), &1),
612-
Property::new(&Processor::new("nvidia-settings", "-q GpuUUID -t"), "fan.speed", "", &Formatter::new(), &1),
613-
]
614-
}, 0);
615-
616-
// SMI
617-
let _smi_prov: Provider = Provider::new(|| {
618-
vec![
619-
Property::new(&Processor::new("nvidia-smi", "--query-gpu=gpu_name --format=csv,noheader"), "utilization.gpu", "", &Formatter::new(), &1),
620-
Property::new(&Processor::new("nvidia-smi", "--query-gpu=gpu_name --format=csv,noheader"), "temperature.gpu", "", &Formatter::new(), &1),
621-
Property::new(&Processor::new("nvidia-smi", "--query-gpu=gpu_name --format=csv,noheader"), "memory.used,memory.total", "", &Formatter::new(), &1),
622-
Property::new(&Processor::new("nvidia-smi", "--query-gpu=gpu_name --format=csv,noheader"), "fan.speed", "", &Formatter::new(), &1),
623-
Property::new(&Processor::new("nvidia-smi", "--query-gpu=gpu_name --format=csv,noheader"), "power.draw", "", &Formatter::new(), &1),
624-
]
625-
}, 1);
626-
627-
// SETTINGS & SMI
628-
let _both_prov: Provider = Provider::new(|| {
629-
vec![
630-
Property::new(&Processor::new("nvidia-settings", "-q GpuUUID -t"), "utilization.gpu", "", &Formatter::new(), &1),
631-
Property::new(&Processor::new("nvidia-settings", "-q GpuUUID -t"), "temperature.gpu", "", &Formatter::new(), &1),
632-
Property::new(&Processor::new("nvidia-settings", "-q GpuUUID -t"), "memory.used,memory.total", "", &Formatter::new(), &1),
633-
Property::new(&Processor::new("nvidia-settings", "-q GpuUUID -t"), "fan.speed", "", &Formatter::new(), &1),
634-
Property::new(&Processor::new("nvidia-smi", "--query-gpu=gpu_name --format=csv,noheader"), "power.draw", "", &Formatter::new(), &1),
635-
]
636-
}, 2);
637-
638-
// OPTIMUS
639-
let _optimus_prov: Provider = Provider::new(|| {
640-
vec![
641-
Property::new(&Processor::new("optirun", "nvidia-smi --query-gpu=gpu_name --format=csv,noheader"), "utilization.gpu", "", &Formatter::new(), &1),
642-
Property::new(&Processor::new("optirun", "nvidia-smi --query-gpu=gpu_name --format=csv,noheader"), "temperature.gpu", "", &Formatter::new(), &1),
643-
Property::new(&Processor::new("optirun", "nvidia-smi --query-gpu=gpu_name --format=csv,noheader"), "memory.used,memory.total", "", &Formatter::new(), &1),
644-
Property::new(&Processor::new("optirun", "nvidia-smi --query-gpu=gpu_name --format=csv,noheader"), "fan.speed", "", &Formatter::new(), &1),
645-
Property::new(&Processor::new("optirun", "nvidia-smi --query-gpu=gpu_name --format=csv,noheader"), "power.draw", "", &Formatter::new(), &1),
646-
]
647-
}, 3);
648-
}));
649-
self.add_action(&providers);
650-
651-
//self.emit_activate_focus()
652525
*/
653526

654527
let open_app_settings: SimpleAction = SimpleAction::new("open_app_settings", None);

src/processor/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ impl Processor {
6868
* Notes:
6969
*
7070
*/
71-
pub fn new(base_call: &'static str, tail_call: &'static str) -> Self {
71+
pub fn new(base_call: &str, tail_call: &str) -> Self {
7272
let obj: Processor = Object::new(&[]).expect("Failed to create `Processor`");
7373

74-
// TODO: set properties
74+
// Set properties
7575
obj.set_property("base-call", base_call.to_string());
7676
obj.set_property("call", base_call.to_string().clone());
7777
obj.set_property("tail-call", tail_call.to_string());

src/property/mod.rs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,32 @@ impl Property {
8181
) -> Self {
8282
let obj: Property = Object::new(&[]).expect("Failed to create `Property`.");
8383

84-
// TODO: set properties
84+
// Set properties
8585
obj.set_property("processor", processor);
8686
obj.set_property("call-extension", call_extension.to_string());
8787
obj.set_property("icon", icon.to_string());
8888
obj.set_property("formatter", formatter);
89-
obj.set_property("gpu_count", gpu_count);
89+
obj.set_property("gpu-count", gpu_count);
9090

9191
obj
9292
}
9393

94+
/*
95+
* Name:
96+
* parse
97+
*
98+
* Description:
99+
*
100+
*
101+
* Made:
102+
* 06/10/2022
103+
*
104+
* Made by:
105+
* Deren Vural
106+
*
107+
* Notes:
108+
*
109+
*/
94110
// Parsing
95111
//https://doc.rust-lang.org/std/primitive.array.html
96112
//https://www.tutorialspoint.com/rust/rust_array.htm
@@ -104,7 +120,7 @@ impl Property {
104120

105121
// For each GPU
106122
for i in 0..self.property::<i32>("gpu-count") {
107-
// format properties using formatter and add to return values
123+
// Format properties using formatter and add to return values
108124
let formatter = self.property::<Formatter>("formatter");
109125
match formatter.format(values.get(i as usize).unwrap().clone(), func) {
110126
Some(result) => results.push(result),
@@ -115,9 +131,47 @@ impl Property {
115131
Some(results)
116132
}
117133

134+
/*
135+
* Name:
136+
* get_call_extension
137+
*
138+
* Description:
139+
* Get the call extension property
140+
*
141+
* Made:
142+
* 06/10/2022
143+
*
144+
* Made by:
145+
* Deren Vural
146+
*
147+
* Notes:
148+
*
149+
*/
118150
pub fn get_call_extension(&self) -> String {
151+
// Return the call-extension property
119152
self.property::<String>("call-extension")
120153
}
154+
155+
/*
156+
* Name:
157+
* update_value
158+
*
159+
* Description:
160+
* Update a property with a new value
161+
*
162+
* Made:
163+
* 29/10/2022
164+
*
165+
* Made by:
166+
* Deren Vural
167+
*
168+
* Notes:
169+
*
170+
*/
171+
pub fn update_value<T: ToValue>(&self, property_name: &str, value: T) {
172+
// Update property with new value
173+
self.set_property(property_name, value);
174+
}
121175
}
122176

123177
/*

src/provider/imp.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ pub struct Provider {
3434
memory_usage: Cell<Property>,
3535
fan_speed: Cell<Property>,
3636
power_usage: Cell<Property>,
37+
provider_type: Cell<i32>,
3738
}
3839

3940
// The central trait for subclassing a GObject
4041
#[glib::object_subclass]
4142
impl ObjectSubclass for Provider {
4243
//Crate+Obj to avoid collisions
43-
const NAME: &'static str = "NvidiaMonitorRustProcessor";
44+
const NAME: &'static str = "NvidiaMonitorRustProvider";
4445
// the actual GObject that will be created
4546
type Type = super::Provider;
4647
// Parent GObject we inherit from
@@ -96,6 +97,7 @@ impl ObjectImpl for Provider {
9697
glib::ParamSpecObject::builder("memory-usage-property", glib::Type::OBJECT).build(),
9798
glib::ParamSpecObject::builder("fan-speed-property", glib::Type::OBJECT).build(),
9899
glib::ParamSpecObject::builder("power-usage-property", glib::Type::OBJECT).build(),
100+
glib::ParamSpecInt::builder("provider-type").build(),
99101
]
100102
});
101103

@@ -125,37 +127,43 @@ impl ObjectImpl for Provider {
125127
//println!("setting: {:?}", pspec.name());//TEST
126128

127129
match pspec.name() {
128-
"utilization" => {
130+
"utilization-property" => {
129131
let input_utilization_property = value
130132
.get()
131133
.expect("The value needs to be of type `Property`.");
132134
self.utilization.replace(input_utilization_property);
133135
}
134-
"temperature" => {
136+
"temperature-property" => {
135137
let input_temperature_property = value
136138
.get()
137139
.expect("The value needs to be of type `Property`.");
138140
self.temperature.replace(input_temperature_property);
139141
}
140-
"memory-usage" => {
142+
"memory-usage-property" => {
141143
let input_memory_usage_property = value
142144
.get()
143145
.expect("The value needs to be of type `Property`.");
144146
self.memory_usage.replace(input_memory_usage_property);
145147
}
146-
"fan-speed" => {
148+
"fan-speed-property" => {
147149
let input_fan_speed_property = value
148150
.get()
149151
.expect("The value needs to be of type `Property`.");
150152
self.fan_speed.replace(input_fan_speed_property);
151153
}
152-
"power-usage" => {
154+
"power-usage-property" => {
153155
let input_power_usage_property = value
154156
.get()
155157
.expect("The value needs to be of type `Property`.");
156158
self.power_usage.replace(input_power_usage_property);
157159
}
158-
_ => unimplemented!(), //TODO
160+
"provider-type" => {
161+
let input_provider_type_property = value
162+
.get()
163+
.expect("The value needs to be of type `i32`.");
164+
self.provider_type.replace(input_provider_type_property);
165+
}
166+
_ => panic!("Property `{}` does not exist..", pspec.name())
159167
}
160168
}
161169

@@ -179,46 +187,54 @@ impl ObjectImpl for Provider {
179187
//println!("getting: {:?}", pspec.name());//TEST
180188

181189
match pspec.name() {
182-
"utilization" => {
190+
"utilization-property" => {
183191
//TODO: this seems ridiculous..
184192
let value = self.utilization.take();
185193

186194
self.utilization.set(value.clone());
187195

188196
value.to_value()
189197
}
190-
"temperature" => {
198+
"temperature-property" => {
191199
//TODO: this seems ridiculous..
192200
let value = self.temperature.take();
193201

194202
self.temperature.set(value.clone());
195203

196204
value.to_value()
197205
}
198-
"memory-usage" => {
206+
"memory-usage-property" => {
199207
//TODO: this seems ridiculous..
200208
let value = self.memory_usage.take();
201209

202210
self.memory_usage.set(value.clone());
203211

204212
value.to_value()
205213
}
206-
"fan-speed" => {
214+
"fan-speed-property" => {
207215
//TODO: this seems ridiculous..
208216
let value = self.fan_speed.take();
209217

210218
self.fan_speed.set(value.clone());
211219

212220
value.to_value()
213221
}
214-
"power-usage" => {
222+
"power-usage-property" => {
215223
//TODO: this seems ridiculous..
216224
let value = self.power_usage.take();
217225

218226
self.power_usage.set(value.clone());
219227

220228
value.to_value()
221229
}
230+
"provider-type" => {
231+
//TODO: this seems ridiculous..
232+
let value = self.provider_type.take();
233+
234+
self.provider_type.set(value.clone());
235+
236+
value.to_value()
237+
}
222238
_ => unimplemented!(), //TODO
223239
}
224240
}

0 commit comments

Comments
 (0)