Skip to content
This repository has been archived by the owner on Feb 24, 2018. It is now read-only.

Commit

Permalink
Manage hotkey by WebExtension (darrinhenein#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Croydon committed Apr 30, 2017
1 parent 57bca5c commit 329adea
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 100 deletions.
67 changes: 14 additions & 53 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@
var preferencesService = require("sdk/preferences/service");
var windows = require("sdk/windows");
var windowUtils = require("sdk/window/utils");
var hotkey = require("sdk/hotkeys").Hotkey;
var { viewFor } = require("sdk/view/core");

//var contentMod = require("sdk/content/mod");
//var Style = require("sdk/stylesheet/style").Style;


// WebExtension
const webExtension = require("sdk/webextension");
var webextPort;
Expand All @@ -40,38 +35,8 @@ function toggleDrawInTitlebar() {
}
}

// Hotkeys
var GLOBAL_SCOPE = this;

function hotkeyPress()
{
let windowID = windowUtils.getOuterId(windowUtils.getToplevelWindow(windowUtils.getFocusedWindow()));
GLOBAL_SCOPE["vt"+windowID].toggleDisplayState();
}

function initHotkeys() {
let toggleKey = webextPreferences["toggleDisplayHotkey"];
if(toggleKey != "")
{
GLOBAL_SCOPE.vtToggleDisplayHotkey = hotkey({
combo: toggleKey,
onPress: hotkeyPress
});
}
}

function destroyHotkey() {
if(typeof GLOBAL_SCOPE.vtToggleDisplayHotkey != undefined)
{
GLOBAL_SCOPE.vtToggleDisplayHotkey.destroy();
}
}

function changeHotkey() {
debugOutput("change Hotkey event!");
destroyHotkey();
initHotkeys();
}
var GLOBAL_SCOPE = this;

//
// WebExtenions Communication
Expand Down Expand Up @@ -147,6 +112,12 @@ function webext_replyHandler(message)
let windowID = windowUtils.getOuterId(windowUtils.getFocusedWindow());
GLOBAL_SCOPE["vt"+windowID].changeFullscreenMode(message.value);
}

if(message.type == "event.toggleTabbrowser")
{
let windowID = windowUtils.getOuterId(windowUtils.getToplevelWindow(windowUtils.getFocusedWindow()));
GLOBAL_SCOPE["vt"+windowID].toggleDisplayState();
}
}

// Send setting to WebExtension
Expand All @@ -166,20 +137,13 @@ function webext_sendChangedSetting(settingName)

function observPrefs(settingName)
{
if(settingName == "toggleDisplayHotkey")
{
changeHotkey();
}
else
{
for (let window of windows.browserWindows)
{
let lowLevelWindow = viewFor(window);
let windowID = windowUtils.getOuterId(lowLevelWindow);
debugOutput("observPrefs: " + settingName);
GLOBAL_SCOPE["vt"+windowID].onPreferenceChange(settingName, webextPreferences);
}
}
for (let window of windows.browserWindows)
{
let lowLevelWindow = viewFor(window);
let windowID = windowUtils.getOuterId(lowLevelWindow);
debugOutput("observPrefs: " + settingName);
GLOBAL_SCOPE["vt"+windowID].onPreferenceChange(settingName, webextPreferences);
}
}

//
Expand Down Expand Up @@ -220,8 +184,6 @@ function sdk_init()
{
deinitialize_window(window)
});

initHotkeys();
}

