ability to use dark mode from system #1084

This commit is contained in:
SPRINX0\prochazka
2025-04-02 10:44:40 +02:00
parent 9a9b18a3ef
commit 8b1da33ffe
4 changed files with 55 additions and 16 deletions

View File

@@ -12,6 +12,7 @@
visibleCommandPalette, visibleCommandPalette,
visibleTitleBar, visibleTitleBar,
visibleToolbar, visibleToolbar,
getSystemTheme,
} from './stores'; } from './stores';
import TabsPanel from './tabpanel/TabsPanel.svelte'; import TabsPanel from './tabpanel/TabsPanel.svelte';
import TabRegister from './tabpanel/TabRegister.svelte'; import TabRegister from './tabpanel/TabRegister.svelte';
@@ -50,7 +51,7 @@
</div> </div>
<div <div
class={`${$currentTheme} ${currentThemeType} root dbgate-screen`} class={`${$currentTheme ?? getSystemTheme()} ${currentThemeType} root dbgate-screen`}
class:isElectron class:isElectron
use:dragDropFileTarget use:dragDropFileTarget
on:contextmenu={e => e.preventDefault()} on:contextmenu={e => e.preventDefault()}

View File

@@ -28,6 +28,8 @@
selectedWidget, selectedWidget,
lockedDatabaseMode, lockedDatabaseMode,
visibleWidgetSideBar, visibleWidgetSideBar,
currentTheme,
getSystemTheme,
} from '../stores'; } from '../stores';
import { isMac } from '../utility/common'; import { isMac } from '../utility/common';
import getElectron from '../utility/getElectron'; import getElectron from '../utility/getElectron';
@@ -280,6 +282,32 @@ ORDER BY
<svelte:fragment slot="3"> <svelte:fragment slot="3">
<div class="heading">Application theme</div> <div class="heading">Application theme</div>
<FormFieldTemplateLarge
label="Use system theme"
type="checkbox"
labelProps={{
onClick: () => {
if ($currentTheme) {
$currentTheme = null;
} else {
$currentTheme = getSystemTheme();
}
},
}}
>
<CheckboxField
checked={!$currentTheme}
on:change={e => {
if (e.target['checked']) {
$currentTheme = null;
} else {
$currentTheme = getSystemTheme();
}
}}
/>
</FormFieldTemplateLarge>
<div class="themes"> <div class="themes">
{#each $extensions.themes as theme} {#each $extensions.themes as theme}
<ThemeSkeleton {theme} /> <ThemeSkeleton {theme} />
@@ -495,16 +523,12 @@ ORDER BY
label="Folder with mysql plugins (for example for authentication). Set only in case of problems" label="Folder with mysql plugins (for example for authentication). Set only in case of problems"
defaultValue="" defaultValue=""
/> />
<FormTextField <FormTextField
name="externalTools.pg_dump" name="externalTools.pg_dump"
label="pg_dump (backup PostgreSQL database)" label="pg_dump (backup PostgreSQL database)"
defaultValue="pg_dump" defaultValue="pg_dump"
/> />
<FormTextField <FormTextField name="externalTools.psql" label="psql (restore PostgreSQL database)" defaultValue="psql" />
name="externalTools.psql"
label="psql (restore PostgreSQL database)"
defaultValue="psql"
/>
</svelte:fragment> </svelte:fragment>
</TabControl> </TabControl>
</FormValues> </FormValues>

View File

@@ -26,15 +26,19 @@ export interface TabDefinition {
focused?: boolean; focused?: boolean;
} }
function getSystemTheme() { export function getSystemTheme() {
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'theme-dark' : 'theme-light'; return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'theme-dark' : 'theme-light';
} }
export function writableWithStorage<T>(defaultValue: T, storageName) { export function writableWithStorage<T>(defaultValue: T, storageName, removeCondition?: (value: T) => boolean) {
const init = localStorage.getItem(storageName); const init = localStorage.getItem(storageName);
const res = writable<T>(init ? safeJsonParse(init, defaultValue, true) : defaultValue); const res = writable<T>(init ? safeJsonParse(init, defaultValue, true) : defaultValue);
res.subscribe(value => { res.subscribe(value => {
localStorage.setItem(storageName, JSON.stringify(value)); if (removeCondition && removeCondition(value)) {
localStorage.removeItem(storageName);
} else {
localStorage.setItem(storageName, JSON.stringify(value));
}
}); });
return res; return res;
} }
@@ -104,8 +108,8 @@ export const extensions = writable<ExtensionsDirectory>(null);
export const visibleCommandPalette = writable(null); export const visibleCommandPalette = writable(null);
export const commands = writable({}); export const commands = writable({});
export const currentTheme = getElectron() export const currentTheme = getElectron()
? writableSettingsValue(getSystemTheme(), 'currentTheme') ? writableSettingsValue(null, 'currentTheme')
: writableWithStorage(getSystemTheme(), 'currentTheme'); : writableWithStorage(null, 'currentTheme', x => x == null);
export const currentEditorTheme = getElectron() export const currentEditorTheme = getElectron()
? writableSettingsValue(null, 'currentEditorTheme') ? writableSettingsValue(null, 'currentEditorTheme')
: writableWithStorage(null, 'currentEditorTheme'); : writableWithStorage(null, 'currentEditorTheme');
@@ -197,12 +201,21 @@ export const connectionAppObjectSearchSettings = writableWithStorage(
'connectionAppObjectSearchSettings2' 'connectionAppObjectSearchSettings2'
); );
export const currentThemeDefinition = derived([currentTheme, extensions], ([$currentTheme, $extensions]) => let currentThemeValue = null;
$extensions?.themes?.find(x => x.themeClassName == $currentTheme) currentTheme.subscribe(value => {
); currentThemeValue = value;
});
export const getCurrentTheme = () => currentThemeValue;
export const currentThemeDefinition = derived([currentTheme, extensions], ([$currentTheme, $extensions]) => {
const usedTheme = $currentTheme ?? getSystemTheme();
return $extensions?.themes?.find(x => x.themeClassName == usedTheme);
});
currentThemeDefinition.subscribe(value => { currentThemeDefinition.subscribe(value => {
if (value?.themeType) { if (value?.themeType && getCurrentTheme()) {
localStorage.setItem('currentThemeType', value?.themeType); localStorage.setItem('currentThemeType', value?.themeType);
} else {
localStorage.removeItem('currentThemeType');
} }
}); });
export const openedConnectionsWithTemporary = derived( export const openedConnectionsWithTemporary = derived(

View File

@@ -14,6 +14,7 @@
<li>Compare database models</li> <li>Compare database models</li>
<li>Synchronize database structure</li> <li>Synchronize database structure</li>
<li>Backup &amp; restore database</li> <li>Backup &amp; restore database</li>
<li>Advanced ER diagram settings</li>
<li>Export database model</li> <li>Export database model</li>
<li>AI assistant</li> <li>AI assistant</li>
<li>libSQL, Turso, CosmosDB, Redshift support</li> <li>libSQL, Turso, CosmosDB, Redshift support</li>