Skip to content

Commit

Permalink
handle panel hide/show and send prefs onReady
Browse files Browse the repository at this point in the history
  • Loading branch information
kenneth-marut-work committed Sep 6, 2023
1 parent 30d8d64 commit dcf7d77
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 31 deletions.
2 changes: 0 additions & 2 deletions src/common/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ export type MemoryWriteResult = DebugProtocol.WriteMemoryResponse['body'];

export const readyType: NotificationType<void> = { method: 'ready' };
export const logMessageType: RequestType<string, void> = { method: 'logMessage' };
export const getMemoryDisplayConfigurationType: RequestType<void, MemoryDisplayConfiguration> = { method: 'getMemoryDisplayConfiguration' };
export const setMemoryDisplayConfigurationType: NotificationType<MemoryDisplayConfigurationChangeRequest> = { method: 'setMemoryDisplayConfiguration' };
export const memoryDisplayConfigurationChangedType: NotificationType<MemoryDisplayConfiguration> = { method: 'memoryDisplayConfigurationChanged' };
export const getColumnsVisibility: RequestType<void, ColumnVisibilityStatus[]> = { method: 'getColumnsVisibility' };
export const columnVisibilityType: NotificationType<ColumnVisibilityStatus> = { method: 'columnVisibility' };
export const setOptionsType: RequestType<Partial<DebugProtocol.ReadMemoryArguments | undefined>, void> = { method: 'setOptions' };
export const readMemoryType: RequestType<DebugProtocol.ReadMemoryArguments, MemoryReadResult> = { method: 'readMemory' };
Expand Down
38 changes: 19 additions & 19 deletions src/plugin/memory-webview-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ import {
memoryDisplayConfigurationChangedType,
columnVisibilityType,
setMemoryDisplayConfigurationType,
getColumnsVisibility,
getMemoryDisplayConfigurationType,
} from '../common/messaging';
import { MemoryProvider } from './memory-provider';
import { outputChannelLogger } from './logger';
Expand All @@ -52,6 +50,10 @@ enum RefreshEnum {
}

const isMemoryVariable = (variable: Variable): variable is Variable => variable && !!(variable as Variable).memoryReference;
const columnConfigurations = [
manifest.CONFIG_SHOW_ASCII_COLUMN,
manifest.CONFIG_SHOW_VARIABLES_COLUMN,
];

