Skip to content

Commit 7f8accd

Browse files
author
Deren Vural
committed
Updated MainWindow class
- Removed unused app_id property - Added update_setting generic function - Added get_setting generic function - Refactor to replace expect/unwrap instances with full match statements - Added gtk properties functions - "provider" now a property, easier to access - Updated mod.rs to use new setting/property functions - Refactor to replace to_string()/into_owned() instances with String::from() - Removed some uneeded comment blocks Updated Processor class - Refactor to replace to_string()/into_owned() instances with String::from() - Simplified some match statements Updated Property class - Refactor to replace to_string()/into_owned() instances with String::from() - Added update_value generic function - Added get_value generic function - Refactor to replace expect/unwrap instances with full match statements Updated Provider class - Refactor to replace to_string()/into_owned() instances with String::from() - Removed some uneeded comment blocks Updated SettingsWindow class - Added update_setting generic function - Updated mod.rs to use new setting/property functions - Refactor to replace expect/unwrap instances with full match statements Signed-off-by: Deren Vural <[email protected]>
1 parent a11478f commit 7f8accd

File tree

8 files changed

+367
-270
lines changed

8 files changed

+367
-270
lines changed

src/mainwindow/imp.rs

Lines changed: 205 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// Imports
2222
use adwaita::{gio, glib, prelude::*, subclass::prelude::*, ActionRow};
2323
use gio::Settings;
24-
use glib::{once_cell::sync::OnceCell, signal::Inhibit, subclass::InitializingObject};
24+
use glib::{once_cell::sync::OnceCell, signal::Inhibit, subclass::InitializingObject, FromVariant, once_cell::sync::Lazy, ParamSpec, Value};
2525
use gtk::{subclass::prelude::*, CompositeTemplate, ListBox, TemplateChild};
2626
use std::{cell::Cell, cell::RefCell, rc::Rc};
2727

