Skip to content

Commit 82f3dee

Browse files
authored
Merge pull request #8 from derenv/property
Property
2 parents 550c426 + e65b19b commit 82f3dee

File tree

6 files changed

+852
-24
lines changed

6 files changed

+852
-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: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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;
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+
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());
103+
}
104+
Err(err) => {
105+
// Catch any errors..
106+
println!("Not a valid number: {}", err);
107+
}
108+
}
109+
}
110+
111+
// Check for empty results
112+
if !results.is_empty() {
113+
// Apply any valid formatting
114+
func(results)
115+
} else {
116+
None
117+
}
118+
}
119+
}
120+
121+
/*
122+
* Trait Name:
123+
* Default
124+
*
125+
* Description:
126+
* Default object
127+
*
128+
* Made:
129+
* 08/10/2022
130+
*
131+
* Made by:
132+
* Deren Vural
133+
*
134+
* Notes:
135+
*
136+
*/
137+
impl Default for Formatter {
138+
fn default() -> Self {
139+
Self::new()
140+
}
141+
}

0 commit comments

Comments
 (0)