~jeremywootten/pantheon-files/fix-sidebar-dragging

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/***
    Copyright (C) 2015 ELementary Developers

    This program is free software: you can redistribute it and/or modify it
    under the terms of the GNU Lesser General Public License version 3, as published
    by the Free Software Foundation.

    This program is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranties of
    MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
    PURPOSE. See the GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program. If not, see <http://www.gnu.org/licenses/>.

    Authors : Jeremy Wootten <jeremy@elementaryos.org>
***/

namespace Marlin.View {
    public class Miller : GOF.AbstractSlot {
        private unowned Marlin.View.ViewContainer ctab;

        /* Need private copy of initial location as Miller
         * does not have its own Asyncdirectory object */
        private GLib.File root_location;

        private Gtk.Box colpane;

        public Gtk.ScrolledWindow scrolled_window;
        public Gtk.Adjustment hadj;
        public unowned Marlin.View.Slot? current_slot;
        public GLib.List<Marlin.View.Slot> slot_list = null;
        public int total_width = 0;

        public Miller (GLib.File loc, Marlin.View.ViewContainer ctab, Marlin.ViewMode mode) {
            base.init ();
            this.ctab = ctab;
            this.root_location = loc;

            (GOF.Preferences.get_default ()).notify["show-hidden-files"].connect ((s, p) => {
                show_hidden_files_changed (((GOF.Preferences)s).show_hidden_files);
            });

            colpane = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0);

            scrolled_window = new Gtk.ScrolledWindow (null, null);
            scrolled_window.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.NEVER);
            hadj = scrolled_window.get_hadjustment ();

            var viewport = new Gtk.Viewport (null, null);
            viewport.set_shadow_type (Gtk.ShadowType.NONE);
            viewport.add (this.colpane);

            scrolled_window.add (viewport);
            content_box.pack_start (scrolled_window);
            content_box.show_all ();

            colpane.add_events (Gdk.EventMask.KEY_RELEASE_MASK);
            colpane.key_release_event.connect (on_key_released);
            make_view ();
        }

        ~Miller () {
            debug ("Miller destruct");
        }

        protected override void make_view () {
            current_slot = null;
            add_location (root_location, null);  /* current slot gets set by this */
        }

        /** Creates a new slot in the host slot hpane */
        public void add_location (GLib.File loc, Marlin.View.Slot? host = null) {
            Marlin.View.Slot new_slot = new Marlin.View.Slot (loc, ctab, Marlin.ViewMode.MILLER_COLUMNS);
            new_slot.slot_number = (host != null) ? host.slot_number + 1 : 0;
            total_width += new_slot.width;

            colpane.set_size_request (total_width, -1);
            nest_slot_in_host_slot (new_slot, host);
            connect_slot_signals (new_slot);
            slot_list.append (new_slot);
            ctab.load_slot_directory (new_slot);
            new_slot.active ();
        }

        private void nest_slot_in_host_slot (Marlin.View.Slot slot, Marlin.View.Slot? host) {
            var hpane1 = new Gtk.Paned (Gtk.Orientation.HORIZONTAL);
            hpane1.hexpand = true;
            slot.hpane = hpane1;

            var box1 = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0);
            slot.colpane = box1;
            slot.colpane.set_size_request (slot.width, -1);

            unowned Gtk.Widget column = slot.get_directory_view () as Gtk.Widget;
            hpane1.pack1 (column, false, false);
            hpane1.pack2 (box1, true, true);
            hpane1.show_all ();

            if (host != null) {
                truncate_list_after_slot (host);
                host.colpane.add (hpane1);
            } else
                this.colpane.add (hpane1);
        }

        private void truncate_list_after_slot (Marlin.View.Slot slot) {
            if (slot_list.length () <= 0)
                return;

            /* destroy the nested slots */
            ((Marlin.View.Slot)(slot)).colpane.@foreach ((w) => {
                    w.destroy ();
            });

            uint n = slot.slot_number;

            slot_list.@foreach ((s) => {
                if (s.slot_number > n) {
                    s.close ();
                    disconnect_slot_signals (s);
                }
            });

            slot_list.nth (n).next = null;
            calculate_total_width ();
            slot.active ();
        }

        private void calculate_total_width () {
            total_width = 100;
            slot_list.@foreach ((slot) => {
                total_width += slot.width;
            });
        }

        private void update_total_width () {
            calculate_total_width ();
            this.colpane.set_size_request (total_width, -1);
        }