@@ -40,7 +40,6 @@ pub struct SettingsWindowContainer {
4040
#[template(resource = "/main-window.ui")]
4141
pub struct MainWindow {
4242
pub settings: OnceCell<Settings>,
43-
pub app_id: Cell<String>,
4443
pub settings_window: Rc<RefCell<SettingsWindowContainer>>,
4544
pub provider: Cell<Option<Provider>>,
4645

@@ -89,6 +88,61 @@ impl ObjectSubclass for MainWindow {
8988
*/
9089
#[gtk::template_callbacks]
9190
impl MainWindow {
91+
/*
92+
* Name:
93+
* update_setting
94+
*
95+
* Description:
96+
* Generic function for updating setting values
97+
*
98+
* Made:
99+
* 30/10/2022
100+
*
101+
* Made by:
102+
* Deren Vural
103+
*
104+
* Notes:
105+
*
106+
*/
107+
pub fn update_setting<T: ToVariant>(&self, name: &str, value: T) {
108+
// Fetch settings
109+
match self.settings.get() {
110+
Some(settings) => {
111+
match settings.set(name, &value) {
112+
Ok(_) => println!("..Setting `{}` updated!", name),
113+
Err(err) => panic!("..Cannot update `{}` setting: `{}`", name, err),
114+
}
115+
},
116+
None => panic!("..Cannot retrieve settings")
117+
}
118+
}
119+
120+
/*
121+
* Name:
122+
* get_setting
123+
*
124+
* Description:
125+
* Generic function for getting setting value
126+
*
127+
* Made:
128+
* 30/10/2022
129+
*
130+
* Made by:
131+
* Deren Vural
132+
*
133+
* Notes:
134+
*
135+
*/
136+
pub fn get_setting<T: FromVariant>(&self, name: &str) -> T {
137+
// Return the value of the property
138+
match self.settings.get() {
139+
Some(settings) => {
140+
settings.get::<T>(name)
141+
},
142+
None => panic!("`settings` should be set in `setup_settings`.")
143+
}
144+
}
145+
92146
/*
93147
* Name:
94148
* card_selected
@@ -379,6 +433,8 @@ impl MainWindow {
379433
}
380434
}
381435

436+
437+
382438
/*
383439
* Name:
384440
* refresh_cards
@@ -397,9 +453,11 @@ impl MainWindow {
397453
*/
398454
#[template_callback]
399455
fn refresh_cards(&self, button: &CustomButton) {
400-
//TEST: Grab button label
401-
let label_val = button.label().expect("cannot grab label of refresh button");
402-
println!("Button Pressed: {}", label_val);
456+
//TEST
457+
match button.label() {
458+
Some(label_val) => println!("Button Pressed: {}", label_val),
459+
None => panic!("..Cannot grab label of refresh button")
460+
}
403461

404462
// Clear current ActionRow objects from GtkListBox
405463
let mut done: bool = false;
@@ -434,41 +492,37 @@ impl MainWindow {
434492

435493
// Update each property
436494
match existing_provider.update_property_value::<i32>("gpu-count", gpu_count) {
437-
Ok(_result) => {}
495+
Ok(_) => {
496+
// Construct a row for each GPU
497+
for uuid in gpu_uuids {
498+
// Get GPU data
499+
let gpu_name = "GPU NAME XXQ";//existing_provider.get_gpu_data(uuid, "name");
500+
501+
// Create new ActionRow object
502+
let current_row: ActionRow =
503+
ActionRow::builder()
504+
.title(gpu_name)
505+
.subtitle(&uuid)
506+
.activatable(true)
507+
.selectable(true)
508+
.build();
509+
510+
// Append new ActionRow object to GtkListBox
511+
self.cards_list.append(&current_row);
512+
}
513+
}
438514
Err(err) => println!("..Attempt to read GPU data failed, returning: {}", err),
439515
}
440-
441-
// Construct a row for each GPU
442-
for uuid in gpu_uuids {
443-
// Get GPU data
444-
let gpu_name = "GPU NAME XXQ";//existing_provider.get_gpu_data(uuid, "name");
445-
446-
// Create new ActionRow object
447-
let current_row: ActionRow =
448-
ActionRow::builder()
449-
.title(gpu_name)
450-
.subtitle(&uuid)
451-
.activatable(true)
452-
.selectable(true)
453-
.build();
454-
455-
// Append new ActionRow object to GtkListBox
456-
self.cards_list.append(&current_row);
457-
}
458516
}
459-
Err(err) => println!("..Attempt to read GPU data failed, returning: {}", err),
517+
Err(err) => println!("..Attempt to update GPU list failed, returning: {}", err),
460518
}
461519

462520
// Re-Store provider
463521
self.provider.set(Some(existing_provider));
464522
}
465523
None => {
466524
// Check provider type
467-
let provider_type: i32 = self
468-
.settings
469-
.get()
470-
.expect("..cannot fetch settings")
471-
.int("provider");
525+
let provider_type: i32 = self.get_setting::<i32>("provider");
472526

473527
let new_provider: Provider = MainWindow::create_provider(provider_type);
474528

@@ -480,29 +534,29 @@ impl MainWindow {
480534

481535
// Update each property
482536
match new_provider.update_property_value::<i32>("gpu-count", gpu_count) {
483-
Ok(_result) => {}
537+
Ok(_) => {
538+
// Construct a row for each GPU
539+
for uuid in gpu_uuids {
540+
// Get GPU data
541+
let gpu_name = "GPU NAME QXX";//existing_provider.get_gpu_data(uuid, "name");
542+
543+
// Create new ActionRow object
544+
let current_row: ActionRow =
545+
ActionRow::builder()
546+
.title(gpu_name)
547+
.subtitle(&uuid)
548+
.activatable(true)
549+
.selectable(true)
550+
.build();
551+
552+
// Append new ActionRow object to GtkListBox
553+
self.cards_list.append(&current_row);
554+
}
555+
}
484556
Err(err) => println!("..Attempt to read GPU data failed, returning: {}", err),
485557
}
486-
487-
// Construct a row for each GPU
488-
for uuid in gpu_uuids {
489-
// Get GPU data
490-
let gpu_name = "GPU NAME QXX";//existing_provider.get_gpu_data(uuid, "name");
491-
492-
// Create new ActionRow object
493-
let current_row: ActionRow =
494-
ActionRow::builder()
495-
.title(gpu_name)
496-
.subtitle(&uuid)
497-
.activatable(true)
498-
.selectable(true)
499-
.build();
500-
501-
// Append new ActionRow object to GtkListBox
502-
self.cards_list.append(&current_row);
503-
}
504558
}
505-
Err(err) => println!("..Attempt to read GPU data failed, returning: {}", err),
559+
Err(err) => println!("..Attempt to update GPU list failed, returning: {}", err),
506560
}
507561

508562
// Store new provider
@@ -539,6 +593,105 @@ impl ObjectImpl for MainWindow {
539593
obj.setup_callbacks();
540594
obj.setup_actions();
541595
}
596+
597+
/*
598+
* Name:
599+
* properties
600+
*
601+
* Description:
602+
* Create list of custom properties for our GObject
603+
*
604+
* Made:
605+
* 06/10/2022
606+
*
607+
* Made by:
608+
* Deren Vural
609+
*
610+
* Notes:
611+
* beware that you need to use kebab-case (https://en.wikipedia.org/wiki/Letter_case#Kebab_case)
612+
*
613+
* ParamSpec Examples:
614+
* glib::ParamSpecString::builder("icon").build(),
615+
* glib::ParamSpecUInt::builder("gpu_count").build(),
616+
* glib::ParamSpecString::builder("call_extension").build(),
617+
* TODO: these are from property class
618+
* glib::ParamSpecBoxed::builder("processor").build(),
619+
* glib::ParamSpecObject::builder("formatter").build(),
620+
*/
621+
fn properties() -> &'static [ParamSpec] {
622+
static PROPERTIES: Lazy<Vec<ParamSpec>> = Lazy::new(|| {
623+
vec![
624+
glib::ParamSpecObject::builder("provider", glib::Type::OBJECT).build(),
625+
]
626+
});
627+
628+
//println!("PROPERTIES: {:?}", PROPERTIES);//TEST
629+
//println!("trying to add `base_call`: {:?}", glib::ParamSpecString::builder("base_call").build());//TEST
630+
631+
PROPERTIES.as_ref()
632+
}
633+
634+
/*
635+
* Name:
636+
* set_property
637+
*
638+
* Description:
639+
* Mutator for custom GObject properties
640+
*
641+
* Made:
642+
* 06/10/2022
643+
*
644+
* Made by:
645+
* Deren Vural
646+
*
647+
* Notes:
648+
*
649+
*/
650+
fn set_property(&self, _obj: &Self::Type, _id: usize, value: &Value, pspec: &ParamSpec) {
651+
//println!("setting: {:?}", pspec.name());//TEST
652+
653+
match pspec.name() {
654+
"provider" => {
655+
let input_provider_property: Option<Provider> = value
656+
.get()
657+
.expect("The value needs to be of type `Provider`.");
658+
self.provider.replace(input_provider_property);
659+
}
660+
_ => panic!("Property `{}` does not exist..", pspec.name())
661+
}
662+
}
663+
664+
/*
665+
* Name:
666+
* property
667+
*
668+
* Description:
669+
* Accessor for custom GObject properties
670+
*
671+
* Made:
672+
* 06/10/2022
673+
*
674+
* Made by:
675+
* Deren Vural
676+
*
677+
* Notes:
678+
*
679+
*/
680+
fn property(&self, _obj: &Self::Type, _id: usize, pspec: &ParamSpec) -> Value {
681+
//println!("getting: {:?}", pspec.name());//TEST
682+
683+
match pspec.name() {
684+
"provider" => {
685+
//TODO: this seems ridiculous..
686+
let value: Option<Provider> = self.provider.take();
687+
688+
self.provider.set(value.clone());
689+
690+
value.to_value()
691+
}
692+
_ => panic!("Property `{}` does not exist..", pspec.name())
693+
}
694+
}
542695
}
543696

544697
/*
@@ -594,13 +747,8 @@ impl WindowImpl for MainWindow {
594747
595748
*/
596749
// Set state in settings
597-
let settings: &Settings = window.settings();
598-
settings
599-
.set_boolean("app-settings-open", false)
600-
.expect("Could not set setting.");
601-
settings
602-
.set_boolean("nvidia-settings-open", false)
603-
.expect("Could not set setting.");
750+
self.update_setting("app-settings-open", false);
751+
self.update_setting("nvidia-settings-open", false);
604752

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

0 commit comments

Comments
 (0)