Skip to content

Commit da6e35c

Browse files
authored
Merge pull request #9 from derenv/provider
Provider
2 parents 82f3dee + 5e2e7ab commit da6e35c

File tree

4 files changed

+401
-2
lines changed

4 files changed

+401
-2
lines changed

src/property/imp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
// Imports
2222
use gtk::glib::once_cell::sync::Lazy;
2323
use gtk::glib::{self, ParamSpec, Value};
24-
use gtk::prelude::*;
2524
use gtk::subclass::prelude::*;
2625
use std::cell::Cell;
26+
use glib::ToValue;
2727

2828
// Modules
2929
use crate::formatter::Formatter;

src/property/mod.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ mod imp;
2424
// Imports
2525
use glib::Object;
2626
use gtk::glib;
27-
use gtk::prelude::ObjectExt;
27+
use gtk::prelude::*;
2828

2929
// Modules
3030
use crate::formatter::Formatter;
@@ -119,4 +119,34 @@ impl Property {
119119
pub fn get_call_extension(&self) -> String {
120120
self.property::<String>("call-extension")
121121
}
122+
123+
pub fn open_settings(&self) {
124+
if self.property::<bool>("settings") {
125+
//
126+
} else {
127+
//
128+
}
129+
}
130+
}
131+
132+
/*
133+
* Trait Name:
134+
* Default
135+
*
136+
* Description:
137+
* Default object
138+
*
139+
* Made:
140+
* 09/10/2022
141+
*
142+
* Made by:
143+
* Deren Vural
144+
*
145+
* Notes:
146+
*
147+
*/
148+
impl Default for Property {
149+
fn default() -> Self {
150+
Self::new(&Processor::new("", ""), &"", &"", &Formatter::new(), &0)
151+
}
122152
}

