-
-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathTerminalView.vala
More file actions
510 lines (414 loc) · 17.6 KB
/
TerminalView.vala
File metadata and controls
510 lines (414 loc) · 17.6 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
/*
* SPDX-License-Identifier: LGPL-3.0-or-later
* SPDX-FileCopyrightText: 2024-2025 elementary, Inc. (https://elementary.io)
*/
public class Terminal.TerminalView : Granite.Bin {
private const int TAB_HISTORY_MAX_ITEMS = 20;
public int n_pages {
get {
return tab_view.n_pages;
}
}
public Adw.TabPage selected_page {
get {
return tab_view.selected_page;
}
set {
tab_view.selected_page = value;
}
}
public unowned MainWindow main_window { private get; construct; }
public Adw.TabBar tab_bar { get; private set; }
public Adw.TabView tab_view { get; private set; }
public Adw.TabPage? tab_menu_target { get; private set; default = null; }
private static GLib.Settings gnome_interface_settings;
private Widgets.ZoomOverlay zoom_overlay;
private Gtk.MenuButton tab_history_button;
private Pango.FontDescription term_font;
static construct {
gnome_interface_settings = new GLib.Settings ("org.gnome.desktop.interface");
}
public TerminalView (MainWindow window) {
Object (main_window: window);
}
construct {
var app_instance = (Gtk.Application) GLib.Application.get_default ();
tab_view = new Adw.TabView () {
hexpand = true,
vexpand = true
};
tab_view.menu_model = create_menu_model ();
tab_view.setup_menu.connect (tab_view_setup_menu);
var new_tab_button = new Gtk.Button.from_icon_name ("list-add-symbolic") {
has_frame = false,
tooltip_markup = Granite.markup_accel_tooltip (
app_instance.get_accels_for_action (MainWindow.ACTION_PREFIX + MainWindow.ACTION_NEW_TAB),
_("New Tab")
),
action_name = MainWindow.ACTION_PREFIX + MainWindow.ACTION_NEW_TAB
};
tab_history_button = new Gtk.MenuButton () {
icon_name = "document-open-recent-symbolic",
tooltip_text = _("Closed Tabs")
};
tab_bar = new Adw.TabBar () {
autohide = Application.settings.get_enum ("tab-bar-behavior") == 1,
expand_tabs = false,
inverted = true,
start_action_widget = new_tab_button,
end_action_widget = tab_history_button,
view = tab_view,
};
var overlay = new Gtk.Overlay () {
child = tab_view
};
zoom_overlay = new Widgets.ZoomOverlay (overlay);
// We don't add tab_bar because it's in the main window header
child = overlay;
Application.settings.changed["tab-bar-behavior"].connect (() => {
tab_bar.autohide = Application.settings.get_enum ("tab-bar-behavior") == 1;
});
var button_target = new Gtk.DropTarget (Type.STRING, Gdk.DragAction.COPY) {
preload = true // So we can predetermine whether string is suitable for dropping here
};
button_target.notify["value"].connect (on_add_button_target_value_changed);
button_target.drop.connect (on_add_button_drop);
new_tab_button.add_controller (button_target);
tab_view.notify["selected-page"].connect (zoom_overlay.hide_zoom_level);
tab_bar.setup_extra_drop_target (Gdk.DragAction.COPY, { Type.STRING });
tab_bar.extra_drag_drop.connect (on_tab_bar_extra_drag_drop);
var key_controller = new Gtk.EventControllerKey () {
propagation_phase = CAPTURE
};
key_controller.key_pressed.connect (key_pressed);
key_controller.key_released.connect (key_released);
add_controller (key_controller);
var focus_controller = new Gtk.EventControllerFocus () {
propagation_phase = CAPTURE
};
focus_controller.leave.connect (() => {
cancel_tab_numbers ();
});
add_controller (focus_controller);
update_font ();
gnome_interface_settings.changed["monospace-font-name"].connect (update_font);
Application.settings.changed["font"].connect (update_font);
}
public void make_restorable (string path) {
if (tab_history_button.menu_model == null) {
tab_history_button.menu_model = new Menu ();
tab_history_button.popover.has_arrow = false;
}
var menu = (Menu) tab_history_button.menu_model;
var 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 ()) {
position_in_menu = i;
break;
}
}
if (position_in_menu != -1) {
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)
);
}
public TerminalWidget add_new_tab (string? location, string program = "", int pos = -1) {
if (pos == -1) {
pos = n_pages;
}
var terminal_widget = new TerminalWidget () {
scrollback_lines = Application.settings.get_int ("scrollback-lines"),
/* Ensure usable scroll speed on Wayland - see https://github.com/elementary/terminal/issues/993 */
enable_fallback_scrolling = false,
scroll_unit_is_pixels = true,
/* Make the terminal occupy the whole GUI */
hexpand = true,
vexpand = true
};
terminal_widget.notify["font-scale"].connect ((obj, pspec) => {
zoom_overlay.show_zoom_level (((TerminalWidget) obj).font_scale);
main_window.save_opened_terminals (false, true);
});
var scrolled = new Gtk.ScrolledWindow () {
vadjustment = terminal_widget.vadjustment,
child = terminal_widget
};
unowned var tab = tab_view.insert (scrolled, pos);
tab.title = location != null ? Path.get_basename (location) : TerminalWidget.DEFAULT_LABEL;
terminal_widget.bind_property ("current-working-directory", tab, "tooltip");
terminal_widget.tab = tab;
terminal_widget.notify["tab-state"].connect (() => {
tab.icon = terminal_widget.tab_state.to_icon ();
});
// Set correct label now to avoid race when spawning shell
terminal_widget.set_font (term_font);
var current_terminal = get_term_widget (tab_view.selected_page);
if (current_terminal != null) {
terminal_widget.font_scale = current_terminal.font_scale;
} else {
terminal_widget.font_scale = Terminal.Application.saved_state.get_double ("zoom");
}
selected_page = tab;
/* Set up the virtual terminal */
if (location == "") {
terminal_widget.active_shell ();
} else {
terminal_widget.active_shell (location);
}
if (program.length > 0) {
terminal_widget.run_program (program);
}
main_window.save_opened_terminals (true, true);
return terminal_widget;
}
public void close_tab () {
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;
}
tab_view.close_pages_after (target);
tab_view.selected_page = target;
}
public void close_other_tabs () {
var target = tab_menu_target ?? tab_view.selected_page;
if (target == null) {
return;
}
tab_view.close_other_pages (target);
tab_view.selected_page = target;
}
public void cycle_tabs (Adw.NavigationDirection direction) {
var pos = tab_view.get_page_position (selected_page);
pos = direction == FORWARD ? pos + 1 : pos - 1;
pos = (pos + n_pages) % n_pages;
selected_page = tab_view.get_nth_page (pos);
}
public void transfer_tab_to_window (MainWindow window) {
var target = tab_menu_target ?? tab_view.selected_page;
if (target == null) {
return;
}
tab_view.transfer_page (target, window.notebook.tab_view, 0);
}
// This is called when tab context menu is opened or closed
private void tab_view_setup_menu (Adw.TabPage? page) {
tab_menu_target = page;
var actions = main_window.actions;
var close_other_tabs_action = Utils.action_from_group (MainWindow.ACTION_CLOSE_OTHER_TABS, actions);
var close_tabs_to_right_action = Utils.action_from_group (MainWindow.ACTION_CLOSE_TABS_TO_RIGHT, actions);
var open_in_new_window_action = Utils.action_from_group (MainWindow.ACTION_MOVE_TAB_TO_NEW_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);
open_in_new_window_action.set_enabled (page != null && tab_view.n_pages > 1);
}
private void key_released (uint keyval, uint keycode, Gdk.ModifierType modifiers) {
cancel_tab_numbers ();
}
private bool key_pressed (uint keyval, uint keycode, Gdk.ModifierType modifiers) {
switch (keyval) {
//NOTE Keys @1 to @9 in combination with Alt_L do not natively change tab - they
// cause `bash` to enter "digit argument" mode. Whether this behaviour is overridden is determined
// by the "alt-changes-tab" setting (default true).
// However keys KP_1 to KP_9 in combination with Alt_L is handled natively by Vte and always
// changes tab regardless of the setting.
// Holding down Alt_L causes tab numbers to appear in the tab labels regardless of the setting
case Gdk.Key.@1: //alt+[1-8]
case Gdk.Key.@2:
case Gdk.Key.@3:
case Gdk.Key.@4:
case Gdk.Key.@5:
case Gdk.Key.@6:
case Gdk.Key.@7:
case Gdk.Key.@8:
if (ALT_MASK in modifiers && Application.settings.get_boolean ("alt-changes-tab")) {
var tab_index = (int) (keyval - Gdk.Key.@1);
if (tab_index < n_pages) {
selected_page = tab_view.get_nth_page (tab_index);
}
return Gdk.EVENT_STOP;
}
break;
case Gdk.Key.@9:
if (n_pages > 0 && ALT_MASK in modifiers && Application.settings.get_boolean ("alt-changes-tab")) {
selected_page = tab_view.get_nth_page (n_pages - 1);
return Gdk.EVENT_STOP;
}
break;
case Gdk.Key.@0:
case Gdk.Key.KP_0:
if (ALT_MASK in modifiers && Application.settings.get_boolean ("alt-changes-tab")) {
show_tab_numbers ();
return Gdk.EVENT_STOP;
}
break;
default:
if (keyval == Gdk.Key.Alt_L) {
schedule_tab_numbers ();
}
break;
}
return Gdk.EVENT_PROPAGATE;
}
private uint tab_number_timeout = 0;
private bool tab_numbers_showing = false;
private void schedule_tab_numbers () {
if (tab_numbers_showing || tab_number_timeout > 0) {
return;
}
tab_number_timeout = Timeout.add (
Gtk.Settings.get_default ().gtk_long_press_time,
() => {
tab_number_timeout = 0;
show_tab_numbers ();
return Source.REMOVE;
}
);
}
private void show_tab_numbers () {
tab_numbers_showing = true;
//TODO Show number as badge? Need way of converting number to suitable icon
// that can be used as TabPage.indicator_icon.
// For now use text
for (int i = 0; i < this.n_pages; i++) {
var tab = this.tab_view.get_nth_page (i);
var badge = "(%d) ".printf (i + 1);
if (!tab.title.has_prefix (badge)) {
tab.title = badge + tab.title;
}
}
}
private void cancel_tab_numbers () {
if (tab_number_timeout > 0) {
Source.remove (tab_number_timeout);
tab_number_timeout = 0;
}
if (tab_numbers_showing) {
for (int i = 0; i < this.n_pages; i++) {
var tab = this.tab_view.get_nth_page (i);
var badge = "(%d) ".printf (i + 1);
if (tab.title.has_prefix (badge)) {
tab.title = tab.title.slice (badge.length, tab.title.length);
}
}
tab_numbers_showing = false;
}
}
private void update_font () {
// We have to fetch both values at least once, otherwise
// GLib.Settings won't notify on their changes
var app_font_name = Application.settings.get_string ("font");
var sys_font_name = gnome_interface_settings.get_string ("monospace-font-name");
if (app_font_name != "") {
term_font = Pango.FontDescription.from_string (app_font_name);
} else {
term_font = Pango.FontDescription.from_string (sys_font_name);
}
for (int i = 0; i < n_pages; i++) {
var term = get_term_widget (tab_view.get_nth_page (i));
term.set_font (term_font);
}
}
private static unowned TerminalWidget? get_term_widget (Adw.TabPage? tab) {
if (tab == null) {
return null;
}
var tab_child = (Gtk.ScrolledWindow) tab.child;
unowned var term = (TerminalWidget) tab_child.child;
return term;
}
public void after_tab_restored (string path) {
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 static Menu create_menu_model () {
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);
var reload_section = new Menu ();
reload_section.append (_("Reload"), MainWindow.ACTION_PREFIX + MainWindow.ACTION_TAB_RELOAD);
var menu = new Menu ();
menu.append_section (null, open_tab_section);
menu.append_section (null, close_tab_section);
menu.append_section (null, reload_section);
return menu;
}
private static void on_add_button_target_value_changed (Object obj, ParamSpec pspec)
requires (obj is Gtk.DropTarget) {
var button_target = (Gtk.DropTarget) obj;
unowned var value = button_target.get_value ();
if (!value.holds (typeof (string))) {
button_target.reject ();
return;
}
var uris = Uri.list_extract_uris (value.get_string ());
foreach (unowned var uri in uris) {
if (!Utils.valid_local_uri (uri, null)) {
button_target.reject ();
break;
}
}
}
private bool on_add_button_drop (Value val, double x, double y) {
var uris = Uri.list_extract_uris (val.get_string ());
var new_tab_action = Utils.action_from_group (MainWindow.ACTION_NEW_TAB_AT, main_window.actions);
// ACTION_NEW_TAB_AT only works with local paths to folders
foreach (var uri in uris) {
string path;
if (!Utils.valid_local_uri (uri, out path)) {
continue;
}
new_tab_action.activate (path);
}
return true;
}
private bool on_tab_bar_extra_drag_drop (Adw.TabPage tab, Value val) {
//TODO Gtk4 Port:Check val contains uri_list
var uris = Uri.list_extract_uris (val.dup_string ());
var active_shell_action = Utils.action_from_group (MainWindow.ACTION_TAB_ACTIVE_SHELL, main_window.actions);
// ACTION_TAB_ACTIVE_SHELL only works with local paths to folders
foreach (var uri in uris) {
var file = GLib.File.new_for_uri (uri);
var scheme = file.get_uri_scheme ();
if (scheme != "file" && scheme != "") {
return false;
}
var type = file.query_file_type (NONE);
string path;
if (type == DIRECTORY) {
path = file.get_path ();
} else if (type == REGULAR) {
path = file.get_parent ().get_path ();
} else {
continue;
}
active_shell_action.activate (path);
}
return true;
}
}