// Entry point of the add-on
Expand Down Expand Up @@ -275,7 +237,6 @@ exports.main = function (options, callbacks) {

exports.onUnload = function (reason) {
//debugOutput("onUnload:" + reason);
destroyHotkey();

for (let window of windows.browserWindows)
{
Expand Down
26 changes: 0 additions & 26 deletions lib/verticaltabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ var VerticalTabsReloaded = class VerticalTabsReloaded
if (this.preferences("compact") == true)
{
this.installStylesheet(this.preferences("dataPath") + "compact.css");
//this.webext_sendMsg({type: "css.get", name: "compact", file: "compact.css"});
}

if (this.preferences("style.tab.status") == true)
Expand Down Expand Up @@ -296,32 +295,9 @@ var VerticalTabsReloaded = class VerticalTabsReloaded
browserbox.dir = "normal";
}
break;
case "css":
/*if(this.parsedStyle != undefined)
{
contentMod.detachFrom(this.parsedStyle, this.window);
}
this.webExtPreferences = newValue;
let newStyle;
Object.keys(this.webExtPreferences["css"]).forEach(k =>
{
this.debugOutput(k);
newStyle = newStyle + this.webExtPreferences["css"][k];
this.debugOutput(this.webExtPreferences["css"][k]);
});
var style = Style({ source: newStyle });
this.parsedStyle = style;
this.debugOutput("css changed!");
this.debugOutput(this.parsedStyle);
contentMod.attachTo(this.parsedStyle, this.window);
break;*/
case "theme":
this.removeThemeStylesheet();
this.webExtPreferences = newValue;
//this.webext_sendMsg({type: "css.get", name: "theme", file: this.getThemeStylesheet(this.preferences("theme"))});
this.applyThemeStylesheet();
break;

Expand All @@ -330,12 +306,10 @@ var VerticalTabsReloaded = class VerticalTabsReloaded
if (this.preferences("compact") == true)
{
this.installStylesheet(this.preferences("dataPath") + "compact.css");
//this.webext_sendMsg({type: "css.get", name: "compact", file: "compact.css"});
}
else
{
this.removeStylesheet(this.preferences("dataPath") + "compact.css");
//this.webext_sendMsg({type: "css.get", name: "compact", file: "none"});
}
break;

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
"icon": "resource://verticaltabsreloaded-at-go-dev-dot-de/webextension/data/icon.png",
"author": "Croydon",
"license": "MPL-2.0",
"version": "0.8.3",
"version": "0.8.4-alpha",
"permissions":
{
"private-browsing": true,
"multiprocess": true
},
"engines":
{
"firefox": "53.0a1 - 55.0"
"firefox": "54.0a1 - 56.0"
},
"hasEmbeddedWebExtension": true
}
7 changes: 7 additions & 0 deletions webextension/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@ setTimeout(function() {

// Set up listener
browser.storage.onChanged.addListener(sdk_send_all_settings);
browser.commands.onCommand.addListener(function(command)
{
if (command == "toggleTabbrowser")
{
sdk_sendMsg({type: "event.toggleTabbrowser"});
}
});
}, 100);


Expand Down
15 changes: 13 additions & 2 deletions webextension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Vertical Tabs Reloaded",
"short_name": "VTR",
"description": "This Firefox add-on arranges tabs in a vertical rather than horizontal fashion.",
"version": "0.8.3",
"version": "0.8.4-alpha",
"manifest_version": 2,
"author": "Michael 'Croydon' Keck",
"developer":
Expand All @@ -20,6 +20,17 @@
"page": "options/options.html",
"browser_style": true
},
"commands":
{
"toggleTabbrowser":
{
"suggested_key":
{
"default": "Ctrl+Shift+V"
},
"description": "Hotkey for hiding/showing the tabbrowser"
}
},
"icons":
{
"16": "data/icon-16.png",
Expand All @@ -32,7 +43,7 @@
"gecko":
{
"id": "[email protected]",
"strict_min_version": "53.0"
"strict_min_version": "55.0"
}
}
}
29 changes: 18 additions & 11 deletions webextension/options/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ function build()

if(setting.description != undefined)
{
var settingsDescription = '<div class="preferences-description">' + setting.description + '</div>';
var description = '<br> <span class="preferences-description">' + setting.description + '</span>';
}
else
{
var settingsDescription = "";
var description = '';
}

if(setting.hidden == true)
Expand All @@ -62,42 +62,49 @@ function build()
var classHidden = "";
}

if(setting.readonly == true)
{
var addition = " disabled";
}
else
{
var addition = "";
}

