<commit_before>import electron = require('electron');
import storage = require('electron-json-storage');
import BrowserWindow = Electron.BrowserWindow;
const platform = require('os').platform();
const path = require('path');


interface Settings {
    alwaysOnTop: boolean;
}

export class Lyricfier {
    protected rootDir = '';
    protected window: Electron.BrowserWindow;
    protected app: Electron.App;
    protected appIcon: Electron.Tray;
    protected settings: Settings = {
        alwaysOnTop: false
    };

    constructor(app, root) {
        this.app = app;
        this.rootDir = root;
        storage.get('settings', (err, savedSettings: Settings) => {
            if (err) savedSettings = <Settings>{};
            if (('alwaysOnTop' in savedSettings) === false) {
                savedSettings['alwaysOnTop'] = this.settings.alwaysOnTop;
            }
            this.settings = savedSettings;
            this.alwaysOnTopSetup();
        });
        this.setupEvents();
    }

    getTrayIcon() {
        let trayImage = this.getImg('icon.png');
        // Determine appropriate icon for platform
        if (platform == 'darwin') {
            trayImage = this.getImg('tray-icon-mac.png');
        }
        else if (platform == 'win32') {
            trayImage = this.getImg('tray-icon-win.ico');
        }
        return trayImage;
    }

    createWindow() {
        let options = {
            width: 500,
            height: 600,
            icon: this.getTrayIcon(),
            frame: false,
            show: false
        };
        this.window = new electron.BrowserWindow(options);
        this.window.on('close', (e) => {
            e.preventDefault();
            this.window.hide();
        });
        this.window.loadURL(this.getView('index'));
        this.window.once('ready-to-show', () => {
            this.window.show();
        })

        return this.window;
    }

    getWindow(): Electron.BrowserWindow {
        return this.window;
    }

    createAppIcon() {
        const iconPath = this.getTrayIcon();
        this.appIcon = new electron.Tray(iconPath);
        this.appIcon.setContextMenu(this.createTrayMenu());
    }

    setupEvents() {
        this.app.on('ready', () => {
            this.createAppIcon();
            this.createWindow();
        });
        electron.ipcMain.on('get-settings', (event) => {
            event.sender.send('settings-update', this.settings);
        });

        electron.ipcMain.on('save-settings', (event, newSettings) => {
            let oldSettings = {};
            for (let attr in newSettings) {
                oldSettings[attr] = this.settings[attr];
            }
            if (JSON.stringify(newSettings) === JSON.stringify(oldSettings)) {
                console.log('no modifications')
            } else {
                console.log('modified settings!')
                for (let attr in newSettings) {
                    this.settings[attr] = newSettings[attr];
                }
                storage.set('settings', this.settings, (err) => {
                    if (err) console.log('Err saving settings', err);
                });
            }
            event.sender.send('settings-update', this.settings);
        });
    }

    getImg(name) {
        return path.join(this.rootDir, 'render', 'img', name );
    }

    getView(name) {
        return `file://${this.rootDir}/render/views/${name}.html`;
    }

    alwaysOnTopSetup() {
        this.getWindow().setAlwaysOnTop(this.settings.alwaysOnTop)
        this.appIcon.setContextMenu(this.createTrayMenu());
    }

    alwaysOnTopToggle() {
        this.settings.alwaysOnTop = !this.settings.alwaysOnTop;
        storage.set('settings', this.settings, (err) => {
            if (err) console.log('Err saving settings', err);
        });
        this.alwaysOnTopSetup();
    }

