From 6064f44a628eaa2f5dbce7f9596c0bec042ddc29 Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Wed, 8 Dec 2021 20:08:33 -0700 Subject: [PATCH] LightRefresh, fs dir watch/refresh --- main.js | 2 +- scripts/fileview/checkdir.js | 14 ++++++++++++++ scripts/fileview/lightrefresh.js | 5 +++++ scripts/fileview/load/load.js | 5 +++-- scripts/fileview/load/loaddir.js | 4 ++-- scripts/fileview/load/render.js | 19 ++++++++++--------- scripts/fileview/load/sort.js | 13 +++++++------ scripts/fileview/refresh.js | 17 ++++++++++------- scripts/startup/preload.js | 20 +++++++++++++------- 9 files changed, 65 insertions(+), 34 deletions(-) create mode 100644 scripts/fileview/checkdir.js create mode 100644 scripts/fileview/lightrefresh.js diff --git a/main.js b/main.js index 95775c6..bfd0d37 100644 --- a/main.js +++ b/main.js @@ -22,7 +22,7 @@ async function createWindow () { // and load the index.html of the app. mainWindow.maximize(); await mainWindow.loadFile('index.html'); - mainWindow.removeMenu(); + //mainWindow.removeMenu(); // Open the DevTools. // mainWindow.webContents.openDevTools() diff --git a/scripts/fileview/checkdir.js b/scripts/fileview/checkdir.js new file mode 100644 index 0000000..5c9f22c --- /dev/null +++ b/scripts/fileview/checkdir.js @@ -0,0 +1,14 @@ +const load = require('./load/load'); +const sort = require('./load/sort'); +const lightRefresh = require("./lightrefresh"); + +module.exports = () => { + if (!window.kade.cpath.length || !window.kade.cdir.length) {return;} + window.kade.chdir = []; + load(true); + sort(undefined, true); + if (JSON.stringify(window.kade.cdir) !== JSON.stringify(window.kade.chdir)) { + //console.log('Detected directory updates.', JSON.stringify(window.kade.cdir), JSON.stringify(window.kade.chdir)); + lightRefresh(); + } +}; \ No newline at end of file diff --git a/scripts/fileview/lightrefresh.js b/scripts/fileview/lightrefresh.js new file mode 100644 index 0000000..af8ceac --- /dev/null +++ b/scripts/fileview/lightrefresh.js @@ -0,0 +1,5 @@ +const refresh = require('./refresh'); + +module.exports = (newPath, customOptions) => { + return refresh(newPath || window.kade.cpath, customOptions || {animate: false, resetNav: false, scroll: false}); +}; \ No newline at end of file diff --git a/scripts/fileview/load/load.js b/scripts/fileview/load/load.js index 9fba0a3..3912913 100644 --- a/scripts/fileview/load/load.js +++ b/scripts/fileview/load/load.js @@ -8,7 +8,8 @@ const trimext = require("../../../json/trimext.json"); const extensions = require("../../../json/extensions.json"); const getsize = require("../../fileview/filesize"); -module.exports = () => { +module.exports = (check=false) => { + let load = check ? window.kade.chdir : window.kade.cdir; let dir = window.kade.cpath; let ldir = fs.readdirSync(dir); @@ -37,6 +38,6 @@ module.exports = () => { ofile.sizeString = ''; ofile.dir = true; } - window.kade.cdir.push(ofile); + load.push(ofile); } }; \ No newline at end of file diff --git a/scripts/fileview/load/loaddir.js b/scripts/fileview/load/loaddir.js index e950cd5..e89e17a 100644 --- a/scripts/fileview/load/loaddir.js +++ b/scripts/fileview/load/loaddir.js @@ -2,11 +2,11 @@ const load = require('./load'); const sort = require('./sort'); const render = require('./render'); -module.exports = (dir) => { +module.exports = (dir, options) => { window.kade.cpath = dir; window.kade.cdir = []; load(); sort(); - render(); + render(undefined, options); }; \ No newline at end of file diff --git a/scripts/fileview/load/render.js b/scripts/fileview/load/render.js index 2123310..eba3cbb 100644 --- a/scripts/fileview/load/render.js +++ b/scripts/fileview/load/render.js @@ -1,9 +1,8 @@ const loadHierarchy = require("../hierarchy"); const isOverflowing = require('../../dep/overflowing'); -const refresh = require("../refresh"); -module.exports = (dir) => { +module.exports = (dir, options) => { const refresh = require("../refresh"); let cdir = dir || window.kade.cdir; @@ -13,7 +12,7 @@ module.exports = (dir) => { const exp = document.getElementById('files'); let cfc = document.createElement("div"); cfc.className = 'file'; - cfc.classList.add('rise'); + if (options.animate) {cfc.classList.add('rise');} cfc.onclick = function () { window.kade.elc = true; if (cfc.classList.contains('file-active')) {if (file.dir) {refresh(`${window.kade.cpath}\\${file.name}`);}} @@ -27,11 +26,13 @@ module.exports = (dir) => { if (window.kade.cl) {window.kade.cl.classList.remove('file-active');} window.kade.cl = cfc; }; - cfc.style = `animation-delay: ${num * .03}s`; - cfc.onanimationend = () => { - cfc.style = ''; - cfc.classList.remove('rise'); - }; + if (options.animate) { + cfc.style = `animation-delay: ${num * .03}s`; + cfc.onanimationend = () => { + cfc.style = ''; + cfc.classList.remove('rise'); + }; + } exp.appendChild(cfc); let im = document.createElement('img'); im.src = file.icon; @@ -67,5 +68,5 @@ module.exports = (dir) => { num++; } - loadHierarchy(); + if (options.resetNav) {loadHierarchy();} }; \ No newline at end of file diff --git a/scripts/fileview/load/sort.js b/scripts/fileview/load/sort.js index 9fe2284..2dd07f1 100644 --- a/scripts/fileview/load/sort.js +++ b/scripts/fileview/load/sort.js @@ -1,26 +1,27 @@ -module.exports = (mode) => { +module.exports = (mode, check=false) => { mode = mode || window.kade.sort; + let ts = check ? window.kade.chdir : window.kade.cdir; switch (mode) { case 'Name': - window.kade.cdir.sort((a, b) => { + ts.sort((a, b) => { a = a.name.toLowerCase(); b = b.name.toLowerCase(); return a > b ? 1 : a < b ? -1 : 0; }); break; case 'Date': - window.kade.cdir.sort((a, b) => a.lastModified - b.lastModified); + ts.sort((a, b) => a.lastModified - b.lastModified); break; case 'Type': - window.kade.cdir.sort((a, b) => { + ts.sort((a, b) => { a = a.type.toLowerCase(); b = b.type.toLowerCase(); return a > b ? 1 : a < b ? -1 : 0; }); break; case 'Size': - window.kade.cdir.sort((a, b) => a.size - b.size); + ts.sort((a, b) => a.size - b.size); break; } - if (!window.kade.ascend) {window.kade.cdir.reverse();} + if (!window.kade.ascend) {ts.reverse();} }; \ No newline at end of file diff --git a/scripts/fileview/refresh.js b/scripts/fileview/refresh.js index 91beac4..11bd48f 100644 --- a/scripts/fileview/refresh.js +++ b/scripts/fileview/refresh.js @@ -2,7 +2,8 @@ const loadDir = require('./load/loaddir'); const headers = require('../../json/headers.json'); -module.exports = (newPath) => { +module.exports = (newPath, options={animate:true,scroll:true,resetNav:true}) => { + ['animate', 'scroll', 'resetNav'].forEach(k => {if (typeof options[k] === 'undefined') {options[k] = true;}}); document.getElementById('files').remove(); let files = document.createElement('div'); files.className = 'files'; @@ -19,10 +20,12 @@ module.exports = (newPath) => { temp.className = 'nosel'; fh.appendChild(temp); }); - document.getElementById('header-nav').remove(); - let nav = document.createElement('div'); - nav.setAttribute('id', 'header-nav'); - document.getElementById('controls').appendChild(nav); - window.scrollTo(0, 0); - loadDir(newPath); + if (options.resetNav) { + document.getElementById('header-nav').remove(); + let nav = document.createElement('div'); + nav.setAttribute('id', 'header-nav'); + document.getElementById('controls').appendChild(nav); + } + if (options.scroll) {window.scrollTo(0, 0);} + loadDir(newPath, options); }; \ No newline at end of file diff --git a/scripts/startup/preload.js b/scripts/startup/preload.js index 4f7b889..2a0ef5a 100644 --- a/scripts/startup/preload.js +++ b/scripts/startup/preload.js @@ -2,15 +2,18 @@ const os = require('os'); const setButtons = require('./setbuttons'); const hideContext = require('../contextmenu/hidecontext'); +const lightRefresh = require('../fileview/lightrefresh'); +const checkDir = require('../fileview/checkdir'); window.addEventListener('DOMContentLoaded', () => { - window.kade = {}; - - window.kade.elc = false; - window.kade.cpath = ''; - window.kade.sort = 'Name'; - window.kade.ascend = true; - window.kade.context = false; + window.kade = { + elc: false, + cpath: '', + sort: 'Name', + ascend: true, + context: false, + chdir: [] + }; const startDir = `${os.homedir}\\Desktop`; @@ -27,6 +30,9 @@ window.addEventListener('DOMContentLoaded', () => { document.getElementById('ctx').style.display = 'none'; + window.kade.refreshInterval = setInterval(lightRefresh, 180000); + window.kade.checkDirInterval = setInterval(checkDir, 5000); + require('../keybinds/handleKey')(); });