Merge lp:~philip.scott/scratch/restore-docs-per-tab into lp:~elementary-apps/scratch/scratch

Proposed by Felipe Escoto
Status: Merged
Approved by: Danielle Foré
Approved revision: 1788
Merged at revision: 1789
Proposed branch: lp:~philip.scott/scratch/restore-docs-per-tab
Merge into: lp:~elementary-apps/scratch/scratch
Diff against target: 424 lines (+169/-84)
6 files modified
schemas/org.pantheon.scratch.gschema.xml (+14/-4)
src/MainWindow.vala (+82/-41)
src/Scratch.vala (+1/-35)
src/Services/Settings.vala (+4/-2)
src/Widgets/DocumentView.vala (+52/-1)
src/Widgets/SplitView.vala (+16/-1)
To merge this branch: bzr merge lp:~philip.scott/scratch/restore-docs-per-tab
Reviewer Review Type Date Requested Status
Danielle Foré Approve
Review via email: mp+313086@code.launchpad.net

Commit message

- Save and restore opened documents per view
- Prevent New Docs from not saving

Description of the change

The settings schema for the opened docs is now split into two, one per view. This is updated when you open, close or move a tab, so it's always up to date

To post a comment you must log in.
1788. By Felipe Escoto

Save documents per view

Revision history for this message
Danielle Foré (danrabbit) wrote :

