Skip to content

Commit

Permalink
Fix expansion and field highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
haydar-metin committed Dec 9, 2024
1 parent 8042783 commit 8d6b3da
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 46 deletions.
9 changes: 4 additions & 5 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ export class PeripheralCommands {
try {
const result = await node.performUpdate(value);
if (result) {
await this.peripheralsForceRefresh();
} else {
this.dataTracker.refresh();
// Update the tree view
this.dataTracker.fireOnDidChange();
}

} catch (error) {
Expand Down Expand Up @@ -75,7 +74,7 @@ export class PeripheralCommands {
}

node.format = result.value;
this.dataTracker.refresh();
this.dataTracker.fireOnDidChange();
}

private async peripheralsForceRefresh(node?: PeripheralBaseNode): Promise<void> {
Expand All @@ -85,7 +84,7 @@ export class PeripheralCommands {
await p.updateData();
}

this.dataTracker.refresh();
this.dataTracker.fireOnDidChange();
} else {
await this.dataTracker.updateData();
}
Expand Down
64 changes: 35 additions & 29 deletions src/components/tree/components/treetable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,41 @@ export const AntDComponentTreeTable = <T,>(props: ComponentTreeTableProps<T>) =>
return props.expansion?.expandedRowKeys;
}, [props.expansion?.expandedRowKeys]);


const expandIcon = useCallback(
({ expanded, onExpand, record, expandable }: RenderExpandIconProps<CDTTreeItem>) => {
if (!expandable) {
// simulate spacing to the left that we gain through expand icon so that leaf items look correctly intended
return <span className='leaf-item-spacer' />;
}

const doExpand = (event: React.MouseEvent<HTMLElement>) => {
event.stopPropagation();
onExpand(record, event);
};

const iconClass = expanded ? 'codicon-chevron-down' : 'codicon-chevron-right';
return (
<div
className={classNames('tree-toggler-container', 'codicon', iconClass)}
onClick={doExpand}
role="button"
tabIndex={0}
aria-label={expanded ? 'Collapse row' : 'Expand row'}
onKeyDown={event => { if (event.key === 'Enter' || event.key === ' ') doExpand(event as unknown as React.MouseEvent<HTMLElement>); }}
></div>
);
},
[]
);

const handleExpand = useCallback(
(expanded: boolean, record: CDTTreeItem) => {
props.expansion?.onExpand?.(expanded, record);
},
[props.expansion]
);

// ==== Renderers ====

