Skip to content

Commit

Permalink
✨ New path for custom css
Browse files Browse the repository at this point in the history
  • Loading branch information
adlerluiz committed May 30, 2020
1 parent f268a59 commit e35c670
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 16 deletions.
47 changes: 39 additions & 8 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const {
shell,
} = require('electron')
const path = require('path')
const fs = require('fs')
const scrobblerProvider = require('./providers/scrobblerProvider')
const __ = require('./providers/translateProvider')
const { statusBarMenu } = require('./providers/templateProvider')
Expand All @@ -30,14 +29,13 @@ const discordRPC = require('./providers/discordRpcProvider')
app.commandLine.appendSwitch('disable-features', 'MediaSessionService') //This keeps chromium from trying to launch up it's own mpris service, hence stopping the double service.
const mprisProvider = require('./providers/mprisProvider')
const { checkWindowPosition, doBehavior } = require('./utils/window')
const fileSystem = require('./utils/fileSystem')

const electronLocalshortcut = require('electron-localshortcut')

const themePath = path.join(app.getAppPath(), '/assets/custom-theme.css')

if (!themePath) {
fs.writeFileSync(themePath, `/** \n * Custom Theme \n */`, { flag: 'w+' })
}
createDocumentsAppDir()
createCustomThemeSubDir()
createCustomThemeFile()

if (settingsProvider.get('settings-companion-server')) {
companionServer.start()
Expand Down Expand Up @@ -939,13 +937,18 @@ function createWindow() {
}

function loadCustomTheme() {
const customThemeFile = path.join(
fileSystem.getAppDocumentsPath(app),
'/custom-theme/styles.css'
)

if (settingsProvider.get('settings-custom-theme')) {
if (fs.existsSync(themePath)) {
if (fileSystem.checkIfExists(customThemeFile)) {
if (customThemeCSSKey) {
removeCustomTheme()
}
view.webContents
.insertCSS(fs.readFileSync(themePath).toString())
.insertCSS(fileSystem.readFile(customThemeFile).toString())
.then(key => {
customThemeCSSKey = key
})
Expand Down Expand Up @@ -1197,6 +1200,34 @@ function bytesToSize(bytes) {
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i]
}

function createDocumentsAppDir() {
if (!fileSystem.checkIfExists(fileSystem.getAppDocumentsPath(app))) {
fileSystem.createDir(fileSystem.getAppDocumentsPath(app))
}
}

function createCustomThemeSubDir() {
const dirCustomTheme = path.join(
fileSystem.getAppDocumentsPath(app),
'/custom-theme'
)

if (!fileSystem.checkIfExists(dirCustomTheme)) {
fileSystem.createDir(dirCustomTheme, { recursive: true })
}
}

function createCustomThemeFile() {
const customThemeFile = path.join(
fileSystem.getAppDocumentsPath(app),
'/custom-theme/styles.css'
)

if (!fileSystem.checkIfExists(customThemeFile)) {
fileSystem.writeFile(customThemeFile, `/** \n * Custom Theme \n */`)
}
}

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
const mediaControl = require('./providers/mediaProvider')
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"ws": "^7.2.1"
},
"optionalDependencies": {
"@nodert-win10/windows.media.playback": "^0.2.95",
"@nodert-win10/windows.media.playback": "^0.2.96",
"mpris-service": "^2.1.0"
}
}
20 changes: 14 additions & 6 deletions pages/editor/editor.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
const { ipcRenderer } = require('electron')
const app = require('electron').remote.app
const path = require('electron').remote.require('path')
const fs = require('electron').remote.require('fs')

const __ = require('../../providers/translateProvider')
const themePath = path.join(__dirname, '../../assets/custom-theme.css')
var editor
const fileSystem = require('../../utils/fileSystem')

const themePath = path.join(
fileSystem.getAppDocumentsPath(app),
'/custom-theme'
)
const file = path.join(themePath, 'styles.css')

const textEditor = document.getElementById('editor')
const btnSave = document.getElementById('btn-save')

var editor

__.loadi18n()

if (fs.existsSync(themePath)) {
textEditor.innerHTML = fs.readFileSync(themePath).toString()
if (fileSystem.checkIfExists(themePath)) {
textEditor.innerHTML = fileSystem.readFile(file).toString()
}

if (btnSave) {
btnSave.addEventListener('click', function() {
var code = editor.getValue()
fs.writeFileSync(themePath, code, { flag: 'w+' })
fileSystem.writeFile(file, code)
ipcRenderer.send('update-custom-theme')
})
}
Expand Down
2 changes: 1 addition & 1 deletion pages/settings/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@
id="btn-editor-custom-theme"
>
<i class="material-icons"
>settings</i
>edit</i
>
</button>
<label>
Expand Down
35 changes: 35 additions & 0 deletions utils/fileSystem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const fs = require('fs')
const path = require('path')

function _createDir(path) {
fs.mkdirSync(path, { recursive: true })
}

function _getDir(path) {
return fs.readdirSync(path)
}

function _writeFile(path, data) {
fs.writeFileSync(path, data, { flag: 'w+' })
}

function _readFile(path) {
return fs.readFileSync(path)
}

function _checkIfExists(path) {
return fs.existsSync(path)
}

function _getAppDocumentsPath(app) {
return path.join(app.getPath('documents'), app.name)
}

module.exports = {
createDir: _createDir,
getDir: _getDir,
writeFile: _writeFile,
readFile: _readFile,
checkIfExists: _checkIfExists,
getAppDocumentsPath: _getAppDocumentsPath,
}

0 comments on commit e35c670

Please sign in to comment.