src/provider/imp.rs

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
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 (Provider)
10+
*
11+
* Made:
12+
* 06/10/2022
13+
*
14+
* Made by:
15+
* Deren Vural
16+
*
17+
* Notes:
18+
*
19+
*/
20+
21+
// Imports
22+
use gtk::glib::once_cell::sync::Lazy;
23+
use gtk::glib::{self, ParamSpec, Value};
24+
use gtk::prelude::*;
25+
use gtk::subclass::prelude::*;
26+
use std::cell::Cell;
27+
28+
// Modules
29+
use crate::property::Property;
30+
31+
// Object holding the State
32+
#[derive(Default)]
33+
pub struct Provider {
34+
utilization: Cell<Property>,
35+
temperature: Cell<Property>,
36+
memory_usage: Cell<Property>,
37+
fan_speed: Cell<Property>,
38+
power_usage: Cell<Property>,
39+
}
40+
41+
// The central trait for subclassing a GObject
42+
#[glib::object_subclass]
43+
impl ObjectSubclass for Provider {
44+
//Crate+Obj to avoid collisions
45+
const NAME: &'static str = "NvidiaExtensionRustProcessor";
46+
// the actual GObject that will be created
47+
type Type = super::Provider;
48+
// Parent GObject we inherit from
49+
type ParentType = gtk::Widget;
50+
}
51+
52+
/*
53+
* Trait Name:
54+
* ObjectImpl
55+
*
56+
* Description:
57+
* Trait shared by all GObjects
58+
*
59+
* Made:
60+
* 06/10/2022
61+
*
62+
* Made by:
63+
* Deren Vural
64+
*
65+
* Notes:
66+
*
67+
*/
68+
impl ObjectImpl for Provider {
69+
/*
70+
* Name:
71+
* properties
72+
*
73+
* Description:
74+
* Create list of custom properties for our GObject
75+
*
76+
* Made:
77+
* 06/10/2022
78+
*
79+
* Made by:
80+
* Deren Vural
81+
*
82+
* Notes:
83+
* beware that you need to use kebab-case (https://en.wikipedia.org/wiki/Letter_case#Kebab_case)
84+
*
85+
* ParamSpec Examples:
86+
* glib::ParamSpecString::builder("icon").build(),
87+
* glib::ParamSpecUInt::builder("gpu_count").build(),
88+
* glib::ParamSpecString::builder("call_extension").build(),
89+
* TODO: these are from property class
90+
* glib::ParamSpecBoxed::builder("processor").build(),
91+
* glib::ParamSpecObject::builder("formatter").build(),
92+
*/
93+
fn properties() -> &'static [ParamSpec] {
94+
static PROPERTIES: Lazy<Vec<ParamSpec>> = Lazy::new(|| vec![
95+
glib::ParamSpecObject::builder("utilization-property", glib::Type::OBJECT).build(),
96+
glib::ParamSpecObject::builder("temperature-property", glib::Type::OBJECT).build(),
97+
glib::ParamSpecObject::builder("memory-usage-property", glib::Type::OBJECT).build(),
98+
glib::ParamSpecObject::builder("fan-speed-property", glib::Type::OBJECT).build(),
99+
glib::ParamSpecObject::builder("power-usage-property", glib::Type::OBJECT).build(),
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+
* 06/10/2022
117+
*
118+
* Made by:
119+
* Deren Vural
120+
*
121+
* Notes:
122+
*
123+
*/
124+
fn set_property(&self, _obj: &Self::Type, _id: usize, value: &Value, pspec: &ParamSpec) {
125+
//println!("setting: {:?}", pspec.name());//TEST
126+
127+
match pspec.name() {
128+
"utilization" => {
129+
let input_utilization_property = value
130+
.get()
131+
.expect("The value needs to be of type `Property`.");
132+
self.utilization.replace(input_utilization_property);
133+
}
134+
"temperature" => {
135+
let input_temperature_property = value
136+
.get()
137+
.expect("The value needs to be of type `Property`.");
138+
self.temperature.replace(input_temperature_property);
139+
}
140+
"memory-usage" => {
141+
let input_memory_usage_property = value
142+
.get()
143+
.expect("The value needs to be of type `Property`.");
144+
self.memory_usage.replace(input_memory_usage_property);
145+
}
146+
"fan-speed" => {
147+
let input_fan_speed_property = value
148+
.get()
149+
.expect("The value needs to be of type `Property`.");
150+
self.fan_speed.replace(input_fan_speed_property);
151+
}
152+
"power-usage" => {
153+
let input_power_usage_property = value
154+
.get()
155+
.expect("The value needs to be of type `Property`.");
156+
self.power_usage.replace(input_power_usage_property);
157+
}
158+
_ => unimplemented!(), //TODO
159+
}
160+
}
161+
162+
/*
163+
* Name:
164+
* property
165+
*
166+
* Description:
167+
* Accessor for custom GObject properties
168+
*
169+
* Made:
170+
* 06/10/2022
171+
*
172+
* Made by:
173+
* Deren Vural
174+
*
175+
* Notes:
176+
*
177+
*/
178+
fn property(&self, _obj: &Self::Type, _id: usize, pspec: &ParamSpec) -> Value {
179+
//println!("getting: {:?}", pspec.name());//TEST
180+
181+
match pspec.name() {
182+
"utilization" => {
183+
//TODO: this seems ridiculous..
184+
let value = self.utilization.take();
185+
186+
self.utilization.set(value.clone());
187+
188+
value.to_value()
189+
}
190+
"temperature" => {
191+
//TODO: this seems ridiculous..
192+
let value = self.temperature.take();
193+
194+
self.temperature.set(value.clone());
195+
196+
value.to_value()
197+
}
198+
"memory-usage" => {
199+
//TODO: this seems ridiculous..
200+
let value = self.memory_usage.take();
201+
202+
self.memory_usage.set(value.clone());
203+
204+
value.to_value()
205+
}
206+
"fan-speed" => {
207+
//TODO: this seems ridiculous..
208+
let value = self.fan_speed.take();
209+
210+
self.fan_speed.set(value.clone());
211+
212+
value.to_value()
213+
}
214+
"power-usage" => {
215+
//TODO: this seems ridiculous..
216+
let value = self.power_usage.take();
217+
218+
self.power_usage.set(value.clone());
219+
220+
value.to_value()
221+
}
222+
_ => unimplemented!(), //TODO
223+
}
224+
}
225+
}
226+
227+
/*
228+
* Trait Name:
229+
* WidgetImpl
230+
*
231+
* Description:
232+
* Trait shared by all widgets
233+
*
234+
* Made:
235+
* 06/10/2022
236+
*
237+
* Made by:
238+
* Deren Vural
239+
*
240+
* Notes:
241+
* leaving blank atm, boilerplate
242+
*/
243+
impl WidgetImpl for Provider {}

0 commit comments

Comments
 (0)