|
| 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