Skip to content

Commit

Permalink
Updated src/property/imp.rs:
Browse files Browse the repository at this point in the history
- Updated reuse copyright year
- Added clearer import headers
- Refactored to now import std::sync::OncelLock as it has been [merged into std](rust-lang/rust#105587)
- Refactored to import Value and ToValue from glib::value as it now has its own module in glib
- Refactored "properties()" functions to reflect OnceLock changes
- Removed now unused "_obj" parameter to "property()" and "set_property()" functions

Updated src/property/mod.rs:
- Updated reuse copyright year
- Added clearer import headers
- Added import of FromValue from glib::value
- Updated "new()" function to use "Object::builder::<Property>().build()" instead of untyped "Obect::new()""
- Commented out "update_value()" function as it is unused

Signed-off-by: Deren Vural <[email protected]>
  • Loading branch information
derenv committed May 20, 2024
1 parent debda60 commit 8de4b47
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 40 deletions.
38 changes: 26 additions & 12 deletions src/property/imp.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-FileCopyrightText: 2022 Deren Vural
// SPDX-FileCopyrightText: 2024 Deren Vural
// SPDX-License-Identifier: GPL-3.0-or-later

use adwaita::glib;
/**
* Name:
* imp.rs
Expand All @@ -19,9 +18,16 @@ use adwaita::glib;
*
*/
// Imports
use glib::{once_cell::sync::Lazy, ParamSpec, ToValue, Value};
use gtk::subclass::prelude::*;
// std
use std::sync::OnceLock;
use std::cell::Cell;
// gtk-rs
use gtk::subclass::prelude::*;
use adwaita::glib;
use glib::{
ParamSpec,
value::ToValue, value::Value
};

// Modules
use crate::formatter::Formatter;
Expand Down Expand Up @@ -81,18 +87,17 @@ impl ObjectImpl for Property {
* beware that you need to use kebab-case (<https://en.wikipedia.org/wiki/Letter_case#Kebab_case>)
*/
fn properties() -> &'static [ParamSpec] {
static PROPERTIES: Lazy<Vec<ParamSpec>> = Lazy::new(|| {
static PROPERTIES: OnceLock<Vec<ParamSpec>> = OnceLock::new();
PROPERTIES.get_or_init(|| {
vec![
glib::ParamSpecString::builder("id").build(),
glib::ParamSpecObject::builder("processor", glib::Type::OBJECT).build(),
glib::ParamSpecObject::builder("formatter", glib::Type::OBJECT).build(),
glib::ParamSpecObject::builder::<Processor>("processor").build(),
glib::ParamSpecObject::builder::<Formatter>("formatter").build(),
]
});
})

//println!("PROPERTIES: {:?}", PROPERTIES);//TEST
//println!("trying to add `base_call`: {:?}", glib::ParamSpecString::builder("base_call").build());//TEST

PROPERTIES.as_ref()
}

/**
Expand All @@ -111,7 +116,12 @@ impl ObjectImpl for Property {
* Notes:
*
*/
fn set_property(&self, _obj: &Self::Type, _id: usize, value: &Value, pspec: &ParamSpec) {
fn set_property(
&self,
_id: usize,
value: &Value,
pspec: &ParamSpec
) {
//println!("setting: {:?}", pspec.name());//TEST

match pspec.name() {
Expand Down Expand Up @@ -153,7 +163,11 @@ impl ObjectImpl for Property {
* Notes:
*
*/
fn property(&self, _obj: &Self::Type, _id: usize, pspec: &ParamSpec) -> Value {
fn property(
&self,
_id: usize,
pspec: &ParamSpec
) -> Value {
//println!("getting: {:?}", pspec.name());//TEST

match pspec.name() {
Expand Down
79 changes: 51 additions & 28 deletions src/property/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2022 Deren Vural
// SPDX-FileCopyrightText: 2024 Deren Vural
// SPDX-License-Identifier: GPL-3.0-or-later

/**
Expand All @@ -20,10 +20,18 @@
// Custom GObjects
mod imp;

use gdk::glib::value::FromValue;
// Imports
use glib::Object;
use gtk::{glib, prelude::*};
// std
//
// gtk-rs
use gtk::{
prelude::*,
glib
};
use glib::{
value::FromValue,
Object
};

// Modules
use crate::formatter::Formatter;
Expand Down Expand Up @@ -72,8 +80,13 @@ impl Property {
*
* given proc and gpuCount
*/
pub fn new(processor: &Processor, formatter: &Formatter, id: &str) -> Self {
let obj: Property = Object::new(&[]).expect("Failed to create `Property`.");
pub fn new(
processor: &Processor,
formatter: &Formatter,
id: &str
) -> Self {
// Create Object
let obj: Property = Object::builder::<Property>().build();

// Set properties
obj.set_property("processor", processor);
Expand All @@ -100,7 +113,10 @@ impl Property {
* Notes:
*
*/
pub fn parse(self, uuid: &str) -> Option<String> {
pub fn parse(
self,
uuid: &str
) -> Option<String> {
// println!("UUID: `{}`", uuid); //TEST
// Grab formatter & processor
let formatter: Formatter = self.property("formatter");
Expand Down Expand Up @@ -159,31 +175,38 @@ impl Property {
* Notes:
*
*/
pub fn get_value<T: for<'a> FromValue<'a> + 'static>(&self, name: &str) -> T {
pub fn get_value<T: for<'a> FromValue<'a> + 'static>(
&self,
name: &str
) -> T {
// Return the value of the property
self.property::<T>(name)
}

/**
* Name:
* update_value
*
* Description:
* Update a property with a new value
*
* Made:
* 29/10/2022
*
* Made by:
* Deren Vural
*
* Notes:
*
*/
pub fn update_value<T: ToValue>(&self, property_name: &str, value: T) {
// Update the property with new value
self.set_property(property_name, value);
}
// /**
// * Name:
// * update_value
// *
// * Description:
// * Update a property with a new value
// *
// * Made:
// * 29/10/2022
// *
// * Made by:
// * Deren Vural
// *
// * Notes:
// *
// */
// pub fn update_value<T: Into<Variant>>(
// &self,
// property_name: &str,
// value: T
// ) {
// // Update the property with new value
// self.set_property(property_name, value);
// }
}

/**
Expand Down

0 comments on commit 8de4b47

Please sign in to comment.