export class MemoryWebview {
public static ViewType = `${manifest.PACKAGE_NAME}.memory`;
Expand Down Expand Up @@ -142,13 +144,13 @@ export class MemoryWebview {
const participant = this.messenger.registerWebviewPanel(panel);

const disposables = [
this.messenger.onNotification(readyType, () => this.refresh(participant, options), { sender: participant }),
this.messenger.onNotification(readyType, () => {
this.refresh(participant, options);
}, { sender: participant }),
this.messenger.onRequest(logMessageType, message => outputChannelLogger.info('[webview]:', message), { sender: participant }),
this.messenger.onRequest(readMemoryType, request => this.readMemory(request), { sender: participant }),
this.messenger.onRequest(writeMemoryType, request => this.writeMemory(request), { sender: participant }),
this.messenger.onRequest(getVariables, request => this.getVariables(request), { sender: participant }),
this.messenger.onRequest(getMemoryDisplayConfigurationType, () => this.getMemoryDisplayConfiguration(), { sender: participant }),
this.messenger.onRequest(getColumnsVisibility, () => this.getColumnConfigurations(), { sender: participant }),
this.messenger.onNotification(setMemoryDisplayConfigurationType, request => this.setConfiguration(request), { sender: participant }),
this.messenger.onNotification(columnVisibilityType, request => this.handleColumnToggled(request), { sender: participant }),

Expand All @@ -160,7 +162,11 @@ export class MemoryWebview {
this.onMemoryDisplayConfigurationChanged(participant),
this.onColumnVisibilityConfigurationChanged(participant),
];

panel.onDidChangeViewState(newState => {
if (newState.webviewPanel.visible) {
this.refresh(participant, options);
}
});
panel.onDidDispose(() => disposables.forEach(disposible => disposible.dispose()));
}

Expand All @@ -176,6 +182,13 @@ export class MemoryWebview {

protected async refresh(participant: WebviewIdMessageParticipant, options?: Partial<DebugProtocol.ReadMemoryArguments>): Promise<void> {
this.messenger.sendRequest(setOptionsType, participant, options);
const memoryDisplayConfiguration = this.getMemoryDisplayConfiguration();
this.messenger.sendNotification(memoryDisplayConfigurationChangedType, participant, memoryDisplayConfiguration);
columnConfigurations.forEach(columnConfiguration => {
const [id] = columnConfiguration.split('Visible');
const active = vscode.workspace.getConfiguration(manifest.PACKAGE_NAME).get<boolean>(columnConfiguration) ?? true;
this.messenger.sendNotification(columnVisibilityType, participant, { id, active });
});
}

protected getMemoryDisplayConfiguration(): MemoryDisplayConfiguration {
Expand All @@ -199,10 +212,6 @@ export class MemoryWebview {
}

protected onColumnVisibilityConfigurationChanged(participant: MessageParticipant): vscode.Disposable {
const columnConfigurations = [
manifest.CONFIG_SHOW_ASCII_COLUMN,
manifest.CONFIG_SHOW_VARIABLES_COLUMN,
];
return vscode.workspace.onDidChangeConfiguration(e => {
columnConfigurations.forEach(configuration => {
if (e.affectsConfiguration(`${manifest.PACKAGE_NAME}.${configuration}`)) {
Expand All @@ -214,15 +223,6 @@ export class MemoryWebview {
});
}

protected getColumnConfigurations(): ColumnVisibilityStatus[] {
const COLUMN_CONFIGURATIONS = [manifest.CONFIG_SHOW_VARIABLES_COLUMN, manifest.CONFIG_SHOW_ASCII_COLUMN];
return COLUMN_CONFIGURATIONS.map(configuration => {
const [id] = configuration.split('Visible');
const active = vscode.workspace.getConfiguration(manifest.PACKAGE_NAME).get<boolean>(configuration) ?? false;
return { id, active };
});
}

protected async readMemory(request: DebugProtocol.ReadMemoryArguments): Promise<MemoryReadResult> {
try {
return await this.memoryProvider.readMemory(request);
Expand Down
6 changes: 1 addition & 5 deletions src/webview/components/memory-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import { MemoryTable } from './memory-table';
import { OptionsWidget } from './options-widget';
import { Decoration, Endianness, Memory, MemoryDisplayConfiguration } from '../utils/view-types';
import { messenger } from '../view-messenger';
import { getMemoryDisplayConfigurationType, memoryDisplayConfigurationChangedType } from '../../common/messaging';
import { HOST_EXTENSION } from 'vscode-messenger-common';
import { memoryDisplayConfigurationChangedType } from '../../common/messaging';
import { ColumnStatus } from '../columns/column-contribution-service';

interface MemoryWidgetProps {
Expand Down Expand Up @@ -56,9 +55,6 @@ export class MemoryWidget extends React.Component<MemoryWidgetProps, MemoryWidge

public componentDidMount(): void {
messenger.onNotification(memoryDisplayConfigurationChangedType, configuration => this.setState(configuration));
messenger.sendRequest(getMemoryDisplayConfigurationType, HOST_EXTENSION, undefined).then(configuration => {
this.setState(configuration);
});
}

override render(): React.ReactNode {
Expand Down
5 changes: 0 additions & 5 deletions src/webview/memory-webview-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
setOptionsType,
readMemoryType,
columnVisibilityType,
getColumnsVisibility
} from '../common/messaging';
import type { DebugProtocol } from '@vscode/debugprotocol';
import { ColumnVisibilityStatus, Decoration, Memory, MemoryState } from './utils/view-types';
Expand Down Expand Up @@ -67,10 +66,6 @@ class App extends React.Component<{}, MemoryAppState> {
messenger.onRequest(setOptionsType, options => this.setOptions(options));
messenger.sendNotification(readyType, HOST_EXTENSION, undefined);
messenger.onNotification(columnVisibilityType, request => this.handleColumnVisibilityChanged(request));
messenger.sendRequest(getColumnsVisibility, HOST_EXTENSION, undefined).then(columnsVisibility => {
columnsVisibility.forEach(columnVisibility => this.handleColumnVisibilityChanged(columnVisibility));
});

}

public render(): React.ReactNode {
Expand Down

0 comments on commit dcf7d77

Please sign in to comment.