Skip to content

Commit 39c6ed0

Browse files
author
Deren Vural
committed
Updated Processor Class
- Added docs in comments - Properly commented traits and functions - Changed to Cell<String> as custom properties should be stored in Cells (and Strings are nicer) - Added properties() function to ObjectImpl to register custom GObject properties - Added set_property() function to ObjectImpl as custom GObject properties mutator - Added property() function to ObjectImpl as custom GObject properties accessor - Finished working constructor, calls to set_property() function with args - Removed test from parse() and removed old version - Added mutator calls to process() function (finally!) - Cleaned up comments in mod.rs - Commented test prints in process() function (will probably need later) Signed-off-by: Deren Vural <[email protected]>
1 parent 25b8901 commit 39c6ed0

File tree

2 files changed

+266
-38
lines changed

2 files changed

+266
-38
lines changed

src/processor/imp.rs

Lines changed: 179 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,27 @@
1515
* Deren Vural
1616
*
1717
* Notes:
18-
*
18+
* https://github.com/gtk-rs/gtk4-rs/blob/master/book/src/g_object_properties.md
19+
* https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/g_object_properties/4/custom_button/imp.rs
20+
* https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/g_object_properties/4/custom_button/mod.rs
1921
*/
2022

2123
// Imports
22-
use gtk::glib;
24+
use gtk::glib::{ self, ParamSpec, Value };
25+
use gtk::prelude::*;
2326
use gtk::subclass::prelude::*;
27+
use gtk::glib::once_cell::sync::Lazy;
28+
use std::cell::Cell;
2429

25-
// Object holding the state
30+
// Object holding the State
2631
#[derive(Default)]
2732
pub struct Processor {
28-
name: &'static str,
29-
//base_call: &'static str,
30-
//call: &'static str,
31-
//tail_call: &'static str,
33+
base_call: Cell<String>,
34+
call: Cell<String>,
35+
tail_call: Cell<String>,
3236
}
3337

