Skip to content

Commit

Permalink
Simplify theme API for views
Browse files Browse the repository at this point in the history
This doesn't yet allow custom themes, but it should handle everything
else.
  • Loading branch information
AbeJellinek committed Feb 19, 2025
1 parent e03c8b6 commit 717dcb6
Showing 1 changed file with 40 additions and 12 deletions.
52 changes: 40 additions & 12 deletions src/common/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ import SnapshotView from '../dom/snapshot/snapshot-view';
import { debounce } from './lib/debounce';
import AnnotationManager from './annotation-manager';
import { DEBOUNCE_STATE_CHANGE, DEBOUNCE_STATS_CHANGE, DEFAULT_THEMES } from './defines';
import { getCurrentColorScheme } from './lib/utilities';

let nop = () => undefined;

class View {
constructor(options) {
this._type = options.type;
this._options = options;

this._lightTheme = (options.lightTheme && DEFAULT_THEMES.find(x => x.id === options.lightTheme))
?? null;
this._darkTheme = options.darkTheme
? DEFAULT_THEMES.find(x => x.id === options.darkTheme) ?? null
: DEFAULT_THEMES.find(x => x.id === 'dark');
this._colorScheme = options.colorScheme;

this._view = this._createView();
this._annotationManager = new AnnotationManager({
readOnly: options.readOnly,
Expand Down Expand Up @@ -68,6 +77,9 @@ class View {
findState,
viewState: this._options.viewState || null,
location: this._options.location || null,
lightTheme: this._lightTheme,
darkTheme: this._darkTheme,
colorScheme: this._colorScheme,
onChangeViewState: debounce(this._options.onChangeViewState, DEBOUNCE_STATE_CHANGE),
onChangeViewStats: debounce(this._options.onChangeViewStats, DEBOUNCE_STATS_CHANGE),
onAddAnnotation,
Expand Down Expand Up @@ -197,28 +209,44 @@ class View {
}

/**
* @param {string} themeName
* @returns {string} Theme ID
*/
setLightTheme(themeName) {
let themes = new Map(DEFAULT_THEMES.map(theme => [theme.id, theme]));
let lightTheme = themes.get(themeName) || null;
this._view.setLightTheme(lightTheme);
getTheme() {
let theme = getCurrentColorScheme(null) === 'dark'
? this._darkTheme
: this._lightTheme;
return theme?.id ?? 'light';
}

/**
* @param {string} themeName
* @param {string} themeID
*/
setDarkTheme(themeName) {
setTheme(themeID) {
let themes = new Map(DEFAULT_THEMES.map(theme => [theme.id, theme]));
let darkTheme = themes.get(themeName) || null;
this._view.setDarkTheme(darkTheme);
let theme = themes.get(themeID) || null;
if (getCurrentColorScheme(this._colorScheme) === 'dark') {
this._darkTheme = theme;
this._view.setDarkTheme(theme);
}
else {
this._lightTheme = theme;
this._view.setLightTheme(theme);
}
}

/**
* @returns {'light' | 'dark' | null}
*/
getColorScheme() {
return this._colorScheme;
}

/**
* @param {'light' | 'dark' | null} colorScheme
* @param {'light' | 'dark' | null} scheme
*/
setColorScheme(colorScheme) {
this._view.setColorScheme(colorScheme);
setColorScheme(scheme) {
this._colorScheme = scheme;
this._view.setColorScheme(scheme);
}
}

Expand Down

0 comments on commit 717dcb6

Please sign in to comment.