Skip to content

Commit 0d7e489

Browse files
author
Deren Vural
committed
Component Modification Working!
Components can now be modified, with the GpuPage updating accordingly. A bunch of bug fixes too. Updated lib.rs: - Updated some links with "<>" brackets to enable for cargo docs Updated MainWindow class: - Updated comment headers of "create_gpu_page()" and "close_request()" functions to enable for cargo docs Updated GpuPage class: - Removed some old (and now invalid) psuedocode - Added a "signals()" function for declaring any signals - Added a signal ("update-views"): this tells the parent GpuPage object to refresh the views stored (and switch back to the correct view if a modification) - Updated comment headers of "load_views()" and "create_updater()" functions to enable for cargo docs - Updated some links with "<>" brackets to enable for cargo docs - Updated ModificationWindow creation to pass a self-reference - Removed the reload call from the ModificationWindow creation - Added "none" to the possible properties, giving pretty-label of "None" - Added check for "none" to "create_updater()" function, to set value to "N/A" rather than attempting a call to the provider - Added a connect_closure call to self that reloads the views (and switches back to the correct one) when recieving the "update-views" signal - Added TODO comments next to lists of properties - need to make this a global list, both normal and pretty versions Updated ModificationWindow class: - Added a "ParentContainer" struct for holding an optional reference to the parent window: will enable sending a signal to update the parent window when changes are made in this window - Made "uuid", "old_view_id", "new_view_id", "old_view_title", "new_view_title" all private struct members - Added "parent_window" public struct member for keeping track of the parent window, stores as reference using "ParentContainer" struct - Added "dropdowns" public struct member for keeping track of the dynamic drop down menus and their state - Modified "update_stored_data()" function to check dropdowns (using struct member "dropdowns") for any modified view components: this enables changing components! - Commented out a lot of debug print statements - Switched from "None" to "none" nomenclature for empty dropdowns, with "None" becoming the pretty-label - Updated "view_components_amount_changed()" function, a scrollwheel template callback, to update the new dropdowns struct member - Updated comment header of "close_request()" function to enable for cargo docs - Updated constructor to store referenec to parent in "parent_window" struct member - Added TODO comments next to lists of properties - need to make this a global list, both normal and pretty versions - Updated "setup_widgets()" function to update "dropdowns" struct member when creating dynamic dropdowns - Added a signal emission call ("update-views") to the apply and delete buttons: this tells the parent GpuPage object to refresh the views stored (and switch back to the correct view if a modification) Updated Formatter class: - Updated comment header of "format()" function to enable for cargo docs Signed-off-by: Deren Vural <[email protected]>
1 parent cd8b224 commit 0d7e489

File tree

7 files changed

+270
-144
lines changed

7 files changed

+270
-144
lines changed

src/formatter/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl Formatter {
101101
.expect("`settings` should not be set before calling `setup_settings`.");
102102
}
103103

104-
/*
104+
/**
105105
* Name:
106106
* format
107107
*

src/gpu_page/imp.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use adwaita::{gio, glib, prelude::*, ViewStack, ViewSwitcherBar};
2323
use gio::Settings;
2424
use glib::{
2525
once_cell::sync::Lazy, once_cell::sync::OnceCell, subclass::InitializingObject, FromVariant,
26-
ParamSpec, ToValue, Value,
26+
ParamSpec, ToValue, Value, subclass::Signal, subclass::SignalType
2727
};
2828
use gtk::{subclass::prelude::*, CompositeTemplate, TemplateChild};
2929
use std::{cell::Cell, cell::RefCell, rc::Rc};
@@ -90,11 +90,7 @@ impl ObjectSubclass for GpuPage {
9090
*/
9191
#[gtk::template_callbacks]
9292
impl GpuPage {
93-
// #[template_callback]
94-
// fn get_gpu_data(&self, _label: &Label) {
95-
// //
96-
// println!("TEST callback");//TEST
97-
// }
93+
//
9894
}
9995