38+
3439
// The central trait for subclassing a GObject
3540
#[glib::object_subclass]
3641
impl ObjectSubclass for Processor {
@@ -42,8 +47,172 @@ impl ObjectSubclass for Processor {
4247
type ParentType = gtk::Widget;
4348
}
4449

45-
// Trait shared by all GObjects
46-
impl ObjectImpl for Processor {}
50+
/*
51+
* Trait Name:
52+
* ObjectImpl
53+
*
54+
* Description:
55+
* Trait shared by all GObjects
56+
*
57+
* Made:
58+
* 05/10/2022
59+
*
60+
* Made by:
61+
* Deren Vural
62+
*
63+
* Notes:
64+
*
65+
*/
66+
impl ObjectImpl for Processor {
67+
/*
68+
* Name:
69+
* properties
70+
*
71+
* Description:
72+
* Create list of custom properties for our GObject
73+
*
74+
* Made:
75+
* 05/10/2022
76+
*
77+
* Made by:
78+
* Deren Vural
79+
*
80+
* Notes:
81+
* beware that you need to use kebab-case (https://en.wikipedia.org/wiki/Letter_case#Kebab_case)
82+
*
83+
* ParamSpec Examples:
84+
* glib::ParamSpecString::builder("icon").build(),
85+
* glib::ParamSpecUInt::builder("gpu_count").build(),
86+
* glib::ParamSpecString::builder("call_extension").build(),
87+
* TODO: these are from property class
88+
* glib::ParamSpecBoxed::builder("processor").build(),
89+
* glib::ParamSpecObject::builder("formatter").build(),
90+
*/
91+
fn properties() -> &'static [ParamSpec] {
92+
static PROPERTIES: Lazy<Vec<ParamSpec>> =
93+
Lazy::new(|| {
94+
vec![
95+
glib::ParamSpecString::builder("base-call").build(),
96+
glib::ParamSpecString::builder("call").build(),
97+
glib::ParamSpecString::builder("tail-call").build(),
98+
]
99+
}
100+
);
101+
102+
//println!("PROPERTIES: {:?}", PROPERTIES);//TEST
103+
//println!("trying to add `base_call`: {:?}", glib::ParamSpecString::builder("base_call").build());//TEST
104+
105+
PROPERTIES.as_ref()
106+
}
107+
108+
/*
109+
* Name:
110+
* set_property
111+
*
112+
* Description:
113+
* Mutator for custom GObject properties
114+
*
115+
* Made:
116+
* 05/10/2022
117+
*
118+
* Made by:
119+
* Deren Vural
120+
*
121+
* Notes:
122+
*
123+
*/
124+
fn set_property(
125+
&self,
126+
_obj: &Self::Type,
127+
_id: usize,
128+
value: &Value,
129+
pspec: &ParamSpec,
130+
) {
131+
//println!("setting: {:?}", pspec.name());//TEST
132+
133+
match pspec.name() {
134+
"base-call" => {
135+
let input_base_call =
136+
value.get().expect("The value needs to be of type `String`.");
137+
self.base_call.replace(input_base_call);
138+
},
139+
"call" => {
140+
let input_call =
141+
value.get().expect("The value needs to be of type `String`.");
142+
self.call.replace(input_call);
143+
},
144+
"tail-call" => {
145+
let input_tail_call =
146+
value.get().expect("The value needs to be of type `String`.");
147+
self.tail_call.replace( input_tail_call);
148+
},
149+
_ => unimplemented!(),//TODO
150+
}
151+
}
152+
153+
/*
154+
* Name:
155+
* property
156+
*
157+
* Description:
158+
* Accessir for custom GObject properties
159+
*
160+
* Made:
161+
* 05/10/2022
162+
*
163+
* Made by:
164+
* Deren Vural
165+
*
166+
* Notes:
167+
*
168+
*/
169+
fn property(&self, _obj: &Self::Type, _id: usize, pspec: &ParamSpec) -> Value {
170+
//println!("getting: {:?}", pspec.name());//TEST
171+
172+
match pspec.name() {
173+
"base-call" => {
174+
//TODO: this seems ridiculous..
175+
let value = self.base_call.take();
47176

48-
// Trait shared by all widgets
177+
self.base_call.set(value.clone());
178+
179+
return value.to_value();
180+
},
181+
"call" => {
182+
//TODO: this seems ridiculous..
183+
let value = self.call.take();
184+
185+
self.call.set(value.clone());
186+
187+
return value.to_value();
188+
},
189+
"tail-call" => {
190+
//TODO: this seems ridiculous..
191+
let value = self.tail_call.take();
192+
193+
self.tail_call.set(value.clone());
194+
195+
return value.to_value();
196+
},
197+
_ => unimplemented!(),//TODO
198+
}
199+
}
200+
}
201+
202+
/*
203+
* Trait Name:
204+
* WidgetImpl
205+
*
206+
* Description:
207+
* Trait shared by all widgets
208+
*
209+
* Made:
210+
* 18/09/2022
211+
*
212+
* Made by:
213+
* Deren Vural
214+
*
215+
* Notes:
216+
* leaving blank atm, boilerplate
217+
*/
49218
impl WidgetImpl for Processor {}

src/processor/mod.rs

Lines changed: 87 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,34 +25,83 @@ mod imp;
2525
// Imports
2626
use super::subprocess;
2727
use glib::Object;
28-
use gtk::{ glib, gio };
28+
use gtk::{ glib, gio, prelude::ObjectExt };
2929
use std::ffi::OsStr;
3030

3131
glib::wrapper! {
32-
//pub struct Processor(ObjectSubclass<imp::Processor<'a>>)
3332
pub struct Processor(ObjectSubclass<imp::Processor>)
3433
@extends gtk::Widget,
3534
@implements gtk::Accessible, gtk::Actionable, gtk::Buildable, gtk::ConstraintTarget;
3635
}
3736