if(setting.type == "bool")
{
settingsHTML += '<tr class="detail-row-complex ' + classHidden + '"><td> <div class="checkboxItem"><label for="' + setting.name + '">' + setting.title + '</label> ' + settingsDescription + ' </td> <td> <input type="checkbox" id="' + setting.name + '"></div></td></tr>';
settingsHTML += '<tr class="detail-row-complex ' + classHidden + '"><td> <div class="checkboxItem"><label for="' + setting.name + '">' + setting.title + '</label>' + description +'</td> <td> <input type="checkbox" id="' + setting.name + '"' + addition + '></div></td></tr>';
}

if(setting.type == "string")
{
if(setting.placeholder == undefined) { setting.placeholder = ""; }

settingsHTML += '<tr class="detail-row-complex ' + classHidden + '"><td>' + setting.title + settingsDescription + '</td> <td> <input type="text" id="' + setting.name + '" placeholder="' + setting.placeholder + '"></td></tr>';
settingsHTML += '<tr class="detail-row-complex ' + classHidden + '"><td>' + setting.title + description +'</td> <td> <input type="text" id="' + setting.name + '" placeholder="' + setting.placeholder + '"' + addition + '></td></tr>';
}

if(setting.type == "integer")
{
if(setting.placeholder == undefined) { setting.placeholder = ""; }

settingsHTML += '<tr class="detail-row-complex ' + classHidden + '"><td>' + setting.title + settingsDescription + '</td> <td> <input type="number" id="' + setting.name + '" placeholder="' + setting.placeholder + '"></td></tr>';
settingsHTML += '<tr class="detail-row-complex ' + classHidden + '"><td>' + setting.title + description +'</td> <td> <input type="number" id="' + setting.name + '" placeholder="' + setting.placeholder + '"' + addition + '></td></tr>';
}

if(setting.type == "menulist")
{
newInnerHTML = '<tr class="detail-row-complex ' + classHidden + '"><td>' + setting.title + settingsDescription + ' </td> <td><select id="' + setting.name + '">';
settingsHTML = '<tr class="detail-row-complex ' + classHidden + '"><td>' + setting.title + description +'</td> <td><select id="' + setting.name + '"' + addition + '>';
Object.keys(setting.options).forEach(function(key)
{
let option = setting.options[key];
newInnerHTML += '<option value="' + option.value + '">' + option.label + '</option>';
settingsHTML += '<option value="' + option.value + '">' + option.label + '</option>';
});

newInnerHTML += '</select></td></tr>';

settingsHTML += newInnerHTML;
settingsHTML += '</select></td></tr>';
}

if(setting.type == "control")
{
settingsHTML += '<tr class="detail-row-complex ' + classHidden + '"><td>' + setting.title + settingsDescription + '</td> <td> <button type="button" id="'+ setting.name +'">'+ setting.label +'</button> </td></tr>';
settingsHTML += '<tr class="detail-row-complex ' + classHidden + '"><td>' + setting.title + description +'</td> <td> <button type="button" id="'+ setting.name +'"' + addition + '>'+ setting.label +'</button> </td></tr>';
}
});

Expand Down
15 changes: 9 additions & 6 deletions webextension/options/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@
}
]
},
"toggleDisplayHotkey": {
"name": "toggleDisplayHotkey",
"type": "string",
"title": "Hotkey for hiding/showing tabbar",
"value": "control-alt-v"
},
"toggleDrawInTitlebar": {
"name": "toggleDrawInTitlebar",
"type": "control",
Expand Down Expand Up @@ -105,6 +99,15 @@
"value": 250,
"hidden": true
},
"toggleDisplayHotkey": {
"name": "toggleDisplayHotkey",
"type": "string",
"title": "Hotkey for hiding/showing tabbar",
"value": "control-alt-v",
"hidden": true,
"readonly": true,
"description": "Is now editable in the respective Firefox user interface"
},
"debug": {
"name": "debug",
"type": "bool",
Expand Down

0 comments on commit 329adea

Please sign in to comment.