forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstyleInjector.tsx
More file actions
145 lines (121 loc) · 5.19 KB
/
Copy pathstyleInjector.tsx
File metadata and controls
145 lines (121 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as monacoEditor from 'monaco-editor/esm/vs/editor/editor.api';
import * as React from 'react';
import { CssMessages, IGetCssResponse, SharedMessages } from '../../client/datascience/messages';
import { IGetMonacoThemeResponse } from '../../client/datascience/monacoMessages';
import { IDataScienceExtraSettings } from '../../client/datascience/types';
import { IMessageHandler, PostOffice } from './postOffice';
import { getSettings } from './settingsReactSide';
import { detectBaseTheme } from './themeDetector';
export interface IStyleInjectorProps {
expectingDark: boolean;
postOffice: PostOffice;
darkChanged?(newDark: boolean): void;
monacoThemeChanged?(theme: string): void;
onReady?(): void;
}
interface IStyleInjectorState {
rootCss?: string;
theme?: string;
knownDark?: boolean;
}
export class StyleInjector extends React.Component<IStyleInjectorProps, IStyleInjectorState> implements IMessageHandler {
constructor(props: IStyleInjectorProps) {
super(props);
this.state = { rootCss: undefined, theme: undefined };
}
public componentWillMount() {
// Add ourselves as a handler for the post office
this.props.postOffice.addHandler(this);
}
public componentWillUnmount() {
// Remove ourselves as a handler for the post office
this.props.postOffice.removeHandler(this);
}
public componentDidMount() {
if (!this.state.rootCss) {
// Set to a temporary value.
this.setState({rootCss: ' '});
this.props.postOffice.sendUnsafeMessage(CssMessages.GetCssRequest, { isDark: this.props.expectingDark });
this.props.postOffice.sendUnsafeMessage(CssMessages.GetMonacoThemeRequest, { isDark: this.props.expectingDark });
}
}
public render() {
return (
<div className='styleSetter'>
<style>
{this.state.rootCss}
</style>
{this.props.children}
</div>
);
}
// tslint:disable-next-line:no-any
public handleMessage = (msg: string, payload?: any) : boolean => {
switch (msg) {
case CssMessages.GetCssResponse:
this.handleCssResponse(payload);
break;
case CssMessages.GetMonacoThemeResponse:
this.handleMonacoThemeResponse(payload);
break;
case SharedMessages.UpdateSettings:
this.updateSettings(payload);
break;
default:
break;
}
return true;
}
// tslint:disable-next-line:no-any
private handleCssResponse(payload?: any) {
const response = payload as IGetCssResponse;
if (response && response.css) {
// Recompute our known dark value from the class name in the body
// VS code should update this dynamically when the theme changes
const computedKnownDark = this.computeKnownDark();
// We also get this in our response, but computing is more reliable
// than searching for it.
if (this.state.knownDark !== computedKnownDark &&
this.props.darkChanged) {
this.props.darkChanged(computedKnownDark);
}
this.setState({
rootCss: response.css,
theme: response.theme,
knownDark: computedKnownDark
}, this.props.onReady);
}
}
// tslint:disable-next-line: no-any
private handleMonacoThemeResponse(payload?: any) {
const response = payload as IGetMonacoThemeResponse;
if (response && response.theme) {
// Tell monaco we have a new theme. THis is like a state update for monaco
monacoEditor.editor.defineTheme('interactiveWindow', response.theme);
// Tell the main panel we have a theme now
if (this.props.monacoThemeChanged) {
this.props.monacoThemeChanged('interactiveWindow');
}
}
}
// tslint:disable-next-line:no-any
private updateSettings(payload: any) {
if (payload) {
const newSettings = JSON.parse(payload as string);
const dsSettings = newSettings as IDataScienceExtraSettings;
if (dsSettings && dsSettings.extraSettings && dsSettings.extraSettings.theme !== this.state.theme) {
// User changed the current theme. Rerender
this.props.postOffice.sendUnsafeMessage(CssMessages.GetCssRequest, { isDark: this.computeKnownDark() });
this.props.postOffice.sendUnsafeMessage(CssMessages.GetMonacoThemeRequest, { isDark: this.computeKnownDark() });
}
}
}
private computeKnownDark() : boolean {
const ignore = getSettings && getSettings().ignoreVscodeTheme ? true : false;
const baseTheme = ignore ? 'vscode-light' : detectBaseTheme();
return baseTheme !== 'vscode-light';
}
}