    createTrayMenu() {
        const alwaysOnTopChecked = this.settings.alwaysOnTop ? '✓' : '';
        const menu = [
            ['Lyrics', 'showLyrics'],
            ['Always on top ' + alwaysOnTopChecked, 'alwaysOnTopToggle'],
            ['Open Developer Tools', 'openDeveloperTools'],
            ['Quit', 'quit']
        ];
<commit_msg>Always on top changes needs call focus to apply changes.<commit_after>import electron = require('electron');
import storage = require('electron-json-storage');
import BrowserWindow = Electron.BrowserWindow;
const platform = require('os').platform();
const path = require('path');


interface Settings {
    alwaysOnTop: boolean;
}

export class Lyricfier {
    protected rootDir = '';
    protected window: Electron.BrowserWindow;
    protected app: Electron.App;
    protected appIcon: Electron.Tray;
    protected settings: Settings = {
        alwaysOnTop: false
    };

    constructor(app, root) {
        this.app = app;
        this.rootDir = root;
        storage.get('settings', (err, savedSettings: Settings) => {
            if (err) savedSettings = <Settings>{};
            if (('alwaysOnTop' in savedSettings) === false) {
                savedSettings['alwaysOnTop'] = this.settings.alwaysOnTop;
            }
            this.settings = savedSettings;
            this.alwaysOnTopSetup();
        });
        this.setupEvents();
    }

    getTrayIcon() {
        let trayImage = this.getImg('icon.png');
        // Determine appropriate icon for platform
        if (platform == 'darwin') {
            trayImage = this.getImg('tray-icon-mac.png');
        }
        else if (platform == 'win32') {
            trayImage = this.getImg('tray-icon-win.ico');
        }
        return trayImage;
    }

    createWindow() {
        let options = {
            width: 500,
            height: 600,
            icon: this.getTrayIcon(),
            frame: false,
            show: false
        };
        this.window = new electron.BrowserWindow(options);
        this.window.on('close', (e) => {
            e.preventDefault();
            this.window.hide();
        });
        this.window.loadURL(this.getView('index'));
        this.window.once('ready-to-show', () => {
            this.window.show();
        })

        return this.window;
    }

    getWindow(): Electron.BrowserWindow {
        return this.window;
    }

    createAppIcon() {
        const iconPath = this.getTrayIcon();
        this.appIcon = new electron.Tray(iconPath);
        this.appIcon.setContextMenu(this.createTrayMenu());
    }

    setupEvents() {
        this.app.on('ready', () => {
            this.createAppIcon();
            this.createWindow();
        });
        electron.ipcMain.on('get-settings', (event) => {
            event.sender.send('settings-update', this.settings);
        });

        electron.ipcMain.on('save-settings', (event, newSettings) => {
            let oldSettings = {};
            for (let attr in newSettings) {
                oldSettings[attr] = this.settings[attr];
            }
            if (JSON.stringify(newSettings) === JSON.stringify(oldSettings)) {
                console.log('no modifications')
            } else {
                console.log('modified settings!')
                for (let attr in newSettings) {
                    this.settings[attr] = newSettings[attr];
                }
                storage.set('settings', this.settings, (err) => {
                    if (err) console.log('Err saving settings', err);
                });
            }
            event.sender.send('settings-update', this.settings);
        });
    }

    getImg(name) {
        return path.join(this.rootDir, 'render', 'img', name );
    }

    getView(name) {
        return `file://${this.rootDir}/render/views/${name}.html`;
    }

    alwaysOnTopSetup() {
        this.getWindow().setAlwaysOnTop(this.settings.alwaysOnTop);
        this.getWindow().focus();
        this.appIcon.setContextMenu(this.createTrayMenu());
    }

    alwaysOnTopToggle() {
        this.settings.alwaysOnTop = !this.settings.alwaysOnTop;
        storage.set('settings', this.settings, (err) => {
            if (err) console.log('Err saving settings', err);
        });
        this.alwaysOnTopSetup();
    }

    createTrayMenu() {
        const alwaysOnTopChecked = this.settings.alwaysOnTop ? '✓' : '';
        const menu = [
            ['Lyrics', 'showLyrics'],
            ['Always on top ' + alwaysOnTopChecked, 'alwaysOnTopToggle'],
            ['Open Developer Tools', 'openDeveloperTools'],
            ['Quit', 'quit']
        ];

{'fraction_non_alphanumeric': 0.13929584120982988, 'fraction_numerical': 0.001890359168241966, 'mean_word_length': 1.9433240611961058, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '446490', 'n_tokens_mistral': 2382, 'n_tokens_neox': 2298, 'n_words': 436}