~kjtehprogrammer/pantheon-files/editable-pathbar

« back to all changes in this revision

Viewing changes to src/View/Window.vala

  • Committer: KJ Lawrence
  • Date: 2014-05-24 03:48:19 UTC
  • mfrom: (1485.1.18 pantheon-files)
  • Revision ID: kjtehprogrammer@gmail.com-20140524034819-veos3kl0rb0mw5n3
Merging with trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
22
 
23
23
namespace Marlin.View {
 
24
 
24
25
    public class Window : Gtk.Window
25
26
    {
26
27
        public Gtk.UIManager ui;
31
32
        public Granite.Widgets.DynamicNotebook tabs;
32
33
        public Marlin.Places.Sidebar sidebar;
33
34
 
34
 
        public ViewContainer? current_tab;
 
35
        public ViewContainer? current_tab = null;
35
36
 
36
37
        public Gtk.ActionGroup main_actions;
37
38
        public Gtk.AccelGroup accel_group;
62
63
 
63
64
        public signal void loading_uri (string location);
64
65
 
 
66
        public bool freeze_view_changes = false;
65
67
 
66
68
        public void update_action_radio_view(int n) {
67
69
            Gtk.RadioAction action = (Gtk.RadioAction) main_actions.get_action("view-as-icons");
70
72
        }
71
73
 
72
74
        protected virtual void action_radio_change_view(){
 
75
            if (freeze_view_changes)
 
76
                return;
 
77
 
73
78
            Gtk.RadioAction action = (Gtk.RadioAction) main_actions.get_action("view-as-icons");
74
79
            assert(action != null);
75
80
            int n = action.get_current_value();
76
81
            /* change the view only for view_mode real change */
77
 
            if (n != current_tab.view_mode)
 
82
            if (current_tab != null && n != current_tab.view_mode)
78
83
                current_tab.change_view(n, null);
79
84
        }
80
85
 
221
226
            });
222
227
 
223
228
            delete_event.connect (() => {
224
 
                save_geometries ();
225
 
                destroy ();
 
229
                if (app.is_first_window ((Gtk.Window) this)) {
 
230
                    save_geometries ();
 
231
                    save_tabs ();
 
232
                }
226
233
 
 
234
                //destroy ();
227
235
                return false;
228
236
            });
229
237
 
304
312
        }
305
313
 
306
314
        public void change_tab (int offset) {
307
 
            ViewContainer old_tab = current_tab;
 
315
            ViewContainer? old_tab = current_tab;
308
316
            current_tab = (tabs.get_tab_by_index (offset)).page as ViewContainer;
309
 
            if (old_tab == current_tab) {
 
317
            if (current_tab == null || old_tab == current_tab)
310
318
                return;
311
 
            }
 
319
 
312
320
            if (old_tab != null) {
313
321
                var old_slot = old_tab.get_active_slot ();
314
322
                if (old_slot != null)
331
339
            }
332
340
        }
333
341
 
