Skip to content

Commit 24e04a1

Browse files
author
Deren Vural
committed
Updated Property Class
- Remade entirely (skipping rebase etc) using new understanding of subclassing Updated Processor Class - Added default implmementation in mod.rs Added Formatter Class - Processes Vec of Strings (removes characters other than digits and '.', checks that they are convertable into floats and then formats using passed closure) - Closure used over subclasses as subclasses of subclasses was hurting my brain Updated Main Program - Made button comments clearer, explaining purpose of each - Added button4 - testing various processor/formatter/property combos Signed-off-by: Deren Vural <[email protected]>
1 parent 550c426 commit 24e04a1

File tree

6 files changed

+849
-24
lines changed

6 files changed

+849
-24
lines changed

src/formatter/imp.rs

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
// SPDX-FileCopyrightText: 2022 Deren Vural
2+
// SPDX-License-Identifier: GPL-3.0-or-later
3+
4+
/*
5+
* Name:
6+
* imp.rs
7+
*
8+
* Description:
9+
* Implementation of our custom GObject class (Formatter)
10+
*
11+
* Made:
12+
* 05/10/2022
13+
*
14+
* Made by:
15+
* Deren Vural
16+
*
17+
* Notes:
18+
* Most of this left blank, may add fields later
19+
*/
20+
21+
// Imports
22+
use gtk::glib::once_cell::sync::Lazy;
23+
use gtk::glib::{self, ParamSpec, Value};
24+
use gtk::subclass::prelude::*;
25+
26+
// Modules
27+
//
28+
29+
// Object holding the state
30+
#[derive(Default)]
31+
pub struct Formatter;
32+
33+
// The central trait for subclassing a GObject
34+
#[glib::object_subclass]
35+
impl ObjectSubclass for Formatter {
36+
//Crate+Obj to avoid collisions
37+
const NAME: &'static str = "NvidiaExtensionRustFormatter";
38+
// the actual GObject that will be created
39+
type Type = super::Formatter;
40+
// Parent GObject we inherit from
41+
type ParentType = gtk::Widget;
42+
}
43+
44+
/*
45+
* Trait Name:
46+
* ObjectImpl
47+
*
48+
* Description:
49+
* Trait shared by all GObjects
50+
*
51+
* Made:
52+
* 05/10/2022
53+
*
54+
* Made by:
55+
* Deren Vural
56+
*
57+
* Notes:
58+
*
59+
*/
60+
impl ObjectImpl for Formatter {
61+
/*
62+
* Name:
63+
* properties
64+
*
65+
* Description:
66+
* Create list of custom properties for our GObject
67+
*
68+
* Made:
69+
* 05/10/2022
70+
*
71+
* Made by:
72+
* Deren Vural
73+
*
74+
* Notes:
75+
* beware that you need to use kebab-case (https://en.wikipedia.org/wiki/Letter_case#Kebab_case)
76+
*
77+
* ParamSpec Examples:
78+
* glib::ParamSpecString::builder("icon").build(),
79+
* glib::ParamSpecUInt::builder("gpu_count").build(),
80+
* glib::ParamSpecString::builder("call_extension").build(),
81+
* TODO: these are from property class
82+
* glib::ParamSpecBoxed::builder("processor").build(),
83+
* glib::ParamSpecObject::builder("formatter").build(),
84+
*/
85+
fn properties() -> &'static [ParamSpec] {
86+
static PROPERTIES: Lazy<Vec<ParamSpec>> = Lazy::new(|| {
87+
vec![
88+
//
89+
]
90+
});
91+
92+
//println!("PROPERTIES: {:?}", PROPERTIES);//TEST
93+
//println!("trying to add `base_call`: {:?}", glib::ParamSpecString::builder("base_call").build());//TEST
94+
95+
PROPERTIES.as_ref()
96+
}
97+
98+
/*
99+
* Name:
100+
* set_property
101+
*
102+
* Description:
103+
* Mutator for custom GObject properties
104+
*
105+
* Made:
106+
* 05/10/2022
107+
*
108+
* Made by:
109+
* Deren Vural
110+
*
111+
* Notes:
112+
*
113+
*/
114+
fn set_property(&self, _obj: &Self::Type, _id: usize, _value: &Value, pspec: &ParamSpec) {
115+
//println!("setting: {:?}", pspec.name());//TEST
116+
117+
match pspec.name() {
118+
//
119+
_ => unimplemented!(), //TODO
120+
}
121+
}
122+
123+
/*
124+
* Name:
125+
* property
126+
*
127+
* Description:
128+
* Accessir for custom GObject properties
129+
*
130+
* Made:
131+
* 05/10/2022
132+
*
133+
* Made by:
134+
* Deren Vural
135+
*
136+
* Notes:
137+
*
138+
*/
139+
fn property(&self, _obj: &Self::Type, _id: usize, pspec: &ParamSpec) -> Value {
140+
//println!("getting: {:?}", pspec.name());//TEST
141+
142+
match pspec.name() {
143+
//
144+
_ => unimplemented!(), //TODO
145+
}
146+
}
147+
}
148+
149+
/*
150+
* Trait Name:
151+
* WidgetImpl
152+
*
153+
* Description:
154+
* Trait shared by all widgets
155+
*
156+
* Made:
157+
* 05/10/2022
158+
*
159+
* Made by:
160+
* Deren Vural
161+
*
162+
* Notes:
163+
* leaving blank atm, boilerplate
164+
*/
165+
impl WidgetImpl for Formatter {}

