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

Dark mode Implimentation for Desktop #1169

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 10 additions & 1 deletion td.vue/public/preload.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
const { contextBridge, ipcRenderer } = require('electron')
console.log('Preload script loaded2');
const { contextBridge, ipcRenderer } = require('electron');

if (process.env.IS_TEST === 'true') {
require('wdio-electron-service/preload');
}

// Expose Dark Mode API to the renderer process
contextBridge.exposeInMainWorld('darkMode', {
toggle: () => ipcRenderer.invoke('dark-mode:toggle'),
system: () => ipcRenderer.invoke('dark-mode:system'),
get: () => ipcRenderer.invoke('dark-mode:get'),
});

// Expose Electron API methods to the renderer process
contextBridge.exposeInMainWorld('electronAPI', {
// renderer to electron main
appClose: () => ipcRenderer.send('close-app'),
Expand Down
6 changes: 6 additions & 0 deletions td.vue/src/components/FormButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ button {
</style>

<script>
import { toggleDarkMode, setSystemTheme } from '@/plugins/dark-mode';

export default {
name: 'TdFormButton',
props: {
Expand All @@ -44,6 +46,10 @@ export default {
required: false,
default: false
}
},
methods: {
toggleDarkMode,
setSystemTheme
}
};
</script>
14 changes: 13 additions & 1 deletion td.vue/src/components/Navbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
</b-navbar-nav>

<b-navbar-nav class="ml-auto">
<b-nav-item>
<b-button size="sm" variant="outline-light" @click="handleToggleDarkMode">Toggle Dark Mode</b-button>
</b-nav-item>
<b-nav-item>
<b-button size="sm" variant="outline-light" @click="handleSetSystemTheme">System Theme</b-button>
</b-nav-item>
<b-nav-text v-show="username" class="logged-in-as">{{ $t('nav.loggedInAs')}} {{ username }}</b-nav-text>
<b-nav-item v-show="username" @click="onLogOut" id="nav-sign-out">
<font-awesome-icon
Expand Down Expand Up @@ -113,7 +119,7 @@ $icon-height: 1.2rem;

<script>
import { mapGetters } from 'vuex';

import { toggleDarkMode, setSystemTheme } from '../plugins/dark-mode';
import { LOGOUT } from '@/store/actions/auth.js';
import TdLocaleSelect from './LocaleSelect.vue';

Expand All @@ -128,6 +134,12 @@ export default {
])
},
methods: {
async handleToggleDarkMode() {
await toggleDarkMode(); // Call the utility function
},
async handleSetSystemTheme() {
await setSystemTheme(); // Call the utility function
},
onLogOut(evt) {
evt.preventDefault();
this.$store.dispatch(LOGOUT);
Expand Down
35 changes: 33 additions & 2 deletions td.vue/src/desktop/desktop.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

import { app, protocol, BrowserWindow, Menu, ipcMain } from 'electron';
'use strict';
console.log('desktop.js is loaded');
import { app, protocol, BrowserWindow, Menu, ipcMain, nativeTheme } from 'electron';
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib';
import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer';
import menu from './menu.js';
Expand Down Expand Up @@ -64,6 +65,33 @@ async function createWindow () {
}
}

// Dark Mode IPC Handlers
function setupDarkModeHandlers() {
// Toggle Dark Mode
ipcMain.handle('dark-mode:toggle', () => {
if (nativeTheme.shouldUseDarkColors) {
nativeTheme.themeSource = 'light';
} else {
nativeTheme.themeSource = 'dark';
}
return nativeTheme.shouldUseDarkColors;
});

// Set System Dark Mode
ipcMain.handle('dark-mode:system', () => {
nativeTheme.themeSource = 'system';
return nativeTheme.shouldUseDarkColors;
});

// Get Current Theme
ipcMain.handle('dark-mode:get', () => {
return {
shouldUseDarkColors: nativeTheme.shouldUseDarkColors,
themeSource: nativeTheme.themeSource
};
});
}

// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
Expand Down Expand Up @@ -102,6 +130,9 @@ app.on('ready', async () => {
}
}

// Setup Dark Mode Handlers
setupDarkModeHandlers();
// Existing IPC Handlers
ipcMain.on('close-app', handleCloseApp);
ipcMain.on('model-closed', handleModelClosed);
ipcMain.on('model-open-confirmed', handleModelOpenConfirmed);
Expand Down
31 changes: 31 additions & 0 deletions td.vue/src/desktop/renderer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
document.addEventListener('DOMContentLoaded', async () => {
const toggleButton = document.getElementById('toggle-dark-mode');
const systemButton = document.getElementById('system-dark-mode');
const themeStatus = document.getElementById('theme-status');

// Initial theme check
const currentTheme = await window.darkMode.get();
updateThemeStatus(currentTheme);

if (toggleButton) {
toggleButton.addEventListener('click', async () => {
const theme = await window.darkMode.toggle();
updateThemeStatus(theme);
});
}

if (systemButton) {
systemButton.addEventListener('click', async () => {
const theme = await window.darkMode.system();
updateThemeStatus(theme);
});
}

function updateThemeStatus(theme) {
if (themeStatus) {
themeStatus.textContent = `Current Theme: ${
theme.shouldUseDarkColors ? 'Dark' : 'Light'
} (Source: ${theme.themeSource})`;
}
}
});
22 changes: 22 additions & 0 deletions td.vue/src/plugins/dark-mode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export async function toggleDarkMode() {
if (window.darkMode) {
const isDark = await window.darkMode.toggle();
document.body.className = isDark ? 'dark' : 'light';
return isDark;
} else {
console.error('Dark mode API is not available.');
return null;
}
}

export async function setSystemTheme() {
if (window.darkMode) {
await window.darkMode.system();
const theme = await window.darkMode.get();
document.body.className = theme.shouldUseDarkColors ? 'dark' : 'light';
return theme;
} else {
console.error('Dark mode API is not available.');
return null;
}
}
5 changes: 5 additions & 0 deletions td.vue/src/styles/colors.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ $gray: #aea79f;
$white: #ffffff;
$black: #333333;

$dark-bg: #121212;
$dark-text: #e0e0e0;
$dark-border: #424242;
$dark-hover-bg: #1e1e1e;
$dark-hover-text: #f5f5f5;
5 changes: 5 additions & 0 deletions td.vue/src/styles/dark-mode.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* Dark Mode Styles */
body.dark {
background-color: $dark-bg;
color: $dark-text;
}