forked from bsaleil/todolist-gnome-shell-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
executable file
·279 lines (231 loc) · 7 KB
/
extension.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
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
// Authors:
// * Baptiste Saleil http://bsaleil.org/
// * Community: https://github.com/bsaleil/todolist-gnome-shell-extension/network
// With code from: https://github.com/vibou/vibou.gTile
//
// Licence: GPLv2+
const St = imports.gi.St;
const Gtk = imports.gi.Gtk;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const Main = imports.ui.main;
const Lang = imports.lang;
const Clutter = imports.gi.Clutter;
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const Shell = imports.gi.Shell;
const Meta = imports.gi.Meta;
const GObject = imports.gi.GObject;
const Gettext = imports.gettext;
const _ = Gettext.domain('todolist').gettext;
const Utils = imports.misc.extensionUtils.getCurrentExtension().imports.utils;
const ExtensionSettings = Utils.getSettings(); // Get settings from utils.js
const MAX_LENGTH = 100;
const KEY_RETURN = 65293;
const KEY_ENTER = 65421;
const BASE_TASKS = "Do something\nDo something else\nDo more stuff\nDo that again\n";
const Clipboard = St.Clipboard.get_default();
const CLIPBOARD_TYPE = St.ClipboardType.CLIPBOARD;
let todolist; // Todolist instance
let meta;
//----------------------------------------------------------------------
// TodoList class
let TodoList = GObject.registerClass(
class TodoList extends PanelMenu.Button {
_init() {
super._init(1.0, null, false);
this.meta = meta;
// Tasks file
this.filePath = GLib.get_home_dir() + "/.list.tasks";
// Locale
let locales = this.meta.path + "/locale";
Gettext.bindtextdomain('todolist', locales);
// Button ui
this.mainBox = null;
this.buttonText = new St.Label({text:_("(...)"), y_align: Clutter.ActorAlign.CENTER});
this.buttonText.set_style("text-align:center;");
this.actor.add_actor(this.buttonText);
this._buildUI();
this._refresh();
}
_buildUI(){
// Destroy previous box
if (this.mainBox != null)
this.mainBox.destroy();
// Create main box
this.mainBox = new St.BoxLayout();
this.mainBox.set_vertical(true);
// Create todos box
this.todosBox = new St.BoxLayout();
this.todosBox.set_vertical(true);
// Create todos scrollview
var scrollView = new St.ScrollView({style_class: 'vfade',
hscrollbar_policy: Gtk.PolicyType.NEVER,
vscrollbar_policy: Gtk.PolicyType.AUTOMATIC});
scrollView.add_actor(this.todosBox);
this.mainBox.add_actor(scrollView);
// Separator
var separator = new PopupMenu.PopupSeparatorMenuItem();
this.mainBox.add_actor(separator.actor);
// Text entry
this.newTask = new St.Entry(
{
name: "newTaskEntry",
hint_text: _("New task..."),
track_hover: true,
can_focus: true
});
let entryNewTask = this.newTask.clutter_text;
entryNewTask.set_max_length(MAX_LENGTH);
entryNewTask.connect('key-press-event', Lang.bind(this,function(o,e)
{
let symbol = e.get_key_symbol();
if (symbol == KEY_RETURN || symbol == KEY_ENTER)
{
this.menu.close();
this.buttonText.set_text(_("(...)"));
addTask(o.get_text(),this.filePath);
entryNewTask.set_text('');
}
}));
// Bottom section
var bottomSection = new PopupMenu.PopupMenuSection();
bottomSection.actor.add_actor(this.newTask);
bottomSection.actor.add_style_class_name("newTaskSection");
this.mainBox.add_actor(bottomSection.actor);
this.menu.box.add(this.mainBox);
}
_refresh(){
// Check if tasks file exists
checkFile(this.filePath);
// Add all tasks to ui
this.todosBox.destroy_all_children();
let content = Shell.get_file_contents_utf8_sync(this.filePath);
let lines = content.toString().split('\n');
let tasks = 0;
for (let i=0; i<lines.length; i++)
{
if (lines[i] != '' && lines[i] != '\n')
{
let item = new PopupMenu.PopupMenuItem(lines[i]);
let textClicked = lines[i];
item.connect('activate', Lang.bind(this,function(){
this.menu.close();
this.buttonText.set_text(_("(...)"));
removeTask(textClicked,this.filePath);
}));
this.todosBox.add(item.actor);
tasks += 1;
}
}
// Update status button
this.buttonText.set_text("(" + tasks + ")");
// Restore hint text
this.newTask.hint_text = _("New task...");
}
_enable() {
// Conect file 'changed' signal to _refresh
let fileM = Gio.file_new_for_path(this.filePath);
let mode = Shell.ActionMode ? Shell.ActionMode.ALL : Shell.KeyBindingMode.ALL;
this.monitor = fileM.monitor(Gio.FileMonitorFlags.WATCH_HARD_LINKS, null);
this.monitor.connect('changed', Lang.bind(this, this._refresh));
// Key binding
Main.wm.addKeybinding('open-todolist',
ExtensionSettings,
Meta.KeyBindingFlags.NONE,
mode,
Lang.bind(this, signalKeyOpen));
}
_disable() {
// Stop monitoring file
this.monitor.cancel();
}
});
//----------------------------------------------------------------------
// Utils
// Called when 'open-todolist' is emitted (binded with Lang.bind)
function signalKeyOpen(){
if (this.menu.isOpen)
this.menu.close();
else
{
this.menu.open();
this.newTask.grab_key_focus();
}
}
// Check if file exists. Create it if not
function checkFile(file){
if (!GLib.file_test(file, GLib.FileTest.EXISTS))
GLib.file_set_contents(file,BASE_TASKS);
}
// Remove task 'text' from file 'file'
function removeTask(text,file){
// Check if file exists
if (!GLib.file_test(file, GLib.FileTest.EXISTS))
{
global.logError("Todo list : Error with file : " + file);
return;
}
// Create new text to write
let content = Shell.get_file_contents_utf8_sync(file);
let tasks = content.toString().split('\n');
let newText = "";
for (let i=0; i<tasks.length; i++)
{
// Add task to new text if not empty and not removed task
if (tasks[i] != text && tasks[i] != '' && tasks[i] != '\n')
{
newText += tasks[i];
newText += "\n";
}
}
// Write new text to file
let f = Gio.file_new_for_path(file);
let out = f.replace(null, false, Gio.FileCreateFlags.NONE, null);
Shell.write_string_to_stream (out, newText);
// Copy removed item to clipboard if enabled
if(ExtensionSettings.get_boolean('clipboard'))
Clipboard.set_text(CLIPBOARD_TYPE, text);
out.close(null);
}
// Add task 'text' to file 'file'
function addTask(text,file)
{
// Don't add empty task
if (text == '' || text == '\n')
return;
// Check if file exists
if (!GLib.file_test(file, GLib.FileTest.EXISTS))
{
global.logError("Todo list : Error with file : " + file);
return;
}
// Append to content
let content = Shell.get_file_contents_utf8_sync(file);
content = content + text + "\n";
// Write new text to file
let f = Gio.file_new_for_path(file);
let out = f.replace(null, false, Gio.FileCreateFlags.NONE, null);
Shell.write_string_to_stream (out, content);
out.close(null);
}
//----------------------------------------------------------------------
// Shell entry points
// Init function
function init(metadata)
{
meta = metadata;
}
function enable()
{
todolist = new TodoList();
todolist._enable();
Main.panel.addToStatusArea('todolist', todolist, 1, 'right');
}
function disable()
{
todolist._disable();
todolist.destroy();
todolist = null;
}
//----------------------------------------------------------------------