Skip to content

Commit 689a38f

Browse files
committed
State gallery: Fix reinitialization after controls hidden and re-rendered
1 parent 2084712 commit 689a38f

File tree

3 files changed

+20
-37
lines changed

3 files changed

+20
-37
lines changed

src/app/extensions/state-gallery/ui.tsx

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,48 +25,33 @@ type Values = PD.ValuesFor<typeof Params>
2525

2626
export class StateGalleryControls extends CollapsableControls<{}, StateGalleryControlsState> {
2727
protected defaultState(): StateGalleryControlsState & CollapsableState {
28+
const existingManager = StateGalleryCustomState(this.plugin).manager?.value;
2829
return {
2930
// CollapsableControls state
3031
header: '3D State Gallery',
3132
brand: { accent: 'green', svg: CollectionsOutlinedSvg },
3233
isCollapsed: false,
3334
// Own state
34-
entryId: '',
35-
manager: undefined,
35+
entryId: existingManager?.entryId ?? '',
36+
manager: existingManager,
3637
isLoading: false,
3738
isBusy: false,
39+
description: existingManager?.entryId.toUpperCase(),
3840
};
3941
}
4042

4143
componentDidMount() {
4244
this.subscribe(this.plugin.managers.structure.hierarchy.behaviors.selection, sel => {
4345
if (this.state.entryId === '' && sel.structures.length > 0) {
4446
const id = sel.structures[0].cell.obj?.data.model.entryId;
45-
if (id) this.setEntryId(id.toLowerCase());
47+
if (id) {
48+
this.setEntryId(id.toLowerCase());
49+
}
4650
}
47-
// this.setState({
48-
// isHidden: !this.canEnable(),
49-
// description: StructureHierarchyManager.getSelectedStructuresDescription(this.plugin),
50-
// description: this.state.entryId,
51-
// });
5251
});
53-
// this.subscribe(this.plugin.state.events.cell.stateUpdated, e => {
54-
// if (e.cell.transform.transformer === AssemblySymmetry3D) this.forceUpdate();
55-
// });
5652
this.subscribe(this.plugin.behaviors.state.isBusy, isBusy => this.setState({ isBusy }));
5753
}
5854

59-
// private get pivot() {
60-
// const structures = this.plugin.managers.structure.hierarchy.selection.structures;
61-
// if (structures.length === 1) {
62-
// return this.plugin.managers.structure.hierarchy.selection.structures[0];
63-
// } else {
64-
// return undefined;
65-
// }
66-
// }
67-
// private canEnable() {
68-
// return isApplicable(this.pivot?.cell.obj?.data);
69-
// }
7055
private get values(): Values {
7156
return {
7257
entryId: this.state.entryId,
@@ -75,24 +60,25 @@ export class StateGalleryControls extends CollapsableControls<{}, StateGalleryCo
7560
private setEntryId = (entryId: string) => {
7661
this.setState(old => ({
7762
entryId,
78-
manager: (entryId === old.manager?.entryId) ? old.manager : undefined,
7963
}));
8064
};
8165
private load = async () => {
66+
if (this.loadDisabled()) return;
8267
this.setState({ isLoading: true, description: undefined });
8368
const manager = await StateGalleryManager.create(this.plugin, this.state.entryId);
8469
this.setState({ manager, isLoading: false, description: this.state.entryId.toUpperCase() });
8570
};
8671
private onChangeValues = (values: Values) => {
8772
this.setEntryId(values.entryId);
8873
};
74+
private loadDisabled = () => !this.state.entryId || this.state.entryId === this.state.manager?.entryId || this.state.isBusy || this.state.isLoading;
8975

9076
protected renderControls(): React.JSX.Element | null {
9177
return <>
9278
<ParameterControls params={Params} values={this.values} onChangeValues={this.onChangeValues} onEnter={this.load} />
93-
{!this.state.manager &&
79+
{(!this.state.manager || this.state.manager.entryId !== this.state.entryId) &&
9480
<Button icon={this.state.isLoading ? undefined : CheckSvg} title='Load'
95-
disabled={!this.state.entryId || this.state.isBusy || this.state.isLoading} onClick={this.load} className='msp-btn-block'>
81+
disabled={this.loadDisabled()} onClick={this.load} className='msp-btn-block'>
9682
{this.state.isLoading ? 'Loading...' : 'Load'}
9783
</Button>
9884
}
@@ -111,25 +97,24 @@ function ManagerControls(props: { manager: StateGalleryManager }) {
11197
const [selected, setSelected] = React.useState<Image | undefined>(undefined);
11298
const [status, setStatus] = React.useState<LoadingStatus>('ready');
11399

100+
const keyDownTargetRef = React.useRef<HTMLDivElement>(null);
101+
const handleKeyDown = async (e: React.KeyboardEvent) => {
102+
if (e.code === 'ArrowLeft') await props.manager.loadPrevious();
103+
if (e.code === 'ArrowRight') await props.manager.loadNext();
104+
};
105+
114106
React.useEffect(() => {
115-
if (images.length > 0) {
107+
if (images.length > 0 && props.manager.events.requestedImage.value === undefined) {
116108
props.manager.load(images[0]);
117109
}
110+
keyDownTargetRef.current?.focus();
118111
const subs = [
119112
props.manager.events.status.subscribe(status => setStatus(status)),
120113
props.manager.events.requestedImage.subscribe(img => setSelected(img)),
121114
];
122115
return () => subs.forEach(sub => sub.unsubscribe());
123116
}, [props.manager]);
124117

125-
const keyDownTargetRef = React.useRef<HTMLDivElement>(null);
126-
React.useEffect(() => keyDownTargetRef.current?.focus(), []);
127-
128-
const handleKeyDown = (e: React.KeyboardEvent) => {
129-
if (e.code === 'ArrowLeft') props.manager.loadPrevious();
130-
if (e.code === 'ArrowRight') props.manager.loadNext();
131-
};
132-
133118
if (nImages === 0) {
134119
return <div style={{ margin: 8 }}>No data available for {props.manager.entryId}.</div>;
135120
}

src/app/styles/index.scss

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ svg.pdbe-animated-logo {
100100
}
101101

102102
.pdbemolstar-state-gallery-title-box {
103-
// line-height: 32px; // line-height: $row-height;
104-
width: 100%; // 400px;
103+
width: 500px;
105104
max-width: 100%;
106105
background-color: #99999930; // background-color: $msp-form-control-background;
107106
}

src/app/ui/pdbe-viewport.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ export class CustomizableDefaultViewport extends DefaultViewport {
3939
</div>
4040
</div>
4141
</div>
42-
{/* <SelectionViewportControls /> */}
4342
<VPControls />
4443
<BackgroundTaskProgress />
4544
<div className='msp-highlight-toast-wrapper'>

0 commit comments

Comments
 (0)