Skip to content

Commit 2a1e129

Browse files
committed
Array of all notes as attribute of the NotesManager singleton
instead of a global variable (#43) well NotesManager is a singleton whose instance is a global variable so it's not *that* big, but it's cleaner the GLOBAL_IS_VISIBLE global variable also becomes an attribute, which fixes a bug (when the layout setting was changed, the visibility of the notes became inconsistent)
1 parent 8f63ba8 commit 2a1e129

File tree

5 files changed

+58
-55
lines changed

5 files changed

+58
-55
lines changed

LICENSE

100755100644
File mode changed.

notes@maestroschan.fr/extension.js

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,16 @@ const NoteBox = Me.imports.noteBox;
2020
const Gettext = imports.gettext.domain('notes-extension');
2121
const _ = Gettext.gettext;
2222

23-
// ~/.local/share/notes@maestroschan.fr
23+
//------------------------------------------------------------------------------
24+
2425
const PATH = GLib.build_pathv('/', [GLib.get_user_data_dir(), 'notes@maestroschan.fr']);
26+
// which is usually ~/.local/share/notes@maestroschan.fr
2527

2628
var NOTES_MANAGER;
27-
let GLOBAL_ARE_VISIBLE;
2829
let SETTINGS;
2930
var Z_POSITION;
3031
var AUTO_FOCUS;
3132

32-
//------------------------------------------------------------------------------
33-
34-
var ALL_NOTES;
35-
3633
function init() {
3734
Convenience.initTranslations();
3835
try {
@@ -48,8 +45,7 @@ function init() {
4845

4946
function enable() {
5047
SETTINGS = Convenience.getSettings();
51-
AUTO_FOCUS = SETTINGS.get_boolean('auto-focus');
52-
ALL_NOTES = new Array(); // TODO en attribut du manager
48+
AUTO_FOCUS = SETTINGS.get_boolean('auto-focus'); // XXX crado
5349

5450
NOTES_MANAGER = new NotesManager();
5551
}
@@ -94,8 +90,9 @@ class NotesManager {
9490
Main.panel.addToStatusArea('NotesButton', this.panel_button, 0, 'right');
9591

9692
// Initialisation of the notes themselves
97-
GLOBAL_ARE_VISIBLE = false; // XXX dégueu + cassé quand on update le layout
98-
this._updateLayoutSetting(); // ALL_NOTES is empty but it inits Z_POSITION
93+
this._allNotes = new Array();
94+
this._notesAreVisible = false;
95+
this._updateLayoutSetting(); // it inits Z_POSITION
9996
this._loadAllNotes();
10097

10198
// Initialisation of the signals connections
@@ -120,7 +117,7 @@ class NotesManager {
120117
let i = 0;
121118
let ended = false;
122119
while(!ended) {
123-
let file2 = GLib.build_filenamev([PATH, '/' + i.toString() + '_state']);
120+
let file2 = GLib.build_filenamev([PATH, i.toString() + '_state']);
124121
if (GLib.file_test(file2, GLib.FileTest.EXISTS)) {
125122
this.createNote('', 16);
126123
} else {
@@ -134,10 +131,10 @@ class NotesManager {
134131
//--------------------------------------------------------------------------
135132
// "Public" methods, accessed by the NoteBox objects -----------------------
136133

137-
createNote (colorAsString, fontSize) {
138-
let nextId = ALL_NOTES.length;
134+
createNote (colorString, fontSize) {
135+
let nextId = this._allNotes.length;
139136
try {
140-
ALL_NOTES.push(new NoteBox.NoteBox(nextId, colorAsString, fontSize));
137+
this._allNotes.push(new NoteBox.NoteBox(nextId, colorString, fontSize));
141138
} catch (e) {
142139
Main.notify(_("Notes extension error: failed to load a note"));
143140
log('failed to create note n°' + nextId.toString());
@@ -147,16 +144,17 @@ class NotesManager {
147144

148145
/*
149146
* When a NoteBox object deletes itself, it calls this method to ensure the
150-
* files go from 0 to (ALL_NOTES.length - 1) without any "gap" in the
147+
* files go from 0 to (this._allNotes.length - 1) without any "gap" in the
151148
* numerotation.
152149
*/
153150
postDelete (deletedNoteId) {
154-
let lastNote = ALL_NOTES.pop();
155-
if (deletedNoteId < ALL_NOTES.length) {
156-
ALL_NOTES[deletedNoteId] = lastNote;
151+
let lastNote = this._allNotes.pop();
152+
if (deletedNoteId < this._allNotes.length) {
153+
this._allNotes[deletedNoteId] = lastNote;
157154
lastNote.id = deletedNoteId;
155+
this._allNotes[deletedNoteId].onlySave();
158156
}
159-
this._deleteNoteFiles(ALL_NOTES.length);
157+
this._deleteNoteFiles(this._allNotes.length);
160158
}
161159

162160
/*
@@ -167,7 +165,7 @@ class NotesManager {
167165
*/
168166
areCoordsUsable (x, y) {
169167
let areaIsFree = true;
170-
ALL_NOTES.forEach(function (n) {
168+
this._allNotes.forEach(function (n) {
171169
if( (Math.abs(n._x - x) < 230) && (Math.abs(n._y - y) < 100) ) {
172170
areaIsFree = false;
173171
}
@@ -179,35 +177,35 @@ class NotesManager {
179177

180178
_toggleState () {
181179
// log('_toggleState');
182-
if(ALL_NOTES.length == 0) {
180+
if(this._allNotes.length == 0) {
183181
this.createNote('', 16);
184182
this._showNotes();
185-
} else if (GLOBAL_ARE_VISIBLE) {
183+
} else if (this._notesAreVisible) {
186184
this._hideNotes();
187185
} else {
188186
this._showNotes();
189187
}
190188
}
191189

192190
_showNotes () {
193-
GLOBAL_ARE_VISIBLE = true;
194-
ALL_NOTES.forEach(function (n) {
191+
this._notesAreVisible = true;
192+
this._allNotes.forEach(function (n) {
195193
n.show();
196194
});
197195
}
198196

199197
_hideNotes () {
200198
this._onlyHideNotes();
201-
ALL_NOTES.forEach(function (n) {
199+
this._allNotes.forEach(function (n) {
202200
n.onlySave();
203201
});
204202
}
205203

206204
_onlyHideNotes () {
207-
ALL_NOTES.forEach(function (n) {
205+
this._allNotes.forEach(function (n) {
208206
n.onlyHide();
209207
});
210-
GLOBAL_ARE_VISIBLE = false;
208+
this._notesAreVisible = false;
211209
}
212210

213211
_deleteNoteFiles (id) {
@@ -222,29 +220,29 @@ class NotesManager {
222220
// Watch the gsettings values and update the extension if they change ------
223221

224222
_connectAllSignals () {
225-
this._SIGNALS = {};
223+
this._settingsSignals = {};
226224

227-
this._SIGNALS['layout'] = SETTINGS.connect(
225+
this._settingsSignals['layout'] = SETTINGS.connect(
228226
'changed::layout-position',
229227
this._updateLayoutSetting.bind(this)
230228
);
231-
this._SIGNALS['bring-back'] = SETTINGS.connect(
229+
this._settingsSignals['bring-back'] = SETTINGS.connect(
232230
'changed::ugly-hack',
233231
this._bringToPrimaryMonitorOnly.bind(this)
234232
);
235-
this._SIGNALS['hide-icon'] = SETTINGS.connect(
233+
this._settingsSignals['hide-icon'] = SETTINGS.connect(
236234
'changed::hide-icon',
237235
this._updateIconVisibility.bind(this)
238236
);
239-
this._SIGNALS['kb-shortcut-1'] = SETTINGS.connect(
237+
this._settingsSignals['kb-shortcut-1'] = SETTINGS.connect(
240238
'changed::use-shortcut',
241239
this._updateShortcut.bind(this)
242240
);
243-
this._SIGNALS['kb-shortcut-2'] = SETTINGS.connect(
241+
this._settingsSignals['kb-shortcut-2'] = SETTINGS.connect(
244242
'changed::notes-kb-shortcut',
245243
this._updateShortcut.bind(this)
246244
);
247-
this._SIGNALS['auto-focus'] = SETTINGS.connect(
245+
this._settingsSignals['auto-focus'] = SETTINGS.connect(
248246
'changed::auto-focus',
249247
this._updateFocusSetting.bind(this)
250248
);
@@ -275,33 +273,43 @@ class NotesManager {
275273
}
276274

277275
_bringToPrimaryMonitorOnly () {
278-
ALL_NOTES.forEach(function (n) {
276+
this._allNotes.forEach(function (n) {
279277
n.fixState();
280278
});
281279
}
282280

281+
/*
282+
* Remove all the notes from where they are, and add them to the layer that
283+
* is actually set by the user. Possible values for the layers can be
284+
* 'above-all', 'on-background' or 'cycle-layers'.
285+
*/
283286
_updateLayoutSetting () {
284-
ALL_NOTES.forEach(function (n) {
287+
this._allNotes.forEach(function (n) {
285288
n.removeFromCorrectLayer();
286289
});
287290

288291
Z_POSITION = SETTINGS.get_string('layout-position');
289292

290-
ALL_NOTES.forEach(function (n) {
293+
this._allNotes.forEach(function (n) {
291294
n.loadIntoCorrectLayer();
292295
});
296+
297+
if(!this._notesAreVisible) {
298+
this._onlyHideNotes();
299+
}
293300
}
294301

295302
//--------------------------------------------------------------------------
296303

297304
destroy() {
298-
SETTINGS.disconnect(this._SIGNALS['layout']);
299-
SETTINGS.disconnect(this._SIGNALS['bring-back']);
300-
SETTINGS.disconnect(this._SIGNALS['hide-icon']);
301-
SETTINGS.disconnect(this._SIGNALS['kb-shortcut-1']);
302-
SETTINGS.disconnect(this._SIGNALS['kb-shortcut-2']);
303-
304-
ALL_NOTES.forEach(function (n) {
305+
SETTINGS.disconnect(this._settingsSignals['layout']);
306+
SETTINGS.disconnect(this._settingsSignals['bring-back']);
307+
SETTINGS.disconnect(this._settingsSignals['hide-icon']);
308+
SETTINGS.disconnect(this._settingsSignals['kb-shortcut-1']);
309+
SETTINGS.disconnect(this._settingsSignals['kb-shortcut-2']);
310+
SETTINGS.disconnect(this._settingsSignals['auto-focus']);
311+
312+
this._allNotes.forEach(function (n) {
305313
n.onlySave();
306314
n.destroy();
307315
});

notes@maestroschan.fr/menus.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ class NoteOptionsMenu {
5555
_redisplay () {
5656
this.super_menu.removeAll(); //-----------------------------------------
5757

58-
// this.super_menu.addAction(_("Edit title"), this._onEditTitle);
58+
// this.super_menu.addAction(_("Edit title"), this._onEditTitle);
5959

60-
// this._appendSeparator(); //---------------------------------------------
60+
// this._appendSeparator(); //---------------------------------------------
6161

6262
this.size_item = new PopupMenu.PopupBaseMenuItem({
6363
reactive: false,
-52 Bytes
Binary file not shown.

notes@maestroschan.fr/schemas/org.gnome.shell.extensions.notes-extension.gschema.xml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,15 @@
2727
<summary>Keyboard shortcut</summary>
2828
<description>If the keyboard shortcut should be active</description>
2929
</key>
30-
<key name="keyboard-shortcut" type="as">
31-
<default><![CDATA[[]]]></default>
32-
<summary>legacy</summary>
33-
<!-- TODO -->
34-
<description>should be deleted</description>
35-
</key>
3630
<key name="notes-kb-shortcut" type="as">
3731
<default><![CDATA[['<Super><Alt>n','']]]></default>
38-
<summary>Keyboard shortcut</summary>
32+
<summary>Keyboard shortcut value</summary>
3933
<description>Keyboard shortcut which shows or hides notes.</description>
4034
</key>
4135
<key type="b" name="hide-icon">
4236
<default>false</default>
4337
<summary>Hide the icon</summary>
44-
<description>Should be true only if there is shortcut</description>
38+
<description>Should be true only if there is shortcut.</description>
4539
</key>
4640

4741
<!-- -->
@@ -50,9 +44,10 @@
5044
<default>true</default>
5145
<summary>This has no actual meaning</summary>
5246
<description>
53-
This setting is used to call an extension.js function from prefs.js
47+
This setting is used to call an extension.js method from prefs.js
5448
</description>
5549
</key>
5650

5751
</schema>
5852
</schemalist>
53+

0 commit comments

Comments
 (0)