Skip to content

Commit e578080

Browse files
committed
Updated src/mainwindow/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) - Added glib::BorrowedObject import - Refactored to import Value 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 - Refactored "properties()" and "signals()" functions to reflect OnceLock changes - Refactored to import glib::signal::Propagation instead of glib::signal::Inhibit - Removed now unused "_obj" parameter to "property()" and "set_property()" functions - Refactored "constructed()" function to get "obj" reference via "self" instead of as a parameter - Updated "parent_constructed()" call in "constructed()" function - Removed deprecated "window" parameter from "close_request()" function - Updated "close_request()" function to return a Propagation type - Updated "parent_close_request()" call in "close_request()" function - Updated "signals()" function to use requisite functions of Signal::builder instead of parameters Updated src/mainwindow/mod.rs: - Updated reuse copyright year - Added clearer import headers - Updated "new()" function to use "Object::builder::<MainWindow>().build()" instead of untyped "Obect::new()"" Signed-off-by: Deren Vural <35734401+derenv@users.noreply.github.com>
1 parent 04d0ec2 commit e578080

File tree

2 files changed

+92
-42
lines changed

2 files changed

+92
-42
lines changed

src/mainwindow/imp.rs

Lines changed: 73 additions & 36 deletions
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
/**
@@ -18,23 +18,35 @@
1818
*
1919
*/
2020
// Imports
21-
use adwaita::{gio, glib, prelude::*, subclass::prelude::*};
22-
use gio::Settings;
23-
use glib::{
24-
once_cell::sync::Lazy, once_cell::sync::OnceCell, signal::Inhibit,
25-
subclass::InitializingObject, subclass::Signal, subclass::SignalType, FromVariant, ParamSpec,
26-
Value,
21+
// std
22+
use std::sync::OnceLock;
23+
use std::cell::{
24+
Cell, OnceCell, RefCell, RefMut
2725
};
26+
use std::rc::Rc;
27+
// gtk-rs
2828
use gtk::{
29-
subclass::prelude::*, Button, CompositeTemplate, PolicyType, ScrolledWindow, Stack,
30-
TemplateChild,
29+
subclass::prelude::*,
30+
Button, CompositeTemplate, PolicyType, ScrolledWindow, Stack, TemplateChild
31+
};
32+
use adwaita::{
33+
gio, glib,
34+
prelude::*, subclass::prelude::*
35+
};
36+
use gio::Settings;
37+
use glib::{
38+
subclass::Signal, signal::Propagation,
39+
subclass::InitializingObject, ParamSpec,
40+
variant::FromVariant, variant::Variant,
41+
value::Value,
42+
BorrowedObject
3143
};
32-
use std::{cell::Cell, cell::RefCell, cell::RefMut, rc::Rc};
3344

3445
// Modules
3546
use crate::{
36-
formatter::Formatter, gpu_page::GpuPage, processor::Processor, property::Property,
37-
provider::Provider, settingswindow::SettingsWindow,
47+
settingswindow::SettingsWindow,
48+
gpu_page::GpuPage,
49+
formatter::Formatter, processor::Processor, property::Property, provider::Provider
3850
};
3951

4052
/// Structure for storing a SettingsWindow object and any related information
@@ -118,7 +130,10 @@ impl MainWindow {
118130
* Notes:
119131
*
120132
*/
121-
pub fn get_setting<T: FromVariant>(&self, name: &str) -> T {
133+
pub fn get_setting<T: FromVariant>(
134+
&self,
135+
name: &str
136+
) -> T {
122137
// Return the value of the property
123138
match self.settings.get() {
124139
Some(settings) => settings.get::<T>(name),
@@ -142,7 +157,11 @@ impl MainWindow {
142157
* Notes:
143158
*
144159
*/
145-
pub fn update_setting<T: ToVariant>(&self, name: &str, value: T) {
160+
pub fn update_setting<T: Into<Variant> + Clone>(
161+
&self,
162+
name: &str,
163+
value: T
164+
) {
146165
// Fetch settings
147166
match self.settings.get() {
148167
Some(settings) => match settings.set(name, &value) {
@@ -169,7 +188,12 @@ impl MainWindow {
169188
* Notes:
170189
*
171190
*/
172-
fn create_gpu_page(&self, uuid: &str, name: &str, provider: Provider) {
191+
fn create_gpu_page(
192+
&self,
193+
uuid: &str,
194+
name: &str,
195+
provider: Provider
196+
) {
173197
// Create new GpuPage object
174198
let new_page: GpuPage = GpuPage::new(uuid, name, provider);
175199

@@ -208,7 +232,10 @@ impl MainWindow {
208232
* Notes:
209233
*
210234
*/
211-
pub fn create_provider(&self, provider_type: i32) -> Provider {
235+
pub fn create_provider(
236+
&self,
237+
provider_type: i32
238+
) -> Provider {
212239
//println!("CREATING PROVIDER");
213240
//println!("..PROVIDER TYPE: `{}`", provider_type);
214241

@@ -1603,11 +1630,12 @@ impl ObjectImpl for MainWindow {
16031630
* Notes:
16041631
*
16051632
*/
1606-
fn constructed(&self, obj: &Self::Type) {
1633+
fn constructed(&self) {
16071634
// Call "constructed" on parent
1608-
self.parent_constructed(obj);
1635+
self.parent_constructed();
16091636

16101637
// Setup
1638+
let obj: BorrowedObject<super::MainWindow> = self.obj();
16111639
obj.setup_settings();
16121640
obj.setup_widgets();
16131641
obj.restore_data();
@@ -1640,14 +1668,15 @@ impl ObjectImpl for MainWindow {
16401668
* glib::ParamSpecObject::builder("formatter").build(),
16411669
*/
16421670
fn properties() -> &'static [ParamSpec] {
1643-
static PROPERTIES: Lazy<Vec<ParamSpec>> = Lazy::new(|| {
1644-
vec![glib::ParamSpecObject::builder("provider", glib::Type::OBJECT).build()]
1645-
});
1671+
static PROPERTIES: OnceLock<Vec<ParamSpec>> = OnceLock::new();
1672+
PROPERTIES.get_or_init(|| {
1673+
vec![
1674+
glib::ParamSpecObject::builder::<Provider>("provider").build()
1675+
]
1676+
})
16461677

16471678
//println!("PROPERTIES: {:?}", PROPERTIES);//TEST
16481679
//println!("trying to add `base_call`: {:?}", glib::ParamSpecString::builder("base_call").build());//TEST
1649-
1650-
PROPERTIES.as_ref()
16511680
}
16521681

16531682
/**
@@ -1666,7 +1695,12 @@ impl ObjectImpl for MainWindow {
16661695
* Notes:
16671696
*
16681697
*/
1669-
fn set_property(&self, _obj: &Self::Type, _id: usize, value: &Value, pspec: &ParamSpec) {
1698+
fn set_property(
1699+
&self,
1700+
_id: usize,
1701+
value: &Value,
1702+
pspec: &ParamSpec
1703+
) {
16701704
//println!("setting: {:?}", pspec.name());//TEST
16711705

16721706
match pspec.name() {
@@ -1696,7 +1730,11 @@ impl ObjectImpl for MainWindow {
16961730
* Notes:
16971731
*
16981732
*/
1699-
fn property(&self, _obj: &Self::Type, _id: usize, pspec: &ParamSpec) -> Value {
1733+
fn property(
1734+
&self,
1735+
_id: usize,
1736+
pspec: &ParamSpec
1737+
) -> Value {
17001738
//println!("getting: {:?}", pspec.name());//TEST
17011739

17021740
match pspec.name() {
@@ -1733,15 +1771,14 @@ impl ObjectImpl for MainWindow {
17331771
* SignalType::from(i32::static_type())
17341772
*/
17351773
fn signals() -> &'static [Signal] {
1736-
static SIGNALS: Lazy<Vec<Signal>> = Lazy::new(|| {
1737-
vec![Signal::builder(
1738-
"update-all-views",
1739-
&[],
1740-
SignalType::from(i32::static_type()),
1741-
)
1742-
.build()]
1743-
});
1744-
SIGNALS.as_ref()
1774+
static SIGNALS: OnceLock<Vec<Signal>> = OnceLock::new();
1775+
SIGNALS.get_or_init(|| {
1776+
vec![
1777+
Signal::builder("update-all-views")
1778+
.return_type::<i32>()
1779+
.build()
1780+
]
1781+
})
17451782
}
17461783
}
17471784

@@ -1796,7 +1833,7 @@ impl WindowImpl for MainWindow {
17961833
* Notes:
17971834
*
17981835
*/
1799-
fn close_request(&self, window: &Self::Type) -> Inhibit {
1836+
fn close_request(&self) -> Propagation {
18001837
/*
18011838
//NOTE: this was meant for saving to json, probably unecessary now that i'm not using it..
18021839
@@ -1853,7 +1890,7 @@ impl WindowImpl for MainWindow {
18531890
self.update_setting("modification-open", false);
18541891

18551892
// Pass close request on to the parent
1856-
self.parent_close_request(window)
1893+
self.parent_close_request()
18571894
}
18581895
}
18591896

src/mainwindow/mod.rs

Lines changed: 19 additions & 6 deletions
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,10 +22,19 @@ mod imp;
2222
use imp::SettingsWindowContainer;
2323

2424
// Imports
25-
use adwaita::{gio, glib, prelude::*, subclass::prelude::*};
26-
use gio::{Settings, SimpleAction};
27-
use glib::{clone, closure, Object};
25+
// std
2826
use std::cell::RefMut;
27+
// gtk-rs
28+
use adwaita::{
29+
gio, glib,
30+
prelude::*, subclass::prelude::*
31+
};
32+
use gio::{
33+
Settings, SimpleAction
34+
};
35+
use glib::{
36+
clone, closure, Object
37+
};
2938

3039
// Modules
3140
use crate::{provider::Provider, settingswindow::SettingsWindow, APP_ID};
@@ -71,9 +80,13 @@ impl MainWindow {
7180
* Notes:
7281
*
7382
*/
74-
pub fn new(app: &adwaita::Application) -> Self {
83+
pub fn new(
84+
app: &adwaita::Application
85+
) -> Self {
7586
// Create new window
76-
Object::new(&[("application", app)]).expect("`MainWindow` should be instantiable.")
87+
Object::builder::<MainWindow>()
88+
.property("application", app)
89+
.build()
7790
}
7891

7992
/**

0 commit comments

Comments
 (0)