Skip to content

Commit 55fe540

Browse files
msivasubramaniaandgolovin
authored andcommitted
change syntaxhighlighting style based on theme selection
Signed-off-by: msivasubramaniaan <msivasub@redhat.com>
1 parent 459f530 commit 55fe540

File tree

6 files changed

+36
-9
lines changed

6 files changed

+36
-9
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@
286286
"onCommand:clusters.openshift.deployment.openConsole",
287287
"onCommand:clusters.openshift.imagestream.openConsole",
288288
"onCommand:openshift.componentTypesView.registry.openInView",
289+
"onCommand:openshift.componentTypesView.registry.setTheme",
289290
"onWalkthrough:openshiftWalkthrough"
290291
],
291292
"contributes": {

src/extension.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import {
1111
StatusBarAlignment,
1212
StatusBarItem,
1313
env,
14-
QuickPickItemKind
14+
QuickPickItemKind,
15+
ColorTheme,
16+
ColorThemeKind
1517
} from 'vscode';
1618
import path = require('path');
1719
import { startTelemetry } from './telemetry';
@@ -88,6 +90,14 @@ export async function activate(extensionContext: ExtensionContext): Promise<any>
8890
];
8991
disposable.forEach((value) => extensionContext.subscriptions.push(value));
9092

93+
let themeKind:ColorThemeKind = window.activeColorTheme.kind;
94+
window.onDidChangeActiveColorTheme((editor: ColorTheme) => {
95+
if (themeKind !== editor.kind) {
96+
themeKind = editor?.kind;
97+
void commands.executeCommand('openshift.componentTypesView.registry.setTheme', themeKind);
98+
}
99+
});
100+
91101
function statusBarFunctions() {
92102
return commands.registerCommand('openshift.openStatusBar', async () => {
93103
const selection = await window.showQuickPick(

src/webview/devfile-registry/app/cardItem.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ import { StarterProject } from '../../../odo/componentTypeDescription';
1212
import { StarterProjectDisplay } from './starterProjectDisplay';
1313
import { Badge, Backdrop, Button, Card, CardActions, Modal } from '@material-ui/core';
1414
import { FileCopy } from '@material-ui/icons';
15-
import { monokai } from 'react-syntax-highlighter/dist/esm/styles/hljs';
1615
import { Tooltip, Typography } from '@mui/material';
16+
import { qtcreatorLight, monokai } from 'react-syntax-highlighter/dist/esm/styles/hljs';
1717

1818
export class CardItem extends React.Component<DevFileProps, {
1919
numOfCall: number,
2020
isExpanded: boolean,
2121
devFileYAML: string,
2222
selectedProject: StarterProject,
2323
copyClicked: boolean
24-
hoverProject: null | StarterProject,
24+
hoverProject: null | StarterProject
2525
}> {
2626

2727
constructor(props: DevFileProps) {
@@ -246,22 +246,22 @@ export class CardItem extends React.Component<DevFileProps, {
246246
<Button
247247
id='tooltip-selector'
248248
component='span'
249-
style={{ cursor: 'pointer' }}
249+
style={{ cursor: 'pointer', backgroundColor: 'var(--vscode-button-background)' }}
250250
onClick={(): void => this.copyClicked(true)}
251251
>
252252
<FileCopy style={{ color: 'white' }} fontSize='small' />
253253
</Button>} />
254254
</CardActions>
255255
</CopyToClipboard>
256256
<SyntaxHighlighter language='yaml' useInlineStyles
257-
style={monokai}
257+
style={this.props.themeKind <= 1 ? qtcreatorLight : monokai}
258258
wrapLines
259259
showLineNumbers
260260
lineNumberStyle={{ marginLeft: '-1.5rem' }}
261261
customStyle={{ marginLeft: '-1.5rem', backgroundColor: 'inherit' }}
262262
codeTagProps={{
263263
style: {
264-
fontFamily: 'inherit', color: 'inherit',
264+
fontFamily: 'inherit',
265265
fontStyle: 'inherit', fontWeight: 'inherit'
266266
}
267267
}}>

src/webview/devfile-registry/app/home.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@ interface CompTypeDesc extends ComponentTypeDescription {
2727

2828
interface HomePageProps extends React.AnchorHTMLAttributes<HTMLAnchorElement> {
2929
compDescriptions: CompTypeDesc[];
30+
themeKind: number;
3031
}
3132

3233
export interface DefaultProps {
3334
analytics?: import('@segment/analytics-next').Analytics;
3435
}
3536

3637
const HomeItem: React.FC<HomePageProps> = ({
37-
compDescriptions
38+
compDescriptions,
39+
themeKind
3840
}: HomePageProps) => {
3941
const homeStyleClass = useHomeStyles();
4042
const cardItemStyle = useCardItemStyles();
@@ -45,7 +47,8 @@ const HomeItem: React.FC<HomePageProps> = ({
4547
compDescriptions.map((compDescription: CompTypeDesc, key: number) => (
4648
<ImageListItem key={`imageList-`+key}>
4749
<CardItem key={key} compDescription={compDescription}
48-
cardItemStyle={cardItemStyle} projectDisplayStyle={projectDisplayStyle} hasGitLink={hasGitLink(compDescription)} />
50+
cardItemStyle={cardItemStyle} projectDisplayStyle={projectDisplayStyle} hasGitLink={hasGitLink(compDescription)}
51+
themeKind={themeKind} />
4952
</ImageListItem>
5053
))
5154
}
@@ -59,6 +62,7 @@ export const Home: React.FC<DefaultProps> = ({ }) => {
5962
const [registries, setRegistries] = React.useState([]);
6063
const [searchValue, setSearchValue] = React.useState('');
6164
const [error, setError] = React.useState('');
65+
const [themeKind, setThemeKind] = React.useState(0);
6266

6367
React.useEffect(() => {
6468
return VSCodeMessage.onMessage((message) => {
@@ -81,6 +85,7 @@ export const Home: React.FC<DefaultProps> = ({ }) => {
8185
}
8286
});
8387
}
88+
setThemeKind(message.data.themeValue);
8489
setCompDescriptions(message.data.compDescriptions);
8590
setRegistries(message.data.registries);
8691
setFilteredcompDescriptions(getFilteredCompDesc(message.data.registries, message.data.compDescriptions, searchValue));
@@ -90,6 +95,8 @@ export const Home: React.FC<DefaultProps> = ({ }) => {
9095
setFilteredcompDescriptions([]);
9196
setCompDescriptions([]);
9297
setSearchValue('');
98+
} else if(message.data.action === 'setTheme') {
99+
setThemeKind(message.data.themeValue);
93100
}
94101
});
95102
});
@@ -130,7 +137,7 @@ export const Home: React.FC<DefaultProps> = ({ }) => {
130137
}}
131138
/>
132139
}
133-
<HomeItem compDescriptions={filteredcompDescriptions} />
140+
<HomeItem compDescriptions={filteredcompDescriptions} themeKind={themeKind} />
134141
{error?.length > 0 ? <ErrorPage message={error} /> : null}
135142
</>
136143
:

src/webview/devfile-registry/app/wrapperCardItem.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface DevFileProps extends React.AnchorHTMLAttributes<HTMLAnchorEleme
1111
compDescription: ComponentTypeDescription;
1212
hasGitLink: boolean;
1313
cardItemStyle: any;
14+
themeKind: number;
1415
projectDisplayStyle: any
1516
}
1617

src/webview/devfile-registry/registryViewLoader.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ export default class RegistryViewLoader {
118118
await RegistryViewLoader.loadView(`Devfile Registry - ${context.name}`, context.url);
119119
}
120120

121+
@vsCommand('openshift.componentTypesView.registry.setTheme')
122+
static async setTheme(kind: vscode.ColorThemeKind): Promise<void> {
123+
if (panel) {
124+
panel.webview.postMessage({ action: 'setTheme', themeValue: kind });
125+
}
126+
}
127+
121128
@vsCommand('openshift.componentTypesView.registry.closeView')
122129
static async closeRegistryInWebview(): Promise<void> {
123130
panel?.dispose();
@@ -149,6 +156,7 @@ function getAllComponents(eventActionName: string, url?: string, error?: string)
149156
action: eventActionName,
150157
compDescriptions: Array.from(componentDescriptions),
151158
registries: registries,
159+
themeValue: vscode.window.activeColorTheme.kind,
152160
errorMessage: error
153161
}
154162
);

0 commit comments

Comments
 (0)