Skip to content

Commit

Permalink
Updated src/gpu_page/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::cell::OnceCell as it has been [merged into std](rust-lang/rust#105587)
- 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 to import Variant and FromVariant from glib::variant as it now has its own module in glib
- Added glib::ControlFlow::Continue import
- Updated "create_updater()" function to reflect glib::ControlFlow::Continue changes
- Removed an unsafe block of code from "create_updater()" function
- Refactored "constructed()" function to get "obj" reference via "self" instead of as a parameter
- Updated "parent_constructed()" call in "constructed()" function
- Refactored "properties()" and "signals()" functions to reflect OnceLock changes
- Removed now unused "_obj" parameter to "property()" and "set_property()" functions

Updated src/gpu_page/mod.rs:
- Updated reuse copyright year
- Added clearer import headers
- Updated "new()" function to use "Object::builder::<GpuPage>().build()" instead of untyped "Obect::new()""

Signed-off-by: Deren Vural <[email protected]>
  • Loading branch information
derenv committed May 20, 2024
1 parent dbea3a1 commit 882c0e3
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 46 deletions.
114 changes: 75 additions & 39 deletions src/gpu_page/imp.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 @@ -19,18 +19,33 @@
*/

// Imports
use adwaita::{gio, glib, prelude::*, ViewStack, ViewSwitcherBar};
use gio::Settings;
use glib::{
once_cell::sync::Lazy, once_cell::sync::OnceCell, subclass::InitializingObject,
subclass::Signal, subclass::SignalType, translate::FromGlib, FromVariant, ParamSpec, SourceId,
ToValue, Value,
// std
use std::sync::{
OnceLock, Arc, Mutex, MutexGuard
};
use std::cell::{
Cell, OnceCell, RefCell
};
use std::rc::Rc;
// gtk-rs
use gtk::{
subclass::prelude::*, Align, CompositeTemplate, Grid, Label, LayoutChild, Orientation,
TemplateChild,
subclass::prelude::*,
Align, CompositeTemplate, Grid, Label, LayoutChild, Orientation, TemplateChild
};
use adwaita::{
gio, glib,
prelude::*,
ViewStack, ViewSwitcherBar
};
use gio::Settings;
use glib::{
subclass::Signal,
translate::FromGlib,
subclass::InitializingObject, ParamSpec, SourceId,
variant::FromVariant, variant::Variant,
value::ToValue, value::Value,
ControlFlow::Continue
};
use std::{cell::Cell, cell::RefCell, rc::Rc, sync::Arc, sync::Mutex, sync::MutexGuard};

// Modules
use crate::{modificationwindow::ModificationWindow, provider::Provider};
Expand Down Expand Up @@ -114,7 +129,10 @@ impl GpuPage {
* Notes:
*
*/
pub fn get_setting<T: FromVariant>(&self, name: &str) -> T {
pub fn get_setting<T: FromVariant>(
&self,
name: &str
) -> T {
// Return the value of the property
match self.settings.get() {
Some(settings) => settings.get::<T>(name),
Expand All @@ -138,7 +156,11 @@ impl GpuPage {
* Notes:
*
*/
pub fn update_setting<T: ToVariant>(&self, name: &str, value: T) {
pub fn update_setting<T: Into<Variant> + Clone>(
&self,
name: &str,
value: T
) {
// Fetch settings
match self.settings.get() {
Some(settings) => match settings.set(name, &value) {
Expand All @@ -165,7 +187,10 @@ impl GpuPage {
* Notes:
*
*/
pub fn replace_stack(&self, stack: Option<&ViewStack>) {
pub fn replace_stack(
&self,
stack: Option<&ViewStack>
) {
self.view_switcher.set_stack(stack);
}

Expand Down Expand Up @@ -462,26 +487,27 @@ impl GpuPage {
}
Err(err) => {
println!("panicked when fetching gpu data: `{}`", err);
return Continue(false);
return Continue;
}
}
}
}
}
None => {
println!("panicked when fetching current provider..");
return Continue(false);
return Continue;
}
}

Continue(true)
Continue
});

// !!UNSAFE CODE HERE!!!!UNSAFE CODE HERE!!!!UNSAFE CODE HERE!!!!UNSAFE CODE HERE!!
// Save ID of recurring closure
unsafe {
self.refreshid.set(id.as_raw());
}
self.refreshid.set(id.as_raw());
// unsafe {
// self.refreshid.set(id.as_raw());
// }
// !!UNSAFE CODE HERE!!!!UNSAFE CODE HERE!!!!UNSAFE CODE HERE!!!!UNSAFE CODE HERE!!
}

Expand Down Expand Up @@ -607,13 +633,14 @@ impl ObjectImpl for GpuPage {
* Notes:
*
*/
fn constructed(&self, obj: &Self::Type) {
fn constructed(&self) {
// println!("CONSTRUCTED");//TEST
// Call "constructed" on parent
self.parent_constructed(obj);
self.parent_constructed();

// Setup
self.refreshid.set(0);
let _obj: glib::BorrowedObject<super::GpuPage> = self.obj();
//obj.setup_settings();
//obj.load_properties();//TODO
//obj.setup_widgets();
Expand Down Expand Up @@ -646,20 +673,18 @@ impl ObjectImpl for GpuPage {
* glib::ParamSpecObject::builder("formatter").build(),
*/
fn properties() -> &'static [ParamSpec] {
//println!("PROPERTIES");//TEST
static PROPERTIES: Lazy<Vec<ParamSpec>> = Lazy::new(|| {
static PROPERTIES: OnceLock<Vec<ParamSpec>> = OnceLock::new();
PROPERTIES.get_or_init(|| {
vec![
glib::ParamSpecString::builder("uuid").build(),
glib::ParamSpecString::builder("name").build(),
glib::ParamSpecObject::builder("provider", glib::Type::OBJECT).build(),
glib::ParamSpecUInt::builder("refreshid").build(),
glib::ParamSpecObject::builder::<Provider>("provider").build(),
glib::ParamSpecUInt::builder("refreshid").build()
]
});
})

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

PROPERTIES.as_ref()
}

/**
Expand All @@ -678,7 +703,12 @@ impl ObjectImpl for GpuPage {
* 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

// println!("setting: {:?}", pspec.name());//TEST
Expand Down Expand Up @@ -744,7 +774,11 @@ impl ObjectImpl for GpuPage {
* 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 Expand Up @@ -792,15 +826,17 @@ impl ObjectImpl for GpuPage {
* SignalType::from(i32::static_type())
*/
fn signals() -> &'static [Signal] {
static SIGNALS: Lazy<Vec<Signal>> = Lazy::new(|| {
vec![Signal::builder(
"update-views",
&[SignalType::from(i32::static_type())],
SignalType::from(i32::static_type()),
)
.build()]
});
SIGNALS.as_ref()
static SIGNALS: OnceLock<Vec<Signal>> = OnceLock::new();
SIGNALS.get_or_init(|| {
vec![
Signal::builder("update-all-views")
.param_types([i32::static_type()])
.return_type::<i32>()
.build()
]
})

//println!("PROPERTIES: {:?}", PROPERTIES);//TEST
}
}

Expand Down
24 changes: 17 additions & 7 deletions src/gpu_page/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 @@ -22,11 +22,21 @@ mod imp;
use imp::ModificationWindowContainer;

// Imports
use adwaita::{gio, glib, Application, ViewStack};
use gio::Settings;
use glib::{clone, closure, Object};
use gtk::{prelude::*, subclass::prelude::*, Align, Button, Grid, Label, LayoutChild, Orientation};
// std
use std::cell::RefMut;
// gtk-rs
use adwaita::{
gio, glib,
Application, ViewStack
};
use gio::Settings;
use glib::{
clone, closure, Object
};
use gtk::{
prelude::*, subclass::prelude::*,
Align, Button, Grid, Label, LayoutChild, Orientation
};

// Modules
use crate::{modificationwindow::ModificationWindow, provider::Provider, APP_ID};
Expand Down Expand Up @@ -72,8 +82,8 @@ impl GpuPage {
*
*/
pub fn new(uuid: &str, name: &str, provider: Provider) -> Self {
// Create new page
let obj: GpuPage = Object::new(&[]).expect("Failed to create `GpuPage`.");
// Create new page
let obj: GpuPage = Object::builder::<GpuPage>().build();

// Set custom properties
obj.set_property("uuid", String::from(uuid));
Expand Down

0 comments on commit 882c0e3

Please sign in to comment.