const renderStringColumn = useCallback(
Expand Down Expand Up @@ -221,13 +256,6 @@ export const AntDComponentTreeTable = <T,>(props: ComponentTreeTableProps<T>) =>

// ==== Handlers ====

const handleExpand = useCallback(
(expanded: boolean, record: CDTTreeItem) => {
props.expansion?.onExpand?.(expanded, record);
},
[props.expansion]
);

const handleRowClick = useCallback(
(record: CDTTreeItem) => () => {
const isExpanded = props.expansion?.expandedRowKeys?.includes(record.id);
Expand All @@ -236,28 +264,6 @@ export const AntDComponentTreeTable = <T,>(props: ComponentTreeTableProps<T>) =>
[props.expansion]
);

const expandIcon = useCallback(
({ expanded, onExpand, record, expandable }: RenderExpandIconProps<CDTTreeItem>) => {
if (!expandable) {
// simulate spacing to the left that we gain through expand icon so that leaf items look correctly intended
return <span className='leaf-item-spacer' />;
}

const iconClass = expanded ? 'codicon-chevron-down' : 'codicon-chevron-right';
return (
<div
className={classNames('tree-toggler-container', 'codicon', iconClass)}
onClick={(e) => onExpand(record, e)}
role="button"
tabIndex={0}
aria-label={expanded ? 'Collapse row' : 'Expand row'}
onKeyDown={event => { if (event.key === 'Enter' || event.key === ' ') onExpand(record, event as unknown as React.MouseEvent<HTMLElement>); }}
></div>
);
},
[]
);

// ==== Return ====

return <div>
Expand Down
5 changes: 3 additions & 2 deletions src/plugin/peripheral/nodes/peripheral-field-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class PeripheralFieldNode extends PeripheralBaseNode {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private enumerationMap: any;
private previousValue?: number;
private currentValue?: number;

constructor(public parent: PeripheralRegisterNode, protected options: FieldOptions) {
super(parent);
Expand Down Expand Up @@ -110,7 +111,6 @@ export class PeripheralFieldNode extends PeripheralBaseNode {
if (val === undefined) {
return false;
}

const numval = this.enumerationMap[val.label];
this.parent.updateBits(this.offset, this.width, numval).then(resolve, reject);
});
Expand All @@ -129,7 +129,8 @@ export class PeripheralFieldNode extends PeripheralBaseNode {
}

public updateData(): Thenable<boolean> {
this.previousValue = this.getCurrentValue();
this.previousValue = this.currentValue;
this.currentValue = this.getCurrentValue();
return Promise.resolve(true);
}

Expand Down
4 changes: 2 additions & 2 deletions src/plugin/peripheral/nodes/peripheral-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ export class PeripheralNode extends PeripheralBaseNode {
try {
const errors = await this.readMemory();
for (const error of errors) {
const str = `Failed to update peripheral ${this.name}: ${error}`;
const str = `Failed to read peripheral ${this.name}: ${error}`;
if (vscode.debug.activeDebugConsole) {
vscode.debug.activeDebugConsole.appendLine(str);
}
}
} catch (e) {
/* This should never happen */
const msg = (e as Error).message || 'unknown error';
const str = `Failed to update peripheral ${this.name}: ${msg}`;
const str = `Failed to read peripheral ${this.name}: ${msg}`;
if (vscode.debug.activeDebugConsole) {
vscode.debug.activeDebugConsole.appendLine(str);
}
Expand Down
1 change: 0 additions & 1 deletion src/plugin/peripheral/nodes/peripheral-register-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export class PeripheralRegisterNode extends ClusterOrRegisterBaseNode {
private hexRegex: RegExp;
private binaryRegex: RegExp;
private currentValue: number;
private prevValue = '';
private previousValue?: number;

constructor(public parent: PeripheralNode | PeripheralClusterNode, protected options: PeripheralRegisterOptions) {
Expand Down
14 changes: 7 additions & 7 deletions src/plugin/peripheral/tree/peripheral-data-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class PeripheralDataTracker {
const children = await tree.getChildren();
children.forEach(c => this.collapseNode(c, false));
}
this.refresh();
this.fireOnDidChange();
}

public toggleNode(
Expand Down Expand Up @@ -152,12 +152,12 @@ export class PeripheralDataTracker {
public async updateData(): Promise<void> {
const trees = this.sessionPeripherals.values();
for (const tree of trees) {
// The tree will trigger a refresh on it's own
await tree.updateData();
}
this.refresh();
}

public refresh(): void {
public fireOnDidChange(): void {
this.onDidChangeEvent.fire();
}

Expand All @@ -170,7 +170,7 @@ export class PeripheralDataTracker {
}

if (this.sessionPeripherals.get(session.id)) {
this.refresh();
this.fireOnDidChange();
vscode.debug.activeDebugConsole.appendLine(`Internal Error: Session ${session.name} id=${session.id} already in the tree view?`);
return;
}
Expand All @@ -180,7 +180,7 @@ export class PeripheralDataTracker {
state = this.sessionPeripherals.size === 0 ? vscode.TreeItemCollapsibleState.Expanded : vscode.TreeItemCollapsibleState.Collapsed;
}
const peripheralTree = new PeripheralTreeForSession(session, this.api, state, () => {
this.refresh();
this.fireOnDidChange();
});

this.sessionPeripherals.set(session.id, peripheralTree);
Expand All @@ -195,7 +195,7 @@ export class PeripheralDataTracker {
} catch (e) {
vscode.debug.activeDebugConsole.appendLine(`Internal Error: Unexpected rejection of promise ${e}`);
} finally {
this.refresh();
this.fireOnDidChange();
}

vscode.commands.executeCommand('setContext', `${PeripheralTreeDataProvider.viewName}.hasData`, this.sessionPeripherals.size > 0);
Expand All @@ -211,7 +211,7 @@ export class PeripheralDataTracker {
this.oldState.set(session.name, regs.myTreeItem.collapsibleState);
this.sessionPeripherals.delete(session.id);
regs.sessionTerminated(this.context);
this.refresh();
this.fireOnDidChange();
}

vscode.commands.executeCommand('setContext', `${PeripheralTreeDataProvider.viewName}.hasData`, this.sessionPeripherals.size > 0);
Expand Down

0 comments on commit 8d6b3da

Please sign in to comment.