Skip to content

Commit 763bb19

Browse files
author
Deren Vural
committed
Major Refactor
Replacing a lot of copypasta code from the extension with improved versions! A lot of work done on the displaying and notably *updating* of stats. Updated MainWindow UI - Removed the last of the example menu buttons Updated CSS - Added outline/border to the GpuPage class grid objects Updated MainWindow class - Moved TemperatureUnit enum here from Formatter class - Moved get_setting(), update_setting() and create_provider() functions to separate MainWindow trait, leaving only the callbacks in the orginal (clean separation of normal functions vs callbacks) - Added `self` param to create_provider() function - Updated to finish memory, fan and temperature closures for Formatter objects - Updated create_gpu_page() function to use reference to a Provider as a param - Updated refresh_cards() function to properly grab and pass Provider object - Removed now unecessary code examples from setup_actions() function Updated GpuPage class: - Changed provider struct member to a Cell<Option<Provider>> instead of Rc<RefCell<Provider>> - Updated build process for page in setup_widgets() function - Now using the parent Provider, this is important to ensure the Property objects available from the Provider and properties being shown are the same (lots of Arc+Mutex) - Updated Default trait for new struct members Updated Processor class: - Removed call and tail_call struct member variables, replaced with start_call and end_call strings - This allows proper building of program call in process() function - Updated Default trait for new struct members Updated Property class: - Removed call_extension, icon and gpu_count struct member variables, replaced with id (String) - Replaced parse() function with full version, now parses info from ONE GPU and is run in a loop by whatever controls it - Updated Default trait for new struct members Updated Provider class: - Replaced explicit properties with a properties struct member, which stores a list of Property objects in a RefCell - This allows custom lists of properties, which will make adding customization in later *much* easier - Updated get_gpu_data() function to handle dynamic lists of properties - Removed update_property_value() function Updated Formatter class: - Cleaned up imports - Added settings object and func closure to struct members - Added get_setting() function to Formatter trait - Added constructed() function to ObjectImpl trait to enable loading settings (it calls setup_settings()) - Added the new func struct member to constructor - Added setup_settings() function - Replaced format() function with full version, now grabs any formatting settings e.g. temperature unit and passes to func when calling - Updated Default trait for new struct members Signed-off-by: Deren Vural <[email protected]>
1 parent 45aa692 commit 763bb19

File tree

14 files changed

+1129
-989
lines changed

14 files changed

+1129
-989
lines changed

src/formatter/imp.rs

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,21 @@
1919
*/
2020

2121
// Imports
22-
use glib::{once_cell::sync::Lazy, ParamSpec, Value};
23-
use gtk::{glib, subclass::prelude::*};
22+
use adwaita::{gio, glib, prelude::*, subclass::prelude::*};
23+
use std::cell::Cell;
24+
use gio::Settings;
25+
use glib::{once_cell::sync::OnceCell, once_cell::sync::Lazy, ParamSpec, Value, FromVariant};
26+
use gtk::{subclass::prelude::*};
2427

2528
// Modules
2629
//
2730

2831
/// Object holding the State and any Template Children
2932
#[derive(Default)]
30-
pub struct Formatter;
33+
pub struct Formatter {
34+
pub settings: OnceCell<Settings>,
35+
pub func: Cell<Option<fn(Vec<String>, Option<Vec<(String, String)>>) -> Option<String>>>
36+
}
3137

3238
/// The central trait for subclassing a GObject
3339
#[glib::object_subclass]
@@ -40,6 +46,34 @@ impl ObjectSubclass for Formatter {
4046
type ParentType = gtk::Widget;
4147
}
4248

