-
Notifications
You must be signed in to change notification settings - Fork 9
/
askRenamePopup.js
89 lines (82 loc) · 3.51 KB
/
askRenamePopup.js
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
/* LICENSE INFORMATION
*
* Desktop Icons: Neo - A desktop icons extension for GNOME with numerous features,
* customizations, and optimizations.
*
* Copyright 2021 Abdurahman Elmawi (cooper64doom@gmail.com)
*
* This project is based on Desktop Icons NG (https://gitlab.com/rastersoft/desktop-icons-ng),
* a desktop icons extension for GNOME licensed under the GPL v3.
*
* This project is free and open source software as described in the GPL v3.
*
* This project (Desktop Icons: Neo) is licensed under the GPL v3. To view the details of this license,
* visit https://www.gnu.org/licenses/gpl-3.0.html for the necessary information
* regarding this project's license.
*/
const Gtk = imports.gi.Gtk;
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const DBusUtils = imports.dbusUtils;
const DesktopIconsUtil = imports.desktopIconsUtil;
const Gettext = imports.gettext.domain('desktopicons-neo');
const _ = Gettext.gettext;
var AskRenamePopup = class {
constructor(fileItem) {
this._desktopPath = GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_DESKTOP);
this._fileItem = fileItem;
this._popover = new Gtk.Popover({relative_to: fileItem._container,
modal: true});
let contentBox = new Gtk.Grid({row_spacing: 6,
column_spacing: 6,
margin: 10});
this._popover.add(contentBox);
let label = new Gtk.Label({label: fileItem.isDirectory ? _("Folder name") : _("File name"),
justify: Gtk.Justification.LEFT,
halign: Gtk.Align.START});
contentBox.attach(label, 0, 0, 2, 1);
this._textArea = new Gtk.Entry();
this._textArea.text = fileItem.fileName;
contentBox.attach(this._textArea, 0, 1, 1, 1);
this._button = new Gtk.Button({label: _("Rename")});
contentBox.attach(this._button, 1, 1, 1, 1);
this._button.connect('clicked', () => {
this._do_rename();
});
this._textArea.connect('changed', () => {
this._validate();
});
this._textArea.connect('activate', () => {
if (this._button.sensitive) {
this._do_rename();
}
});
this._textArea.set_can_default(true);
this._popover.set_default_widget(this._textArea);
this._button.get_style_context().add_class("suggested-action");
this._popover.show_all();
this._validate();
this._textArea.grab_focus_without_selecting();
this._textArea.select_region(0, DesktopIconsUtil.getFileExtensionOffset(fileItem.fileName, fileItem.isDirectory));
}
_validate() {
let text = this._textArea.text;
let final_path = this._desktopPath + '/' + text;
let final_file = Gio.File.new_for_commandline_arg(final_path);
if ((text == '') || (-1 != text.indexOf('/')) || (text == this._fileItem.fileName) || final_file.query_exists(null)) {
this._button.sensitive = false;
} else {
this._button.sensitive = true;
}
}
_do_rename() {
DBusUtils.NautilusFileOperations2Proxy.RenameURIRemote(
this._fileItem.file.get_uri(), this._textArea.text,
DBusUtils.NautilusFileOperations2Proxy.platformData(),
(result, error) => {
if (error)
throw new Error('Error renaming file: ' + error.message);
}
);
}
};