Skip to content

Commit

Permalink
Save wallpaper in localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
1j01 committed Dec 9, 2021
1 parent ff510f1 commit 10ec55f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
25 changes: 24 additions & 1 deletion src/$desktop.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,37 @@ var folder_view = new FolderView(desktop_folder_path, {
});
$(folder_view.element).appendTo($desktop);

function setDesktopWallpaper(file, repeat) {
function setDesktopWallpaper(file, repeat, saveToLocalStorage) {
const blob_url = URL.createObjectURL(file);
$desktop.css({
backgroundImage: `url(${blob_url})`,
backgroundRepeat: repeat,
backgroundPosition: "center",
backgroundSize: "auto",
});
if (saveToLocalStorage) {
var fr = new FileReader();
window.fr = fr;
fr.onload = () => {
localStorage.setItem("wallpaper-data-url", fr.result);
localStorage.setItem("wallpaper-repeat", repeat);
};
fr.onerror = () => {
console.error("Error reading file (for setting wallpaper)", file);
};
fr.readAsDataURL(file);
}
}
try {
var wallpaper_data_url = localStorage.getItem("wallpaper-data-url");
var wallpaper_repeat = localStorage.getItem("wallpaper-repeat");
if (wallpaper_data_url) {
fetch(wallpaper_data_url).then(r => r.blob()).then(file => {
setDesktopWallpaper(file, wallpaper_repeat, false);
});
}
} catch (error) {
console.error(error);
}

// Prevent drag and drop from redirecting the page (the browser default behavior for files)
Expand Down
5 changes: 2 additions & 3 deletions src/programs.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,15 +315,14 @@ function Paint(file_path) {
});
});
},
// TODO: maybe save the wallpaper in localStorage
setWallpaperCentered: (canvas) => {
canvas.toBlob((blob) => {
setDesktopWallpaper(blob, "no-repeat");
setDesktopWallpaper(blob, "no-repeat", true);
});
},
setWallpaperTiled: (canvas) => {
canvas.toBlob((blob) => {
setDesktopWallpaper(blob, "repeat");
setDesktopWallpaper(blob, "repeat", true);
});
},
};
Expand Down

0 comments on commit 10ec55f

Please sign in to comment.