@@ -42,7 +42,7 @@ import { SelectionService } from 'browser/services/SelectionService';
4242import { ICharSizeService , ICharacterJoinerService , ICoreBrowserService , IKeyboardService , ILinkProviderService , IMouseService , IRenderService , ISelectionService , IThemeService } from 'browser/services/Services' ;
4343import { ThemeService } from 'browser/services/ThemeService' ;
4444import { KeyboardService } from 'browser/services/KeyboardService' ;
45- import { channels , color } from 'common/Color' ;
45+ import { channels , color , rgb } from 'common/Color' ;
4646import { CoreTerminal } from 'common/CoreTerminal' ;
4747import * as Browser from 'common/Platform' ;
4848import { ColorRequestType , CoreMouseAction , CoreMouseButton , CoreMouseEventType , IColorEvent , ITerminalOptions , KeyboardResultType , SpecialColorIndex } from 'common/Types' ;
@@ -252,6 +252,20 @@ export class CoreBrowserTerminal extends CoreTerminal implements ITerminal {
252252 }
253253 }
254254
255+ /**
256+ * Reports the current color scheme (dark or light) based on the relative luminance
257+ * of the background and foreground theme colors.
258+ * Sends CSI ? 997 ; 1 n for dark mode or CSI ? 997 ; 2 n for light mode.
259+ */
260+ private _reportColorScheme ( ) : void {
261+ if ( ! this . _themeService ) return ;
262+ const bgLuminance = rgb . relativeLuminance ( this . _themeService . colors . background . rgba >> 8 ) ;
263+ const fgLuminance = rgb . relativeLuminance ( this . _themeService . colors . foreground . rgba >> 8 ) ;
264+ // Dark mode = background is darker than foreground (lower luminance)
265+ const colorSchemeMode = bgLuminance < fgLuminance ? 1 : 2 ;
266+ this . coreService . triggerDataEvent ( `${ C0 . ESC } [?997;${ colorSchemeMode } n` ) ;
267+ }
268+
255269 protected _setup ( ) : void {
256270 super . _setup ( ) ;
257271
@@ -495,6 +509,16 @@ export class CoreBrowserTerminal extends CoreTerminal implements ITerminal {
495509 this . _themeService = this . _instantiationService . createInstance ( ThemeService ) ;
496510 this . _instantiationService . setService ( IThemeService , this . _themeService ) ;
497511
512+ // CSI ? 996 n - color scheme query (https://contour-terminal.org/vt-extensions/color-palette-update-notifications/)
513+ this . _register ( this . _inputHandler . onRequestColorSchemeQuery ( ( ) => this . _reportColorScheme ( ) ) ) ;
514+
515+ // Emit unsolicited color scheme notification on theme change when DECSET 2031 is enabled
516+ this . _register ( this . _themeService . onChangeColors ( ( ) => {
517+ if ( this . coreService . decPrivateModes . colorSchemeUpdates ) {
518+ this . _reportColorScheme ( ) ;
519+ }
520+ } ) ) ;
521+
498522 this . _characterJoinerService = this . _instantiationService . createInstance ( CharacterJoinerService ) ;
499523 this . _instantiationService . setService ( ICharacterJoinerService , this . _characterJoinerService ) ;
500524
0 commit comments