Skip to content

Commit

Permalink
Add new autoremember-timer-minutes setting (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
cxw42 committed Nov 24, 2024
1 parent 59293d4 commit 17e9315
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
6 changes: 6 additions & 0 deletions app/common/setting-definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ _VAL[_NAM.CFGS_FAVICON_SOURCE] = (v)=>{
return (( v === FAVICON_SITE || v === FAVICON_CHROME || v === FAVICON_DDG ) ? v : undefined);
};

// #316. How often to autoremember. Empty or <= 0 == don't autosave
_NAM.CFGS_AUTOREMEMBER_MINUTES = 'autoremember-timer-minutes';
_DEF[_NAM.CFGS_AUTOREMEMBER_MINUTES] = '';
_VAL[_NAM.CFGS_AUTOREMEMBER_MINUTES] = _vint;


// }}}2

/// The default values for the configuration settings.
Expand Down
12 changes: 12 additions & 0 deletions app/settings/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,15 @@ setting_definitions.push(
"type": "checkbox",
"label": future_i18n('Prompt for confirmation before closing or deleting a tab that is currently playing audio (<i class="fa fa-music"></i>)'),
},
{
"tab": future_i18n("Behaviour"),
"group": future_i18n("Autoremember"),
"name": S.S_AUTOREMEMBER_MINUTES,
"type": "text",
"label": future_i18n('If this is an integer <tt>I</tt> &gt;= 0, automatically '
+ 'remember all open windows/tabs every <tt>I</tt> minutes. Refresh the '
+ 'TabFern window to apply changes to this option.'),
},

// Appearance
{
Expand Down Expand Up @@ -490,6 +499,9 @@ setting_definitions.push(
"type": "description",
"text": (
`<ul>
<li>TabFern can now automatically mark windows as remembered on a timer.
Set the timer you want in ${settings} Behaviour ${gt} Autoremember
${issue(316)}.</li>
<li>The prompt for confirmation when closing audible tabs now applies to
individual tabs ${issue(306)}.</li>
<li>Bugfixes ${issue(322)}</li>
Expand Down
57 changes: 55 additions & 2 deletions app/win/main_tl.js
Original file line number Diff line number Diff line change
Expand Up @@ -4301,6 +4301,50 @@ function addEventListeners(done)
done();
} //addEventListeners

// Mark all windows as remembered
function rememberAll()
{
var modified = false;
L.log.info('Marking all windows as remembered')
try {
const root = T.treeobj.get_node($.jstree.root);
root.children.forEach( (kid_node_id, kid_idx)=>{
const val = D.windows.by_node_id(kid_node_id);
if(val && (val.keep !== K.WIN_KEEP)) {
modified = true;
M.remember(kid_node_id);
}
});

} catch(e) {
log.warn({'Failure when trying to auto-remember windows': e})
// But don't throw the error since we still want to save the tree.
}

if(modified) {
saveTree();
}
} //rememberAll

// Timer handler to call rememberAll() and set the next timer.
function autoRememberAll(minutes, only_start_timer = false)
{
if(!only_start_timer) {
rememberAll();
}

L.log.info(`Starting autoremember timer for ${minutes} minute(s)`);
window.setTimeout(()=>{autoRememberAll(minutes)}, minutes*60*1000);
} // autoRememberAll

function startAutoRememberTimerIfRequested(done) {
const minutes = S.getInt(S.S_AUTOREMEMBER_MINUTES, 0);
if(minutes > 0) {
autoRememberAll(minutes, true); // true => just start the timer
}
done();
} // startAutoRememberTimerIfRequested()

/// The last function to be called after all other initialization has
/// completed successfully.
function initTreeFinal(done)
Expand Down Expand Up @@ -4410,6 +4454,8 @@ function main()
};
//let spin_timer = window.setTimeout(spin_starter, 1000);

// --- Init steps ---

s.then(determine_devel_mode)
.then(basicInit)

Expand Down Expand Up @@ -4439,9 +4485,11 @@ function main()
})
.then(addOpenWindowsToTree)
.then(addEventListeners)
.then(initTreeFinal)
.then(initTreeFinal) // This needs to be the last real init step

.val(check_init_step_count) // This is a sanity check after initTreeFinal

.val(check_init_step_count)
// --- Post-init steps ---

// Stop the spinner, if it started
.val(()=>{
Expand All @@ -4450,6 +4498,11 @@ function main()
//clearTimeout(spin_timer);
})

// Start the autoremember timer now that all the state is consistent
.then(startAutoRememberTimerIfRequested)

// --- Error handling ---

.or((err)=>{
$(K.INIT_MSG_SEL).text(
$(K.INIT_MSG_SEL).text() + "\n" + err
Expand Down

0 comments on commit 17e9315

Please sign in to comment.