Skip to content

Commit

Permalink
Don't prune pages we open (#131)
Browse files Browse the repository at this point in the history
Settings and other pages opened by the TabFern popup don't get pruned,
so there may be extra tabs in those windows.  Ctrl+N windows will get
pruned, and won't get immediately merged back into the last-closed saved
window.  Definitely getting closer.
  • Loading branch information
Chris White committed Aug 9, 2018
1 parent fba6aec commit f6a3550
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
2 changes: 0 additions & 2 deletions tabfern/src/settings/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,13 @@
"label": i18n.get('Prompt for confirmation before deleting <b>tabs</b> in <b>unsaved</b> windows'),
},

/*
{
"tab": i18n.get("Behaviour"),
"group": i18n.get("When Chrome..."),
"name": CFG_PRUNE_NEW_WINDOWS,
"type": "checkbox",
"label": i18n.get("Adds extra tabs to a new window I've just opened, get rid of them!"),
},
*/

// Appearance
{
Expand Down
4 changes: 4 additions & 0 deletions tabfern/src/view/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,13 @@
/// Open a new window with a given URL. Also remove the default
/// tab that appears because we are letting the window open at the
/// default size. Yes, this is a bit like a FOUC, but oh well.
/// @return An ASQ instance
module.openWindowForURL = function(url) {
let win_id; // TODO is there a better way to pass data down
// the sequence?
let tab0_id; ///< The tab automatically created with the window

let seq =
ASQH.NowCC((cc)=>{
chrome.windows.create(cc);
})
Expand All @@ -135,6 +137,8 @@
// To start the sequence paused, use `let seq = ASQ().duplicate()` above
// instead of just ASQ(). Then, to fire it off, `seq.unpause();`.

return seq;

} //openWindowForURL

return Object.freeze(module); // all fields constant
Expand Down
57 changes: 46 additions & 11 deletions tabfern/src/view/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ var currently_focused_winid = null;
/// tabs to carry the node ID for confirmation.
var window_is_being_restored = false;

/// HACK to not prune windows we create!
var do_not_prune_right_now = false;

/// The size of the last-closed window, to be used as the
/// size of newly-opened windows (whence the name).
/// Should always have a valid value.
Expand All @@ -103,6 +106,11 @@ var ShowWhatIsNew = false;
/// Array of URLs of the last-deleted window
var lastDeletedWindow;

/// Node ID of the last-closed saved window --- merging is prohibited with
/// this node. It's the last-closed saved and not the last-closed overall
/// because nodes for unsaved windows disappear with their windows.
var lastSavedClosedWindow_node_id = undefined;

/// Did initialization complete successfully?
var did_init_complete = false;

Expand Down Expand Up @@ -263,7 +271,7 @@ function unflagAllWindows() {
/// the window empty except for a chrome://newtab.
function pruneWindow(cwin, expected_tab_count)
{
if(!cwin || +expected_tab_count < 0) return;
if(!cwin || +expected_tab_count < 0 || do_not_prune_right_now) return;
let count = +expected_tab_count || 1; // 0 => one tab

ASQH.NowCC( (cc)=>{ chrome.windows.get(cwin.id, {populate: true}, cc); } )
Expand Down Expand Up @@ -1115,6 +1123,8 @@ let pruneWindowSetTimer = function(win_val, cwin) {
win_val.prune_data.timer_id = undefined;
}

if(do_not_prune_right_now) return;

log.debug({" Bumping prune timer for":win_val, cwin});
win_val.prune_data.timer_id = window.setTimeout(
()=>{
Expand All @@ -1125,6 +1135,7 @@ let pruneWindowSetTimer = function(win_val, cwin) {
return;
}

if(do_not_prune_right_now) return;
log.info({'Checking for pruning-to-0 of':win_val.prune_data.cwin});
pruneWindow(win_val.prune_data.cwin, 0);
// 0 => only leave a chrome://newtab there
Expand Down Expand Up @@ -1165,7 +1176,7 @@ function createNodeForWindow(cwin, keep)
}

// Remove extra tabs if the user wants
if(getBoolSetting(CFG_PRUNE_NEW_WINDOWS)) {
if(getBoolSetting(CFG_PRUNE_NEW_WINDOWS) && !do_not_prune_right_now) {
pruneWindowSetTimer(val, cwin);
}

Expand Down Expand Up @@ -1836,6 +1847,11 @@ function winOnRemoved(cwin_id)
if(!node_val) return; // e.g., already closed
let node_id = node_val.node_id;
if(!node_id) return;

if(node_val.keep === K.WIN_KEEP) {
lastSavedClosedWindow_node_id = node_id;
}

let node = T.treeobj.get_node(node_id);
if(!node) return;

Expand Down Expand Up @@ -2094,8 +2110,9 @@ var tabOnCreated = (function(){

let cwin = cwin_or_err;
let merge_to_win = winAlreadyExistsInTree(cwin);
MERGE: if(merge_to_win && merge_to_win.val &&
!merge_to_win.val.isOpen // don't hijack other open wins
MERGE: if(merge_to_win && merge_to_win.val
&& !merge_to_win.val.isOpen // don't hijack other open wins
&& merge_to_win.val.node_id
) {
log.info({
[`merge ${cwin.id} Found merge target in tree for`]: cwin,
Expand All @@ -2115,6 +2132,12 @@ var tabOnCreated = (function(){
break MERGE;
}

if(merge_to_win.val.node_id === lastSavedClosedWindow_node_id) {
log.info(`merge ${cwin.id}: bail - I ` +
"won't merge with the most-recently-closed window");
break MERGE;
}

// Detach the existing nodes from their chrome wins/tabs
if(!destroy_subtree_but_not_widgets(merge_from_win_val.node_id)) {
log.debug(`merge ${cwin.id}: bail - could not remove subtree for open window`);
Expand Down Expand Up @@ -2563,10 +2586,25 @@ function timedResizeDetector()
////////////////////////////////////////////////////////////////////////// }}}1
// Hamburger menu // {{{1

/// Open a window, setting do_not_prune_right_now around the call.
let open_window_with_url = function(url) {
do_not_prune_right_now = true;
let seq =
K.openWindowForURL(url || 'about:blank');
seq
.val(()=>{
do_not_prune_right_now = false;
})
.or(()=>{
do_not_prune_right_now = false;
});
return seq;
};

/// Open a new window with the TabFern homepage.
function hamAboutWindow()
{
K.openWindowForURL('https://cxw42.github.io/TabFern/');
open_window_with_url('https://cxw42.github.io/TabFern/');
} //hamAboutWindow()

/// Reload the TabFern window (or, at least, the tree iframe)
Expand All @@ -2579,11 +2617,8 @@ function hamReloadTree()
/// information used by checkWhatIsNew().
function hamSettings()
{
// TODO: figure out the interaction of pruning with openWindowForURL.
// Currently, pruning zeros out the Settings window, too.

// Actually open the window
K.openWindowForURL(chrome.extension.getURL(
open_window_with_url(chrome.extension.getURL(
'/src/settings/index.html' +
(ShowWhatIsNew ? '#open=last' : ''))
);
Expand Down Expand Up @@ -2702,7 +2737,7 @@ function hamSorter(compare_fn)

function hamRunJasmineTests()
{
K.openWindowForURL(chrome.extension.getURL('/test/index.html'));
open_window_with_url(chrome.extension.getURL('/test/index.html'));
} // hamRunJasmineTests

function hamSortOpenToTop()
Expand Down Expand Up @@ -3539,7 +3574,7 @@ function checkWhatIsNew(selector)
{ [K.LASTVER_KEY]: 'installed, but no version viewed yet' },
function() {
ignore_chrome_error();
K.openWindowForURL('https://cxw42.github.io/TabFern/#usage');
open_window_with_url('https://cxw42.github.io/TabFern/#usage');
}
);
}
Expand Down

0 comments on commit f6a3550

Please sign in to comment.