mirror of
https://github.com/marktext/marktext.git
synced 2025-05-02 22:22:18 +08:00
User-defined titlebar settings (#721)
This commit is contained in:
parent
62633c498e
commit
359387a390
@ -1,6 +1,7 @@
|
|||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
import { ipcMain, BrowserWindow } from 'electron'
|
import { ipcMain, BrowserWindow } from 'electron'
|
||||||
|
import { isOsx } from './config'
|
||||||
import appWindow from './window'
|
import appWindow from './window'
|
||||||
import { getPath, hasSameKeys, log, ensureDir } from './utils'
|
import { getPath, hasSameKeys, log, ensureDir } from './utils'
|
||||||
|
|
||||||
@ -18,18 +19,23 @@ class Preference {
|
|||||||
}
|
}
|
||||||
|
|
||||||
init () {
|
init () {
|
||||||
// If the preference.md is not existed or can not load json from it.
|
|
||||||
// Rebuild the preference.md
|
|
||||||
const { userDataPath, staticPath } = this
|
const { userDataPath, staticPath } = this
|
||||||
|
const defaultSettings = this.loadJson(staticPath)
|
||||||
let userSetting = null
|
let userSetting = null
|
||||||
|
|
||||||
|
// Try to load settings or write default settings if file doesn't exists.
|
||||||
if (!fs.existsSync(userDataPath) || !this.loadJson(userDataPath)) {
|
if (!fs.existsSync(userDataPath) || !this.loadJson(userDataPath)) {
|
||||||
ensureDir(getPath('userData'))
|
ensureDir(getPath('userData'))
|
||||||
const content = fs.readFileSync(staticPath, 'utf-8')
|
const content = fs.readFileSync(staticPath, 'utf-8')
|
||||||
fs.writeFileSync(userDataPath, content, 'utf-8')
|
fs.writeFileSync(userDataPath, content, 'utf-8')
|
||||||
} else {
|
|
||||||
const defaultSettings = this.loadJson(staticPath)
|
|
||||||
userSetting = this.loadJson(userDataPath)
|
userSetting = this.loadJson(userDataPath)
|
||||||
this.validateSettings(userSetting)
|
this.validateSettings(userSetting)
|
||||||
|
} else {
|
||||||
|
userSetting = this.loadJson(userDataPath)
|
||||||
|
this.validateSettings(userSetting)
|
||||||
|
|
||||||
|
// Update outdated settings
|
||||||
const requiresUpdate = !hasSameKeys(defaultSettings, userSetting)
|
const requiresUpdate = !hasSameKeys(defaultSettings, userSetting)
|
||||||
if (requiresUpdate) {
|
if (requiresUpdate) {
|
||||||
// remove outdated settings
|
// remove outdated settings
|
||||||
@ -48,7 +54,13 @@ class Preference {
|
|||||||
.catch(log)
|
.catch(log)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!userSetting) userSetting = this.loadJson(userDataPath)
|
|
||||||
|
if (!userSetting) {
|
||||||
|
console.error('ERROR: Cannot load settings.')
|
||||||
|
userSetting = defaultSettings
|
||||||
|
this.validateSettings(userSetting)
|
||||||
|
}
|
||||||
|
|
||||||
this.cache = userSetting
|
this.cache = userSetting
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,18 +120,27 @@ class Preference {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let brokenSettings = false
|
let brokenSettings = false
|
||||||
if (!settings.theme || (settings.theme && !/dark|light/.test(settings.theme))) {
|
if (!settings.theme || (settings.theme && !/^(dark|light)$/.test(settings.theme))) {
|
||||||
brokenSettings = true
|
brokenSettings = true
|
||||||
settings.theme = 'light'
|
settings.theme = 'light'
|
||||||
}
|
}
|
||||||
if (!settings.bulletListMarker ||
|
if (!settings.bulletListMarker ||
|
||||||
(settings.bulletListMarker && !/\+|-|\*/.test(settings.bulletListMarker))) {
|
(settings.bulletListMarker && !/^(\+|-|\*)$/.test(settings.bulletListMarker))) {
|
||||||
brokenSettings = true
|
brokenSettings = true
|
||||||
settings.bulletListMarker = '-'
|
settings.bulletListMarker = '-'
|
||||||
}
|
}
|
||||||
|
if (!settings.titleBarStyle || !/^(native|csd|custom)$/.test(settings.titleBarStyle)) {
|
||||||
|
settings.titleBarStyle = 'csd'
|
||||||
|
}
|
||||||
if (brokenSettings) {
|
if (brokenSettings) {
|
||||||
log('Broken settings detected; fallback to default value(s).')
|
log('Broken settings detected; fallback to default value(s).')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Currently no CSD is available on Linux and Windows (GH#690)
|
||||||
|
const titleBarStyle = settings.titleBarStyle.toLowerCase()
|
||||||
|
if (!isOsx && titleBarStyle === 'csd') {
|
||||||
|
settings.titleBarStyle = 'custom'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,6 +57,16 @@ class AppWindow {
|
|||||||
|
|
||||||
const { x, y, width, height } = this.ensureWindowPosition(mainWindowState)
|
const { x, y, width, height } = this.ensureWindowPosition(mainWindowState)
|
||||||
const winOpt = Object.assign({ x, y, width, height }, defaultWinOptions, options)
|
const winOpt = Object.assign({ x, y, width, height }, defaultWinOptions, options)
|
||||||
|
|
||||||
|
// Enable native or custom window
|
||||||
|
const { titleBarStyle } = userPreference.getAll()
|
||||||
|
if (titleBarStyle === 'custom') {
|
||||||
|
winOpt.titleBarStyle = ''
|
||||||
|
} else if (titleBarStyle === 'native') {
|
||||||
|
winOpt.frame = true
|
||||||
|
winOpt.titleBarStyle = ''
|
||||||
|
}
|
||||||
|
|
||||||
const win = new BrowserWindow(winOpt)
|
const win = new BrowserWindow(winOpt)
|
||||||
windows.set(win.id, {
|
windows.set(win.id, {
|
||||||
win,
|
win,
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<template>
|
<template>
|
||||||
<div
|
<div
|
||||||
class="title-bar"
|
class="title-bar"
|
||||||
:class="[{ 'active': active }, theme, { 'frameless': platform !== 'darwin' }]"
|
:class="[{ 'active': active }, theme, { 'frameless': titleBarStyle === 'custom' }, { 'isOsx': platform === 'darwin' }]"
|
||||||
>
|
>
|
||||||
<div class="title">
|
<div class="title">
|
||||||
<span v-if="!filename">Mark Text</span>
|
<span v-if="!filename">Mark Text</span>
|
||||||
@ -15,27 +15,33 @@
|
|||||||
<use xlink:href="#icon-arrow-right"></use>
|
<use xlink:href="#icon-arrow-right"></use>
|
||||||
</svg>
|
</svg>
|
||||||
</span>
|
</span>
|
||||||
<span @click="rename" class="filename">{{ filename }}</span>
|
<span
|
||||||
|
class="filename"
|
||||||
|
:class="{'isOsx': platform === 'darwin'}"
|
||||||
|
@click="rename"
|
||||||
|
>
|
||||||
|
{{ filename }}
|
||||||
|
</span>
|
||||||
<span class="save-dot" :class="{'show': !isSaved}"></span>
|
<span class="save-dot" :class="{'show': !isSaved}"></span>
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<div :class="platform !== 'darwin' ? 'left-toolbar title-no-drag' : 'right-toolbar'">
|
<div :class="titleBarStyle === 'custom' ? 'left-toolbar title-no-drag' : 'right-toolbar'">
|
||||||
<div
|
<div
|
||||||
v-if="platform !== 'darwin'"
|
v-if="titleBarStyle === 'custom'"
|
||||||
class="frameless-titlebar-menu title-no-drag"
|
class="frameless-titlebar-menu title-no-drag"
|
||||||
@click.stop="handleMenuClick"
|
@click.stop="handleMenuClick"
|
||||||
>☰</div>
|
>☰</div>
|
||||||
<div
|
<div
|
||||||
v-if="wordCount"
|
v-if="wordCount"
|
||||||
class="word-count"
|
class="word-count"
|
||||||
:class="[{ 'title-no-drag': platform !== 'darwin' }]"
|
:class="[{ 'title-no-drag': titleBarStyle === 'custom' }]"
|
||||||
@click.stop="handleWordClick"
|
@click.stop="handleWordClick"
|
||||||
>{{ `${HASH[show]} ${wordCount[show]}` }}</div>
|
>{{ `${HASH[show]} ${wordCount[show]}` }}</div>
|
||||||
</div>
|
</div>
|
||||||
<div
|
<div
|
||||||
v-if="platform !== 'darwin'"
|
v-if="titleBarStyle === 'custom'"
|
||||||
class="right-toolbar"
|
class="right-toolbar"
|
||||||
:class="[{ 'title-no-drag': platform !== 'darwin' }]"
|
:class="[{ 'title-no-drag': titleBarStyle === 'custom' }]"
|
||||||
>
|
>
|
||||||
<div class="frameless-titlebar-button frameless-titlebar-close" @click.stop="handleCloseClick">
|
<div class="frameless-titlebar-button frameless-titlebar-close" @click.stop="handleCloseClick">
|
||||||
<div>
|
<div>
|
||||||
@ -65,6 +71,7 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { remote } from 'electron'
|
import { remote } from 'electron'
|
||||||
|
import { mapState } from 'vuex'
|
||||||
import { minimizePath, restorePath, maximizePath, closePath } from '../assets/window-controls.js'
|
import { minimizePath, restorePath, maximizePath, closePath } from '../assets/window-controls.js'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@ -101,6 +108,9 @@
|
|||||||
isSaved: Boolean
|
isSaved: Boolean
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
|
...mapState({
|
||||||
|
'titleBarStyle': state => state.preferences.titleBarStyle,
|
||||||
|
}),
|
||||||
paths () {
|
paths () {
|
||||||
if (!this.pathname) return []
|
if (!this.pathname) return []
|
||||||
const pathnameToken = this.pathname.split('/').filter(i => i)
|
const pathnameToken = this.pathname.split('/').filter(i => i)
|
||||||
@ -215,7 +225,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.title-bar:not(.frameless) .title .filename:hover {
|
.title-bar .title .filename.isOsx:hover {
|
||||||
color: var(--primary);
|
color: var(--primary);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@ const state = {
|
|||||||
autoPairQuote: true,
|
autoPairQuote: true,
|
||||||
tabSize: 4,
|
tabSize: 4,
|
||||||
hideQuickInsertHint: false,
|
hideQuickInsertHint: false,
|
||||||
|
titleBarStyle: 'csd',
|
||||||
// edit modes (they are not in preference.md, but still put them here)
|
// edit modes (they are not in preference.md, but still put them here)
|
||||||
typewriter: false, // typewriter mode
|
typewriter: false, // typewriter mode
|
||||||
focus: false, // focus mode
|
focus: false, // focus mode
|
||||||
|
@ -12,6 +12,8 @@ Edit and save to update preferences. You can only change the JSON below!
|
|||||||
|
|
||||||
- **textDirection**: *String* `ltr` or `rtl`
|
- **textDirection**: *String* `ltr` or `rtl`
|
||||||
|
|
||||||
|
- **titleBarStyle**: *String* `csd` (macOS only), `custom` or `native`
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"fontSize": "16px",
|
"fontSize": "16px",
|
||||||
@ -32,7 +34,8 @@ Edit and save to update preferences. You can only change the JSON below!
|
|||||||
"autoPairQuote": true,
|
"autoPairQuote": true,
|
||||||
"endOfLine": "default",
|
"endOfLine": "default",
|
||||||
"tabSize": 4,
|
"tabSize": 4,
|
||||||
"textDirection": "ltr"
|
"textDirection": "ltr",
|
||||||
|
"titleBarStyle": "csd"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user