Skip to content

Commit e78e537

Browse files
author
Deren Vural
committed
Working view repositioning (can now modify position)
Updated ModificationWindow UI: - Added a new "AdwActionRow" object "view_position_input" for view position Updated ModificationWindow class: - Added template_child "view_position_input" - Fixed a bug in the "reorder_data()" function that meant that re-ordering views broke the stored config - Cleaned up some debug print statements - Fixed bug where "reorder_data()" function was called from "update_stored_data()" function with the wrong "remove" input parameter - Added "view_position_changed()" function as template callback for "view_position_input" object - Updated add/remove component row code for the new button - View component and position spinbuttons are limited to number of views/components Signed-off-by: Deren Vural <derenv@live.co.uk>
1 parent f61d834 commit e78e537

File tree

3 files changed

+99
-15
lines changed

3 files changed

+99
-15
lines changed

src/modificationwindow/imp.rs

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ pub struct ModificationWindow {
6969
#[template_child]
7070
pub view_components_amount_input: TemplateChild<SpinButton>,
7171
#[template_child]
72+
pub view_position_input: TemplateChild<SpinButton>,
73+
#[template_child]
7274
pub view_modifier_listbox: TemplateChild<ListBox>,
7375
#[template_child]
7476
pub button_row: TemplateChild<ActionRow>,
@@ -146,6 +148,9 @@ impl ModificationWindow {
146148
let new_view_title: String = self.new_view_title.take();
147149
self.new_view_title.set(new_view_title.clone());
148150

151+
// Get old ID
152+
let old_id: i32 = self.old_view_id.clone().get().expect("`old-view-id` invalid..").to_owned();
153+
149154
// If present in saved settings
150155
if stored_views_data.len() == 0 {
151156
// no views exist
@@ -155,9 +160,7 @@ impl ModificationWindow {
155160
for index in 0..stored_views_data.len() {
156161
// Split current viewconfig
157162
let sub_items: Vec<&str> = stored_views_data[index].split(':').collect();
158-
// println!("current title: `{}`", sub_items[2]); //TEST
159-
// println!("searching for old title: `{}`", old_view_title); //TEST
160-
// println!("searching for new title: `{}`", new_view_title); //TEST
163+
// println!("current item: `{}`", stored_views_data[index]); //TEST
161164

162165
// If viewconfig is for this GPU (i.e. has valid UUID) and IS NOT the current view
163166
if (sub_items[0] == uuid, sub_items[2] != new_view_title) == (true, true) {
@@ -202,22 +205,29 @@ impl ModificationWindow {
202205
// println!("edit required"); //TEST
203206
// println!("reorder"); //TEST
204207

205-
// If same position & re-order
206-
new_id += 1; // Modify order
208+
// If same position & re-order, we need to check direction of change
209+
if old_id < new_id {
210+
new_id -= 1; // Modify order
211+
} else if old_id > new_id {
212+
new_id += 1; // Modify order
213+
}
207214

208215
// Update record
209216
stored_views_data[index] =
210217
uuid.clone() + ":" + &new_id.to_string() + ":" + sub_items[2];
211218
}
212219
_ => {
220+
// println!("tuple `{} {} {}`", new_id > this_id, new_id == this_id, remove);
221+
213222
// otherwise ignore
214-
// println!("NO edit required"); //TEST
223+
//println!("NO edit required"); //TEST
215224
}
216225
}
217226
}
218227
}
219228

220229
// Update stored viewconfigs
230+
// println!("updated views: `{:?}`", stored_views_data); //TEST
221231
self.update_setting::<Vec<String>>("viewconfigs", stored_views_data);
222232
}
223233
}
@@ -512,7 +522,7 @@ impl ModificationWindow {
512522
// println!("viewconfig updated.."); //TEST
513523

514524
// Re-order remaining views
515-
self.reorder_data(true);
525+
self.reorder_data(false);
516526
}
517527
// MATCH name is the same, id is different
518528
(true, false) => {
@@ -532,7 +542,7 @@ impl ModificationWindow {
532542
// println!("viewconfig updated.."); //TEST
533543

534544
// Re-order remaining views
535-
self.reorder_data(true);
545+
self.reorder_data(false);
536546
}
537547
// MATCH name is the same, id is the same
538548
(true, true) => {
@@ -718,7 +728,7 @@ impl ModificationWindow {
718728
self.update_setting::<Vec<String>>("viewconfigs", stored_views_data);
719729

720730
// Re-order remaining views
721-
self.reorder_data(true);
731+
self.reorder_data(false);
722732

723733
// Get current components
724734
let mut current_components: Vec<ViewComponent> = self.view_components_list.take();
@@ -876,6 +886,35 @@ impl ModificationWindow {
876886
// println!("NEW NAME INVALID..");
877887
// }
878888
}
889+
890+
/**
891+
* Name:
892+
* view_position_changed
893+
*
894+
* Description:
895+
* Template callback for changing the position of the current view
896+
*
897+
* Made:
898+
* 08/01/2023
899+
*
900+
* Made by:
901+
* Deren Vural
902+
*
903+
* Notes:
904+
*
905+
*/
906+
#[template_callback]
907+
fn view_position_changed(&self, spinbutton: &SpinButton) {
908+
// Validate amount
909+
let new_amount: i32 = spinbutton.value() as i32;
910+
// println!("input view position: `{}`", new_amount); //TEST
911+
// println!("new view position: `{}`", new_amount - 1); //TEST
912+
913+
// Update stored ID
914+
self.new_view_id.set(new_amount - 1);
915+
}
916+
917+
879918
/**
880919
* Name:
881920
* view_components_amount_changed
@@ -909,7 +948,7 @@ impl ModificationWindow {
909948
self.view_modifier_listbox.remove(
910949
&self
911950
.view_modifier_listbox
912-
.row_at_index((1 + components.len()) as i32)
951+
.row_at_index((2 + components.len()) as i32)
913952
.unwrap(),
914953
);
915954

@@ -960,7 +999,7 @@ impl ModificationWindow {
960999
// Add new item, needs defaults (i.e. None)
9611000
let pos: i32 = components.len() as i32;
9621001
// println!("inserting in position: `{}`", pos); //TEST
963-
self.view_modifier_listbox.insert(&row, 2 + pos);
1002+
self.view_modifier_listbox.insert(&row, 3 + pos);
9641003

9651004
// Create new item
9661005
let new_item: ViewComponent = ViewComponent {

src/modificationwindow/mod.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,18 @@ impl ModificationWindow {
174174
let mut view_title: String = String::from("Default");
175175

176176
// Retrieve name of current view
177-
if view_title_list.len() == 0 {
177+
let mut view_amount: usize = view_title_list.len();
178+
if view_amount == 0 {
178179
// First view being added
179180
// Set new view id ('0' as is first view)
180181
self.set_property("new-view-id", 0);
181182
} else if view_id == "-1" {
182183
// Create new view title and id
183184
view_title = String::from("New");
184-
self.set_property("new-view-id", view_title_list.len() as i32);
185+
self.set_property("new-view-id", view_amount as i32);
186+
187+
// Increment count for spinwheel
188+
view_amount += 1;
185189

186190
// println!(" View ID: {}", view_id); //TEST
187191
// println!("View Title: {}", view_title); //TEST
@@ -213,8 +217,11 @@ impl ModificationWindow {
213217
let mut dropdowns: Vec<DropDown> = vec![];
214218

215219
// println!("LETS GET LOOPIN"); //TEST
220+
// If list of components is not empty
216221
if view_components_list.len() != 0 {
222+
// For the list of components
217223
for index in 0..view_components_list.len() {
224+
// Check if valid component
218225
// println!("item: `{}`", view_components_list[index]); //TEST
219226
let sub_items: Vec<&str> = view_components_list[index].split(':').collect();
220227
if sub_items[1] == self.property::<String>("old-view-title") {
@@ -313,7 +320,7 @@ impl ModificationWindow {
313320
// println!("inserting in position: `{}`", (1 + final_components.len())); //TEST
314321
self.imp()
315322
.view_modifier_listbox
316-
.insert(&row, (1 + final_components.len()) as i32);
323+
.insert(&row, (2 + final_components.len()) as i32);
317324
}
318325
}
319326
}
@@ -342,13 +349,30 @@ impl ModificationWindow {
342349
self.imp().view_name_input.set_max_width_chars(10);
343350

344351
// Create adjustment settings for number of view components SpinButton
345-
//TODO: link the upper limit to the total different properties
352+
// NOTE: Linked to the upper limit to the total different properties
346353
let adjustment: Adjustment =
347354
Adjustment::new(current_view_component_amount, 0.0, 10.0, 1.0, 2.0, 0.0);
348355
self.imp()
349356
.view_components_amount_input
350357
.configure(Some(&adjustment), 1.0, 0);
351358

359+
360+
// Create adjustment settings for view position SpinButton
361+
// NOTE: linked to the upper limit to the total views
362+
if view_id == "-1" {
363+
let adjustment: Adjustment =
364+
Adjustment::new(view_amount as f64, 1.0, view_amount as f64, 1.0, 2.0, 0.0);
365+
self.imp()
366+
.view_position_input
367+
.configure(Some(&adjustment), 1.0, 0);
368+
} else {
369+
let adjustment: Adjustment =
370+
Adjustment::new(view_id.parse::<f64>().expect("Missing `old-view-id`..") + 1.0, 1.0, view_amount as f64, 1.0, 2.0, 0.0);
371+
self.imp()
372+
.view_position_input
373+
.configure(Some(&adjustment), 1.0, 0);
374+
}
375+
352376
// Buttons
353377
// Apply
354378
self.imp().view_modification_apply_button.connect_clicked(

src/resources/modification-window.ui

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,27 @@ SPDX-License-Identifier: GPL-3.0-or-later
7878
</child>
7979
</object>
8080
</child>
81+
<child>
82+
<object class="AdwActionRow">
83+
<!-- Properties -->
84+
<property name="title">View Position</property>
85+
<property name="subtitle">The position of this view</property>
86+
<property name="activatable">false</property>
87+
<property name="selectable">false</property>
88+
89+
<!-- Children -->
90+
<child>
91+
<object class="GtkSpinButton" id="view_position_input">
92+
<!-- Signals -->
93+
<signal name="value-changed" handler="view_position_changed" swapped="true"/>
94+
95+
<!-- Properties -->
96+
<property name="digits" translatable="yes">0</property>
97+
<property name="numeric">True</property>
98+
</object>
99+
</child>
100+
</object>
101+
</child>
81102

82103
<child>
83104
<object class="AdwActionRow" id="button_row">

0 commit comments

Comments
 (0)