334
 
        private void make_new_tab (File location = File.new_for_commandline_arg (Environment.get_home_dir ())) {
335
 
            var content = new View.ViewContainer (this, location,
336
 
                                    current_tab != null ? current_tab.view_mode : Preferences.settings.get_enum("default-viewmode"));
 
342
        private void make_new_tab (File location = File.new_for_commandline_arg (Environment.get_home_dir ()),
 
343
                                   int viewmode = -1) {
 
344
            if (viewmode < 0) {
 
345
                if (current_tab != null)
 
346
                    viewmode = current_tab.view_mode;
 
347
                else
 
348
                    viewmode = Preferences.settings.get_enum ("default-viewmode");
 
349
            }
337
350
 
 
351
            var content = new View.ViewContainer (this, location, viewmode);
338
352
            var tab = new Granite.Widgets.Tab ("", null, content);
339
 
 
340
353
            content.tab_name_changed.connect ((tab_name) => {
341
354
                tab.label = tab_name;
342
355
            });
343
356
 
344
 
            tabs.insert_tab (tab, -1);
345
 
 
 
357
            change_tab ((int)tabs.insert_tab (tab, -1));
346
358
            tabs.current = tab;
347
 
            change_tab (tabs.get_tab_position (tab));
348
359
        }
349
360
 
350
 
        public void add_tab (File location) {
351
 
            make_new_tab (location);
352
 
 
 
361
        public void add_tab (File location, int viewmode = -1) {
 
362
            make_new_tab (location, viewmode);
353
363
            /* The following fixes a bug where upon first opening
354
364
               Files, the overlay status bar is shown empty. */
355
365
            if (tabs.n_tabs == 1) {
360
370
        }
361
371
 
362
372
        public void remove_tab (ViewContainer view_container) {
363
 
            var tab = tabs.get_tab_by_widget (view_container as Gtk.Widget);
 
373
            actual_remove_tab (tabs.get_tab_by_widget (view_container as Gtk.Widget));
 
374
        }
 
375
 
 
376
        private void actual_remove_tab (Granite.Widgets.Tab tab) {
 
377
            /* signal for restore_data to be set and a new tab to be created if this is last tab */
 
378
            tabs.close_tab_requested (tab);
 
379
            /* now close the tab */
364
380
            tab.close ();
365
381
        }
366
382
 
415
431
        }
416
432
 
417
433
        private void action_remove_tab (Gtk.Action action) {
418
 
            tabs.remove_tab (tabs.current);
 
434
            actual_remove_tab (tabs.current);
419
435
        }
420
436
 
421
437
        private void save_geometries () {
431
447
            Preferences.settings.set_boolean("maximized", is_maximized);
432
448
        }
433
449
 
 
450
        private void save_tabs () {
 
451
            VariantBuilder vb = new VariantBuilder (new VariantType ("a(uss)"));
 
452
 
 
453
            foreach (var tab in tabs.tabs) {
 
454
                assert (tab != null);
 
455
                var view = tab.page as ViewContainer;
 
456
 
 
457
                /* Do not save if "File does not exist" or "Does not belong to you" */
 
458
                if (!view.can_show_folder)
 
459
                    continue;
 
460
 
 
461
                vb.add ("(uss)",
 
462
                        view.view_mode,
 
463
                        GLib.Uri.escape_string (view.get_root_uri () ?? Environment.get_home_dir ()),
 
464
                        GLib.Uri.escape_string (view.get_tip_uri () ?? "")
 
465
                       );
 
466
            }
 
467
 
 
468
            Preferences.settings.set_value ("tab-info-list", vb.end ());
 
469
            Preferences.settings.set_int ("active-tab-position", tabs.get_tab_position (tabs.current));
 
470
        }
 
471
 
 
472
        public uint restore_tabs () {
 
473
            GLib.Variant tab_info_array = Preferences.settings.get_value ("tab-info-list");
 
474
            GLib.VariantIter iter = new GLib.VariantIter (tab_info_array);
 
475
            int tabs_added = 0;
 
476
            int viewmode = -1;
 
477
            string root_uri = null;
 
478
            string tip_uri = null;
 
479
 
 
480
            /* inhibit unnecessary changes of view and rendering of location bar while restoring tabs
 
481
             * as this causes all sorts of problems */
 
482
            freeze_view_changes = true;
 
483
            while (iter.next ("(uss)", out viewmode, out root_uri, out tip_uri)) {
 
484
                if (viewmode < 0 || viewmode > 2 || root_uri == null || root_uri == "" || tip_uri == null)
 
485
                    continue;
 
486
 
 
487
                GLib.File root_location = GLib.File.new_for_uri (GLib.Uri.unescape_string (root_uri));
 
488
 
 
489
                add_tab (root_location, viewmode);
 
490
 
 
491
                if (viewmode == ViewMode.MILLER && tip_uri != root_uri)
 
492
                    expand_miller_view (tip_uri, root_location);
 
493
 
 
494
                tabs_added++;
 
495
                viewmode = -1;
 
496
                root_uri = null;
 
497
                tip_uri = null;
 
498
            }
 
499
 
 
500
            freeze_view_changes = false;
 
501
 
 
502
            int active_tab_position = Preferences.settings.get_int ("active-tab-position");
 
503
            if (active_tab_position >=0 && active_tab_position < tabs_added) {
 
504
                tabs.current = tabs.get_tab_by_index (active_tab_position);
 
505
                change_tab (active_tab_position);
 
506
            }
 
507
 
 
508
            string path = current_tab.get_tip_uri ();
 
509
            if (path == "")
 
510
                path = current_tab.get_root_uri ();
 
511
 
 
512
            /* Render the final path in the location bar without animation */
 
513
            top_menu.location_bar.bread.animation_visible = false;
 
514
            top_menu.location_bar.path = path;
 
515
            /* restore location bar animation */
 
516
            top_menu.location_bar.bread.animation_visible = true;
 
517
            return tabs_added;
 
518
        }
 
519
 
 
520
        private void expand_miller_view (string tip_uri, GLib.File root_location) {
 
521
            var tab = tabs.current;
 
522
            var view = tab.page as ViewContainer;
 
523
            var mwcols = view.mwcol;
 
524
            var unescaped_tip_uri = GLib.Uri.unescape_string (tip_uri);
 
525
            var tip_location = GLib.File.new_for_uri (unescaped_tip_uri);
 
526
            var relative_path = root_location.get_relative_path (tip_location);
 
527
            var slot = mwcols.active_slot;
 
528
 
 
529
            GLib.File gfile;
 
530
            FM.Directory.View dview;
 
531
 
 
532
            if (relative_path != null) {
 
533
                string [] dirs = relative_path.split (GLib.Path.DIR_SEPARATOR_S);
 
534
                string uri = root_location.get_uri ();
 
535
 
 
536
                foreach (string dir in dirs) {
 
537
                    uri += (GLib.Path.DIR_SEPARATOR_S + dir);
 
538
                    gfile = GLib.File.new_for_uri (uri);
 
539
                    dview = slot.view_box as FM.Directory.View;
 
540
 
 
541
                    dview.column_add_location (gfile);
 
542
                    slot = mwcols.get_last_slot ();
 
543
                }
 
544
            } else {
 
545
                warning ("Invalid tip uri for Miller View");
 
546
            }
 
547
        }
 
548
 
434
549
        public Gtk.ActionGroup get_actiongroup () {
435
550
            return this.main_actions;
436
551
        }
715
830
 
716
831
        };
717
832
    }
718
 
}
 
 
b'\\ No newline at end of file'
 
833
}