forked from marktext/marktext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.js
More file actions
130 lines (112 loc) · 3.34 KB
/
base.js
File metadata and controls
130 lines (112 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import EventEmitter from 'events'
import { isLinux } from '../config'
/**
* A MarkText window.
* @typedef {BaseWindow} IApplicationWindow
* @property {number | null} id Identifier (= browserWindow.id) or null during initialization.
* @property {Electron.BrowserWindow} browserWindow The browse window.
* @property {WindowLifecycle} lifecycle The window lifecycle state.
* @property {WindowType} type The window type.
*/
// Window type marktext support.
export const WindowType = {
BASE: 'base', // You shold never create a `BASE` window.
EDITOR: 'editor',
SETTINGS: 'settings'
}
export const WindowLifecycle = {
NONE: 0,
LOADING: 1,
READY: 2,
QUITTED: 3
}
class BaseWindow extends EventEmitter {
/**
* @param {Accessor} accessor The application accessor for application instances.
*/
constructor (accessor) {
super()
this._accessor = accessor
this.id = null
this.browserWindow = null
this.lifecycle = WindowLifecycle.NONE
this.type = WindowType.BASE
}
bringToFront () {
const { browserWindow: win } = this
if (win.isMinimized()) win.restore()
if (!win.isVisible()) win.show()
if (isLinux) {
win.focus()
} else {
win.moveTop()
}
}
reload () {
this.browserWindow.reload()
}
destroy () {
this.lifecycle = WindowLifecycle.QUITTED
this.emit('window-closed')
this.removeAllListeners()
if (this.browserWindow) {
this.browserWindow.destroy()
this.browserWindow = null
}
this.id = null
}
// --- private ---------------------------------
_buildUrlWithSettings (windowId, env, userPreference) {
// NOTE: Only send absolutely necessary values. Full settings are delay loaded.
const { type } = this
const { debug, paths } = env
const {
codeFontFamily,
codeFontSize,
hideScrollbar,
theme,
titleBarStyle
} = userPreference.getAll()
/* eslint-disable */
const baseUrl = process.env.NODE_ENV === 'development'
? 'http://localhost:9091'
: `file://${__dirname}/index.html`
/* eslint-enable */
const url = new URL(baseUrl)
url.searchParams.set('udp', paths.userDataPath)
url.searchParams.set('debug', debug ? '1' : '0')
url.searchParams.set('wid', windowId)
url.searchParams.set('type', type)
// Settings
url.searchParams.set('cff', codeFontFamily)
url.searchParams.set('cfs', codeFontSize)
url.searchParams.set('hsb', hideScrollbar ? '1' : '0')
url.searchParams.set('theme', theme)
url.searchParams.set('tbs', titleBarStyle)
return url
}
_buildUrlString (windowId, env, userPreference) {
return this._buildUrlWithSettings(windowId, env, userPreference).toString()
}
_getPreferredBackgroundColor (theme) {
// Hardcode the theme background color and show the window direct for the fastet window ready time.
// Later with custom themes we need the background color (e.g. from meta information) and wait
// that the window is loaded and then pass theme data to the renderer.
switch (theme) {
case 'dark':
return '#282828'
case 'material-dark':
return '#34393f'
case 'ulysses':
return '#f3f3f3'
case 'graphite':
return '#f7f7f7'
case 'one-dark':
return '#282c34'
case 'light':
default:
return '#ffffff'
}
}
}
export default BaseWindow