import { Settings } from "@common/settings" ; import { app } from "electron" ; import * as pathlib from "path" ; import { promises as fs } from "fs" ; import { EventEmitter } from "events" ; import { FSAL } from "@main/fsal/fsal" ; import { FsalEvents , ChokidarEvents } from "@common/events" ; declare const __static ; export enum ThemeEvent { THEME_CHANGED = "theme-changed" } todo export type ThemeId = { type : "default" ; id : string ; } | { type : "custom" ; path : string ; } ; export class ThemeService extends EventEmitter { constructor ( private _fsal ) { super ( ) ; this . initThemeFolder ( ) ; } async initThemeFolder ( ) { let themeFolder = this . getThemeFolder ( ) ; fs . mkdir ( themeFolder ) . then ( ( ) => { console . log ( "app :: theme folder created at" , themeFolder ) ; } ) . catch ( ( ) => { console . log ( "app :: theme folder already exists at" , themeFolder ) ; } ) ; this . _fsal . watchGlobal ( themeFolder ) ; this . _fsal . addListener ( FsalEvents . GLOBAL_CHOKIDAR_EVENT , ( event , info ) => { console . log ( "theme-service :: chokidar event" ) ; if ( info . path . startsWith ( themeFolder ) ) { console . log ( "theme-service :: Theme Folder Changed!" ) ; this . refreshCustomThemes ( ) ; } } ) ; } async refreshCustomThemes ( ) { todo this . refreshTheme ( ) ; } async refreshTheme ( ) { this . setTheme ( null ) ; } async setTheme ( theme = null ) { if ( theme == null ) { theme = Settings . get ( "theme" ) ; } if ( theme . type == "default" ) { let themeCssPath = "" ; switch ( theme . id ) { case "default-dark" : themeCssPath = pathlib . resolve ( __static , 'themes/theme-default-dark.css' ) ; break ; case "default-light" : themeCssPath = pathlib . resolve ( __static , 'themes/theme-default-light.css' ) ; break ; case "typewriter-light" : themeCssPath = pathlib . resolve ( __static , 'themes/theme-typewriter-light.css' ) ; break ; case "academic-light" : themeCssPath = pathlib . resolve ( __static , 'themes/theme-academic-light.css' ) ; break ; default : console . error ( `theme '${ theme . id }' not found` ) ; return ; } let cssString = await fs . readFile ( themeCssPath , { encoding : 'utf8' } ) ; this . emit ( ThemeEvent . THEME_CHANGED , cssString ) ; Settings . set ( "theme" , theme ) ; } else if ( theme . type == "custom" ) { let cssString = await fs . readFile ( theme . path , { encoding : 'utf8' } ) ; this . emit ( ThemeEvent . THEME_CHANGED , cssString ) ; Settings . set ( "theme" , theme ) ; } } getThemeFolder ( ) { todo return pathlib . join ( app . getPath ( "userData" ) , "themes" ) ; } async getThemes ( ) { return { "default" : [ { title : "Default Light" , id : "default-light" } , { title : "Default Dark" , id : "default-dark" } , { title : "Typewriter Light" , id : "typewriter-light" } , { title : "Academic Light" , id : "academic-light" } , ] , "custom" : await this . getCustomThemes ( ) } ; } async getCustomThemes ( ) { let themeFolder = this . getThemeFolder ( ) ; let filePaths = [ ] ; try { filePaths = await fs . readdir ( themeFolder ) ; } catch ( err ) { console . error ( "themes folder does not exist\n" , err ) ; } return filePaths . filter ( fileName => ( pathlib . extname ( fileName ) == ".css" ) ) . map ( fileName => { let path = pathlib . join ( themeFolder , fileName ) ; return ( { title : fileName , path : path } ) ; } ) ; } }