10096
impl GpuPage {
@@ -366,6 +362,38 @@ impl ObjectImpl for GpuPage {
366362
_ => panic!("Property `{}` does not exist..", pspec.name()),
367363
}
368364
}
365+
366+
/**
367+
* Name:
368+
* signals
369+
*
370+
* Description:
371+
* Defines the list of signals
372+
*
373+
* Made:
374+
* 07/01/2023
375+
*
376+
* Made by:
377+
* Deren Vural
378+
*
379+
* Notes:
380+
* beware that you need to use kebab-case (<https://en.wikipedia.org/wiki/Letter_case#Kebab_case>)
381+
*
382+
* <https://gtk-rs.org/gtk4-rs/stable/latest/book/g_object_signals.html>
383+
*
384+
* SignalType::from(i32::static_type())
385+
*/
386+
fn signals() -> &'static [Signal] {
387+
static SIGNALS: Lazy<Vec<Signal>> = Lazy::new(|| {
388+
vec![
389+
Signal::builder("update-views",
390+
&[SignalType::from(i32::static_type())],
391+
SignalType::from(i32::static_type()))
392+
.build()
393+
]
394+
});
395+
SIGNALS.as_ref()
396+
}
369397
}
370398

371399
/**

src/gpu_page/mod.rs

Lines changed: 69 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use imp::ModificationWindowContainer;
2424
// Imports
2525
use adwaita::{gio, glib, Application, ViewStack};
2626
use gio::Settings;
27-
use glib::{clone, translate::FromGlib, Object, SourceId};
27+
use glib::{clone, translate::FromGlib, Object, SourceId, closure_local, closure};
2828
use gtk::{prelude::*, subclass::prelude::*, Align, Button, Grid, Label, LayoutChild, Orientation};
2929
use std::{cell::RefMut, sync::Arc, sync::Mutex, sync::MutexGuard};
3030

@@ -133,7 +133,7 @@ impl GpuPage {
133133
.expect("`settings` should be set in `setup_settings`.")
134134
}
135135

136-
/*
136+
/**
137137
* Name:
138138
* load_views
139139
*
@@ -250,18 +250,14 @@ impl GpuPage {
250250
let app: Application = Application::builder().application_id(APP_ID).build();
251251

252252
// Create new modification window
253-
let new_modification_window: ModificationWindow = ModificationWindow::new(&app, 0, &gpage.property::<String>("uuid"));
253+
let new_modification_window: ModificationWindow = ModificationWindow::new(&app, 0, &gpage.property::<String>("uuid"), &gpage);
254254

255255
// Show new modification window
256256
new_modification_window.show();
257257

258258
// Store object and state back in container
259259
modification_window_container.open = true;
260260
modification_window_container.window = Some(new_modification_window);
261-
262-
// Re-create views & properties
263-
//NOTE: any changes are saved to settings on close
264-
gpage.load_views();
265261
},
266262
true => {
267263
println!("....window already open");//DEBUG
@@ -276,18 +272,14 @@ impl GpuPage {
276272
let app: Application = Application::builder().application_id(APP_ID).build();
277273

278274
// Create modification window
279-
let new_modification_window: ModificationWindow = ModificationWindow::new(&app, 0, &gpage.property::<String>("uuid"));
275+
let new_modification_window: ModificationWindow = ModificationWindow::new(&app, 0, &gpage.property::<String>("uuid"), &gpage);
280276

281277
// Show new modification window
282278
new_modification_window.show();
283279

284280
// Store object and state back in container
285281
modification_window_container.open = true;
286282
modification_window_container.window = Some(new_modification_window);
287-
288-
// Re-create views & properties
289-
//NOTE: any changes are saved to settings on close
290-
gpage.load_views();
291283
},
292284
}
293285

@@ -309,7 +301,7 @@ impl GpuPage {
309301
// Add object
310302
new_stack.add_titled(&new_grid, Some("default"), "Default");
311303
// Add icon
312-
//NOTE: see https://world.pages.gitlab.gnome.org/Rust/libadwaita-rs/stable/latest/docs/libadwaita/struct.ViewStack.html
304+
//NOTE: see <https://world.pages.gitlab.gnome.org/Rust/libadwaita-rs/stable/latest/docs/libadwaita/struct.ViewStack.html>
313305
// function "add_titled_with_icon" not in stable yet
314306
let new_item = new_stack.page(&new_grid);
315307
new_item.set_icon_name(Some("package-x-generic-symbolic"));
@@ -428,18 +420,14 @@ impl GpuPage {
428420
let app: Application = Application::builder().application_id(APP_ID).build();
429421

430422
// Create new modification window
431-
let new_modification_window: ModificationWindow = ModificationWindow::new(&app, index as i32, &gpage.property::<String>("uuid"));
423+
let new_modification_window: ModificationWindow = ModificationWindow::new(&app, index as i32, &gpage.property::<String>("uuid"), &gpage);
432424

433425
// Show new modification window
434426
new_modification_window.show();
435427

436428
// Store object and state back in container
437429
modification_window_container.open = true;
438430
modification_window_container.window = Some(new_modification_window);
439-
440-
// Re-create views & properties
441-
//NOTE: any changes are saved to settings on close
442-
gpage.load_views();
443431
},
444432
true => {
445433
println!("....window already open");//DEBUG
@@ -454,18 +442,14 @@ impl GpuPage {
454442
let app: Application = Application::builder().application_id(APP_ID).build();
455443

456444
// Create modification window
457-
let new_modification_window: ModificationWindow = ModificationWindow::new(&app, index as i32, &gpage.property::<String>("uuid"));
445+
let new_modification_window: ModificationWindow = ModificationWindow::new(&app, index as i32, &gpage.property::<String>("uuid"), &gpage);
458446

459447
// Show new modification window
460448
new_modification_window.show();
461449

462450
// Store object and state back in container
463451
modification_window_container.open = true;
464452
modification_window_container.window = Some(new_modification_window);
465-
466-
// Re-create views & properties
467-
//NOTE: any changes are saved to settings on close
468-
gpage.load_views();
469453
},
470454
}
471455

@@ -491,7 +475,7 @@ impl GpuPage {
491475
);
492476
// println!("NEW STACK ITEM: `{}`", new_stack_item_name);//TEST
493477
// Add icon
494-
//NOTE: see https://world.pages.gitlab.gnome.org/Rust/libadwaita-rs/stable/latest/docs/libadwaita/struct.ViewStack.html
478+
//NOTE: see <https://world.pages.gitlab.gnome.org/Rust/libadwaita-rs/stable/latest/docs/libadwaita/struct.ViewStack.html>
495479
// function "add_titled_with_icon" not in stable yet
496480
let new_item = new_stack.page(&new_view_grid);
497481
new_item.set_icon_name(Some("package-x-generic-symbolic"));
@@ -653,6 +637,7 @@ impl GpuPage {
653637
// Decide on title label size
654638
let space: i32;
655639
let pretty_label: &str;
640+
//TODO: Update to use global list
656641
match property.to_owned().as_str() {
657642
"util" => {
658643
pretty_label = "GPU Utilization";
@@ -690,6 +675,10 @@ impl GpuPage {
690675
pretty_label = "Power Usage";
691676
space = 8
692677
}
678+
"none" => {
679+
pretty_label = "None";
680+
space = 5
681+
}
693682
_ => {
694683
pretty_label = property;
695684
space = 5
@@ -721,7 +710,7 @@ impl GpuPage {
721710
// Decide on content label size
722711
let space: i32;
723712
match property.to_owned().as_str() {
724-
"util" | "fan_speed" | "temp" => space = 5,
713+
"util" | "fan_speed" | "temp" | "none" => space = 5,
725714
"memory_usage" | "memory_total" => space = 8,
726715
_ => space = 5,
727716
}
@@ -758,7 +747,7 @@ impl GpuPage {
758747
(grid, labels)
759748
}
760749

761-
/*
750+
/**
762751
* Name:
763752
* create_updater
764753
*
@@ -840,23 +829,36 @@ impl GpuPage {
840829
match &mut *provider_container {
841830
Some(current_provider) => {
842831
for property in properties.iter() {
843-
// Grab current Property from provider
844-
match current_provider.get_gpu_data(&uuid, property) {
845-
Ok(property_value) => {
846-
// For each output label of the page
847-
for label in labels_container.iter() {
848-
// println!("COMPARING AGAINST 1: `{}`", property);
849-
// println!("COMPARING AGAINST 2: `{}`", label.widget_name());
850-
851-
// Check if correct label
852-
if *property.to_owned() == label.widget_name() {
853-
label.set_label(&property_value);
854-
}
832+
// println!("property: `{}`", property); //TEST
833+
834+
if property == "none" {
835+
// Check if correct label
836+
for label in labels_container.iter() {
837+
if *property.to_owned() == label.widget_name() {
838+
label.set_label("N/A");
839+
// no break here - could be duplicates
855840
}
856841
}
857-
Err(err) => {
858-
println!("panicked when fetching gpu data: `{}`", err);
859-
return Continue(false);
842+
} else {
843+
// Grab current Property from provider
844+
match current_provider.get_gpu_data(&uuid, property) {
845+
Ok(property_value) => {
846+
// For each output label of the page
847+
for label in labels_container.iter() {
848+
// println!("COMPARING AGAINST 1: `{}`", property);
849+
// println!("COMPARING AGAINST 2: `{}`", label.widget_name());
850+
851+
// Check if correct label
852+
if *property.to_owned() == label.widget_name() {
853+
label.set_label(&property_value);
854+
// no break here - could be duplicates
855+
}
856+
}
857+
}
858+
Err(err) => {
859+
println!("panicked when fetching gpu data: `{}`", err);
860+
return Continue(false);
861+
}
860862
}
861863
}
862864
}
@@ -895,7 +897,33 @@ impl GpuPage {
895897
*
896898
*/
897899
fn setup_widgets(&self) {
900+
// Load stored views
898901
self.load_views();
902+
903+
// Connect closure to re-load (now updated) stored views when a modification window is closed
904+
//NOTE: expected return value seems to be broken - look at imp.rs:395
905+
self.connect_closure("update-views", false, closure!(move |gpage: GpuPage, current_view: i32| {
906+
// println!("closure parameter: `{}`", current_view); //TEST
907+
908+
// Reload views
909+
// println!("reloading views.."); //TEST
910+
gpage.load_views();
911+
// println!("views reloaded.."); //TEST
912+
913+
// If and edit is made (and not a delete)
914+
if current_view != -1 {
915+
// println!("switching to page: `{}`", current_view); //TEST
916+
917+
// Set to correct view
918+
gpage.imp().view_switcher.stack().unwrap().set_visible_child_name((current_view.to_string() + "_stack_item").as_str());
919+
}
920+
921+
// Return final value
922+
//0.to_value() // "Invalid return value: expected (), got gint",
923+
0 // "Invalid return value: expected (), got gint",
924+
//() // 'Closure returned no value but the caller expected a value of type gint'
925+
// // 'Closure returned no value but the caller expected a value of type gint'
926+
}));
899927
}
900928
}
901929

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ pub fn create_app() -> Application {
9292
* <https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/todo/5/main.rs>
9393
* <https://gtk-rs.org/gtk4-rs/stable/latest/book/todo_3.html>
9494
*
95-
* https://gtk-rs.org/gtk4-rs/git/book/actions.html
96-
* https://gtk-rs.org/gtk4-rs/git/docs/gtk4/struct.Window.html#actions
95+
* <https://gtk-rs.org/gtk4-rs/git/book/actions.html>
96+
* <https://gtk-rs.org/gtk4-rs/git/docs/gtk4/struct.Window.html#actions>
9797
*/
9898
fn setup_shortcuts(app: &Application) {
9999
//app.set_accels_for_action("win.filter('All')", &["<Ctrl>a"]);

src/mainwindow/imp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl MainWindow {
149149
}
150150
}
151151

152-
/*
152+
/**
153153
* Name:
154154
* create_gpu_page
155155
*
@@ -1777,7 +1777,7 @@ impl WidgetImpl for MainWindow {}
17771777
*
17781778
*/
17791779
impl WindowImpl for MainWindow {
1780-
/*
1780+
/**
17811781
* Name:
17821782
* close_request
17831783
*

0 commit comments

Comments
 (0)