Skip to content

Commit

Permalink
Fix #29 with a three-states cycle option (instead of #65)
Browse files Browse the repository at this point in the history
  • Loading branch information
maoschanz committed Nov 20, 2021
1 parent 9c5074d commit f06f504
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 206 deletions.
130 changes: 61 additions & 69 deletions [email protected]/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const PATH = GLib.build_pathv('/', [GLib.get_user_data_dir(), 'notes@maestroscha

var NOTES_MANAGER;
var SETTINGS;
var Z_POSITION;
var LAYER_SETTING;
var AUTO_FOCUS;

function init() {
Expand All @@ -37,7 +37,7 @@ function init() {
} catch (e) {
log(e.message);
}
Z_POSITION = '';
LAYER_SETTING = '';
}

function enable() {
Expand Down Expand Up @@ -72,7 +72,7 @@ class NotesManager {
this.panel_button.add_child(icon);
this.panel_button.connect(
'button-press-event',
this._toggleState.bind(this)
this._onButtonPressed.bind(this)
);
this._updateIconVisibility();
// `0` is the position within the chosen box (here, the `right` one)
Expand All @@ -81,39 +81,25 @@ class NotesManager {
// Initialisation of the notes themselves
this._allNotes = new Array();
this._notesAreVisible = false;
this._updateLayoutSetting(); // it inits Z_POSITION
this._updateLayerSetting(); // it inits LAYER_SETTING

this._notesLoaded = false; // this will tell the toggleState method that
// notes need to be loaded first, thus doing the actual initialisation

// Initialisation of the signals connections
this._bindVisibilityShortcut();
this._bindLayerShortcut();
this._connectAllSignals();
}

_bindVisibilityShortcut () {
this.USE_VISIBILITY_SHORTCUT = Convenience.getSettings().get_boolean('use-shortcut');
if (this.USE_VISIBILITY_SHORTCUT) {
this.USE_SHORTCUT = Convenience.getSettings().get_boolean('use-shortcut');
if (this.USE_SHORTCUT) {
Main.wm.addKeybinding(
'notes-kb-shortcut',
Convenience.getSettings(),
Meta.KeyBindingFlags.NONE,
Shell.ActionMode.ALL,
this._toggleState.bind(this)
);
}
}

_bindLayerShortcut () {
this.USE_LAYER_SHORTCUT = Convenience.getSettings().get_boolean('use-layer-shortcut');
if (this.USE_LAYER_SHORTCUT) {
Main.wm.addKeybinding(
'notes-layer-kb-shortcut',
Convenience.getSettings(),
Meta.KeyBindingFlags.NONE,
Shell.ActionMode.ALL,
this._toggleLayer.bind(this)
this._onButtonPressed.bind(this)
);
}
}
Expand Down Expand Up @@ -179,31 +165,11 @@ class NotesManager {
return areaIsFree;
}

//--------------------------------------------------------------------------

_toggleLayer () {
if(SETTINGS.get_string('layout-position') === 'above-all') {
SETTINGS.set_string('layout-position', 'on-background');
} else { // 'on-background':
SETTINGS.set_string('layout-position', 'above-all');
}
notesNeedChromeTracking () {
return this._layer_id == 'above-all';
}

_toggleState () {
if(!this._notesLoaded) {
this._loadAllNotes();
}

// log('_toggleState');
if(this._allNotes.length == 0) {
this.createNote('', 16);
this._showNotes();
} else if (this._notesAreVisible) {
this._hideNotes();
} else {
this._showNotes();
}
}
//--------------------------------------------------------------------------

_showNotes () {
this._notesAreVisible = true;
Expand Down Expand Up @@ -239,6 +205,48 @@ class NotesManager {
statefile.delete(null); // may not do anything
}

_onButtonPressed () {
if(!this._notesLoaded) {
this._loadAllNotes();
}
// log('_onButtonPressed');

let preventReshowing = false;
if(LAYER_SETTING === 'cycle-layers') {
this._allNotes.forEach(function (n) {
n.removeFromCorrectLayer();
});

// the notes' visibility will be inverted later
if(!this._notesAreVisible) {
this._layer_id = 'on-background';
this._notesAreVisible = false;
} else if(this._layer_id === 'on-background') {
this._layer_id = 'above-all';
this._notesAreVisible = false;
preventReshowing = true;
} else if(this._layer_id === 'above-all') {
this._layer_id = 'above-all';
this._notesAreVisible = true;
}

this._allNotes.forEach(function (n) {
n.loadIntoCorrectLayer();
});
}

if(this._allNotes.length == 0) {
this.createNote('', 16);
this._showNotes();
} else if (this._notesAreVisible) {
this._hideNotes();
} else if (preventReshowing) {
this._notesAreVisible = true;
} else {
this._showNotes();
}
}

//--------------------------------------------------------------------------
// Watch the gsettings values and update the extension if they change ------

Expand All @@ -247,7 +255,7 @@ class NotesManager {

this._settingsSignals['layout'] = SETTINGS.connect(
'changed::layout-position',
this._updateLayoutSetting.bind(this)
this._updateLayerSetting.bind(this)
);
this._settingsSignals['bring-back'] = SETTINGS.connect(
'changed::ugly-hack',
Expand All @@ -265,34 +273,19 @@ class NotesManager {
'changed::notes-kb-shortcut',
this._updateShortcut.bind(this)
);
this._settingsSignals['kb-shortcut-3'] = SETTINGS.connect(
'changed::use-layer-shortcut',
this._updateLayerShortcut.bind(this)
);
this._settingsSignals['kb-shortcut-4'] = SETTINGS.connect(
'changed::notes-layer-kb-shortcut',
this._updateLayerShortcut.bind(this)
);
this._settingsSignals['auto-focus'] = SETTINGS.connect(
'changed::auto-focus',
this._updateFocusSetting.bind(this)
);
}

_updateShortcut () {
if(this.USE_VISIBILITY_SHORTCUT) {
if(this.USE_SHORTCUT) {
Main.wm.removeKeybinding('notes-kb-shortcut');
}
this._bindVisibilityShortcut();
}

_updateLayerShortcut () {
if(this.USE_LAYER_SHORTCUT) {
Main.wm.removeKeybinding('notes-layer-kb-shortcut');
}
this._bindLayerShortcut();
}

_updateFocusSetting () {
// XXX currently not very user-friendly
Main.notify(
Expand All @@ -317,12 +310,16 @@ class NotesManager {
* is actually set by the user. Possible values for the layers can be
* 'above-all', 'on-background' or 'cycle-layers'.
*/
_updateLayoutSetting () {
_updateLayerSetting () {
this._allNotes.forEach(function (n) {
n.removeFromCorrectLayer();
});

Z_POSITION = SETTINGS.get_string('layout-position');
LAYER_SETTING = SETTINGS.get_string('layout-position');
this._layer_id = (LAYER_SETTING == 'on-background')
? 'on-background'
: 'above-all'
;

this._allNotes.forEach(function (n) {
n.loadIntoCorrectLayer();
Expand All @@ -341,21 +338,16 @@ class NotesManager {
SETTINGS.disconnect(this._settingsSignals['hide-icon']);
SETTINGS.disconnect(this._settingsSignals['kb-shortcut-1']);
SETTINGS.disconnect(this._settingsSignals['kb-shortcut-2']);
SETTINGS.disconnect(this._settingsSignals['kb-shortcut-3']);
SETTINGS.disconnect(this._settingsSignals['kb-shortcut-4']);
SETTINGS.disconnect(this._settingsSignals['auto-focus']);

this._allNotes.forEach(function (n) {
n.onlySave(false);
n.destroy();
});

if(this.USE_VISIBILITY_SHORTCUT) {
if(this.USE_SHORTCUT) {
Main.wm.removeKeybinding('notes-kb-shortcut');
}
if(this.USE_LAYER_SHORTCUT) {
Main.wm.removeKeybinding('notes-layer-kb-shortcut');
}

this.panel_button.destroy();
}
Expand Down
10 changes: 5 additions & 5 deletions [email protected]/noteBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ var NoteBox = class NoteBox {

// Each note sets its own actor where it should be. This isn't a problem
// since the related setting isn't directly accessed, but is stored in
// 'Extension.Z_POSITION' instead, which prevent inconsistencies.
// 'Extension.NOTES_MANAGER' instead, which prevents inconsistencies.
this.loadIntoCorrectLayer();
this._setNotePosition();
this._loadText();
Expand Down Expand Up @@ -266,7 +266,7 @@ var NoteBox = class NoteBox {
// "Public" methods called by NotesManager ---------------------------------

loadIntoCorrectLayer () {
if (Extension.Z_POSITION == 'above-all') {
if (Extension.NOTES_MANAGER.notesNeedChromeTracking()) {
Main.layoutManager.addChrome(this.actor, {
affectsInputRegion: true
});
Expand All @@ -276,7 +276,7 @@ var NoteBox = class NoteBox {
}

removeFromCorrectLayer () {
if (Extension.Z_POSITION == 'above-all') {
if (Extension.NOTES_MANAGER.notesNeedChromeTracking()) {
// Main.layoutManager.untrackChrome(this.actor);
Main.layoutManager.removeChrome(this.actor);
} else {
Expand All @@ -286,14 +286,14 @@ var NoteBox = class NoteBox {

show () {
this.actor.visible = true;
if (Extension.Z_POSITION == 'above-all') {
if (Extension.NOTES_MANAGER.notesNeedChromeTracking()) {
Main.layoutManager.trackChrome(this.actor);
}
}

onlyHide () {
this.actor.visible = false;
if (Extension.Z_POSITION == 'above-all') {
if (Extension.NOTES_MANAGER.notesNeedChromeTracking()) {
Main.layoutManager.untrackChrome(this.actor);
}
}
Expand Down
42 changes: 5 additions & 37 deletions [email protected]/prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,22 @@ const NotesSettingsWidget = new GObject.Class({
// Position of the notes (layer)
let radioBtn1 = builder.get_object('radio1');
let radioBtn2 = builder.get_object('radio2');
// let radioBtn3 = builder.get_object('radio3');
let radioBtn3 = builder.get_object('radio3');
switch (SETTINGS.get_string('layout-position')) {
case 'above-all':
radioBtn1.set_active(true);
break;
case 'on-background':
radioBtn2.set_active(true);
break;
// case 'cycle-layers':
// radioBtn3.set_active(true);
// break;
case 'cycle-layers':
radioBtn3.set_active(true);
break;
default: break;
}
this._connectRadioBtn('above-all', radioBtn1);
this._connectRadioBtn('on-background', radioBtn2);
// this._connectRadioBtn('cycle-layers', radioBtn3);
this._connectRadioBtn('cycle-layers', radioBtn3);

//----------------------------------------------------------------------

Expand Down Expand Up @@ -121,38 +121,6 @@ const NotesSettingsWidget = new GObject.Class({
this._hide_switch.sensitive = widget.active;
});

// Context: %s will be replaced with the default keyboard shortcut
let default_kbs_label2 = _("Default value is %s");
default_kbs_label2 = default_kbs_label2.replace('%s', "<Super><Alt>j");

// Text entry
let keybinding_entry2 = builder.get_object('keybinding_entry2');
keybinding_entry2.set_sensitive(SETTINGS.get_boolean('use-layer-shortcut'));
keybinding_entry2.set_tooltip_text(default_kbs_label2);

if (SETTINGS.get_strv('notes-layer-kb-shortcut') != '') {
keybinding_entry2.text = SETTINGS.get_strv('notes-layer-kb-shortcut')[0];
}

// "Apply" button
let keybinding_button2 = builder.get_object('keybinding_button2');
keybinding_button2.set_sensitive(SETTINGS.get_boolean('use-layer-shortcut'));

keybinding_button2.connect('clicked', (widget) => {
SETTINGS.set_strv('notes-layer-kb-shortcut', [keybinding_entry2.text]);
});

// "Enable shortcut" switch
let keybinding_switch2 = builder.get_object('keybinding_switch2');
keybinding_switch2.set_state(SETTINGS.get_boolean('use-layer-shortcut'));

keybinding_switch2.connect('notify::active', (widget) => {
SETTINGS.set_boolean('use-layer-shortcut', widget.active);
keybinding_entry2.sensitive = widget.active;
keybinding_button2.sensitive = widget.active;
this._hide_switch.sensitive = widget.active;
});

//----------------------------------------------------------------------

// The default color of the very first note
Expand Down
Loading

0 comments on commit f06f504

Please sign in to comment.