/*********************/
/** Signal handling **/
/*********************/

        public override void user_path_change_request (GLib.File loc, bool allow_mode_change = false) {
            /* user request always make new root */
            var slot = slot_list.first().data;
            assert (slot != null);
            truncate_list_after_slot (slot);
            slot.user_path_change_request (loc, false);
            root_location = loc;
        }

        private void connect_slot_signals (Slot slot) {
            slot.frozen_changed.connect (on_slot_frozen_changed);
            slot.active.connect (on_slot_active);
            slot.horizontal_scroll_event.connect (on_slot_horizontal_scroll_event);
            slot.miller_slot_request.connect (on_miller_slot_request);
            slot.size_change.connect (update_total_width);
            slot.folder_deleted.connect (on_slot_folder_deleted);
        }

        private void disconnect_slot_signals (Slot slot) {
            slot.frozen_changed.disconnect (on_slot_frozen_changed);
            slot.active.disconnect (on_slot_active);
            slot.horizontal_scroll_event.disconnect (on_slot_horizontal_scroll_event);
            slot.miller_slot_request.disconnect (on_miller_slot_request);
            slot.size_change.disconnect (update_total_width);
            slot.folder_deleted.disconnect (on_slot_folder_deleted);
        }

        private void on_miller_slot_request (Marlin.View.Slot slot, GLib.File loc, bool make_root) {
            if (make_root)
                user_path_change_request (loc);
            else
                add_location (loc, slot);
        }

        private bool on_slot_horizontal_scroll_event (double delta_x) {
            /* We can assume this is a horizontal or smooth scroll without control pressed*/
            double increment = 0.0;
            increment = delta_x * 10.0;

            if (increment != 0.0)
                hadj.set_value (hadj.get_value () + increment);

            return true;
        }

        private void on_slot_folder_deleted (Slot slot, GOF.File file, GOF.Directory.Async dir) {
            Slot? next_slot = slot_list.nth_data (slot.slot_number +1);
            if (next_slot != null && next_slot.directory == dir)
                truncate_list_after_slot (slot);
        }

        /** Called in response to slot active signal.
         *  Should not be called directly
         **/
        private void on_slot_active (Marlin.View.Slot slot, bool scroll = true) {
            if (scroll)
                scroll_to_slot (slot);

            if (this.current_slot == slot)
                return;

            slot_list.@foreach ((s) => {
                if (s != slot)
                    s.inactive ();
            });

            current_slot = slot;
        }

        private void show_hidden_files_changed (bool show_hidden) {
            if (!show_hidden) {
                /* we are hiding hidden files - check whether any slot is a hidden directory */
                int i = -1;
                int hidden = -1;

                slot_list.@foreach ((s) => {
                    i ++;
                    if (s.directory.file.is_hidden && hidden <= 0)
                        hidden = i;
                });

                /* Return if no hidden folder found or only first folder hidden */
                if (hidden <= 0)
                    return;

                /* Remove hidden slots and make the slot before the first hidden slot active */
                Marlin.View.Slot slot = slot_list.nth_data (hidden - 1);
                truncate_list_after_slot (slot);
                slot.active ();
            }
        }

        private bool on_key_released (Gtk.Widget box, Gdk.EventKey event) {
            /* Only handle unmodified keys */
            if ((event.state & Gtk.accelerator_get_default_mod_mask ()) > 0)
                return false;

            int current_position = slot_list.index (current_slot);

            if (slot_list.nth_data (current_position).get_directory_view ().renaming)
                return false;

            Marlin.View.Slot to_activate = null;

            switch (event.keyval) {
                case Gdk.Key.Left:
                    if (current_position > 0)
                        to_activate = slot_list.nth_data (current_position - 1);

                    break;

                case Gdk.Key.Right:
                    if (current_slot.get_selected_files () == null)
                        return true;

                    GOF.File? selected_file = current_slot.get_selected_files ().data;

                    if (selected_file == null)
                        return true;

                    GLib.File current_location = selected_file.location;
                    GLib.File? next_location = null;

                    if (current_position < slot_list.length () - 1)
                        next_location = slot_list.nth_data (current_position + 1).location;

                    if (next_location != null && next_location.equal (current_location)) {
                        to_activate = slot_list.nth_data (current_position + 1);
                    } else if (selected_file.is_folder ()) {
                        add_location (current_location, current_slot);
                        return true;
                    }
                    break;
            }

            if (to_activate != null) {
                to_activate.active ();
                to_activate.select_first_for_empty_selection ();
                return true;
            } else
                return false;
        }

        private void on_slot_frozen_changed (Slot slot, bool frozen) {
            /* Ensure all slots synchronise the frozen state and
             *  suppress key press event processing when frozen */
            if (frozen)
                this.colpane.key_release_event.disconnect (on_key_released);
            else
                this.colpane.key_release_event.connect (on_key_released);

            slot_list.@foreach ((abstract_slot) => {
                var s = abstract_slot as Marlin.View.Slot;
                if (s != null) {
                    s.frozen_changed.disconnect (on_slot_frozen_changed);
                    s.set_view_updates_frozen (frozen);
                    s.frozen_changed.connect (on_slot_frozen_changed);
                }
            });
        }


