You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by "scottyaslan (via GitHub)" <gi...@apache.org> on 2024/02/02 19:48:58 UTC

[PR] [NIFI-12727] Detect theme based on OS setting, allow user override [nifi]

scottyaslan opened a new pull request, #8352:
URL: https://github.com/apache/nifi/pull/8352

   NIFI-12727
   
   Detect theme from OS settings. Add user override option persisted in local storage.
   
   ![Kapture 2024-02-02 at 14 47 45](https://github.com/apache/nifi/assets/6797571/b034d66c-6df1-4fb2-96fc-b8e1b70d3fd1)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [NIFI-12727] Detect theme based on OS setting, allow user override [nifi]

Posted by "mcgilman (via GitHub)" <gi...@apache.org>.
mcgilman commented on code in PR #8352:
URL: https://github.com/apache/nifi/pull/8352#discussion_r1480501385


##########
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-frontend/src/main/nifi/src/app/app.component.ts:
##########
@@ -37,5 +43,22 @@ export class AppComponent {
                 this.guardLoading = false;
             }
         });
+
+        let theme = this.storage.getItem('theme');
+
+        // Initially check if dark mode is enabled on system
+        const darkModeOn = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
+
+        // If dark mode is enabled then directly switch to the dark-theme
+        this.themingService.toggleTheme(darkModeOn, theme);
+
+        if (window.matchMedia) {
+            // Watch for changes of the preference
+            window.matchMedia('(prefers-color-scheme: dark)').addListener((e) => {
+                theme = this.storage.getItem('theme');
+                const newColorScheme = e.matches ? 'dark' : 'light';

Review Comment:
   If `e.matches` is a boolean, do we need to convert it into a string here? Can `e.matches` be passed to `toggleTheme` just like it is above?



##########
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-frontend/src/main/nifi/src/app/ui/common/navigation/navigation.component.ts:
##########
@@ -94,10 +114,10 @@ export class Navigation {
         );
     }
 
-    toggleTheme(value = !this.isDarkMode) {
-        this.isDarkMode = value;
-        this._document.body.classList.toggle('dark-theme', value);
-
-        // TODO: save to local storage or read OS settings???
+    toggleTheme(theme: string) {
+        this.theme = theme;
+        this.storage.setItem('theme', theme);
+        this.darkModeOn = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;

Review Comment:
   Does `darkModeOn` need to be re-assigned here? It is currently being assigned in the listener above so I suspect it should be always up to date.



##########
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-frontend/src/main/nifi/src/app/service/theming.service.ts:
##########
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { Inject, Injectable } from '@angular/core';
+import { DOCUMENT } from '@angular/common';
+
+export const DARK_THEME = 'DARK_THEME';
+export const LIGHT_THEME = 'LIGHT_THEME';
+export const OS_SETTING = 'OS_SETTING';
+
+@Injectable({ providedIn: 'root' })
+export class ThemingService {
+    constructor(@Inject(DOCUMENT) private _document: Document) {}
+
+    toggleTheme(darkModeOn: boolean, theme: any) {
+        if (darkModeOn) {
+            if (theme === DARK_THEME) {
+                this._document.body.classList.toggle('dark-theme', true);
+            } else if (theme === LIGHT_THEME) {
+                this._document.body.classList.toggle('dark-theme', false);
+            } else {
+                this._document.body.classList.toggle('dark-theme', true);
+            }
+        } else if (!darkModeOn) {

Review Comment:
   Can this just be `else` instead of `else if`?



##########
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-frontend/src/main/nifi/src/app/ui/common/navigation/navigation.component.ts:
##########
@@ -44,23 +47,40 @@ import { MatSlideToggleModule } from '@angular/material/slide-toggle';
         NgIf,
         RouterLink,
         MatButtonModule,
-        MatSlideToggleModule,
-        FormsModule
+        FormsModule,
+        MatCheckboxModule
     ],
     templateUrl: './navigation.component.html',
     styleUrls: ['./navigation.component.scss']
 })
 export class Navigation {
+    theme: any | undefined;
+    darkModeOn: boolean | undefined;
+    LIGHT_THEME: string = LIGHT_THEME;
+    DARK_THEME: string = DARK_THEME;
+    OS_SETTING: string = OS_SETTING;
     currentUser$ = this.store.select(selectCurrentUser);
     flowConfiguration$ = this.store.select(selectFlowConfiguration);
-    isDarkMode = false;
 
     constructor(
         private store: Store<NiFiState>,
         private authStorage: AuthStorage,
         private authService: AuthService,
-        @Inject(DOCUMENT) private _document: Document
-    ) {}
+        private storage: Storage,
+        private themingService: ThemingService
+    ) {
+        this.darkModeOn = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
+        this.theme = this.storage.getItem('theme');
+
+        if (window.matchMedia) {
+            // Watch for changes of the preference
+            window.matchMedia('(prefers-color-scheme: dark)').addListener((e) => {
+                const newColorScheme = e.matches ? 'dark' : 'light';
+                this.darkModeOn = newColorScheme === 'dark';

Review Comment:
   Can `darkModeOn` just be set to `e.matches`? 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [NIFI-12727] Detect theme based on OS setting, allow user override [nifi]

Posted by "mcgilman (via GitHub)" <gi...@apache.org>.
mcgilman merged PR #8352:
URL: https://github.com/apache/nifi/pull/8352


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [NIFI-12727] Detect theme based on OS setting, allow user override [nifi]

Posted by "mcgilman (via GitHub)" <gi...@apache.org>.
mcgilman commented on PR #8352:
URL: https://github.com/apache/nifi/pull/8352#issuecomment-1927117759

   Will review...


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org