'use strict';

const electronWindow = require('electron-window');
const fs             = require('fs');

// @TODO Make this an env var
const ICON_DIR = 'app.iconset';

module.exports = {
	captureIcons() {
		// @TODO report err
		fs.mkdir(ICON_DIR, () => {
			for (const key in electronWindow.windows) {
				if (electronWindow.windows.hasOwnProperty(key)) {
					const _window = electronWindow.windows[key];
					
					this.captureIcon(_window);
				}
			}
		});
	},
	
	captureIcon(_window) {
		const bounds = _window.getBounds();
		
		_window.capturePage((image) => {
			const png = image.toPng();
			
			if (bounds.width < 512) {
				// @TODO report err
				fs.writeFile(`${ICON_DIR}/icon_${bounds.width * 2}x${bounds.height * 2}.png`, png);
			}
			
			if (bounds.width >= 16) {
				// @TODO report err
				fs.writeFile(`${ICON_DIR}/icon_${bounds.width}x${bounds.height}@2x.png`, png);
			}
		});
	},
	
	logWindowBounds() {
		for (const key in electronWindow.windows) {
			if (electronWindow.windows.hasOwnProperty(key)) {
				console.log(electronWindow.windows[key].getBounds());
			}
		}
	},
	
	reloadWindows() {
		for (const key in electronWindow.windows) {
			if (electronWindow.windows.hasOwnProperty(key)) {
				electronWindow.windows[key].reload();
			}
		}
	}
};
@@ -29,16 +29,17 @@
 nWindow 
+ 
 = requir
@@ -81,16 +81,17 @@
         
+ 
 = requir
@@ -98,16 +98,106 @@
 e('fs');
+%0Aconst icongen         = require('icon-gen');%0Aconst %7B nativeImage %7D = require('electron');
 %0A%0A// @TO
@@ -662,19 +662,20 @@
 %09%09const 
-png
+size
  = image
@@ -679,203 +679,398 @@
 age.
-toPng();%0A%09%09%09%0A%09%09%09if (
+getSize();%0A%09%09%09let buffer;%0A%09%09%09%0A%09%09%09// On high-pixel-density displays, window's 
 bounds
-.width %3C 512) %7B%0A%09%09%09%09// @TODO report err%0A%09%09%09%09fs.writeFile(%60$%7BICON_DIR%7D/icon_$%7Bbounds.width * 2%7Dx$%7Bbounds.height * 2%7D.png%60, p
+ will be smaller%0A%09%09%09// than the image's size.%0A%09%09%09if (bounds.width %3C size.width) %7B%0A%09%09%09%09let scaleFactor = bounds.width / size.width;%0A%09%09%09%09// resize the image down to 1x dimensions%0A%09%09%09%09buffer = nativeImage.createFromBuffer(image.toPng(), scaleFactor).toP
 ng
+(
 );%0A%09%09%09%7D
-%0A%09%09%09%0A%09%09%09if (bounds.width %3E= 16) %7B
+ else %7B%0A%09%09%09%09buffer = image.toPng();%0A%09%09%09%7D
 %0A%09%09%09
+%0A%09%09
 %09// 
@@ -1086,17 +1086,16 @@
 ort err%0A
-%09
 %09%09%09fs.wr
@@ -1119,13 +1119,8 @@
 IR%7D/
-icon_
 $%7Bbo
@@ -1134,46 +1134,241 @@
 dth%7D
-x$%7Bbounds.height%7D@2x.png%60, png);%0A%09%09%09%7D%0A
+.png%60, buffer);%0A%09%09%7D);%0A%09%7D,%0A%09%0A%09createIcons() %7B%0A%09%09icongen(%60$%7BICON_DIR%7D%60, './', %7B type: 'png', report: true, modes: %5B'ico', 'icns'%5D %7D)%0A%09%09%09.then((results) =%3E %7B%0A%09%09%09%09console.log(results);%0A%09%09%09%7D)%0A%09%09%09.catch((err) =%3E %7B%0A%09%09%09%09console.error(err);%0A%09
 %09%09%7D)

Add createIcons(). Resize high-pixel-density images.