forked from bsaleil/todolist-gnome-shell-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
executable file
·259 lines (221 loc) · 6.61 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
// This extension was developed by :
// * 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 Utils = imports.misc.extensionUtils.getCurrentExtension().imports.utils;
const mySettings = Utils.getSettings();
const Gettext = imports.gettext;
const _ = Gettext.domain('todolist').gettext;
const MAX_LENGTH = 100;
const KEY_RETURN = 65293;
const KEY_ENTER = 65421;
const key_open = 'open-todolist'; // Schema key for key binding
const BASE_TASKS = "Do something\nDo something else\nDo more stuff\nDo that again\n";
let meta; // Metadata
let todo; // Todolist instance
let filePath; // Tasks file path
// TasksManager function
function TasksManager(metadata)
{
// File in home directory
filePath = GLib.get_home_dir() + "/.list.tasks";
let locales = metadata.path + "/locale";
Gettext.bindtextdomain('todolist', locales);
this._init();
}
// Prototype
TasksManager.prototype =
{
__proto__: PanelMenu.Button.prototype,
_init: function()
{
// Check if file exists and create it if not
if (!GLib.file_test(filePath, GLib.FileTest.EXISTS))
createBaseFile();
PanelMenu.Button.prototype._init.call(this, St.Align.START);
this.mainBox = null;
this.buttonText = new St.Label({text:_("(...)")});
this.buttonText.set_style("text-align:center;");
this.actor.add_actor(this.buttonText);
this.buttonText.get_parent().add_style_class_name("panelButtonWidth");
// Add keybinding
global.display.add_keybinding
(
key_open,
mySettings,
Meta.KeyBindingFlags.NONE,
Lang.bind(this, function() { this.menu.open(); })
);
// Auto focus
this.menu.connect('open-state-changed', Lang.bind(this, function(menu, open)
{
if (open) { this.newTask.grab_key_focus(); }
else { this.newTask.get_stage().set_key_focus(null); }
}));
this._refresh();
},
_refresh: function()
{
let varFile = filePath;
let tasksMenu = this.menu;
let buttonText = this.buttonText;
// 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
this.scrollView = new St.ScrollView({style_class: 'vfade',
hscrollbar_policy: Gtk.PolicyType.NEVER,
vscrollbar_policy: Gtk.PolicyType.AUTOMATIC});
this.scrollView.add_actor(this.todosBox);
this.mainBox.add_actor(this.scrollView);
// Sync
if (GLib.file_test(filePath, GLib.FileTest.EXISTS))
{
let content = Shell.get_file_contents_utf8_sync(filePath);
let lines = content.toString().split('\n');
let tasks = 0;
for (let i=0; i<lines.length; i++)
{
// if not empty
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();
buttonText.set_text(_("(...)"));
removeTask(textClicked,varFile);
}));
this.todosBox.add(item.actor);
tasks += 1;
}
}
buttonText.set_text("(" + tasks + ")");
if (tasks < 10) buttonText.get_parent().add_style_class_name("panelButtonWidth");
else buttonText.get_parent().remove_style_class_name("panelButtonWidth");
}
else { createBaseFile(); }
// Separator
this.Separator = new PopupMenu.PopupSeparatorMenuItem();
this.mainBox.add_actor(this.Separator.actor);
// Bottom section
let bottomSection = new PopupMenu.PopupMenuSection();
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', function(o,e)
{
let symbol = e.get_key_symbol();
if (symbol == KEY_RETURN || symbol == KEY_ENTER)
{
tasksMenu.close();
buttonText.set_text(_("(...)"));
addTask(o.get_text(),varFile);
entryNewTask.set_text('');
}
});
bottomSection.actor.add_actor(this.newTask);
bottomSection.actor.add_style_class_name("newTaskSection");
this.mainBox.add_actor(bottomSection.actor);
tasksMenu.box.add(this.mainBox);
},
_enable: function()
{
// Refresh menu
let fileM = Gio.file_new_for_path(filePath);
this.monitor = fileM.monitor(Gio.FileMonitorFlags.NONE, null);
this.monitor.connect('changed', Lang.bind(this, this._refresh));
},
_disable: function()
{
global.display.remove_keybinding(key_open);
this.monitor.cancel();
}
}
// Create base file (base tasks list) TODO
function createBaseFile()
{
GLib.file_set_contents(filePath,BASE_TASKS);
}
// Remove task "text" from file "file"
function removeTask(text,file)
{
let newText = "";
if (GLib.file_test(file, GLib.FileTest.EXISTS))
{
let content = Shell.get_file_contents_utf8_sync(file);
let tasks = content.toString().split('\n');
for (let i=0; i<tasks.length; i++)
{
// if not corresponding
if (tasks[i] != text)
{
newText += "\n";
newText += tasks[i];
}
}
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);
out.close(null);
}
else
{ global.logError("Todo list : Error with file : " + filePath); }
}
// Add task "text" to file "file"
function addTask(text,file)
{
if (GLib.file_test(file, GLib.FileTest.EXISTS))
{
let content = Shell.get_file_contents_utf8_sync(file);
content = content + text + "\n";
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);
}
else
{ global.logError("Todo list : Error with file : " + filePath); }
}
// Init function
function init(metadata)
{
meta = metadata;
}
function enable()
{
todo = new TasksManager(meta);
todo._enable();
Main.panel.addToStatusArea('todo', todo);
}
function disable()
{
todo._disable();
todo.destroy();
todo = null;
}