Skip to content
This repository has been archived by the owner on Jul 16, 2019. It is now read-only.

major rewrite, fixes #16 and #17 #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 100 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,56 @@ var chroma = require('chroma-js');

exports.main = function(){
var maxTabs = simplePrefs.prefs.maxTabs;
var max = (maxTabs > 1) ? maxTabs : 10; // Max at least 2 tabs please
var max = (maxTabs > 1) ? maxTabs : 2; // Max at least 2 tabs please
var maxPrivateBrowsing = simplePrefs.prefs.maxPrivateBrowsing;
var maxOpenInNextWindow = simplePrefs.prefs.maxOpenInNextWindow;
var maxExcludePinnedTabs = simplePrefs.prefs.maxExcludePinnedTabs;

var button = ActionButton({
id: 'max-tabs-button',
label: title,
label: title,
icon: './icon.svg',
disabled: true
});

var calcTabsLen = function(win){
let len = win.tabs.length;
if(maxExcludePinnedTabs){
let pinned = 0;
for(let i = 0; i < len; i++){
if(win.tabs[i].isPinned)
pinned++;
}
len -= pinned;
}

return len;
}

var notifyMax = function(){
notifications.notify({
title: title,
text: _('not_open_max_tabs', max)
});
};

var colorScale = chroma.scale(['#A6A6A6', '#B90000']);
var updateButton = function(win, tabsLen){
if (win == 'window') tabsLen = windows.browserWindows.activeWindow.tabs.length;
if (win == 'window') tabsLen = calcTabsLen(windows.browserWindows.activeWindow);
button.state(win, {
label: title + ' - ' + tabsLen + '/' + max,
badge: (tabsLen > 99) ? '99+' : tabsLen,
badgeColor: colorScale(tabsLen/max).hex()
});
};
var updateAllButtons = function(){
for (let window of windows.browserWindows){
updateButton(window, window.tabs.length);
for (let win of windows.browserWindows){
updateButton(win, calcTabsLen(win));
}
};
updateAllButtons();


simplePrefs.on('maxTabs', function(){
var maxTabs = simplePrefs.prefs.maxTabs;
max = (maxTabs > 1) ? maxTabs : 10;
Expand All @@ -45,28 +69,88 @@ exports.main = function(){
simplePrefs.on('maxPrivateBrowsing', function(){
maxPrivateBrowsing = simplePrefs.prefs.maxPrivateBrowsing;
});
simplePrefs.on('maxOpenInNextWindow', function(){
maxOpenInNextWindow = simplePrefs.prefs.maxOpenInNextWindow;
});
simplePrefs.on('maxExcludePinnedTabs', function(){
maxExcludePinnedTabs = simplePrefs.prefs.maxExcludePinnedTabs;
updateAllButtons();
});


// TODO: hook tab (un)pinned events


tabs.on('open', function(tab){
var window = tab.window;
// setTimeout is needed because window.tabs.length value seems to update *slower*
let win = tab.window;
if(privateBrowsing.isPrivate(win) && !maxPrivateBrowsing){
setTimeout(function(){
updateButton(win, calcTabsLen(win));
}, 1);
return;
}

// setTimeout is needed because win.tabs.length value seems to update *slower*
setTimeout(function(){
var tabsLen = window.tabs.length;
if (tabsLen <= max || (privateBrowsing.isPrivate(window) && !maxPrivateBrowsing)){
updateButton(window, tabsLen);
let tabsLen = calcTabsLen(win);
if (tabsLen <= max){
updateButton(win, tabsLen);
} else if (maxOpenInNextWindow){
let tabHasFocus = (tab === win.activeTab);
let numWindows = windows.browserWindows.length;
let i = 0;
let nextWin;

// lookup another available window (private windows are not listed)
while(i < numWindows){
nextWin = windows.browserWindows[i];
tabsLen = calcTabsLen(nextWin);

if (nextWin !== win && tabsLen < max){
// real moving of tabs in FF is low level stuff and not much portable
// see http://stackoverflow.com/a/26744281/2566213

let onReady = function(){
// to keep total number stable, first tab needs to be closed before opening a new one
let url = tab.url;
tab.close();
nextWin.tabs.open(url);
if(tabHasFocus){
nextWin.activate();
tab.activate();
}
};

// FF sometimes doesn't fire ready event
// http://stackoverflow.com/a/30013206/2566213
if(tab.readyState == "complete")
onReady();
else
tab.on('ready', onReady);

break;
}

i++;
}

if(i >= numWindows){
tab.close();
notifyMax();
}
} else {
tab.close();
notifications.notify({
title: title,
text: _('not_open_max_tabs', max)
});
notifyMax();
}
}, 1);
});

var updateOnClose = function(tab){
var window = tab.window;
let win = tab.window;
let len = calcTabsLen(win);
if(len <= 0) return; // tab closed the window too
setTimeout(function(){
updateButton(window, window.tabs.length);
updateButton(win, len);
}, 1);
};
tabs.on('close', updateOnClose);
Expand Down
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@
"title": "Works for Private Browsing",
"type": "bool",
"value": true
},
{
"name": "maxOpenInNextWindow",
"title": "Open in next available window",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps make this more descriptive, like Open in next available window if exceeded maximum number of tabs.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Open in next available window" looks fine for me, because the word "available" already implies that the current one reached the maximum number of tabs and it's unavailable.

You can of course be more explicit if you want to, but to use "exceeded maximum" sounds weird, since the whole point of the addon is never "to exceed" a specific number of tabs.

My two cents! ;-)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@robsonsobral I get your point 😄 Well, this feature wouldn't even be needed if the point is to never "exceed" the max number of tabs 😛

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there wasn't another window already open, I could agree, @cheeaun ! ;-)


What is the issue 19, @rindeal ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is what? @robsonsobral ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind! I was being dumb! I'm sorry!

"description": "Open in next available window, if the current one is full",
"type": "bool",
"value": false
},
{
"name": "maxExcludePinnedTabs",
"title": "Exclude pinned tabs",
"type": "bool",
"value": false
}
],
"permissions": {
Expand Down