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
1=== modified file 'schemas/org.pantheon.scratch.gschema.xml'
2--- schemas/org.pantheon.scratch.gschema.xml 2016-04-13 05:03:04 +0000
3+++ schemas/org.pantheon.scratch.gschema.xml 2016-12-13 02:07:05 +0000
4@@ -59,9 +59,14 @@
5 <summary>Show the welcome screen or the last opened files at Scratch launch</summary>
6 <description>Show the welcome screen or the last opened files at Scratch launch.</description>
7 </key>
8- <key type="as" name="opened-files">
9- <default>['']</default>
10- <summary>Files opened in the previously session of Scratch</summary>
11+ <key name="opened-files-view1" type="as">
12+ <default>[]</default>
13+ <summary>Files opened in the previously session of Scratch on the first view</summary>
14+ <description>It is used to open the files opened in the last Scratch session</description>
15+ </key>
16+ <key name="opened-files-view2" type="as">
17+ <default>[]</default>
18+ <summary>Files opened in the previously session of Scratch on the second view</summary>
19 <description>It is used to open the files opened in the last Scratch session</description>
20 </key>
21 <key name="autosave" type="b">
22@@ -139,7 +144,12 @@
23 <summary>Preferred Style Scheme</summary>
24 <description>Set the preferred style scheme.</description>
25 </key>
26- <key name="focused-document" type="s">
27+ <key name="focused-document-view1" type="s">
28+ <default>''</default>
29+ <summary>Remember the last focused document.</summary>
30+ <description>Restore the focused document from a previous session when opening Scratch.</description>
31+ </key>
32+ <key name="focused-document-view2" type="s">
33 <default>''</default>
34 <summary>Remember the last focused document.</summary>
35 <description>Restore the focused document from a previous session when opening Scratch.</description>
36
37=== modified file 'src/MainWindow.vala'
38--- src/MainWindow.vala 2016-08-27 15:11:14 +0000
39+++ src/MainWindow.vala 2016-12-13 02:07:05 +0000
40@@ -294,6 +294,74 @@
41 set_widgets_sensitive (!split_view.is_empty ());
42 }
43
44+ public void restore_opened_documents () {
45+ if (settings.show_at_start == "last-tabs") {
46+ start_loading ();
47+
48+ string[] uris_view1 = settings.opened_files_view1;
49+ string[] uris_view2 = settings.opened_files_view2;
50+ string focused_document1 = settings.focused_document_view1;
51+ string focused_document2 = settings.focused_document_view2;
52+
53+ if (uris_view1.length > 0) {
54+ var view = add_view ();
55+ load_files_for_view (view, uris_view1);
56+ set_focused_document (view, focused_document1);
57+
58+ if (view.is_empty ()) {
59+ split_view.remove_view (view);
60+ }
61+ }
62+
63+ if (uris_view2.length > 0) {
64+ var view = add_view ();
65+ load_files_for_view (view, uris_view2);
66+ set_focused_document (view, focused_document2);
67+
68+ if (view.is_empty ()) {
69+ split_view.remove_view (view);
70+ }
71+ }
72+
73+ stop_loading ();
74+ }
75+ }
76+
77+ private void load_files_for_view (Scratch.Widgets.DocumentView view, string[] uris) {
78+ foreach (string uri in uris) {
79+ if (uri != "") {
80+ var file = File.new_for_uri (uri);
81+ if (file.query_exists ()) {
82+ var doc = new Scratch.Services.Document (main_actions, file);
83+
84+ if (!doc.is_file_temporary || doc.exists ()) {
85+ open_document (doc, view);
86+ }
87+ }
88+ }
89+ }
90+ }
91+
92+ // Set focus to last focused document, after all documents finished loading
93+ private void set_focused_document (Scratch.Widgets.DocumentView view, string focused_document) {
94+ if (focused_document != "") {
95+ Scratch.Services.Document document_to_focus = null;
96+
97+ foreach (Scratch.Services.Document doc in view.docs) {
98+ if (doc.file != null) {
99+ if (doc.file.get_uri() == focused_document) {
100+ document_to_focus = doc;
101+ break;
102+ }
103+ }
104+ }
105+
106+ if (document_to_focus != null) {
107+ view.notebook.current = document_to_focus;
108+ }
109+ }
110+ }
111+
112 private bool on_key_pressed (Gdk.EventKey event) {
113 switch (Gdk.keyval_name (event.keyval)) {
114 case "Escape":
115@@ -392,17 +460,25 @@
116 }
117
118 // Open a document
119- public void open_document (Scratch.Services.Document doc) {
120+ public void open_document (Scratch.Services.Document doc, Scratch.Widgets.DocumentView? view_ = null) {
121 while (Gtk.events_pending ()) {
122 Gtk.main_iteration ();
123 }
124
125- Scratch.Widgets.DocumentView? view = null;
126+ Scratch.Widgets.DocumentView view = null;
127+
128+ if (view_ != null) {
129+ view = view_;
130+ }
131+
132 if (this.split_view.is_empty ()) {
133 view = split_view.add_view ();
134 view.open_document (doc);
135 } else {
136- view = split_view.get_focus_child () as Scratch.Widgets.DocumentView;
137+ if (view == null) {
138+ view = split_view.get_focus_child () as Scratch.Widgets.DocumentView;
139+ }
140+
141 if (view == null) {
142 view = this.split_view.current_view;
143 }
144@@ -452,6 +528,7 @@
145 if (!is_empty ()) {
146 foreach (var w in this.split_view.views) {
147 var view = w as Scratch.Widgets.DocumentView;
148+ view.is_closing = true;
149 foreach (var doc in view.docs) {
150 if (!doc.close (true)) {
151 view.set_current_document (doc);
152@@ -530,41 +607,6 @@
153 Scratch.saved_state.vp_size = vp.get_position ();
154 }
155
156- // Update files-opened settings key
157- private void update_opened_files () {
158- // File list
159- string[] opened_files = {};
160- this.split_view.views.foreach ((view) => {
161- view.notebook.tabs.foreach ((tab) => {
162- var doc = tab as Scratch.Services.Document;
163- if (doc.file != null && doc.exists ()) {
164- opened_files += doc.file.get_uri ();
165- }
166- });
167- });
168-
169-
170- // Update the opened-files setting
171- if (settings.show_at_start == "last-tabs") {
172- settings.opened_files = opened_files;
173-
174- // Update the focused-document setting
175- string file_uri = "";
176- if (this.split_view.current_view != null) {
177- var current_document = this.split_view.current_view.get_current_document();
178- if (current_document != null) {
179- file_uri = current_document.file.get_uri();
180- }
181- }
182-
183- if (file_uri != "") {
184- settings.focused_document = file_uri;
185- } else {
186- settings.schema.reset ("focused-document");
187- }
188- }
189- }
190-
191 // SIGTERM/SIGINT Handling
192 public bool quit_source_func () {
193 action_quit ();
194@@ -574,7 +616,6 @@
195 // For exit cleanup
196 private void handle_quit () {
197 update_saved_state ();
198- update_opened_files ();
199 }
200
201 public void set_default_zoom () {
202@@ -762,9 +803,9 @@
203 if (fetch_action.sensitive) {
204 /* Toggling the fetch action causes this function to be called again but the search_revealer child
205 * is still not revealed so nothing more happens. We use the map signal on the search entry
206- * to set it up once it has been revealed. */
207+ * to set it up once it has been revealed. */
208 fetch_action.active = true;
209- }
210+ }
211 } else {
212 set_search_text ();
213 }
214
215=== modified file 'src/Scratch.vala'
216--- src/Scratch.vala 2016-09-01 04:13:38 +0000
217+++ src/Scratch.vala 2016-12-13 02:07:05 +0000
218@@ -242,44 +242,10 @@
219 if (window == null) {
220 window = this.new_window ();
221 window.show ();
222- // Restore opened documents
223- if (settings.show_at_start == "last-tabs") {
224- window.start_loading ();
225-
226- string[] uris = settings.schema.get_strv ("opened-files");
227-
228- foreach (string uri in uris) {
229- if (uri != "") {
230- var file = File.new_for_uri (uri);
231- if (file.query_exists ()) {
232- var doc = new Scratch.Services.Document (window.main_actions, file);
233- window.open_document (doc);
234- }
235- }
236- }
237-
238- // Set focus to last focused document, after all documents finished loading
239- string focused_document = settings.schema.get_string("focused-document");
240- if (focused_document != "" && window.split_view.current_view != null) {
241- Scratch.Services.Document document_to_focus = null;
242- var document_view = window.split_view.current_view;
243- foreach (Scratch.Services.Document doc in document_view.docs) {
244- if (doc.file != null) {
245- if (doc.file.get_uri() == focused_document) {
246- document_to_focus = doc;
247- break;
248- }
249- }
250- }
251- if (document_to_focus != null)
252- window.split_view.current_view.notebook.current = document_to_focus;
253- }
254- window.stop_loading ();
255- }
256+ window.restore_opened_documents ();
257 } else {
258 window.present ();
259 }
260-
261 }
262
263 protected override void open (File[] files, string hint) {
264
265=== modified file 'src/Services/Settings.vala'
266--- src/Services/Settings.vala 2016-04-13 05:03:04 +0000
267+++ src/Services/Settings.vala 2016-12-13 02:07:05 +0000
268@@ -62,9 +62,11 @@
269 public string style_scheme { get; set; }
270 public string[] plugins_enabled { get; set;}
271 public string show_at_start { get; set; }
272- public string[] opened_files { get; set; }
273+ public string[] opened_files_view1 { get; set; }
274+ public string[] opened_files_view2 { get; set; }
275 public bool autosave { get; set; }
276- public string focused_document { get; set; }
277+ public string focused_document_view1 { get; set; }
278+ public string focused_document_view2 { get; set; }
279 public bool show_mini_map { get; set; }
280
281 public Settings () {
282
283=== modified file 'src/Widgets/DocumentView.vala'
284--- src/Widgets/DocumentView.vala 2015-11-09 21:25:02 +0000
285+++ src/Widgets/DocumentView.vala 2016-12-13 02:07:05 +0000
286@@ -36,6 +36,9 @@
287 }
288 }
289
290+ public uint view_id = -1;
291+ public bool is_closing = false;
292+
293 // Signals
294 public signal void document_change (Services.Document? document);
295 public signal void empty ();
296@@ -70,6 +73,7 @@
297
298 this.notebook.tab_switched.connect ((old_tab, new_tab) => {
299 document_change (new_tab as Services.Document);
300+ save_current_file (new_tab as Services.Document);
301 });
302
303 this.notebook.tab_restored.connect ((label, restore_data, icon) => {
304@@ -224,7 +228,7 @@
305 doc.source_view.focus_in_event.connect (this.on_focus_in_event);
306 doc.source_view.drag_data_received.connect (this.drag_received);
307 doc.source_view.drag_motion.connect (this.drag_motion);
308-
309+ save_opened_files ();
310 }
311
312 private void on_doc_removed (Granite.Widgets.Tab tab) {
313@@ -239,6 +243,10 @@
314 if (this.is_empty ()) {
315 empty ();
316 }
317+
318+ if (!is_closing) {
319+ save_opened_files ();
320+ }
321 }
322
323 private void on_doc_moved (Granite.Widgets.Tab tab, int x, int y) {
324@@ -266,6 +274,8 @@
325 this.docs.insert (doc, new_pos);
326
327 doc.focus ();
328+
329+ save_opened_files ();
330 }
331
332 private bool on_focus_in_event () {
333@@ -293,5 +303,46 @@
334 Gtk.drag_finish (ctx, true, false, time);
335 }
336 }
337+
338+ public void save_opened_files () {
339+ if (settings.show_at_start == "last-tabs") {
340+ string[] opened_files = {};
341+
342+ notebook.tabs.foreach ((tab) => {
343+ var doc = tab as Scratch.Services.Document;
344+ if (doc.file != null && doc.exists ()) {
345+ opened_files += doc.file.get_uri ();
346+ }
347+ });
348+
349+ if (view_id == 1) {
350+ settings.opened_files_view1 = opened_files;
351+ } else {
352+ settings.opened_files_view2 = opened_files;
353+ }
354+ }
355+ }
356+
357+ public void save_current_file (Services.Document? current_document) {
358+ string file_uri = "";
359+
360+ if (current_document != null) {
361+ file_uri = current_document.file.get_uri();
362+ }
363+
364+ if (file_uri != "") {
365+ if (view_id == 1) {
366+ settings.focused_document_view1 = file_uri;
367+ } else {
368+ settings.focused_document_view2 = file_uri;
369+ }
370+ } else {
371+ if (view_id == 1) {
372+ settings.schema.reset ("focused-document_view1");
373+ } else {
374+ settings.schema.reset ("focused-document_view2");
375+ }
376+ }
377+ }
378 }
379 }
380
381=== modified file 'src/Widgets/SplitView.vala'
382--- src/Widgets/SplitView.vala 2016-11-20 10:49:23 +0000
383+++ src/Widgets/SplitView.vala 2016-12-13 02:07:05 +0000
384@@ -120,11 +120,13 @@
385 view.show_all ();
386
387 views.append (view);
388+ view.view_id = views.length ();
389 this.current_view = view;
390 debug ("View added successfully");
391
392 // Enbale/Disable useless GtkActions about views
393 check_actions ();
394+
395 return view;
396 }
397
398@@ -137,12 +139,25 @@
399 return;
400 }
401
402+ foreach (var doc in view.docs) {
403+ if (!doc.close (true)) {
404+ view.set_current_document (doc);
405+ return;
406+ }
407+ }
408+
409 // Swap the position of the second view in the pane when we delete the first one
410 if (get_child1 () == view && get_child2 () != null) {
411- var right_view = get_child2 ();
412+ var right_view = get_child2 () as Scratch.Widgets.DocumentView;
413 remove (view);
414 remove (right_view);
415 pack1 (right_view, true, true);
416+
417+ view.view_id = 2;
418+ right_view.view_id = 1;
419+
420+ view.save_opened_files ();
421+ right_view.save_opened_files ();
422 } else {
423 remove (view);
424 }

Subscribers

People subscribed via source and target branches