-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathDocumentView.vala
More file actions
666 lines (541 loc) · 22.1 KB
/
DocumentView.vala
File metadata and controls
666 lines (541 loc) · 22.1 KB
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
/***
BEGIN LICENSE
Copyright (C) 2013 Mario Guerriero <mario@elementaryos.org>
2024 Colin Kiama <colinkiama@gmail.com>
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/>
END LICENSE
***/
public class Scratch.Widgets.DocumentView : Gtk.Box {
const int TAB_HISTORY_MAX_ITEMS = 20;
public enum TargetType {
URI_LIST
}
public signal void document_change (Services.Document? document, DocumentView parent);
public signal void request_placeholder ();
public signal void tab_added (Services.Document document);
public signal void tab_removed (Services.Document document);
public GLib.List<Services.Document> docs;
private Services.Document _current_document;
public Services.Document current_document {
get {
return _current_document;
}
set {
if (is_closing) {
return;
}
_current_document = value;
document_change (_current_document, this);
_current_document.focus ();
save_focused_document_uri (current_document);
if (tab_view.selected_page != value.tab && value.tab != null) {
tab_view.selected_page = value.tab;
}
}
}
public unowned MainWindow window { get; construct; }
public bool is_closing = false;
public bool outline_visible { get; set; default = false; }
public int outline_width { get; set; }
private Hdy.TabView tab_view;
private Hdy.TabBar tab_bar;
private weak Hdy.TabPage? tab_menu_target = null;
private Gtk.CssProvider style_provider;
private Gtk.MenuButton tab_history_button;
public DocumentView (Scratch.MainWindow window) {
Object (
window: window,
orientation: Gtk.Orientation.VERTICAL,
hexpand: true,
vexpand: true
);
}
construct {
docs = new GLib.List<Services.Document> ();
var app_instance = (Gtk.Application) GLib.Application.get_default ();
tab_view = new Hdy.TabView () {
hexpand = true,
vexpand = true
};
tab_view.menu_model = create_menu_model ();
tab_view.setup_menu.connect (tab_view_setup_menu);
tab_view.notify["selected-page"].connect (() => {
if (tab_view.selected_page == null) {
return;
}
current_document = tab_view.selected_page.child as Services.Document;
});
var new_tab_button = new Gtk.Button.from_icon_name ("list-add-symbolic") {
relief = Gtk.ReliefStyle.NONE,
tooltip_markup = Granite.markup_accel_tooltip (
app_instance.get_accels_for_action (MainWindow.ACTION_PREFIX + MainWindow.ACTION_NEW_TAB),
_("New Tab")
)
};
new_tab_button.clicked.connect (() => {
new_document ();
});
tab_history_button = new Gtk.MenuButton () {
image = new Gtk.Image.from_icon_name ("document-open-recent-symbolic", Gtk.IconSize.MENU),
tooltip_text = _("Closed Tabs"),
use_popover = false,
};
tab_bar = new Hdy.TabBar () {
autohide = false,
expand_tabs = false,
inverted = true,
start_action_widget = new_tab_button,
end_action_widget = tab_history_button,
view = tab_view,
};
// TabView tab events
tab_view.close_page.connect ((tab) => {
var doc = tab.child as Services.Document;
if (doc == null || doc.closing) {
return true; // doc.do_close () already called once
}
if (doc == null) {
tab_view.close_page_finish (tab, true);
} else {
doc.do_close.begin (false, (obj, res) => {
var should_close = doc.do_close.end (res);
// Ensure removed doc is saved by handling this first
if (!is_closing) {
save_opened_files ();
}
//`page-detached` handler will perform rest of necessary cleanup
tab_view.close_page_finish (tab, should_close);
});
}
return true;
});
tab_view.page_attached.connect (on_doc_added);
tab_view.page_detached.connect (on_page_detached);
tab_view.page_reordered.connect (on_doc_reordered);
tab_view.create_window.connect (on_doc_to_new_window);
style_provider = new Gtk.CssProvider ();
Gtk.StyleContext.add_provider_for_screen (
Gdk.Screen.get_default (),
style_provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
);
update_inline_tab_colors ();
Scratch.settings.changed["style-scheme"].connect (update_inline_tab_colors);
Scratch.settings.changed["follow-system-style"].connect (update_inline_tab_colors);
var granite_settings = Granite.Settings.get_default ();
granite_settings.notify["prefers-color-scheme"].connect (update_inline_tab_colors);
notify["outline-visible"].connect (update_outline_visible);
Scratch.saved_state.bind ("outline-width", this, "outline-width", DEFAULT);
this.notify["outline-width"].connect (() => {
foreach (var doc in docs) {
doc.set_outline_width (outline_width);
}
});
// Handle Drag-and-drop of files onto add-tab button to create document
Gtk.TargetEntry uris = {"text/uri-list", 0, TargetType.URI_LIST};
Gtk.drag_dest_set (tab_bar, Gtk.DestDefaults.ALL, {uris}, Gdk.DragAction.COPY);
tab_bar.drag_data_received.connect (drag_received);
add (tab_bar);
add (tab_view);
}
private void update_inline_tab_colors () {
var style_scheme = "";
if (settings.get_boolean ("follow-system-style")) {
var system_prefers_dark = Granite.Settings.get_default ().prefers_color_scheme == Granite.Settings.ColorScheme.DARK;
if (system_prefers_dark) {
style_scheme = "elementary-dark";
} else {
style_scheme = "elementary-light";
}
} else {
style_scheme = Scratch.settings.get_string ("style-scheme");
}
var sssm = Gtk.SourceStyleSchemeManager.get_default ();
if (style_scheme in sssm.scheme_ids) {
var theme = sssm.get_scheme (style_scheme);
var text_color_data = theme.get_style ("text");
// Default gtksourceview background color is white
var color = "#FFFFFF";
if (text_color_data != null) {
// If the current style has a background color, use that
color = text_color_data.background;
}
var define = "@define-color tab_base_color %s;".printf (color);
try {
style_provider.load_from_data (define);
} catch (Error e) {
critical ("Unable to set inline tab styling, going back to classic notebook tabs");
}
}
}
public void transfer_tab_to_new_window () {
var target = tab_menu_target ?? tab_view.selected_page;
if (target == null) {
return;
}
var new_window = new MainWindow (false);
tab_view.transfer_page (target, new_window.document_view.tab_view, 0);
}
public void close_document (Services.Document? doc = null) {
if (doc != null) {
tab_view.close_page (doc.tab);
return;
}
var target = tab_menu_target ?? tab_view.selected_page;
if (target == null) {
return;
}
tab_view.close_page (target);
}
public void close_tabs_to_right () {
var target = tab_menu_target ?? tab_view.selected_page;
if (target == null) {
return;
}
if (current_document != null) {
tab_view.close_pages_after (target);
}
}
public void close_other_tabs () {
var target = tab_menu_target ?? tab_view.selected_page;
if (target == null) {
return;
}
if (current_document != null) {
tab_view.close_other_pages (target);
}
}
public void duplicate_tab () {
var target = tab_menu_target ?? tab_view.selected_page;
if (target == null) {
return;
}
var original_doc = target.get_child () as Services.Document;
if (original_doc == null) {
return;
}
try {
var file = File.new_for_path (unsaved_duplicated_file_path_builder (original_doc.file.get_basename ()));
file.create (FileCreateFlags.PRIVATE);
var doc = new Services.Document (window.actions, file);
doc.source_view.set_text (original_doc.get_text ());
doc.source_view.language = original_doc.source_view.language;
if (Scratch.settings.get_boolean ("autosave")) {
doc.save_with_hold.begin (true);
}
insert_document (doc, tab_view.get_page_position (target) + 1);
current_document = doc;
doc.focus ();
} catch (Error e) {
warning ("Cannot copy \"%s\": %s", original_doc.get_basename (), e.message);
}
}
public void new_document () {
var file = File.new_for_path (unsaved_file_path_builder ());
try {
file.create (FileCreateFlags.PRIVATE);
var doc = new Services.Document (window.actions, file);
// Must open document in order to unlock it.
open_document.begin (doc);
} catch (Error e) {
critical (e.message);
}
}
public void new_document_from_clipboard (string clipboard) {
var file = File.new_for_path (unsaved_file_path_builder ());
// Set clipboard content
try {
file.create (FileCreateFlags.PRIVATE);
file.replace_contents (clipboard.data, null, false, 0, null);
var doc = new Services.Document (window.actions, file);
open_document.begin (doc);
} catch (Error e) {
critical ("Cannot insert clipboard: %s", clipboard);
}
}
public async void open_document (
Services.Document doc,
bool focus = true,
int cursor_position = 0,
SelectionRange range = SelectionRange.EMPTY
) {
for (int n = 0; n <= docs.length (); n++) {
var nth_doc = docs.nth_data (n);
if (nth_doc == null) {
continue;
}
if (nth_doc.file != null && nth_doc.file.get_uri () == doc.file.get_uri ()) {
after_insert_document (nth_doc, focus, cursor_position, range);
return;
}
}
insert_document (doc, (int) docs.length ());
yield doc.open (false);
after_insert_document (doc, focus, cursor_position, range);
}
private void after_insert_document (
Scratch.Services.Document doc,
bool focus = true,
int cursor_position = 0,
SelectionRange range = SelectionRange.EMPTY
) {
if (focus) {
current_document = doc;
doc.focus ();
}
if (range != SelectionRange.EMPTY) {
doc.source_view.select_range (range);
// It is recommended to run scroll to mark in a low priority idle otherwise may not work
Idle.add_full (Priority.LOW, () => {
doc.source_view.scroll_to_mark (doc.source_view.buffer.get_insert (), 0.1, true, 0.5, 0.5);
return Source.REMOVE;
});
} else if (cursor_position > 0) {
doc.source_view.cursor_position = cursor_position;
}
if (Scratch.saved_state.get_boolean ("outline-visible")) {
debug ("setting outline visible");
doc.show_outline (true);
}
save_opened_files ();
}
public void next_document () {
uint current_index = docs.index (current_document) + 1;
if (current_index < docs.length ()) {
var next_doc = docs.nth_data (current_index++);
current_document = next_doc;
next_doc.focus ();
} else if (docs.length () > 0) {
var next_doc = docs.nth_data (0);
current_document = next_doc;
next_doc.focus ();
}
}
public void previous_document () {
uint current_index = docs.index (current_document);
if (current_index > 0) {
var previous_doc = docs.nth_data (--current_index);
current_document = previous_doc;
previous_doc.focus ();
} else if (docs.length () > 0) {
var previous_doc = docs.nth_data (docs.length () - 1);
current_document = previous_doc;
previous_doc.focus ();
}
}
public void request_placeholder_if_empty () {
if (docs.length () == 0) {
request_placeholder ();
}
}
public void save_opened_files () {
if (privacy_settings.get_boolean ("remember-recent-files")) {
var vb = new VariantBuilder (new VariantType ("a(si)"));
docs.foreach ((doc) => {
if (doc.file != null && doc.exists ()) {
vb.add ("(si)", doc.file.get_uri (), doc.source_view.cursor_position);
}
});
Scratch.settings.set_value ("opened-files", vb.end ());
}
}
public void update_outline_visible () {
docs.@foreach ((doc) => {
doc.show_outline (outline_visible);
});
}
public new void focus () {
current_document.focus ();
}
// This is called when tab context menu is opened or closed
private void tab_view_setup_menu (Hdy.TabPage? page) {
tab_menu_target = page;
var close_other_tabs_action = Utils.action_from_group (MainWindow.ACTION_CLOSE_OTHER_TABS, window.actions);
var close_tabs_to_right_action = Utils.action_from_group (MainWindow.ACTION_CLOSE_TABS_TO_RIGHT, window.actions);
int page_position = page != null ? tab_view.get_page_position (page) : -1;
close_other_tabs_action.set_enabled (page != null && tab_view.n_pages > 1);
close_tabs_to_right_action.set_enabled (page != null && page_position != tab_view.n_pages - 1);
}
private void insert_document (Scratch.Services.Document doc, int pos) {
tab_view.insert (doc, pos);
if (Scratch.saved_state.get_boolean ("outline-visible")) {
debug ("setting outline visible");
doc.show_outline (true);
}
}
private string unsaved_file_path_builder (string extension = "txt") {
var timestamp = new DateTime.now_local ();
string new_text_file = _("Text file from %s:%d").printf (
timestamp.format ("%Y-%m-%d %H:%M:%S"), timestamp.get_microsecond ()
);
return Path.build_filename (window.app.data_home_folder_unsaved, new_text_file) + "." + extension;
}
private string unsaved_duplicated_file_path_builder (string original_filename) {
string extension = "txt";
string[] parts = original_filename.split (".", 2);
if (parts.length > 1) {
extension = parts[parts.length - 1];
}
return unsaved_file_path_builder (extension);
}
private void on_page_detached (Hdy.TabPage tab, int position) {
var doc = tab.get_child () as Services.Document;
if (doc == null) {
return;
}
if (tab_history_button.menu_model == null) {
tab_history_button.menu_model = new Menu ();
}
var path = doc.file.get_path ();
var path_in_menu = false;
var menu = (Menu) tab_history_button.menu_model;
int position_in_menu = -1;
for (var i = 0; i < menu.get_n_items (); i++) {
if (path == menu.get_item_attribute_value (i, Menu.ATTRIBUTE_TARGET, VariantType.STRING).get_string ()) {
path_in_menu = true;
position_in_menu = i;
break;
}
}
if (path_in_menu) {
menu.remove (position_in_menu);
}
if (menu.get_n_items () == TAB_HISTORY_MAX_ITEMS) {
menu.remove (TAB_HISTORY_MAX_ITEMS - 1);
}
menu.prepend (
path,
"%s::%s".printf (MainWindow.ACTION_PREFIX + MainWindow.ACTION_RESTORE_CLOSED_TAB, path)
);
docs.remove (doc);
tab_removed (doc);
Scratch.Services.DocumentManager.get_instance ().remove_open_document (doc);
doc.source_view.focus_in_event.disconnect (on_focus_in_event);
if (docs.length () > 0) {
if (!doc.is_file_temporary) {
foreach (var d in docs) {
rename_tabs_with_same_title (d);
}
}
}
request_placeholder_if_empty ();
}
public void restore_closed_tab (string path) {
var file = File.new_for_path (path);
var doc = new Services.Document (window.actions, file);
open_document.begin (doc);
var menu = (Menu) tab_history_button.menu_model;
for (var i = 0; i < menu.get_n_items (); i++) {
if (path == menu.get_item_attribute_value (i, Menu.ATTRIBUTE_TARGET, VariantType.STRING).get_string ()) {
menu.remove (i);
break;
}
}
if (menu.get_n_items () == 0) {
tab_history_button.menu_model = null;
}
}
private void on_doc_reordered (Hdy.TabPage tab, int new_position) {
var doc = tab.child as Services.Document;
if (doc != null) {
docs.remove (doc);
docs.insert (doc, new_position);
current_document = doc;
}
save_opened_files ();
}
private unowned Hdy.TabView? on_doc_to_new_window (Hdy.TabView tab_view) {
var other_window = new MainWindow (false);
return other_window.document_view.tab_view;
}
private void on_doc_added (Hdy.TabPage page, int position) {
var doc = page.get_child () as Services.Document;
doc.init_tab (page);
docs.append (doc);
doc.actions = window.actions;
Scratch.Services.DocumentManager.get_instance ().add_open_document (doc);
if (!doc.is_file_temporary) {
rename_tabs_with_same_title (doc);
}
doc.source_view.focus_in_event.connect_after (on_focus_in_event);
tab_added (doc);
}
private bool on_focus_in_event () {
var doc = current_document;
if (doc == null) {
warning ("Focus event callback cannot get current document");
} else {
document_change (doc, this);
}
return false;
}
private void rename_tabs_with_same_title (Services.Document doc) {
if (doc.is_file_temporary) {
return;
}
string doc_tab_name = doc.file.get_basename ();
foreach (var d in docs) {
if (d.is_file_temporary) {
continue;
}
string new_tabname_doc, new_tabname_d;
if (Utils.find_unique_path (d.file, doc.file, out new_tabname_d, out new_tabname_doc)) {
if (d.title.length < new_tabname_d.length) {
d.title = new_tabname_d;
}
if (doc_tab_name.length < new_tabname_doc.length) {
doc_tab_name = new_tabname_doc;
}
}
}
doc.title = doc_tab_name;
}
private void save_focused_document_uri (Services.Document? current_document) {
if (privacy_settings.get_boolean ("remember-recent-files")) {
var file_uri = "";
if (current_document != null) {
file_uri = current_document.file.get_uri ();
}
Scratch.settings.set_string ("focused-document", file_uri);
}
}
private GLib.Menu create_menu_model () {
var menu = new GLib.Menu ();
var close_tab_section = new Menu ();
close_tab_section.append (_("Close Tabs to the Right"), MainWindow.ACTION_PREFIX + MainWindow.ACTION_CLOSE_TABS_TO_RIGHT);
close_tab_section.append (_("Close Other Tabs"), MainWindow.ACTION_PREFIX + MainWindow.ACTION_CLOSE_OTHER_TABS);
close_tab_section.append (_("Close Tab"), MainWindow.ACTION_PREFIX + MainWindow.ACTION_CLOSE_TAB + "::");
var open_tab_section = new Menu ();
open_tab_section.append (_("Open in New Window"), MainWindow.ACTION_PREFIX + MainWindow.ACTION_MOVE_TAB_TO_NEW_WINDOW);
open_tab_section.append (_("Duplicate Tab"), MainWindow.ACTION_PREFIX + MainWindow.ACTION_DUPLICATE_TAB);
menu.append_section (null, open_tab_section);
menu.append_section (null, close_tab_section);
return menu;
}
private void drag_received (Gtk.Widget w,
Gdk.DragContext ctx,
int x,
int y,
Gtk.SelectionData sel,
uint info,
uint time) {
if (info == TargetType.URI_LIST) {
var uris = sel.get_uris ();
foreach (var filename in uris) {
var file = File.new_for_uri (filename);
var doc = new Services.Document (window.actions, file);
open_document.begin (doc);
}
Gtk.drag_finish (ctx, true, false, time);
}
}
}