49+
impl Formatter {
50+
/**
51+
* Name:
52+
* get_setting
53+
*
54+
* Description:
55+
* Generic function for getting setting value
56+
*
57+
* Made:
58+
* 30/10/2022
59+
*
60+
* Made by:
61+
* Deren Vural
62+
*
63+
* Notes:
64+
*
65+
*/
66+
pub fn get_setting<T: FromVariant>(&self, name: &str) -> T {
67+
// Return the value of the property
68+
match self.settings.get() {
69+
Some(settings) => {
70+
settings.get::<T>(name)
71+
},
72+
None => panic!("`settings` should be set in `setup_settings`.")
73+
}
74+
}
75+
}
76+
4377
/**
4478
* Trait Name:
4579
* ObjectImpl
@@ -57,6 +91,30 @@ impl ObjectSubclass for Formatter {
5791
*
5892
*/
5993
impl ObjectImpl for Formatter {
94+
/**
95+
* Name:
96+
* constructed
97+
*
98+
* Description:
99+
* Called during construction, allows calling setup functions
100+
*
101+
* Made:
102+
* 18/11/2022
103+
*
104+
* Made by:
105+
* Deren Vural
106+
*
107+
* Notes:
108+
*
109+
*/
110+
fn constructed(&self, obj: &Self::Type) {
111+
// Call "constructed" on parent
112+
self.parent_constructed(obj);
113+
114+
// Setup
115+
obj.setup_settings();
116+
}
117+
60118
/**
61119
* Name:
62120
* properties

src/formatter/mod.rs

Lines changed: 111 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@
2222
mod imp;
2323

2424
// Imports
25+
use adwaita::{gio, glib};
2526
use glib::Object;
26-
use gtk::glib;
27-
27+
use gio::Settings;
28+
use gtk::subclass::prelude::*;
2829
// Modules
29-
//
30+
use crate::{APP_ID};
3031

3132
// GObject wrapper for Formatter
3233
glib::wrapper! {
@@ -68,52 +69,113 @@ impl Formatter {
6869
* Notes:
6970
*
7071
*/
71-
pub fn new() -> Self {
72-
Object::new(&[]).expect("Failed to create `Formatter`.")
72+
pub fn new(func: fn(Vec<String>, Option<Vec<(String, String)>>) -> Option<String>) -> Self {
73+
let obj: Formatter = Object::new(&[]).expect("Failed to create `Formatter`.");
74+
75+
// Set properties
76+
obj.imp().func.set(Some(func));
77+
78+
obj
7379
}
7480

75-
pub fn format(
76-
self,
77-
values: Vec<String>,
78-
func: fn(Vec<String>) -> Option<String>,
79-
) -> Option<String> {
80-
let mut results: Vec<String> = Vec::new();
81-
82-
// For each item in input list
83-
for i in values {
84-
// Remove all non-number characters
85-
let cleaned_value: String = i
86-
.chars()
87-
.filter(|c| {
88-
// check if (base 10) digit
89-
if c.is_digit(10) {
90-
true
91-
} else {
92-
// check if full-stop
93-
c.eq(&'.')
94-
}
95-
})
96-
.collect();
97-
98-
// Convert to float
99-
match cleaned_value.parse::<f64>() {
100-
Ok(parsed_value) => {
101-
// Convert to string
102-
results.push(parsed_value.to_string());
81+
/**
82+
* Name:
83+
* setup_settings
84+
*
85+
* Description:
86+
* Load settings for APP_ID
87+
*
88+
* Made:
89+
* 18/11/2022
90+
*
91+
* Made by:
92+
* Deren Vural
93+
*
94+
* Notes:
95+
*
96+
*/
97+
fn setup_settings(&self) {
98+
let settings = Settings::new(APP_ID);
99+
self.imp()
100+
.settings
101+
.set(settings)
102+
.expect("`settings` should not be set before calling `setup_settings`.");
103+
}
104+
105+
/*
106+
* Name:
107+
* format
108+
*
109+
* Description:
110+
* Given some valid string value, remove non-digit chars (excluding points) and apply formatting
111+
*
112+
* Made:
113+
* 12/11/2022
114+
*
115+
* Made by:
116+
* Deren Vural
117+
*
118+
* Notes:
119+
*
120+
*/
121+
pub fn format(self, value: String) -> Option<String> {
122+
//println!("FORMATTING");//TEST
123+
124+
// Remove all non-number characters
125+
let cleaned_value: String = value
126+
.chars()
127+
.filter(|c| {
128+
// check if (base 10) digit
129+
if c.is_digit(10) {
130+
true
131+
} else {
132+
// check if full-stop
133+
c.eq(&'.')
103134
}
104-
Err(err) => {
105-
// Catch any errors..
106-
println!("Not a valid number: {}", err);
135+
})
136+
.collect();
137+
138+
// Convert to float
139+
match cleaned_value.parse::<f64>() {
140+
Ok(parsed_value) => {
141+
// Apply any valid formatting
142+
match self.imp().func.take() {
143+
Some(func) => {
144+
// Grab all format info from settings (this is done here to keep in one place)
145+
//===
146+
// Temperature format
147+
let temp_format: i32 = self.imp().get_setting::<i32>("tempformat");
148+
let mut params: Vec<(String, String)> = vec![];
149+
if let 0 = temp_format {
150+
params.push((String::from("tempformat"), String::from("C")));
151+
} else if let 1 = temp_format {
152+
params.push((String::from("tempformat"), String::from("F")));
153+
}
154+
//TODO: ???
155+
//
156+
//===
157+
158+
// Use function
159+
let result: Option<String>;
160+
if params.len() > 0 {
161+
result = func(vec![parsed_value.to_string()], Some(params));
162+
} else {
163+
result = func(vec![parsed_value.to_string()], None);
164+
}
165+
// Return it!
166+
self.imp().func.set(Some(func));
167+
168+
result
169+
},
170+
None => panic!("Missing formatting function!")
107171
}
108172
}
109-
}
173+
Err(err) => {
174+
// Catch any errors..
175+
println!("Not a valid number: {}", err);
110176

111-
// Check for empty results
112-
if !results.is_empty() {
113-
// Apply any valid formatting
114-
func(results)
115-
} else {
116-
None
177+
None
178+
}
117179
}
118180
}
119181
}
@@ -136,6 +198,11 @@ impl Formatter {
136198
*/
137199
impl Default for Formatter {
138200
fn default() -> Self {
139-
Self::new()
201+
let func: fn(Vec<String>, Option<Vec<(String, String)>>) -> Option<String> = |input: Vec<String>, params: Option<Vec<(String, String)>>| {
202+
Some(String::from(input.get(0).unwrap()))
203+
};
204+
205+
206+
Self::new(func)
140207
}
141208
}

src/gpu_page/imp.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use glib::{
2626
ToValue, Value,
2727
};
2828
use gtk::{subclass::prelude::*, CompositeTemplate};
29-
use std::{cell::Cell, cell::Ref, cell::RefCell, rc::Rc};
29+
use std::{cell::Cell, sync::Arc};
3030

3131
// Modules
3232
use crate::provider::Provider;
@@ -38,7 +38,7 @@ pub struct GpuPage {
3838
pub settings: OnceCell<Settings>,
3939
uuid: Cell<String>,
4040
name: Cell<String>,
41-
provider: Rc<RefCell<Provider>>,
41+
provider: Cell<Option<Provider>>,
4242
}
4343

4444
/// The central trait for subclassing a GObject
@@ -264,9 +264,9 @@ impl ObjectImpl for GpuPage {
264264
value.to_value()
265265
}
266266
"provider" => {
267-
let value: Ref<Provider> = self.provider.borrow();
267+
let value: Option<Provider> = self.provider.take();
268268

269-
//self.provider.set(value.clone());
269+
self.provider.set(value.clone());
270270

271271
value.to_value()
272272
}

0 commit comments

Comments
 (0)