I can confirm that this works as expected

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'schemas/org.pantheon.scratch.gschema.xml'
--- schemas/org.pantheon.scratch.gschema.xml 2016-04-13 05:03:04 +0000
+++ schemas/org.pantheon.scratch.gschema.xml 2016-12-13 02:07:05 +0000
@@ -59,9 +59,14 @@
59 <summary>Show the welcome screen or the last opened files at Scratch launch</summary>59 <summary>Show the welcome screen or the last opened files at Scratch launch</summary>
60 <description>Show the welcome screen or the last opened files at Scratch launch.</description>60 <description>Show the welcome screen or the last opened files at Scratch launch.</description>
61 </key>61 </key>
62 <key type="as" name="opened-files">62 <key name="opened-files-view1" type="as">
63 <default>['']</default>63 <default>[]</default>
64 <summary>Files opened in the previously session of Scratch</summary>64 <summary>Files opened in the previously session of Scratch on the first view</summary>
65 <description>It is used to open the files opened in the last Scratch session</description>
66 </key>
67 <key name="opened-files-view2" type="as">
68 <default>[]</default>
69 <summary>Files opened in the previously session of Scratch on the second view</summary>
65 <description>It is used to open the files opened in the last Scratch session</description>70 <description>It is used to open the files opened in the last Scratch session</description>
66 </key>71 </key>
67 <key name="autosave" type="b">72 <key name="autosave" type="b">
@@ -139,7 +144,12 @@
139 <summary>Preferred Style Scheme</summary>144 <summary>Preferred Style Scheme</summary>
140 <description>Set the preferred style scheme.</description>145 <description>Set the preferred style scheme.</description>
141 </key>146 </key>
142 <key name="focused-document" type="s">147 <key name="focused-document-view1" type="s">
148 <default>''</default>
149 <summary>Remember the last focused document.</summary>
150 <description>Restore the focused document from a previous session when opening Scratch.</description>
151 </key>
152 <key name="focused-document-view2" type="s">
143 <default>''</default>153 <default>''</default>
144 <summary>Remember the last focused document.</summary>154 <summary>Remember the last focused document.</summary>
145 <description>Restore the focused document from a previous session when opening Scratch.</description>155 <description>Restore the focused document from a previous session when opening Scratch.</description>
146156
=== modified file 'src/MainWindow.vala'
--- src/MainWindow.vala 2016-08-27 15:11:14 +0000
+++ src/MainWindow.vala 2016-12-13 02:07:05 +0000
@@ -294,6 +294,74 @@
294 set_widgets_sensitive (!split_view.is_empty ());294 set_widgets_sensitive (!split_view.is_empty ());
295 }295 }
296296
297 public void restore_opened_documents () {
298 if (settings.show_at_start == "last-tabs") {
299 start_loading ();
300
301 string[] uris_view1 = settings.opened_files_view1;
302 string[] uris_view2 = settings.opened_files_view2;
303 string focused_document1 = settings.focused_document_view1;
304 string focused_document2 = settings.focused_document_view2;
305
306 if (uris_view1.length > 0) {
307 var view = add_view ();
308 load_files_for_view (view, uris_view1);
309 set_focused_document (view, focused_document1);
310
311 if (view.is_empty ()) {
312 split_view.remove_view (view);
313 }
314 }
315
316 if (uris_view2.length > 0) {
317 var view = add_view ();
318 load_files_for_view (view, uris_view2);
319 set_focused_document (view, focused_document2);
320
321 if (view.is_empty ()) {
322 split_view.remove_view (view);
323 }
324 }
325
326 stop_loading ();
327 }
328 }
329
330 private void load_files_for_view (Scratch.Widgets.DocumentView view, string[] uris) {
331 foreach (string uri in uris) {
332 if (uri != "") {
333 var file = File.new_for_uri (uri);
334 if (file.query_exists ()) {
335 var doc = new Scratch.Services.Document (main_actions, file);
336
337 if (!doc.is_file_temporary || doc.exists ()) {
338 open_document (doc, view);
339 }
340 }
341 }
342 }
343 }
344
345 // Set focus to last focused document, after all documents finished loading
346 private void set_focused_document (Scratch.Widgets.DocumentView view, string focused_document) {
347 if (focused_document != "") {
348 Scratch.Services.Document document_to_focus = null;
349
350 foreach (Scratch.Services.Document doc in view.docs) {
351 if (doc.file != null) {
352 if (doc.file.get_uri() == focused_document) {
353 document_to_focus = doc;
354 break;
355 }
356 }
357 }
358
359 if (document_to_focus != null) {
360 view.notebook.current = document_to_focus;
361 }
362 }
363 }
364
297 private bool on_key_pressed (Gdk.EventKey event) {365 private bool on_key_pressed (Gdk.EventKey event) {
298 switch (Gdk.keyval_name (event.keyval)) {366 switch (Gdk.keyval_name (event.keyval)) {
299 case "Escape":367 case "Escape":
@@ -392,17 +460,25 @@
392 }460 }
393461
394 // Open a document462 // Open a document
395 public void open_document (Scratch.Services.Document doc) {463 public void open_document (Scratch.Services.Document doc, Scratch.Widgets.DocumentView? view_ = null) {
396 while (Gtk.events_pending ()) {464 while (Gtk.events_pending ()) {
397 Gtk.main_iteration ();465 Gtk.main_iteration ();
398 }466 }
399467
400 Scratch.Widgets.DocumentView? view = null;468 Scratch.Widgets.DocumentView view = null;
469
470 if (view_ != null) {
471 view = view_;
472 }
473
401 if (this.split_view.is_empty ()) {474 if (this.split_view.is_empty ()) {
402 view = split_view.add_view ();475 view = split_view.add_view ();
403 view.open_document (doc);476 view.open_document (doc);
404 } else {477 } else {
405 view = split_view.get_focus_child () as Scratch.Widgets.DocumentView;478 if (view == null) {
479 view = split_view.get_focus_child () as Scratch.Widgets.DocumentView;
480 }
481
406 if (view == null) {482 if (view == null) {
407 view = this.split_view.current_view;483 view = this.split_view.current_view;
408 }484 }
@@ -452,6 +528,7 @@
452 if (!is_empty ()) {528 if (!is_empty ()) {
453 foreach (var w in this.split_view.views) {529 foreach (var w in this.split_view.views) {
454 var view = w as Scratch.Widgets.DocumentView;530 var view = w as Scratch.Widgets.DocumentView;
531 view.is_closing = true;
455 foreach (var doc in view.docs) {532 foreach (var doc in view.docs) {
456 if (!doc.close (true)) {533 if (!doc.close (true)) {
457 view.set_current_document (doc);534 view.set_current_document (doc);
@@ -530,41 +607,6 @@
530 Scratch.saved_state.vp_size = vp.get_position ();607 Scratch.saved_state.vp_size = vp.get_position ();
531 }608 }
532609
533 // Update files-opened settings key
534 private void update_opened_files () {
535 // File list
536 string[] opened_files = {};
537 this.split_view.views.foreach ((view) => {
538 view.notebook.tabs.foreach ((tab) => {
539 var doc = tab as Scratch.Services.Document;
540 if (doc.file != null && doc.exists ()) {
541 opened_files += doc.file.get_uri ();
542 }
543 });
544 });
545
546
547 // Update the opened-files setting
548 if (settings.show_at_start == "last-tabs") {
549 settings.opened_files = opened_files;
550
551 // Update the focused-document setting
552 string file_uri = "";
553 if (this.split_view.current_view != null) {
554 var current_document = this.split_view.current_view.get_current_document();
555 if (current_document != null) {
556 file_uri = current_document.file.get_uri();
557 }
558 }
559
560 if (file_uri != "") {
561 settings.focused_document = file_uri;
562 } else {
563 settings.schema.reset ("focused-document");
564 }
565 }
566 }
567
568 // SIGTERM/SIGINT Handling610 // SIGTERM/SIGINT Handling
569 public bool quit_source_func () {611 public bool quit_source_func () {
570 action_quit ();612 action_quit ();
@@ -574,7 +616,6 @@
574 // For exit cleanup616 // For exit cleanup
575 private void handle_quit () {617 private void handle_quit () {
576 update_saved_state ();618 update_saved_state ();
577 update_opened_files ();
578 }619 }
579620
580 public void set_default_zoom () {621 public void set_default_zoom () {
@@ -762,9 +803,9 @@
762 if (fetch_action.sensitive) {803 if (fetch_action.sensitive) {
763 /* Toggling the fetch action causes this function to be called again but the search_revealer child804 /* Toggling the fetch action causes this function to be called again but the search_revealer child
764 * is still not revealed so nothing more happens. We use the map signal on the search entry805 * is still not revealed so nothing more happens. We use the map signal on the search entry
765 * to set it up once it has been revealed. */ 806 * to set it up once it has been revealed. */
766 fetch_action.active = true;807 fetch_action.active = true;
767 } 808 }
768 } else {809 } else {
769 set_search_text ();810 set_search_text ();
770 }811 }
771812
=== modified file 'src/Scratch.vala'
--- src/Scratch.vala 2016-09-01 04:13:38 +0000
+++ src/Scratch.vala 2016-12-13 02:07:05 +0000
@@ -242,44 +242,10 @@
242 if (window == null) {242 if (window == null) {
243 window = this.new_window ();243 window = this.new_window ();
244 window.show ();244 window.show ();
245 // Restore opened documents245 window.restore_opened_documents ();
246 if (settings.show_at_start == "last-tabs") {
247 window.start_loading ();
248
249 string[] uris = settings.schema.get_strv ("opened-files");
250
251 foreach (string uri in uris) {
252 if (uri != "") {
253 var file = File.new_for_uri (uri);
254 if (file.query_exists ()) {
255 var doc = new Scratch.Services.Document (window.main_actions, file);
256 window.open_document (doc);
257 }
258 }
259 }
260
261 // Set focus to last focused document, after all documents finished loading
262 string focused_document = settings.schema.get_string("focused-document");
263 if (focused_document != "" && window.split_view.current_view != null) {
264 Scratch.Services.Document document_to_focus = null;
265 var document_view = window.split_view.current_view;
266 foreach (Scratch.Services.Document doc in document_view.docs) {
267 if (doc.file != null) {
268 if (doc.file.get_uri() == focused_document) {
269 document_to_focus = doc;
270 break;
271 }
272 }
273 }
274 if (document_to_focus != null)
275 window.split_view.current_view.notebook.current = document_to_focus;
276 }
277 window.stop_loading ();
278 }
279 } else {246 } else {
280 window.present ();247 window.present ();
281 }248 }
282
283 }249 }
284250
285 protected override void open (File[] files, string hint) {251 protected override void open (File[] files, string hint) {
286252
=== modified file 'src/Services/Settings.vala'
--- src/Services/Settings.vala 2016-04-13 05:03:04 +0000
+++ src/Services/Settings.vala 2016-12-13 02:07:05 +0000
@@ -62,9 +62,11 @@
62 public string style_scheme { get; set; }62 public string style_scheme { get; set; }
63 public string[] plugins_enabled { get; set;}63 public string[] plugins_enabled { get; set;}
64 public string show_at_start { get; set; }64 public string show_at_start { get; set; }
65 public string[] opened_files { get; set; }65 public string[] opened_files_view1 { get; set; }
66 public string[] opened_files_view2 { get; set; }
66 public bool autosave { get; set; }67 public bool autosave { get; set; }
67 public string focused_document { get; set; }68 public string focused_document_view1 { get; set; }
69 public string focused_document_view2 { get; set; }
68 public bool show_mini_map { get; set; }70 public bool show_mini_map { get; set; }
69 71
70 public Settings () {72 public Settings () {
7173
=== modified file 'src/Widgets/DocumentView.vala'
--- src/Widgets/DocumentView.vala 2015-11-09 21:25:02 +0000
+++ src/Widgets/DocumentView.vala 2016-12-13 02:07:05 +0000
@@ -36,6 +36,9 @@
36 }36 }
37 }37 }
3838
39 public uint view_id = -1;
40 public bool is_closing = false;
41
39 // Signals42 // Signals
40 public signal void document_change (Services.Document? document);43 public signal void document_change (Services.Document? document);
41 public signal void empty ();44 public signal void empty ();
@@ -70,6 +73,7 @@
7073
71 this.notebook.tab_switched.connect ((old_tab, new_tab) => {74 this.notebook.tab_switched.connect ((old_tab, new_tab) => {
72 document_change (new_tab as Services.Document);75 document_change (new_tab as Services.Document);
76 save_current_file (new_tab as Services.Document);
73 });77 });
7478
75 this.notebook.tab_restored.connect ((label, restore_data, icon) => {79 this.notebook.tab_restored.connect ((label, restore_data, icon) => {
@@ -224,7 +228,7 @@
224 doc.source_view.focus_in_event.connect (this.on_focus_in_event);228 doc.source_view.focus_in_event.connect (this.on_focus_in_event);
225 doc.source_view.drag_data_received.connect (this.drag_received);229 doc.source_view.drag_data_received.connect (this.drag_received);
226 doc.source_view.drag_motion.connect (this.drag_motion);230 doc.source_view.drag_motion.connect (this.drag_motion);
227231 save_opened_files ();
228 }232 }
229233
230 private void on_doc_removed (Granite.Widgets.Tab tab) {234 private void on_doc_removed (Granite.Widgets.Tab tab) {
@@ -239,6 +243,10 @@
239 if (this.is_empty ()) {243 if (this.is_empty ()) {
240 empty ();244 empty ();
241 }245 }
246
247 if (!is_closing) {
248 save_opened_files ();
249 }
242 }250 }
243251
244 private void on_doc_moved (Granite.Widgets.Tab tab, int x, int y) {252 private void on_doc_moved (Granite.Widgets.Tab tab, int x, int y) {
@@ -266,6 +274,8 @@
266 this.docs.insert (doc, new_pos);274 this.docs.insert (doc, new_pos);
267275
268 doc.focus ();276 doc.focus ();
277
278 save_opened_files ();
269 }279 }
270280
271 private bool on_focus_in_event () {281 private bool on_focus_in_event () {
@@ -293,5 +303,46 @@
293 Gtk.drag_finish (ctx, true, false, time);303 Gtk.drag_finish (ctx, true, false, time);
294 }304 }
295 }305 }
306
307 public void save_opened_files () {
308 if (settings.show_at_start == "last-tabs") {
309 string[] opened_files = {};
310
311 notebook.tabs.foreach ((tab) => {
312 var doc = tab as Scratch.Services.Document;
313 if (doc.file != null && doc.exists ()) {
314 opened_files += doc.file.get_uri ();
315 }
316 });
317
318 if (view_id == 1) {
319 settings.opened_files_view1 = opened_files;
320 } else {
321 settings.opened_files_view2 = opened_files;
322 }
323 }
324 }
325
326 public void save_current_file (Services.Document? current_document) {
327 string file_uri = "";
328
329 if (current_document != null) {
330 file_uri = current_document.file.get_uri();
331 }
332
333 if (file_uri != "") {
334 if (view_id == 1) {
335 settings.focused_document_view1 = file_uri;
336 } else {
337 settings.focused_document_view2 = file_uri;
338 }
339 } else {
340 if (view_id == 1) {
341 settings.schema.reset ("focused-document_view1");
342 } else {
343 settings.schema.reset ("focused-document_view2");
344 }
345 }
346 }
296 }347 }
297}348}
298349
=== modified file 'src/Widgets/SplitView.vala'
--- src/Widgets/SplitView.vala 2016-11-20 10:49:23 +0000
+++ src/Widgets/SplitView.vala 2016-12-13 02:07:05 +0000
@@ -120,11 +120,13 @@
120 view.show_all ();120 view.show_all ();
121121
122 views.append (view);122 views.append (view);
123 view.view_id = views.length ();
123 this.current_view = view;124 this.current_view = view;
124 debug ("View added successfully");125 debug ("View added successfully");
125126
126 // Enbale/Disable useless GtkActions about views127 // Enbale/Disable useless GtkActions about views
127 check_actions ();128 check_actions ();
129
128 return view;130 return view;
129 }131 }
130132
@@ -137,12 +139,25 @@
137 return;139 return;
138 }140 }
139141
142 foreach (var doc in view.docs) {
143 if (!doc.close (true)) {
144 view.set_current_document (doc);
145 return;
146 }
147 }
148
140 // Swap the position of the second view in the pane when we delete the first one149 // Swap the position of the second view in the pane when we delete the first one
141 if (get_child1 () == view && get_child2 () != null) {150 if (get_child1 () == view && get_child2 () != null) {
142 var right_view = get_child2 ();151 var right_view = get_child2 () as Scratch.Widgets.DocumentView;
143 remove (view);152 remove (view);
144 remove (right_view);153 remove (right_view);
145 pack1 (right_view, true, true);154 pack1 (right_view, true, true);
155
156 view.view_id = 2;
157 right_view.view_id = 1;
158
159 view.save_opened_files ();
160 right_view.save_opened_files ();
146 } else {161 } else {
147 remove (view);162 remove (view);
148 }163 }

Subscribers

People subscribed via source and target branches