Skip to content

Commit a8e47a3

Browse files
authored
Merge pull request #60 from derenv/reload_on_settings_exit
Reload on settings exit
2 parents 45dffbc + 6d2b8cd commit a8e47a3

File tree

8 files changed

+566
-483
lines changed

8 files changed

+566
-483
lines changed

src/gpu_page/imp.rs

Lines changed: 411 additions & 3 deletions
Large diffs are not rendered by default.

src/gpu_page/mod.rs

Lines changed: 8 additions & 394 deletions
Large diffs are not rendered by default.

src/mainwindow/imp.rs

Lines changed: 79 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ use adwaita::{gio, glib, prelude::*, subclass::prelude::*};
2222
use gio::Settings;
2323
use glib::{
2424
once_cell::sync::Lazy, once_cell::sync::OnceCell, signal::Inhibit,
25-
subclass::InitializingObject, FromVariant, ParamSpec, Value,
25+
subclass::InitializingObject, subclass::Signal, subclass::SignalType, FromVariant, ParamSpec,
26+
Value,
2627
};
2728
use gtk::{
2829
subclass::prelude::*, Button, CompositeTemplate, PolicyType, ScrolledWindow, Stack,
@@ -53,12 +54,15 @@ enum TemperatureUnit {
5354
#[derive(CompositeTemplate, Default)]
5455
#[template(resource = "/main-window.ui")]
5556
pub struct MainWindow {
57+
// Public
5658
pub settings: OnceCell<Settings>,
5759
pub settings_window: Rc<RefCell<SettingsWindowContainer>>,
5860
pub provider: Cell<Option<Provider>>,
5961

62+
// Private
6063
gpu_pages: RefCell<Vec<GpuPage>>,
6164

65+
// Template Children
6266
#[template_child]
6367
pub gpu_stack: TemplateChild<Stack>,
6468
}
@@ -1344,32 +1348,13 @@ impl MainWindow {
13441348
}
13451349
}
13461350
}
1347-
}
13481351

1349-
/**
1350-
* Name:
1351-
* MainWindow
1352-
*
1353-
* Description:
1354-
* Trait defining template callbacks shared by all MainWindow objects
1355-
*
1356-
* Made:
1357-
* 13/10/2022
1358-
*
1359-
* Made by:
1360-
* Deren Vural
1361-
*
1362-
* Notes:
1363-
*
1364-
*/
1365-
#[gtk::template_callbacks]
1366-
impl MainWindow {
13671352
/**
13681353
* Name:
13691354
* refresh_cards
13701355
*
13711356
* Description:
1372-
* Template callback for GPU list refresh button
1357+
* Re-create the GpuPages and stack contents when called
13731358
*
13741359
* Made:
13751360
* 28/10/2022
@@ -1380,10 +1365,7 @@ impl MainWindow {
13801365
* Notes:
13811366
*
13821367
*/
1383-
#[template_callback]
1384-
fn refresh_cards(&self, _button: &Button) {
1385-
// println!("GPU Scan Button Pressed!"); //TEST
1386-
1368+
pub fn refresh_cards(&self) {
13871369
// Clear current ActionRow objects from GtkListBox
13881370
let mut done: bool = false;
13891371
while !done {
@@ -1544,58 +1526,47 @@ impl MainWindow {
15441526
}
15451527
}
15461528
}
1529+
}
1530+
}
15471531

1548-
/*
1549-
// Load refresh time (s) from settings
1550-
let refresh_rate: u32 = self.get_setting::<i32>("refreshrate") as u32;
1551-
1552-
// Create thread safe container for provider
1553-
// Grab copy of current provider
1554-
let provider_container: Option<Provider> = self.provider.take();
1555-
self.provider.set(provider_container.clone());
1556-
let provider_store: Arc<Mutex<Option<Provider>>> = Arc::new(Mutex::new(provider_container));
1557-
1558-
// Wrapper around `NonNull<RawType>` that just implements `Send`
1559-
struct WrappedPointer(Settings);
1560-
unsafe impl Send for WrappedPointer {}
1561-
// Safe wrapper around `WrappedPointer` that gives access to the pointer
1562-
// only with the mutex locked.
1563-
struct SafeType {
1564-
inner: Mutex<WrappedPointer>,
1565-
}
1566-
let settings_store: SafeType = SafeType
1567-
{
1568-
inner: Mutex::new(WrappedPointer(self.settings.get().expect("").to_owned()))
1569-
};
1570-
1571-
// Async check for provider changes
1572-
//glib::timeout_add_seconds(refresh_rate, clone!(@strong self as window => move || {
1573-
//glib::timeout_add_seconds(refresh_rate, clone!(@strong settings_store as window => move || {
1574-
glib::timeout_add_seconds_local(refresh_rate, move || {
1575-
// Grab locked data
1576-
// settings object
1577-
let settings_container: MutexGuard<WrappedPointer> = settings_store.inner.lock().unwrap();
1578-
// current provider for scanning gpu data
1579-
let provider_lock: Arc<Mutex<Option<Provider>>> = Arc::clone(&provider_store);
1580-
let mut provider_container: MutexGuard<Option<Provider>> = provider_lock.lock().unwrap();
1581-
1582-
1583-
// Check provider type in settings
1584-
let provider_type: i32 = settings_container.0.get("provider");
1585-
1586-
// If type has been changed, update provider
1587-
match &mut *provider_container {
1588-
Some(prov) => {
1589-
if prov.property::<i32>("provider_type") != provider_type {
1590-
//
1591-
}
1592-
}
1593-
None => todo!(),
1594-
}
1595-
1596-
Continue(true)
1597-
});
1598-
*/
1532+
/**
1533+
* Name:
1534+
* MainWindow
1535+
*
1536+
* Description:
1537+
* Trait defining template callbacks shared by all MainWindow objects
1538+
*
1539+
* Made:
1540+
* 13/10/2022
1541+
*
1542+
* Made by:
1543+
* Deren Vural
1544+
*
1545+
* Notes:
1546+
*
1547+
*/
1548+
#[gtk::template_callbacks]
1549+
impl MainWindow {
1550+
/**
1551+
* Name:
1552+
* refresh_cards
1553+
*
1554+
* Description:
1555+
* Template callback for GPU list refresh button
1556+
*
1557+
* Made:
1558+
* 28/10/2022
1559+
*
1560+
* Made by:
1561+
* Deren Vural
1562+
*
1563+
* Notes:
1564+
*
1565+
*/
1566+
#[template_callback]
1567+
fn refresh_cards_clicked(&self, _button: &Button) {
1568+
// println!("GPU Scan Button Pressed!"); //TEST
1569+
self.refresh_cards();
15991570
}
16001571
}
16011572

