dididy commented on code in PR #5078:
URL: https://github.com/apache/zeppelin/pull/5078#discussion_r2399289333


##########
zeppelin-web-angular/src/app/services/theme.service.ts:
##########
@@ -0,0 +1,197 @@
+/*
+ * Licensed 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 { Injectable, OnDestroy } from '@angular/core';
+import { BehaviorSubject, Observable } from 'rxjs';
+
+export type ThemeMode = 'light' | 'dark' | 'system';
+
+const THEME_STORAGE_KEY = 'zeppelin-theme';
+const MONACO_THEMES = {
+  light: 'vs',
+  dark: 'vs-dark'
+} as const;
+
+@Injectable({
+  providedIn: 'root'
+})
+export class ThemeService implements OnDestroy {
+  private currentTheme: BehaviorSubject<ThemeMode>;
+  public theme$: Observable<ThemeMode>;
+  private currentEffectiveTheme: BehaviorSubject<'light' | 'dark'>;
+  public effectiveTheme$: Observable<'light' | 'dark'>;
+  private mediaQuery?: MediaQueryList;
+  private systemThemeListener?: (e: MediaQueryListEvent) => void;
+  private systemStartedWith: 'light' | 'dark' | null = null;
+
+  ngOnDestroy() {
+    this.removeSystemThemeListener();
+    this.currentTheme.complete();
+    this.currentEffectiveTheme.complete();
+  }
+
+  constructor() {
+    const initialTheme = this.detectInitialTheme();
+    this.currentTheme = new BehaviorSubject<ThemeMode>(initialTheme);
+    this.theme$ = this.currentTheme.asObservable();
+
+    const initialEffectiveTheme = this.resolveEffectiveTheme(initialTheme);
+    this.currentEffectiveTheme = new BehaviorSubject<'light' | 
'dark'>(initialEffectiveTheme);
+    this.effectiveTheme$ = this.currentEffectiveTheme.asObservable();
+
+    this.initSystemThemeDetection();
+
+    this.applyTheme(initialEffectiveTheme);
+  }
+
+  detectInitialTheme(): ThemeMode {
+    try {
+      const savedTheme = localStorage.getItem(THEME_STORAGE_KEY);
+      if (savedTheme && this.isValidTheme(savedTheme)) {
+        return savedTheme;
+      }
+      return 'system';
+    } catch {
+      return 'system';
+    }
+  }
+
+  isValidTheme(theme: string): theme is ThemeMode {
+    return theme === 'light' || theme === 'dark' || theme === 'system';
+  }
+
+  resolveEffectiveTheme(theme: ThemeMode): 'light' | 'dark' {
+    if (theme === 'system') {
+      return window.matchMedia?.('(prefers-color-scheme: dark)')?.matches ? 
'dark' : 'light';
+    }
+    return theme;
+  }
+
+  getCurrentTheme(): ThemeMode {
+    return this.currentTheme.value;
+  }
+
+  getEffectiveTheme(): 'light' | 'dark' {
+    return this.currentEffectiveTheme.value;
+  }
+
+  isDarkMode(): boolean {
+    return this.currentEffectiveTheme.value === 'dark';
+  }
+
+  setTheme(theme: ThemeMode, save: boolean = true) {
+    if (this.currentTheme.value === theme) {
+      return;
+    }
+
+    this.currentTheme.next(theme);
+    const effectiveTheme = this.resolveEffectiveTheme(theme);
+    this.currentEffectiveTheme.next(effectiveTheme);
+    this.applyTheme(effectiveTheme);
+
+    if (save) {
+      localStorage.setItem(THEME_STORAGE_KEY, theme);
+    }
+
+    // Update system theme listener based on new theme
+    if (theme === 'system') {
+      this.initSystemThemeDetection();
+    } else {
+      this.removeSystemThemeListener();
+    }
+  }
+
+  toggleTheme() {
+    const currentTheme = this.getCurrentTheme();
+
+    if (currentTheme === 'system') {
+      const currentEffectiveTheme = this.getEffectiveTheme();
+      this.systemStartedWith = currentEffectiveTheme;
+      this.setTheme(currentEffectiveTheme === 'dark' ? 'light' : 'dark');
+    } else if (currentTheme === 'dark') {
+      if (this.systemStartedWith === 'dark') {
+        this.setTheme('system');
+        this.systemStartedWith = null;
+      } else {
+        this.setTheme('light');
+      }
+    } else {
+      if (this.systemStartedWith === 'light') {
+        this.setTheme('system');
+        this.systemStartedWith = null;
+      } else if (this.systemStartedWith === 'dark') {
+        this.setTheme('dark');
+      } else {
+        this.setTheme('system');
+      }
+    }
+  }

Review Comment:
   4818168
   I wanted to control the flow based on the system theme, but it got 
complicated in many ways, so I decided to ignore the system theme and just fix 
the direction. The sequence is now light -> dark -> system.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to