Skip to content

Commit

Permalink
Updated src/modificationwindow/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)
- 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()"function to reflect OnceLock changes
- Removed now unused "_obj" parameter to "property()" and "set_property()" functions
- Refactored to use glib::signal::Propagation instead of glib::signal::Inhibit
- 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 src/modificationwindow/mod.rs:
- Updated reuse copyright year
- Added clearer import headers
- Removed GString import
- Updated "new()" function to use "Object::builder::<ModificationWindow>().build()" instead of untyped "Obect::new()""
- Commented out "settings()" function
- Refactored "setup_widgets()" function to use self.imp().get_setting::<Vec<String>>() instead of getting GString directly

Signed-off-by: Deren Vural <35734401+derenv@users.noreply.github.com>
  • Loading branch information
derenv committed May 20, 2024
1 parent e578080 commit eec6a8a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 37 deletions.
72 changes: 50 additions & 22 deletions src/modificationwindow/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 @@ -18,17 +18,29 @@
*
*/
// Imports
use adwaita::{gio, glib, prelude::*, subclass::prelude::*, ActionRow};
use gio::Settings;
use glib::{
once_cell::sync::Lazy, once_cell::sync::OnceCell, signal::Inhibit,
subclass::InitializingObject, FromVariant, ParamSpec, Value,
// std
use std::sync::OnceLock;
use std::cell::{
Cell, OnceCell, RefCell
};
use std::rc::Rc;
// gtk-rs
use gtk::{
subclass::prelude::*, Button, CompositeTemplate, DropDown, Entry, ListBox, SpinButton,
StringList, TemplateChild,
subclass::prelude::*,
Button, CompositeTemplate, DropDown, Entry, ListBox, SpinButton, StringList, TemplateChild
};
use adwaita::{
gio, glib,
prelude::*, subclass::prelude::*,
ActionRow
};
use gio::Settings;
use glib::{
signal::Propagation,
subclass::InitializingObject, ParamSpec,
variant::FromVariant, variant::Variant,
value::Value
};
use std::{cell::Cell, cell::RefCell, rc::Rc};

// Modules
use crate::gpu_page::GpuPage;
Expand Down Expand Up @@ -825,7 +837,10 @@ impl ModificationWindow {
* 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 @@ -849,7 +864,11 @@ impl ModificationWindow {
* 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 Down Expand Up @@ -1082,11 +1101,12 @@ impl ObjectImpl for ModificationWindow {
* Notes:
*
*/
fn constructed(&self, obj: &Self::Type) {
fn constructed(&self) {
// Call "constructed" on parent
self.parent_constructed(obj);
self.parent_constructed();

// Setup
let _obj: glib::BorrowedObject<super::ModificationWindow> = self.obj();
// obj.setup_settings();
// obj.setup_widgets();
// obj.restore_data();
Expand Down Expand Up @@ -1119,20 +1139,19 @@ impl ObjectImpl for ModificationWindow {
* glib::ParamSpecObject::builder("formatter").build(),
*/
fn properties() -> &'static [ParamSpec] {
static PROPERTIES: Lazy<Vec<ParamSpec>> = Lazy::new(|| {
static PROPERTIES: OnceLock<Vec<ParamSpec>> = OnceLock::new();
PROPERTIES.get_or_init(|| {
vec![
glib::ParamSpecInt::builder("old-view-id").build(),
glib::ParamSpecInt::builder("new-view-id").build(),
glib::ParamSpecString::builder("old-view-title").build(),
glib::ParamSpecString::builder("new-view-title").build(),
glib::ParamSpecString::builder("uuid").build(),
glib::ParamSpecString::builder("uuid").build()
]
});
})

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

PROPERTIES.as_ref()
}

/**
Expand All @@ -1151,7 +1170,12 @@ impl ObjectImpl for ModificationWindow {
* 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 @@ -1204,7 +1228,11 @@ impl ObjectImpl for ModificationWindow {
* 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 @@ -1284,12 +1312,12 @@ impl WindowImpl for ModificationWindow {
* Notes:
*
*/
fn close_request(&self, window: &Self::Type) -> Inhibit {
fn close_request(&self) -> Propagation {
// Store state in settings
self.update_setting("modification-open", false);

// Pass close request on to the parent
self.parent_close_request(window)
self.parent_close_request()
}
}

Expand Down
44 changes: 29 additions & 15 deletions src/modificationwindow/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 @@ -21,11 +21,21 @@
mod imp;

// Imports
use adwaita::{gio, glib, prelude::*, subclass::prelude::*, ActionRow};
use gio::Settings;
use glib::{clone, GString, Object};
use gtk::{Adjustment, DropDown, StringList};
// std
use std::cell::RefMut;
// gtk-rs
use adwaita::{
gio, glib,
prelude::*, subclass::prelude::*,
ActionRow
};
use gio::Settings;
use glib::{
clone, Object
};
use gtk::{
Adjustment, DropDown, StringList
};

// Modules
use crate::{
Expand Down Expand Up @@ -81,8 +91,9 @@ impl ModificationWindow {
parent_window: &GpuPage,
) -> Self {
// Create new window
let obj: ModificationWindow = Object::new(&[("application", app)])
.expect("`ModificationWindow` should be instantiable.");
let obj: ModificationWindow = Object::builder::<ModificationWindow>()
.property("application", app)
.build();

// Set custom properties
obj.set_property("old-view-id", view_id);
Expand Down Expand Up @@ -144,12 +155,12 @@ impl ModificationWindow {
* Notes:
*
*/
fn settings(&self) -> &Settings {
self.imp()
.settings
.get()
.expect("`settings` should be set in `setup_settings`.")
}
// fn settings(&self) -> &Settings {
// self.imp()
// .settings
// .get()
// .expect("`settings` should be set in `setup_settings`.")
// }

/**
* Name:
Expand All @@ -169,7 +180,9 @@ impl ModificationWindow {
*/
fn setup_widgets(&self) {
// Retrieve names of stored views
let view_title_list: Vec<GString> = self.settings().strv("viewconfigs");
let view_title_list: Vec<String> = self.imp().get_setting::<Vec<String>>("viewconfigs");
// let view_title_list: Vec<GString> = self.imp().get_setting::<Vec<GString>>("viewconfigs");
// let view_title_list: Vec<GString> = self.settings().strv("viewconfigs");
let view_id: String = self.property::<i32>("old-view-id").to_string();

// Create empty string for the title
Expand Down Expand Up @@ -209,7 +222,8 @@ impl ModificationWindow {
self.set_property("new-view-title", view_title);

// Retrieve list of in-use properties
let view_components_list = self.settings().strv("viewcomponentconfigs");
let view_components_list: Vec<String> = self.imp().get_setting::<Vec<String>>("viewcomponentconfigs");
// let view_components_list = self.settings().strv("viewcomponentconfigs");
// println!("Possible Components: {:?}", view_components_list); //TEST

// Create list of components in current view
Expand Down

0 comments on commit eec6a8a

Please sign in to comment.