-
-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy paththeme.ts
194 lines (171 loc) · 5.67 KB
/
theme.ts
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { Capacitor } from '@capacitor/core'
import { Toast } from '@capacitor/toast'
import { Filesystem, Directory, ReadFileResult } from '@capacitor/filesystem'
import { StatusBar, Style as StatusBarStyle } from '@capacitor/status-bar'
import settings from './settings'
const baseUrl = 'https://veloce.github.io/lichobile-themes'
let styleEl: HTMLStyleElement
type Theme = 'bg' | 'board'
interface ThemeEntry {
key: string
name: string
ext: string
}
export function isTransparent(key: string) {
return key !== 'dark' && key !== 'light' && key !== 'system'
}
export function init() {
const bgTheme = settings.general.theme.background()
const boardTheme = settings.general.theme.board()
setStatusBarStyle(bgTheme)
// load background theme
if (isTransparent(bgTheme)) {
const filename = getFilenameFromKey('bg', bgTheme)
getLocalFile('bg', filename).then(r => {
createStylesheetRule('bg', bgTheme, filename, r)
})
.catch(() => {
settings.general.theme.background('dark')
})
}
// load board theme
if (!settings.general.theme.bundledBoardThemes.includes(boardTheme)) {
const filename = getFilenameFromKey('board', boardTheme)
getLocalFile('board', filename).then(r => {
createStylesheetRule('board', boardTheme, filename, r)
})
.catch(() => {
settings.general.theme.board('brown')
})
}
}
export function getLocalFile(theme: Theme, fileName: string): Promise<ReadFileResult> {
return Filesystem.readFile({
path: theme + '-' + fileName,
directory: Directory.Data
})
}
export function getLocalFiles(theme: Theme): Promise<readonly string[]> {
return Filesystem.readdir({
path: '',
directory: Directory.Data
}).then(({ files }) => files.map(f => f.name).filter(f => f.startsWith(theme)))
}
export function filename(entry: ThemeEntry): string {
return entry.key + '.' + entry.ext
}
// either download it from server of get it from filesystem
export function loadImage(
theme: Theme,
key: string,
onProgress: (e: ProgressEvent) => void
): Promise<void> {
const filename = getFilenameFromKey(theme, key)
return getLocalFile(theme, filename)
.catch(() => {
// if not found, download
return download(theme, filename, onProgress)
.then(() => getLocalFile(theme, filename))
})
.then(res => {
createStylesheetRule(theme, key, filename, res)
})
}
export function handleError(err: any) {
console.error(err)
Toast.show({ text: 'Cannot load theme file', duration: 'short' })
}
function createStylesheetRule(
theme: Theme,
key: string,
filename: string,
{ data }: ReadFileResult
): void {
if (!styleEl) {
styleEl = document.createElement('style')
styleEl.type = 'text/css'
document.head.appendChild(styleEl)
}
const cleanData = data.replace(/\n/g, '') // FIXME should be fixed in capacitor
const ext = filename.split('.').pop()
const mime = ext === 'png' ?
'data:image/png;base64,' : 'data:image/jpeg;base64,'
const dataUrl = mime + cleanData
const css = theme === 'bg' ?
`.view-container.transp.${key} > main { background-image: url(${dataUrl}); }` :
`:root { --board-background: url(${dataUrl}); }\n` +
`.board-${key} > .cg-board { background-image: var(--board-background); }\n` +
`.game_menu_button.${key}::before { background-image: var(--board-background); background-size: 40px; }`
styleEl.appendChild(document.createTextNode(css))
}
function getFilenameFromKey(theme: Theme, key: string): string {
const avails = theme === 'bg' ?
settings.general.theme.availableBackgroundThemes :
settings.general.theme.availableBoardThemes
const t = avails.find(t => t.key === key)!
return filename(t)
}
function download(
theme: Theme,
fileName: string,
onProgress: (e: ProgressEvent) => void
): Promise<void> {
return new Promise((resolve, reject) => {
const client = new XMLHttpRequest()
const themePath = theme === 'bg' ? '/background' : '/board'
client.open('GET', `${baseUrl}${themePath}/${fileName}`, true)
client.responseType = 'blob'
if (onProgress) {
client.onprogress = onProgress
}
client.onload = () => {
if (client.status === 200) {
const blob = client.response
if (blob) {
const reader = new FileReader()
reader.readAsDataURL(blob)
reader.onloadend = () => {
const base64data = reader.result as string
Filesystem.writeFile({
path: theme + '-' + fileName,
data: base64data,
directory: Directory.Data,
})
.then(() => resolve())
}
} else {
reject('could not get file')
}
} else {
reject(`Request returned ${client.status}`)
}
}
client.send()
})
}
let systemTheme: string
export function getSystemTheme(): string {
if (systemTheme === undefined) {
const mql = window.matchMedia('(prefers-color-scheme: light)')
systemTheme = mql.matches ? 'light' : 'dark'
mql.addEventListener('change', e => {
systemTheme = e.matches ? 'light' : 'dark'
})
}
return systemTheme
}
export function setStatusBarStyle(key: string): Promise<void> {
const bgTheme = key === 'system' ? getSystemTheme() : key
return Promise.all([
Capacitor.getPlatform() === 'android' ? StatusBar.setBackgroundColor({
color: bgTheme === 'light' ? '#edebe9' :
bgTheme === 'dark' ? '#161512' : '#000000'
}) : Promise.resolve(),
StatusBar.setStyle({
style: bgTheme === 'light' ? StatusBarStyle.Light : StatusBarStyle.Dark
}),
// StatusBar.setOverlaysWebView({
// overlay: isTransparent(bgTheme)
// }),
]).then(() => { /* noop */ })
}