aboutsummaryrefslogtreecommitdiffstats
path: root/apps/theming/src/components/BackgroundSettings.vue
blob: 0d07071cb935a522e0f808f3bdcf2ee12c3fa980 (plain)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<!--
  - @copyright Copyright (c) 2020 Julius Härtl <jus@bitgrid.net>
  -
  - @author Christopher Ng <chrng8@gmail.com>
  - @author Greta Doci <gretadoci@gmail.com>
  - @author John Molakvoæ <skjnldsv@protonmail.com>
  - @author Julius Härtl <jus@bitgrid.net>
  -
  - @license GNU AGPL version 3 or any later version
  -
  - This program is free software: you can redistribute it and/or modify
  - it under the terms of the GNU Affero General Public License as
  - published by the Free Software Foundation, either version 3 of the
  - License, or (at your option) any later version.
  -
  - This program is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  - GNU Affero General Public License for more details.
  -
  - You should have received a copy of the GNU Affero General Public License
  - along with this program. If not, see <http://www.gnu.org/licenses/>.
  -
  -->

<template>
	<div class="background-selector" data-user-theming-background-settings>
		<!-- Custom background -->
		<button class="background background__filepicker"
			:class="{ 'icon-loading': loading === 'custom', 'background--active': backgroundImage === 'custom' }"
			:data-color-bright="invertTextColor(Theming.color)"
			data-user-theming-background-custom
			tabindex="0"
			@click="pickFile">
			{{ t('theming', 'Custom background') }}
			<ImageEdit v-if="backgroundImage !== 'custom'" :size="26" />
			<Check :size="44" />
		</button>

		<!-- Default background -->
		<button class="background background__default"
			:class="{ 'icon-loading': loading === 'default', 'background--active': backgroundImage === 'default' }"
			:data-color-bright="invertTextColor(Theming.defaultColor)"
			:style="{ '--border-color': Theming.defaultColor }"
			data-user-theming-background-default
			tabindex="0"
			@click="setDefault">
			{{ t('theming', 'Default background') }}
			<Check :size="44" />
		</button>

		<!-- Custom color picker -->
		<NcColorPicker v-model="Theming.color" @input="debouncePickColor">
			<button class="background background__color"
				:data-color="Theming.color"
				:data-color-bright="invertTextColor(Theming.color)"
				:style="{ backgroundColor: Theming.color, '--border-color': Theming.color}"
				data-user-theming-background-color
				tabindex="0">
				{{ t('theming', 'Change color') }}
			</button>
		</NcColorPicker>

		<!-- Remove background -->
		<button class="background background__delete"
			:class="{ 'background--active': isBackgroundDisabled }"
			data-user-theming-background-clear
			tabindex="0"
			@click="removeBackground">
			{{ t('theming', 'No background') }}
			<Close v-if="!isBackgroundDisabled" :size="32" />
			<Check :size="44" />
		</button>

		<!-- Background set selection -->
		<button v-for="shippedBackground in shippedBackgrounds"
			:key="shippedBackground.name"
			:title="shippedBackground.details.attribution"
			:aria-label="shippedBackground.details.attribution"
			:class="{ 'icon-loading': loading === shippedBackground.name, 'background--active': backgroundImage === shippedBackground.name }"
			:data-color-bright="shippedBackground.details.theming === 'dark'"
			:data-user-theming-background-shipped="shippedBackground.name"
			:style="{ backgroundImage: 'url(' + shippedBackground.preview + ')', '--border-color': shippedBackground.details.primary_color }"
			class="background background__shipped"
			tabindex="0"
			@click="setShipped(shippedBackground.name)">
			<Check :size="44" />
		</button>
	</div>
</template>

<script>
import { generateFilePath, generateRemoteUrl, generateUrl } from '@nextcloud/router'
import { loadState } from '@nextcloud/initial-state'
import axios from '@nextcloud/axios'
import Check from 'vue-material-design-icons/Check.vue'
import Close from 'vue-material-design-icons/Close.vue'
import ImageEdit from 'vue-material-design-icons/ImageEdit.vue'
import debounce from 'debounce'
import NcColorPicker from '@nextcloud/vue/dist/Components/NcColorPicker.js'
import Vibrant from 'node-vibrant'
import { Palette } from 'node-vibrant/lib/color.js'
import { getFilePickerBuilder } from '@nextcloud/dialogs'
import { getCurrentUser } from '@nextcloud/auth'

const backgroundImage = loadState('theming', 'backgroundImage')
const shippedBackgroundList = loadState('theming', 'shippedBackgrounds')
const themingDefaultBackground = loadState('theming', 'themingDefaultBackground')
const defaultShippedBackground = loadState('theming', 'defaultShippedBackground')

const prefixWithBaseUrl = (url) => generateFilePath('theming', '', 'img/background/') + url
const picker = getFilePickerBuilder(t('theming', 'Select a background from your files'))
	.setMultiSelect(false)
	.setModal(true)
	.setType(1)
	.setMimeTypeFilter(['image/png', 'image/gif', 'image/jpeg', 'image/svg+xml', 'image/svg'])
	.build()