/** Helper functions */

        private void scroll_to_slot (GOF.AbstractSlot slot) {
            if (!content_box.get_realized ())
                return;

            int width = 0;
            int previous_width = 0;

            /* Calculate width up to left-hand edge of given slot */
            slot_list.@foreach ((abs) => {
                if (abs.slot_number < slot.slot_number) {
                    previous_width = width;
                    width += abs.width;
                }
            });

            /* previous width = left edge of slot before the active slot
             * width = left edge of active slot */
            int page_size = (int) this.hadj.get_page_size ();
            int current_value = (int) this.hadj.get_value ();
            int new_value = current_value;

            if (current_value > previous_width) /*scroll right until left hand edge of slot before the active slot is in view*/
                new_value = previous_width;

            int offset = slot.slot_number < slot_list.length () -1 ? 90 : 0;
            int val = page_size - (width + slot.width + offset);

            if (val < 0)  /*scroll left until right hand edge of active slot is in view*/
                new_value = -val;

            if (slot.width + offset > page_size) /*scroll right until left hand edge of active slot is in view*/
                new_value = width;

            Marlin.Animation.smooth_adjustment_to (this.hadj, new_value);
        }

        public override unowned GOF.AbstractSlot? get_current_slot () {
            return current_slot;
        }

        public override unowned GLib.List<unowned GOF.File>? get_selected_files () {
            return ((Marlin.View.Slot)(current_slot)).get_selected_files ();
        }


        public override void set_active_state (bool set_active) {
            if (set_active)
                current_slot.active ();
            else
                current_slot.inactive ();
        }

        public override string? get_tip_uri () {
            if (slot_list != null &&
                slot_list.last () != null &&
                slot_list.last ().data is GOF.AbstractSlot) {

                return slot_list.last ().data.uri;
            } else {
                return null;
            }
        }

        public override string? get_root_uri () {
            return root_location.get_uri ();
        }

        public override void select_glib_files (GLib.List<GLib.File> files, GLib.File? focus_location) {
            current_slot.select_glib_files (files, focus_location);
        }

        public override void select_first_for_empty_selection () {
            current_slot.select_first_for_empty_selection ();
        }

        public override void zoom_in () {
            ((Marlin.View.Slot)(current_slot)).zoom_in ();
        }

        public override void zoom_out () {
            ((Marlin.View.Slot)(current_slot)).zoom_out ();
        }

        public override void zoom_normal () {
            ((Marlin.View.Slot)(current_slot)).zoom_normal ();
        }

        public override void grab_focus () {
            ((Marlin.View.Slot)(current_slot)).grab_focus ();
        }

        public override void reload (bool non_local_only = false) {
            ((Marlin.View.Slot)(current_slot)).reload (non_local_only);
        }

        public override void cancel () {
            slot_list.@foreach ((slot) => {
                if (slot != null)
                    slot.cancel ();
            });
        }

        public override void close () {
            slot_list.@foreach ((slot) => {
                if (slot != null)
                    slot.close ();
            });
        }
    }
}