Skip to content

Commit f5beda2

Browse files
authored
Merge pull request #15 from derenv/property_refactor
Property refactor
2 parents 09d8b58 + b9e38e5 commit f5beda2

File tree

14 files changed

+1156
-1007
lines changed

14 files changed

+1156
-1007
lines changed

src/formatter/imp.rs

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,22 @@
1717
* Notes:
1818
* Most of this left blank, may add fields later
1919
*/
20-
2120
// Imports
22-
use glib::{once_cell::sync::Lazy, ParamSpec, Value};
23-
use gtk::{glib, subclass::prelude::*};
21+
use adwaita::{gio, glib, prelude::*, subclass::prelude::*};
22+
use gio::Settings;
23+
use glib::{once_cell::sync::Lazy, once_cell::sync::OnceCell, FromVariant, ParamSpec, Value};
24+
use gtk::subclass::prelude::*;
25+
use std::cell::Cell;
2426

2527
// Modules
2628
//
2729

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

3237
/// The central trait for subclassing a GObject
3338
#[glib::object_subclass]
@@ -40,6 +45,32 @@ impl ObjectSubclass for Formatter {
4045
type ParentType = gtk::Widget;
4146
}
4247

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

116171
match pspec.name() {
117172
//
118-
_ => panic!("Property `{}` does not exist..", pspec.name())
173+
_ => panic!("Property `{}` does not exist..", pspec.name()),
119174
}
120175
}
121176

@@ -140,7 +195,7 @@ impl ObjectImpl for Formatter {
140195

141196
match pspec.name() {
142197
//
143-
_ => panic!("Property `{}` does not exist..", pspec.name())
198+
_ => panic!("Property `{}` does not exist..", pspec.name()),
144199
}
145200
}
146201
}

src/formatter/mod.rs

Lines changed: 109 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717
* Notes:
1818
*
1919
*/
20-
2120
// Custom GObjects
2221
mod imp;
2322

2423
// Imports
24+
use adwaita::{gio, glib};
25+
use gio::Settings;
2526
use glib::Object;
26-
use gtk::glib;
27-
27+
use gtk::subclass::prelude::*;
2828
// Modules
29-
//
29+
use crate::APP_ID;
3030

3131
// GObject wrapper for Formatter
3232
glib::wrapper! {
@@ -68,52 +68,113 @@ impl Formatter {
6868
* Notes:
6969
*
7070
*/
71-
pub fn new() -> Self {
72-
Object::new(&[]).expect("Failed to create `Formatter`.")
71+
pub fn new(func: fn(Vec<String>, Option<Vec<(String, String)>>) -> Option<String>) -> Self {
72+
let obj: Formatter = Object::new(&[]).expect("Failed to create `Formatter`.");
73+
74+
// Set properties
75+
obj.imp().func.set(Some(func));
76+
77+
obj
7378
}
7479

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();
80+
/**
81+
* Name:
82+
* setup_settings
83+
*
84+
* Description:
85+
* Load settings for APP_ID
86+
*
87+
* Made:
88+
* 18/11/2022
89+
*
90+
* Made by:
91+
* Deren Vural
92+
*
93+
* Notes:
94+
*
95+
*/
96+
fn setup_settings(&self) {
97+
let settings = Settings::new(APP_ID);
98+
self.imp()
99+
.settings
100+
.set(settings)
101+
.expect("`settings` should not be set before calling `setup_settings`.");
102+
}
81103

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();
104+
/*
105+
* Name:
106+
* format
107+
*
108+
* Description:
109+
* Given some valid string value, remove non-digit chars (excluding points) and apply formatting
110+
*
111+
* Made:
112+
* 12/11/2022
113+
*
114+
* Made by:
115+
* Deren Vural
116+
*
117+
* Notes:
118+
*
119+
*/
120+
pub fn format(self, value: String) -> Option<String> {
121+
//println!("FORMATTING");//TEST
97122

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());
123+
// Remove all non-number characters
124+
let cleaned_value: String = value
125+
.chars()
126+
.filter(|c| {
127+
// check if (base 10) digit
128+
if c.is_digit(10) {
129+
true
130+
} else {
131+
// check if full-stop
132+
c.eq(&'.')
103133
}
104-
Err(err) => {
105-
// Catch any errors..
106-
println!("Not a valid number: {}", err);
134+
})
135+
.collect();
136+
137+
// Convert to float
138+
match cleaned_value.parse::<f64>() {
139+
Ok(parsed_value) => {
140+
// Apply any valid formatting
141+
match self.imp().func.take() {
142+
Some(func) => {
143+
// Grab all format info from settings (this is done here to keep in one place)
144+
//===
145+
// Temperature format
146+
let temp_format: i32 = self.imp().get_setting::<i32>("tempformat");
147+
let mut params: Vec<(String, String)> = vec![];
148+
if let 0 = temp_format {
149+
params.push((String::from("tempformat"), String::from("C")));
150+
} else if let 1 = temp_format {
151+
params.push((String::from("tempformat"), String::from("F")));
152+
}
153+
//TODO: ???
154+
//
155+
//===
156+
157+
// Use function
158+
let result: Option<String>;
159+
if !params.is_empty() {
160+
result = func(vec![parsed_value.to_string()], Some(params));
161+
} else {
162+
result = func(vec![parsed_value.to_string()], None);
163+
}
164+
// Return it!
165+
self.imp().func.set(Some(func));
166+
167+
result
168+
}
169+
None => panic!("Missing formatting function!"),
107170
}
108171
}
109-
}
172+
Err(err) => {
173+
// Catch any errors..
174+
println!("Not a valid number: {}", err);
110175

111-
// Check for empty results
112-
if !results.is_empty() {
113-
// Apply any valid formatting
114-
func(results)
115-
} else {
116-
None
176+
None
177+
}
117178
}
118179
}
119180
}
@@ -136,6 +197,11 @@ impl Formatter {
136197
*/
137198
impl Default for Formatter {
138199
fn default() -> Self {
139-
Self::new()
200+
let func: fn(Vec<String>, Option<Vec<(String, String)>>) -> Option<String> =
201+
|input: Vec<String>, _params: Option<Vec<(String, String)>>| {
202+
Some(String::from(input.get(0).unwrap()))
203+
};
204+
205+
Self::new(func)
140206
}
141207
}

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;
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)