@@ -1740,6 +1711,38 @@ impl ObjectImpl for MainWindow {
17401711
_ => panic!("Property `{}` does not exist..", pspec.name()),
17411712
}
17421713
}
1714+
1715+
/**
1716+
* Name:
1717+
* signals
1718+
*
1719+
* Description:
1720+
* Defines the list of signals
1721+
*
1722+
* Made:
1723+
* 10/01/2023
1724+
*
1725+
* Made by:
1726+
* Deren Vural
1727+
*
1728+
* Notes:
1729+
* beware that you need to use kebab-case (<https://en.wikipedia.org/wiki/Letter_case#Kebab_case>)
1730+
*
1731+
* <https://gtk-rs.org/gtk4-rs/stable/latest/book/g_object_signals.html>
1732+
*
1733+
* SignalType::from(i32::static_type())
1734+
*/
1735+
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()
1745+
}
17431746
}
17441747

17451748
/**

src/mainwindow/mod.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use imp::SettingsWindowContainer;
2424
// Imports
2525
use adwaita::{gio, glib, prelude::*, subclass::prelude::*};
2626
use gio::{Settings, SimpleAction};
27-
use glib::{clone, Object};
27+
use glib::{clone, closure, Object};
2828
use std::cell::RefMut;
2929

3030
// Modules
@@ -72,6 +72,7 @@ impl MainWindow {
7272
*
7373
*/
7474
pub fn new(app: &adwaita::Application) -> Self {
75+
// Create new window
7576
Object::new(&[("application", app)]).expect("`MainWindow` should be instantiable.")
7677
}
7778

