Skip to content

Commit 5e2e7ab

Browse files
author
Deren Vural
committed
working provider class
Signed-off-by: Deren Vural <[email protected]>
1 parent c3b6025 commit 5e2e7ab

File tree

4 files changed

+152
-11
lines changed

4 files changed

+152
-11
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: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,18 @@ use gtk::prelude::*;
2525
use gtk::subclass::prelude::*;
2626
use std::cell::Cell;
2727

28+
// Modules
29+
use crate::property::Property;
30+
2831
// Object holding the State
2932
#[derive(Default)]
30-
pub struct Provider;
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+
}
3140

3241
// The central trait for subclassing a GObject
3342
#[glib::object_subclass]
@@ -82,7 +91,13 @@ impl ObjectImpl for Provider {
8291
* glib::ParamSpecObject::builder("formatter").build(),
8392
*/
8493
fn properties() -> &'static [ParamSpec] {
85-
static PROPERTIES: Lazy<Vec<ParamSpec>> = Lazy::new(|| vec![]);
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+
]);
86101

87102
//println!("PROPERTIES: {:?}", PROPERTIES);//TEST
88103
//println!("trying to add `base_call`: {:?}", glib::ParamSpecString::builder("base_call").build());//TEST
@@ -110,6 +125,36 @@ impl ObjectImpl for Provider {
110125
//println!("setting: {:?}", pspec.name());//TEST
111126

112127
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+
}
113158
_ => unimplemented!(), //TODO
114159
}
115160
}
@@ -134,6 +179,46 @@ impl ObjectImpl for Provider {
134179
//println!("getting: {:?}", pspec.name());//TEST
135180

136181
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+
}
137222
_ => unimplemented!(), //TODO
138223
}
139224
}

src/provider/mod.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
// Custom GObjects
2222
mod imp;
2323

24+
use crate::property::Property;
25+
2426
// Imports
25-
use super::subprocess;
2627
use glib::Object;
27-
use gtk::{gio, glib, prelude::ObjectExt};
28-
use std::ffi::OsStr;
28+
use gtk::{ glib, prelude::ObjectExt };
29+
//use shell::*;
30+
use gtk::gio;
2931

3032
// GObject wrapper for Provider
3133
glib::wrapper! {
@@ -34,6 +36,14 @@ glib::wrapper! {
3436
@implements gtk::Accessible, gtk::Actionable, gtk::Buildable, gtk::ConstraintTarget;
3537
}
3638

39+
#[derive(Default)]
40+
pub enum ProviderType {
41+
#[default]
42+
NvidiaSettings = 0,
43+
NvidiaSmi = 1,
44+
NvidiaOptimus = 2,
45+
}
46+
3747
/*
3848
* Trait Name:
3949
* Provider
@@ -67,14 +77,30 @@ impl Provider {
6777
* Notes:
6878
*
6979
*/
70-
pub fn new() -> Self {
80+
pub fn new(func: fn() -> Vec<Property>/*, provider_type: ProviderType*/) -> Self {
7181
let obj: Provider = Object::new(&[]).expect("Failed to create `Provider`");
7282

73-
// TODO: set properties
74-
//obj.set_property("call", base_call.to_string().clone());
83+
//obj.set_property("provider-type", provider_type);
84+
85+
// Set properties
86+
let properties: Vec<Property> = func();
87+
obj.set_property("utilization", properties[0].clone());
88+
obj.set_property("temperature", properties[1].clone());
89+
obj.set_property("memory-usage", properties[2].clone());
90+
obj.set_property("fan-speed", properties[3].clone());
91+
if properties.len() == 5 {
92+
obj.set_property("power-usage", properties[4].clone());
93+
}
7594

7695
obj
7796
}
97+
98+
pub fn open_settings() {
99+
//let defaultAppSystem = Shell.AppSystem.get_default();
100+
//let nvidiaSettingsApp = defaultAppSystem.lookup_app('nvidia-settings.desktop');
101+
//let def = shell::Edge::Top;
102+
let dd = gio::DesktopAppInfo::from_filename("nvidia-settings.desktop");
103+
}
78104
}
79105

80106
/*
@@ -95,6 +121,6 @@ impl Provider {
95121
*/
96122
impl Default for Provider {
97123
fn default() -> Self {
98-
Self::new()
124+
Self::new(|| Vec::new()/*, ProviderType::NvidiaSettings*/)
99125
}
100126
}

0 commit comments

Comments
 (0)