38-
// Trait shared by all processors
37+
/*
38+
* Trait Name:
39+
* Processor
40+
*
41+
* Description:
42+
* Trait shared by all processors
43+
*
44+
* Made:
45+
* 18/09/2022
46+
*
47+
* Made by:
48+
* Deren Vural
49+
*
50+
* Notes:
51+
*
52+
*/
3953
impl Processor {
40-
pub fn new(name: &'static str, /*base_call: &'static str, tail_call: &'static str*/) -> Self {
41-
Object::new(&[
42-
("name", &name),/*
43-
("base_call", &base_call),
44-
("call", &base_call.clone()),
45-
("tail_call", &tail_call),*/
46-
]).expect("Failed to create `Processor`")
54+
/*
55+
* Name:
56+
* new
57+
*
58+
* Description:
59+
* Create a new Processor object
60+
*
61+
* Made:
62+
* 18/09/2022
63+
*
64+
* Made by:
65+
* Deren Vural
66+
*
67+
* Notes:
68+
*
69+
*/
70+
pub fn new(base_call: &'static str, tail_call: &'static str) -> Self {
71+
let obj: Processor = Object::new(&[]).expect("Failed to create `Processor`");
72+
73+
// TODO: set properties
74+
obj.set_property("base-call", base_call.to_string());
75+
obj.set_property("call", base_call.to_string().clone());
76+
obj.set_property("tail-call", tail_call.to_string());
77+
78+
return obj;
4779
}
4880

81+
/*
82+
* Name:
83+
* process
84+
*
85+
* Description:
86+
* Runs call stack and return result
87+
*
88+
* Made:
89+
* 18/09/2022
90+
*
91+
* Made by:
92+
* Deren Vural
93+
*
94+
* Notes:
95+
* we'll know what possible sizes will exist (wherever this gets implemented)
96+
*/
4997
pub fn process(self) -> Result<Option<String>, glib::Error> {
50-
// Create string of args
51-
//NOTE: we'll know what possible sizes will exist (wherever this gets implemented)
52-
let mut call_stack: String = String::from("nvidia-settings");
53-
let tail_call: &str = "-q GpuUUID -t";
98+
// Create call stack of program and args
99+
let tail_call = self.property::<String>("tail-call");
100+
let mut call_stack = self.property::<String>("call");
54101
call_stack.push_str(" ");
55-
call_stack.push_str(tail_call);
102+
call_stack.push_str(tail_call.as_str());
103+
104+
// Turn call stack into bytes and create vector for final call stack
56105
let call_stack_bytes: &[u8] = call_stack.as_bytes();
57106
let mut call_stack_items: Vec<&OsStr> = Vec::new();
58107

@@ -64,7 +113,7 @@ impl Processor {
64113
let item_osstr: &OsStr;
65114
match std::str::from_utf8(&call_stack_bytes[start..i]) {
66115
Ok(result) => {
67-
println!("item: {}", result);
116+
//println!("item: {}", result);//TEST
68117
item_osstr = OsStr::new(result)
69118
},
70119
Err(err) => panic!("{}", err),
@@ -77,7 +126,7 @@ impl Processor {
77126
let item_osstr: &OsStr;
78127
match std::str::from_utf8(&call_stack_bytes[start..]) {
79128
Ok(result) => {
80-
println!("item: {}", result);
129+
//println!("item: {}", result);//TEST
81130
item_osstr = OsStr::new(result)
82131
},
83132
Err(err) => panic!("{}", err),
@@ -86,7 +135,7 @@ impl Processor {
86135
}
87136
}
88137

89-
// Build OsStr array from vector
138+
// Build OsStr array from vector (if matching a specific size)
90139
match call_stack_items.len() {
91140
4 => {
92141
// Build array
@@ -166,19 +215,29 @@ impl Processor {
166215
todo!()
167216
//self.name.
168217
}
169-
170-
fn parse(self, input: &String) -> String {
171-
// Grab input string as owned, then return
172-
//(this function is designed to be overloaded by subclasses)
173-
input.replace("\n", "").to_owned()
174-
}
175218
*/
176219

220+
/*
221+
* Name:
222+
* parse
223+
*
224+
* Description:
225+
* Grab input string as owned, append test formatting and then return
226+
*
227+
* Made:
228+
* 18/09/2022
229+
*
230+
* Made by:
231+
* Deren Vural
232+
*
233+
* Notes:
234+
* This function is designed to be overloaded by subclasses
235+
*/
177236
fn parse(self, input: &String) -> String {
178-
// Grab input string as owned, append test formatting and then return
179-
let mut output = input.replace("\n", "").to_owned();
180-
output.push_str("-FUCK");
237+
//NOTE: leaving this here for future use..
238+
//let mut output = input.replace("\n", "").to_owned();
239+
//output.push_str("-FUCK");
181240

182-
output
241+
input.replace("\n", "").to_owned()
183242
}
184243
}

0 commit comments

Comments
 (0)