src/formatter/mod.rs

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
// SPDX-FileCopyrightText: 2022 Deren Vural
2+
// SPDX-License-Identifier: GPL-3.0-or-later
3+
4+
/*
5+
* Name:
6+
* mod.rs
7+
*
8+
* Description:
9+
* Public-facing interface/wrapper for our custom GObject (Formatter)
10+
*
11+
* Made:
12+
* 05/10/2022
13+
*
14+
* Made by:
15+
* Deren Vural
16+
*
17+
* Notes:
18+
*
19+
*/
20+
21+
// Custom GObjects
22+
mod imp;
23+
24+
// Imports
25+
use glib::Object;
26+
use gtk::{glib, prelude::ObjectExt};
27+
28+
// Modules
29+
//
30+
31+
// GObject wrapper for Formatter
32+
glib::wrapper! {
33+
pub struct Formatter(ObjectSubclass<imp::Formatter>)
34+
@extends gtk::Widget,
35+
@implements gtk::Accessible, gtk::Actionable, gtk::Buildable, gtk::ConstraintTarget;
36+
}
37+
38+
/*
39+
* Trait Name:
40+
* Formatter
41+
*
42+
* Description:
43+
* Trait shared by all properties
44+
*
45+
* Made:
46+
* 05/10/2022
47+
*
48+
* Made by:
49+
* Deren Vural
50+
*
51+
* Notes:
52+
*
53+
*/
54+
impl Formatter {
55+
/*
56+
* Name:
57+
* new
58+
*
59+
* Description:
60+
* Create a new Formatter object
61+
*
62+
* Made:
63+
* 05/10/2022
64+
*
65+
* Made by:
66+
* Deren Vural
67+
*
68+
* Notes:
69+
*
70+
*/
71+
pub fn new() -> Self {
72+
Object::new(&[]).expect("Failed to create `Formatter`.")
73+
}
74+
75+
76+
pub fn format(self, values: Vec<String>, func: fn(Vec<String>) -> Option<String>) -> Option<String>{
77+
let mut results: Vec<String> = Vec::new();
78+
79+
// For each item in input list
80+
for i in values {
81+
// Remove all non-number characters
82+
let cleaned_value: String = i.chars().filter(|c| {
83+
// check if (base 10) digit
84+
if c.is_digit(10) {
85+
true
86+
} else {
87+
// check if full-stop
88+
if c.eq(&'.') {
89+
true
90+
} else {
91+
false
92+
}
93+
}
94+
}).collect();
95+
96+
// Convert to float
97+
match cleaned_value.parse::<f64>() {
98+
Ok(parsed_value) => {
99+
// Convert to string
100+
results.push(parsed_value.to_string());
101+
},
102+
Err(err) => {
103+
// Catch any errors..
104+
println!("Not a valid number: {}", err);
105+
},
106+
}
107+
}
108+
109+
// Check for empty results
110+
if results.len() > 0 {
111+
// Apply any valid formatting
112+
match func(results) {
113+
Some(formatted_results) => Some(formatted_results),
114+
None => return None,
115+
}
116+
} else {
117+
None
118+
}
119+
}
120+
}
121+
122+
/*
123+
* Trait Name:
124+
* Default
125+
*
126+
* Description:
127+
* Default object
128+
*
129+
* Made:
130+
* 08/10/2022
131+
*
132+
* Made by:
133+
* Deren Vural
134+
*
135+
* Notes:
136+
*
137+
*/
138+
impl Default for Formatter {
139+
fn default() -> Self {
140+
Self::new()
141+
}
142+
}
143+

0 commit comments

Comments
 (0)