Skip to content

Commit

Permalink
Fix window positions not being restored
Browse files Browse the repository at this point in the history
Chrome has started limiting the length of window IDs so now we hash the ID to make sure it is short enough
  • Loading branch information
Jermolene committed Aug 26, 2022
1 parent b2f2e0f commit 64712c1
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
7 changes: 4 additions & 3 deletions source/js/backstage-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Class for backstage windows
/*jslint browser: true */
"use strict";

var windowBase = require("../js/window-base.js");
var windowBase = require("../js/window-base.js"),
hash = require("../js/utils/hash.js");

// Constructor
function BackstageWindow(options) {
Expand All @@ -20,7 +21,7 @@ function BackstageWindow(options) {
this.mustQuitOnClose = options.mustQuitOnClose;
// Open the window
$tw.desktop.gui.Window.open("html/backstage-tiddler-window.html",{
id: this.getIdentifier(),
id: hash.simpleHash(this.getIdentifier()),
show: true,
icon: "images/app_icon.png"
},function(win) {
Expand All @@ -44,7 +45,7 @@ BackstageWindow.prototype.matchInfo = function(info) {

// The identifier for wiki file windows is the prefix `backstage://` plus the title of the tiddler
BackstageWindow.prototype.getIdentifier = function() {
return "backstage://" + this.tiddler;
return BackstageWindow.getIdentifierFromInfo({tiddler: this.tiddler});
};

// Load handler for window
Expand Down
1 change: 1 addition & 0 deletions source/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ trayMenu.append(new gui.MenuItem({
label: "Quit",
click: function() {
gui.App.quit();
// gui.App.closeAllWindows();
}
}));
tray.menu = trayMenu;
Expand Down
22 changes: 22 additions & 0 deletions source/js/utils/hash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Hash utility
*/

(function(){

/*jslint browser: true */
"use strict";

// https://gist.github.com/jlevy/c246006675becc446360a798e2b2d781

exports.simpleHash = function(str) {
let hash = 0;
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
hash = (hash << 5) - hash + char;
hash &= hash; // Convert to 32bit integer
}
return new Uint32Array([hash])[0].toString(36);
};

})();
8 changes: 5 additions & 3 deletions source/js/wiki-file-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Class for wiki file windows
"use strict";

var windowBase = require("../js/window-base.js"),
hash = require("../js/utils/hash.js"),
fs = require("fs");

// Constructor
Expand All @@ -20,9 +21,10 @@ function WikiFileWindow(options) {
this.pathname = options.info.pathname;
this.mustQuitOnClose = options.mustQuitOnClose;
// Open the window
console.log("Opening window with id",this.getIdentifier());
$tw.desktop.gui.Window.open("html/wiki-file-window.html",{
id: this.getIdentifier(),
show: false,
id: hash.simpleHash(this.getIdentifier()),
show: true,
icon: "images/app_icon.png"
},function(win) {
self.window_nwjs = win;
Expand Down Expand Up @@ -55,7 +57,7 @@ WikiFileWindow.prototype.matchInfo = function(info) {

// The identifier for wiki file windows is the prefix `wikifile://` plus the pathname of the file
WikiFileWindow.prototype.getIdentifier = function() {
return "wikifile://" + this.pathname;
return WikiFileWindow.getIdentifierFromInfo({pathname: this.pathname});
};

// Load handler for window
Expand Down
5 changes: 3 additions & 2 deletions source/js/wiki-folder-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Class for wiki folder windows
/*jslint browser: true */
"use strict";

var windowBase = require("../js/window-base.js");
var windowBase = require("../js/window-base.js"),
hash = require("../js/utils/hash.js");

// Constructor
function WikiFolderWindow(options) {
Expand All @@ -29,7 +30,7 @@ function WikiFolderWindow(options) {
// Open the window
$tw.desktop.gui.Window.open("html/wiki-folder-window.html?pathname=" + encodeURIComponent(this.pathname) + "&host=" + encodeURIComponent(host) + "&port=" + encodeURIComponent(port)
+ "&credentials=" + encodeURIComponent(credentials) + "&readers=" + encodeURIComponent(readers) + "&writers=" + encodeURIComponent(writers),{
id: this.getIdentifier(),
id: hash.simpleHash(this.getIdentifier()),
show: true,
new_instance: true,
icon: "images/app_icon.png"
Expand Down

0 comments on commit 64712c1

Please sign in to comment.