export default {
	name: 'BackgroundSettings',

	components: {
		Check,
		Close,
		ImageEdit,
		NcColorPicker,
	},

	data() {
		return {
			loading: false,
			Theming: loadState('theming', 'data', {}),

			// User background image and color settings
			backgroundImage,
		}
	},

	computed: {
		shippedBackgrounds() {
			return Object.keys(shippedBackgroundList)
				.map(fileName => {
					return {
						name: fileName,
						url: prefixWithBaseUrl(fileName),
						preview: prefixWithBaseUrl('preview/' + fileName),
						details: shippedBackgroundList[fileName],
					}
				})
				.filter(background => {
					// If the admin did not changed the global background
					// let's hide the default background to not show it twice
					if (!this.isGlobalBackgroundDeleted && !this.isGlobalBackgroundDefault) {
						return background.name !== defaultShippedBackground
					}
					return true
				})
		},

		isGlobalBackgroundDefault() {
			return !!themingDefaultBackground
		},

		isGlobalBackgroundDeleted() {
			return themingDefaultBackground === 'backgroundColor'
		},

		isBackgroundDisabled() {
			return this.backgroundImage === 'disabled'
			|| !this.backgroundImage
		},
	},

	methods: {
		/**
		 * Do we need to invert the text if color is too bright?
		 *
		 * @param {string} color the hex color
		 */
		invertTextColor(color) {
			return this.calculateLuma(color) > 0.6
		},

		/**
		 * Calculate luminance of provided hex color
		 *
		 * @param {string} color the hex color
		 */
		calculateLuma(color) {
			const [red, green, blue] = this.hexToRGB(color)
			return (0.2126 * red + 0.7152 * green + 0.0722 * blue) / 255
		},

		/**
		 * Convert hex color to RGB
		 *
		 * @param {string} hex the hex color
		 */
		hexToRGB(hex) {
			const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
			return result
				? [parseInt(result[1], 16), parseInt(result[2], 16), parseInt(result[3], 16)]
				: null
		},

		/**
		 * Update local state
		 *
		 * @param {object} data destructuring object
		 * @param {string} data.backgroundColor background color value
		 * @param {string} data.backgroundImage background image value
		 * @param {string} data.version cache buster number
		 * @see https://github.com/nextcloud/server/blob/c78bd45c64d9695724fc44fe8453a88824b85f2f/apps/theming/lib/Controller/UserThemeController.php#L187-L191
		 */
		async update(data) {
			// Update state
			this.backgroundImage = data.backgroundImage
			this.Theming.color = data.backgroundColor

			// Notify parent and reload style
			this.$emit('update:background')
			this.loading = false
		},

		async setDefault() {
			this.loading = 'default'
			const result = await axios.post(generateUrl('/apps/theming/background/default'))
			this.update(result.data)
		},

		async setShipped(shipped) {
			this.loading = shipped
			const result = await axios.post(generateUrl('/apps/theming/background/shipped'), { value: shipped })
			this.update(result.data)
		},

		async setFile(path, color = null) {
			this.loading = 'custom'
			const result = await axios.post(generateUrl('/apps/theming/background/custom'), { value: path, color })
			this.update(result.data)
		},

		async removeBackground() {
			this.loading = 'remove'
			const result = await axios.delete(generateUrl('/apps/theming/background/custom'))
			this.update(result.data)
		},

		async pickColor(event) {
			this.loading = 'color'
			const color = event?.target?.dataset?.color || this.Theming?.color || '#0082c9'
			const result = await axios.post(generateUrl('/apps/theming/background/color'), { color })
			this.update(result.data)
		},
		debouncePickColor: debounce(function() {
			this.pickColor(...arguments)
		}, 200),

		async pickFile() {
			const path = await picker.pick()
			this.loading = 'custom'

			// Extract primary color from image
			let response = null
			let color = null
			try {
				const fileUrl = generateRemoteUrl('dav/files/' + getCurrentUser().uid + path)
				response = await axios.get(fileUrl, { responseType: 'blob' })
				const blobUrl = URL.createObjectURL(response.data)
				const palette = await this.getColorPaletteFromBlob(blobUrl)

				// DarkVibrant is accessible AND visually pleasing
				// Vibrant is not accessible enough and others are boring
				color = palette?.DarkVibrant?.hex
				this.setFile(path, color)

				// Log data
				console.debug('Extracted colour', color, 'from custom image', path, palette)
			} catch (error) {
				this.setFile(path)
				console.error('Unable to extract colour from custom image', { error, path, response, color })
			}
		},

		/**
		 * Extract a Vibrant color palette from a blob URL
		 *
		 * @param {string} blobUrl the blob URL
		 * @return {Promise<Palette>}
		 */
		getColorPaletteFromBlob(blobUrl) {
			return new Promise((resolve, reject) => {
				const vibrant = new Vibrant(blobUrl)
				vibrant.getPalette((error, palette) => {
					if (error) {
						reject(error)
					}
					resolve(palette)
				})
			})
		},
	},
}
</script>

<style scoped lang="scss">
.background-selector {
	display: flex;
	flex-wrap: wrap;
	justify-content: center;

	.background {
		overflow: hidden;
		width: 176px;
		height: 96px;
		margin: 8px;
		text-align: center;
		border: 2px solid var(--color-main-background);
		border-radius: var(--border-radius-large);
		background-position: center center;
		background-size: cover;

		&__filepicker {
			&.background--active {
				color: white;
				background-image: var(--image-background);
			}
		}

		&__default {
			background-color: var(--color-primary-default);
			background-image: var(--image-background-plain, var(--image-background-default));
		}

		&__filepicker, &__default, &__color {
			border-color: var(--color-border);
		}

		&__color {
			color: var(--color-primary-text);
			background-color: var(--color-primary-default);
		}

		// Over a background image
		&__default,
		&__shipped {
			color: white;
		}

		// Text and svg icon dark on bright background
		&[data-color-bright] {
			color: black;
		}

		&--active,
		&:hover,
		&:focus {
			// Use theme color primary, see inline css variable in template
			border: 2px solid var(--border-color, var(--color-primary)) !important;
		}

		// Icon
		span {
			margin: 4px;
		}

		.check-icon {
			display: none;
		}

		&--active:not(.icon-loading) {
			.check-icon {
				// Show checkmark
				display: block !important;
			}
		}
	}
}

</style>