Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui): updates to dark mode logic #3740

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 41 additions & 16 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<v-app :dark="darkMode">
<v-app>
<div
v-if="$route.meta.requiresAuth && auth !== undefined && !hideTopbar"
>
Expand Down Expand Up @@ -592,8 +592,11 @@ export default {
this.title = value.name || ''
this.startSocket()
},
systemDark() {
this.updateDarkMode(this.darkMode)
},
darkMode(val) {
this.$vuetify.theme.dark = !!val
this.updateDarkMode(val)
},
pages() {
// this.verifyRoute()
Expand Down Expand Up @@ -666,6 +669,8 @@ export default {
hideTopbar: false,
title: '',
messages: [],
systemDark: false,
mq_system_dark: null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't use a watch for this instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also it's not clear to me what mq_system_dark is used for, you never re-assign it, I would instead use a let in beforeMount instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First time using Vue. Will re look at the logic here. Thanks for the notes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ok no worries I appreciate a lot your help 🙏🏼

}
},
methods: {
Expand Down Expand Up @@ -1570,6 +1575,14 @@ export default {
log.error(error)
}
},
updateSystemDark(update) {
this.systemDark = update.matches
},
updateDarkMode(value) {
// Set to system defualt if null
this.$vuetify.theme.dark =
value === null ? this.systemDark : !!value
},
},
beforeMount() {
manager.register(instances.APP, this)
Expand All @@ -1587,22 +1600,26 @@ export default {

const settings = useBaseStore().settings

// system dark mode
const systemThemeDark = !!window.matchMedia(
'(prefers-color-scheme: dark)',
).matches

// set the dark mode to the system dark mode if it's different
if (settings.load('dark') === undefined) {
useBaseStore().setDarkMode(systemThemeDark)
} else {
// load the theme from localstorage
// this is needed to prevent the theme switch on load
// this will be overriden by settings value once `initSettings`
// base store method is called
this.$vuetify.theme.dark = settings.load('dark', false)
// Event listener to capture system dark changes.
if (this.mq_system_dark === null && window) {
this.mq_system_dark = window.matchMedia(
'(prefers-color-scheme: dark)',
)
this.mq_system_dark.addEventListener(
'change',
this.updateSystemDark,
)
}

// pre-load System Dark
this.updateSystemDark(this.mq_system_dark)

// load the theme from localstorage
// this is needed to prevent the theme switch on load
// this will be overriden by settings value once `initSettings`
// base store method is called
this.updateDarkMode(settings.load('dark', null))

useBaseStore().$onAction(({ name, args }) => {
if (name === 'showSnackbar') {
this.showSnackbar(...args)
Expand All @@ -1612,6 +1629,14 @@ export default {
}
})
},
beforeUnmount() {
if (this.mq_system_dark !== null) {
this.mq_system_dark.removeEventListener(
'change',
this.updateSystemDark,
)
}
},
Comment on lines +1635 to +1642
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bit useless as the App will only be unmounted when the page is destroyed

beforeDestroy() {
if (this.socket) this.socket.close()
},
Expand Down
2 changes: 1 addition & 1 deletion src/stores/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const useBaseStore = defineStore('base', {
frequency: false,
},
ui: {
darkMode: settings.load('dark', false),
darkMode: settings.load('dark', null), // Null = System Default
navTabs: settings.load('navTabs', false),
compactMode: settings.load('compact', false),
},
Expand Down
37 changes: 29 additions & 8 deletions src/views/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,29 @@
<v-expansion-panel-content>
<v-row class="mb-5">
<v-col cols="12" sm="6">
<v-switch
hint="Enable dark mode"
persistent-hint
<v-input
label="Dark mode"
v-model="internalDarkMode"
></v-switch>
hint="Eanble System Defined, Dark, or Light modes"
persistent-hint
>
<v-btn-toggle
v-model="btnGrpDarkMode"
divided
manditory
>
<v-btn value="auto">
<v-icon>laptop</v-icon>
</v-btn>

<v-btn value="true">
<v-icon>dark_mode</v-icon>
</v-btn>

<v-btn value="false">
<v-icon>light_mode</v-icon>
</v-btn>
</v-btn-toggle>
</v-input>
</v-col>
<v-col cols="12" sm="6">
<v-switch
Expand Down Expand Up @@ -1917,12 +1934,16 @@ export default {
},
},
computed: {
internalDarkMode: {
btnGrpDarkMode: {
get() {
return this.darkMode
if (this.darkMode === null) return 'auto'
if (this.darkMode === true) return 'true'
return 'false'
},
set(value) {
this.setDarkMode(value)
if (value === 'auto') this.setDarkMode(null)
else if (value === 'true') this.setDarkMode(true)
else this.setDarkMode(false)
},
},
internalNavTabs: {
Expand Down
Loading