Skip to content

Commit 882c0e3

Browse files
committed
Updated src/gpu_page/imp.rs:
- 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 <35734401+derenv@users.noreply.github.com>
1 parent dbea3a1 commit 882c0e3

File tree

2 files changed

+92
-46
lines changed

2 files changed

+92
-46
lines changed

src/gpu_page/imp.rs

+75-39
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2022 Deren Vural
1+
// SPDX-FileCopyrightText: 2024 Deren Vural
22
// SPDX-License-Identifier: GPL-3.0-or-later
33

44
/*
@@ -19,18 +19,33 @@
1919
*/
2020

2121
// Imports
22-
use adwaita::{gio, glib, prelude::*, ViewStack, ViewSwitcherBar};
23-
use gio::Settings;
24-
use glib::{
25-
once_cell::sync::Lazy, once_cell::sync::OnceCell, subclass::InitializingObject,
26-
subclass::Signal, subclass::SignalType, translate::FromGlib, FromVariant, ParamSpec, SourceId,
27-
ToValue, Value,
22+
// std
23+
use std::sync::{
24+
OnceLock, Arc, Mutex, MutexGuard
25+
};
26+
use std::cell::{
27+
Cell, OnceCell, RefCell
2828
};
29+
use std::rc::Rc;
30+
// gtk-rs
2931
use gtk::{
30-
subclass::prelude::*, Align, CompositeTemplate, Grid, Label, LayoutChild, Orientation,
31-
TemplateChild,
32+
subclass::prelude::*,
33+
Align, CompositeTemplate, Grid, Label, LayoutChild, Orientation, TemplateChild
34+
};
35+
use adwaita::{
36+
gio, glib,
37+
prelude::*,
38+
ViewStack, ViewSwitcherBar
39+
};
40+
use gio::Settings;
41+
use glib::{
42+
subclass::Signal,
43+
translate::FromGlib,
44+
subclass::InitializingObject, ParamSpec, SourceId,
45+
variant::FromVariant, variant::Variant,
46+
value::ToValue, value::Value,
47+
ControlFlow::Continue
3248
};
33-
use std::{cell::Cell, cell::RefCell, rc::Rc, sync::Arc, sync::Mutex, sync::MutexGuard};
3449

3550
// Modules
3651
use crate::{modificationwindow::ModificationWindow, provider::Provider};
@@ -114,7 +129,10 @@ impl GpuPage {
114129
* Notes:
115130
*
116131
*/
117-
pub fn get_setting<T: FromVariant>(&self, name: &str) -> T {
132+
pub fn get_setting<T: FromVariant>(
133+
&self,
134+
name: &str
135+
) -> T {
118136
// Return the value of the property
119137
match self.settings.get() {
120138
Some(settings) => settings.get::<T>(name),
@@ -138,7 +156,11 @@ impl GpuPage {
138156
* Notes:
139157
*
140158
*/
141-
pub fn update_setting<T: ToVariant>(&self, name: &str, value: T) {
159+
pub fn update_setting<T: Into<Variant> + Clone>(
160+
&self,
161+
name: &str,
162+
value: T
163+
) {
142164
// Fetch settings
143165
match self.settings.get() {
144166
Some(settings) => match settings.set(name, &value) {
@@ -165,7 +187,10 @@ impl GpuPage {
165187
* Notes:
166188
*
167189
*/
168-
pub fn replace_stack(&self, stack: Option<&ViewStack>) {
190+
pub fn replace_stack(
191+
&self,
192+
stack: Option<&ViewStack>
193+
) {
169194
self.view_switcher.set_stack(stack);
170195
}
171196

@@ -462,26 +487,27 @@ impl GpuPage {
462487
}
463488
Err(err) => {
464489
println!("panicked when fetching gpu data: `{}`", err);
465-
return Continue(false);
490+
return Continue;
466491
}
467492
}
468493
}
469494
}
470495
}
471496
None => {
472497
println!("panicked when fetching current provider..");
473-
return Continue(false);
498+
return Continue;
474499
}
475500
}
476501

477-
Continue(true)
502+
Continue
478503
});
479504

480505
// !!UNSAFE CODE HERE!!!!UNSAFE CODE HERE!!!!UNSAFE CODE HERE!!!!UNSAFE CODE HERE!!
481506
// Save ID of recurring closure
482-
unsafe {
483-
self.refreshid.set(id.as_raw());
484-
}
507+
self.refreshid.set(id.as_raw());
508+
// unsafe {
509+
// self.refreshid.set(id.as_raw());
510+
// }
485511
// !!UNSAFE CODE HERE!!!!UNSAFE CODE HERE!!!!UNSAFE CODE HERE!!!!UNSAFE CODE HERE!!
486512
}
487513

@@ -607,13 +633,14 @@ impl ObjectImpl for GpuPage {
607633
* Notes:
608634
*
609635
*/
610-
fn constructed(&self, obj: &Self::Type) {
636+
fn constructed(&self) {
611637
// println!("CONSTRUCTED");//TEST
612638
// Call "constructed" on parent
613-
self.parent_constructed(obj);
639+
self.parent_constructed();
614640

615641
// Setup
616642
self.refreshid.set(0);
643+
let _obj: glib::BorrowedObject<super::GpuPage> = self.obj();
617644
//obj.setup_settings();
618645
//obj.load_properties();//TODO
619646
//obj.setup_widgets();
@@ -646,20 +673,18 @@ impl ObjectImpl for GpuPage {
646673
* glib::ParamSpecObject::builder("formatter").build(),
647674
*/
648675
fn properties() -> &'static [ParamSpec] {
649-
//println!("PROPERTIES");//TEST
650-
static PROPERTIES: Lazy<Vec<ParamSpec>> = Lazy::new(|| {
676+
static PROPERTIES: OnceLock<Vec<ParamSpec>> = OnceLock::new();
677+
PROPERTIES.get_or_init(|| {
651678
vec![
652679
glib::ParamSpecString::builder("uuid").build(),
653680
glib::ParamSpecString::builder("name").build(),
654-
glib::ParamSpecObject::builder("provider", glib::Type::OBJECT).build(),
655-
glib::ParamSpecUInt::builder("refreshid").build(),
681+
glib::ParamSpecObject::builder::<Provider>("provider").build(),
682+
glib::ParamSpecUInt::builder("refreshid").build()
656683
]
657-
});
684+
})
658685

659686
//println!("PROPERTIES: {:?}", PROPERTIES);//TEST
660687
//println!("trying to add `base_call`: {:?}", glib::ParamSpecString::builder("base_call").build());//TEST
661-
662-
PROPERTIES.as_ref()
663688
}
664689

665690
/**
@@ -678,7 +703,12 @@ impl ObjectImpl for GpuPage {
678703
* Notes:
679704
*
680705
*/
681-
fn set_property(&self, _obj: &Self::Type, _id: usize, value: &Value, pspec: &ParamSpec) {
706+
fn set_property(
707+
&self,
708+
_id: usize,
709+
value: &Value,
710+
pspec: &ParamSpec
711+
) {
682712
//println!("setting: {:?}", pspec.name());//TEST
683713

684714
// println!("setting: {:?}", pspec.name());//TEST
@@ -744,7 +774,11 @@ impl ObjectImpl for GpuPage {
744774
* Notes:
745775
*
746776
*/
747-
fn property(&self, _obj: &Self::Type, _id: usize, pspec: &ParamSpec) -> Value {
777+
fn property(
778+
&self,
779+
_id: usize,
780+
pspec: &ParamSpec
781+
) -> Value {
748782
//println!("getting: {:?}", pspec.name());//TEST
749783

750784
match pspec.name() {
@@ -792,15 +826,17 @@ impl ObjectImpl for GpuPage {
792826
* SignalType::from(i32::static_type())
793827
*/
794828
fn signals() -> &'static [Signal] {
795-
static SIGNALS: Lazy<Vec<Signal>> = Lazy::new(|| {
796-
vec![Signal::builder(
797-
"update-views",
798-
&[SignalType::from(i32::static_type())],
799-
SignalType::from(i32::static_type()),
800-
)
801-
.build()]
802-
});
803-
SIGNALS.as_ref()
829+
static SIGNALS: OnceLock<Vec<Signal>> = OnceLock::new();
830+
SIGNALS.get_or_init(|| {
831+
vec![
832+
Signal::builder("update-all-views")
833+
.param_types([i32::static_type()])
834+
.return_type::<i32>()
835+
.build()
836+
]
837+
})
838+
839+
//println!("PROPERTIES: {:?}", PROPERTIES);//TEST
804840
}
805841
}
806842

src/gpu_page/mod.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2022 Deren Vural
1+
// SPDX-FileCopyrightText: 2024 Deren Vural
22
// SPDX-License-Identifier: GPL-3.0-or-later
33

44
/**
@@ -22,11 +22,21 @@ mod imp;
2222
use imp::ModificationWindowContainer;
2323

2424
// Imports
25-
use adwaita::{gio, glib, Application, ViewStack};
26-
use gio::Settings;
27-
use glib::{clone, closure, Object};
28-
use gtk::{prelude::*, subclass::prelude::*, Align, Button, Grid, Label, LayoutChild, Orientation};
25+
// std
2926
use std::cell::RefMut;
27+
// gtk-rs
28+
use adwaita::{
29+
gio, glib,
30+
Application, ViewStack
31+
};
32+
use gio::Settings;
33+
use glib::{
34+
clone, closure, Object
35+
};
36+
use gtk::{
37+
prelude::*, subclass::prelude::*,
38+
Align, Button, Grid, Label, LayoutChild, Orientation
39+
};
3040

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

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

0 commit comments

Comments
 (0)