@@ -139,7 +140,23 @@ impl MainWindow {
139140
*
140141
*/
141142
fn setup_widgets(&self) {
142-
//
143+
// Connect closure to re-load stored views (with different settings) when a settings window is closed
144+
//NOTE: expected return value seems to be broken - look at imp.rs:1772
145+
self.connect_closure(
146+
"update-all-views",
147+
false,
148+
closure!(move |window: MainWindow| {
149+
println!("closure called!"); //TEST
150+
151+
// Reload views
152+
println!("reloading views.."); //TEST
153+
window.imp().refresh_cards();
154+
println!("views reloaded.."); //TEST
155+
156+
// Return final value
157+
0
158+
}),
159+
);
143160
}
144161

145162
/**
@@ -339,7 +356,7 @@ impl MainWindow {
339356
let app: adwaita::Application = adwaita::Application::builder().application_id(APP_ID).build();
340357

341358
// Create new settings window
342-
let new_settings_window: SettingsWindow = SettingsWindow::new(&app);
359+
let new_settings_window: SettingsWindow = SettingsWindow::new(&app, &window);
343360

344361
// Show new settings window
345362
new_settings_window.show();
@@ -361,7 +378,7 @@ impl MainWindow {
361378
let app: adwaita::Application = adwaita::Application::builder().application_id(APP_ID).build();
362379

363380
// Create settings window
364-
let new_settings_window: SettingsWindow = SettingsWindow::new(&app);
381+
let new_settings_window: SettingsWindow = SettingsWindow::new(&app, &window);
365382

366383
// Show new settings window
367384
new_settings_window.show();

src/modificationwindow/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ impl ModificationWindow {
8080
uuid: &str,
8181
parent_window: &GpuPage,
8282
) -> Self {
83+
// Create new window
8384
let obj: ModificationWindow = Object::new(&[("application", app)])
8485
.expect("`ModificationWindow` should be instantiable.");
8586

@@ -99,6 +100,7 @@ impl ModificationWindow {
99100
obj.setup_settings();
100101
obj.setup_widgets();
101102

103+
// Return final object
102104
obj
103105
}
104106

@@ -387,7 +389,7 @@ impl ModificationWindow {
387389
window.imp().update_stored_data();
388390
// println!("CHANGES APPLIED.."); //TEST
389391

390-
// TODO: Emit signal to notify changes made to view (and thus reload required)
392+
// Emit signal to notify changes made to view (and thus reload required)
391393
let modification_window_container: RefMut<ParentContainer> = window.imp().parent_window.borrow_mut();
392394
let _result = modification_window_container.window.as_ref().unwrap().emit_by_name::<i32>("update-views", &[&window.property::<i32>("new-view-id")]);
393395

@@ -417,7 +419,7 @@ impl ModificationWindow {
417419
window.imp().delete_stored_data();
418420
// println!("VIEW DELETED.."); //TEST
419421

420-
// TODO: Emit signal to notify changes made to view (and thus reload required)
422+
// Emit signal to notify changes made to view (and thus reload required)
421423
let modification_window_container: RefMut<ParentContainer> = window.imp().parent_window.borrow_mut();
422424
let _result = modification_window_container.window.as_ref().unwrap().emit_by_name::<i32>("update-views", &[&(-1).to_value()]);
423425

src/resources/main-window.ui

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
4343
<property name="tooltip-text" translatable="yes">Scan for GPUs</property>
4444

4545
<!-- Signals -->
46-
<signal name="clicked" handler="refresh_cards" swapped="true"/>
46+
<signal name="clicked" handler="refresh_cards_clicked" swapped="true"/>
4747
</object>
4848
</child>
4949
</object>

src/settingswindow/imp.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,27 @@ use gio::Settings;
2323
use glib::{once_cell::sync::Lazy, ParamSpec, Value};
2424
use glib::{once_cell::sync::OnceCell, signal::Inhibit, subclass::InitializingObject};
2525
use gtk::{subclass::prelude::*, CheckButton, CompositeTemplate, SpinButton, TemplateChild};
26+
use std::{cell::RefCell, cell::RefMut, rc::Rc};
2627

2728
// Modules
2829
//use crate::utils::data_path;
30+
use crate::mainwindow::MainWindow;
31+
32+
/// Structure for storing a SettingsWindow object and any related information
33+
#[derive(Default)]
34+
pub struct ParentContainer {
35+
pub window: Option<MainWindow>,
36+
}
2937

3038
/// Object holding the State and any Template Children
3139
#[derive(CompositeTemplate, Default)]
3240
#[template(resource = "/settings-window.ui")]
3341
pub struct SettingsWindow {
42+
// Public
3443
pub settings: OnceCell<Settings>,
44+
pub parent_window: Rc<RefCell<ParentContainer>>,
45+
46+
// Template Children
3547
#[template_child]
3648
pub refreshrate_input: TemplateChild<SpinButton>,
3749
#[template_child]
@@ -368,6 +380,15 @@ impl WindowImpl for SettingsWindow {
368380
// Store state in settings
369381
self.update_setting("app-settings-open", false);
370382

383+
// Emit signal to notify changes made to view (and thus reload required)
384+
let modification_window_container: RefMut<ParentContainer> =
385+
self.parent_window.borrow_mut();
386+
let _result = modification_window_container
387+
.window
388+
.as_ref()
389+
.unwrap()
390+
.emit_by_name::<i32>("update-all-views", &[]);
391+
371392
// Pass close request on to the parent
372393
self.parent_close_request(window)
373394
}

src/settingswindow/mod.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ use adwaita::{gio, glib, prelude::*, subclass::prelude::*};
2525
use gio::Settings;
2626
use glib::{clone, Object};
2727
use gtk::{Adjustment, CheckButton, StringList};
28+
use std::cell::RefMut;
2829

2930
// Modules
30-
use crate::APP_ID;
31+
use crate::{mainwindow::MainWindow, settingswindow::imp::ParentContainer, APP_ID};
3132

3233
// GObject wrapper for Property
3334
glib::wrapper! {
@@ -70,9 +71,26 @@ impl SettingsWindow {
7071
* Notes:
7172
*
7273
*/
73-
pub fn new(app: &adwaita::Application) -> Self {
74+
pub fn new(app: &adwaita::Application, parent_window: &MainWindow) -> Self {
7475
// Create new window
75-
Object::new(&[("application", app)]).expect("`SettingsWindow` should be instantiable.")
76+
let obj: SettingsWindow = Object::new(&[("application", app)])
77+
.expect("`SettingsWindow` should be instantiable.");
78+
79+
// Set custom properties
80+
//
81+
82+
// Set ref to parent
83+
{
84+
let mut modification_window_container: RefMut<ParentContainer> =
85+
obj.imp().parent_window.borrow_mut();
86+
modification_window_container.window = Some(parent_window.to_owned());
87+
}
88+
89+
// Apply any setup actions that need the above properties
90+
//
91+
92+
// Return final object
93+
obj
7694
}
7795

7896
/**

0 commit comments

Comments
 (0)