@@ -25,48 +25,33 @@ type Values = PD.ValuesFor<typeof Params>
25
25
26
26
export class StateGalleryControls extends CollapsableControls < { } , StateGalleryControlsState > {
27
27
protected defaultState ( ) : StateGalleryControlsState & CollapsableState {
28
+ const existingManager = StateGalleryCustomState ( this . plugin ) . manager ?. value ;
28
29
return {
29
30
// CollapsableControls state
30
31
header : '3D State Gallery' ,
31
32
brand : { accent : 'green' , svg : CollectionsOutlinedSvg } ,
32
33
isCollapsed : false ,
33
34
// Own state
34
- entryId : '' ,
35
- manager : undefined ,
35
+ entryId : existingManager ?. entryId ?? '' ,
36
+ manager : existingManager ,
36
37
isLoading : false ,
37
38
isBusy : false ,
39
+ description : existingManager ?. entryId . toUpperCase ( ) ,
38
40
} ;
39
41
}
40
42
41
43
componentDidMount ( ) {
42
44
this . subscribe ( this . plugin . managers . structure . hierarchy . behaviors . selection , sel => {
43
45
if ( this . state . entryId === '' && sel . structures . length > 0 ) {
44
46
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
+ }
46
50
}
47
- // this.setState({
48
- // isHidden: !this.canEnable(),
49
- // description: StructureHierarchyManager.getSelectedStructuresDescription(this.plugin),
50
- // description: this.state.entryId,
51
- // });
52
51
} ) ;
53
- // this.subscribe(this.plugin.state.events.cell.stateUpdated, e => {
54
- // if (e.cell.transform.transformer === AssemblySymmetry3D) this.forceUpdate();
55
- // });
56
52
this . subscribe ( this . plugin . behaviors . state . isBusy , isBusy => this . setState ( { isBusy } ) ) ;
57
53
}
58
54
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
- // }
70
55
private get values ( ) : Values {
71
56
return {
72
57
entryId : this . state . entryId ,
@@ -75,24 +60,25 @@ export class StateGalleryControls extends CollapsableControls<{}, StateGalleryCo
75
60
private setEntryId = ( entryId : string ) => {
76
61
this . setState ( old => ( {
77
62
entryId,
78
- manager : ( entryId === old . manager ?. entryId ) ? old . manager : undefined ,
79
63
} ) ) ;
80
64
} ;
81
65
private load = async ( ) => {
66
+ if ( this . loadDisabled ( ) ) return ;
82
67
this . setState ( { isLoading : true , description : undefined } ) ;
83
68
const manager = await StateGalleryManager . create ( this . plugin , this . state . entryId ) ;
84
69
this . setState ( { manager, isLoading : false , description : this . state . entryId . toUpperCase ( ) } ) ;
85
70
} ;
86
71
private onChangeValues = ( values : Values ) => {
87
72
this . setEntryId ( values . entryId ) ;
88
73
} ;
74
+ private loadDisabled = ( ) => ! this . state . entryId || this . state . entryId === this . state . manager ?. entryId || this . state . isBusy || this . state . isLoading ;
89
75
90
76
protected renderControls ( ) : React . JSX . Element | null {
91
77
return < >
92
78
< 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 ) &&
94
80
< 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' >
96
82
{ this . state . isLoading ? 'Loading...' : 'Load' }
97
83
</ Button >
98
84
}
@@ -111,25 +97,24 @@ function ManagerControls(props: { manager: StateGalleryManager }) {
111
97
const [ selected , setSelected ] = React . useState < Image | undefined > ( undefined ) ;
112
98
const [ status , setStatus ] = React . useState < LoadingStatus > ( 'ready' ) ;
113
99
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
+
114
106
React . useEffect ( ( ) => {
115
- if ( images . length > 0 ) {
107
+ if ( images . length > 0 && props . manager . events . requestedImage . value === undefined ) {
116
108
props . manager . load ( images [ 0 ] ) ;
117
109
}
110
+ keyDownTargetRef . current ?. focus ( ) ;
118
111
const subs = [
119
112
props . manager . events . status . subscribe ( status => setStatus ( status ) ) ,
120
113
props . manager . events . requestedImage . subscribe ( img => setSelected ( img ) ) ,
121
114
] ;
122
115
return ( ) => subs . forEach ( sub => sub . unsubscribe ( ) ) ;
123
116
} , [ props . manager ] ) ;
124
117
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
-
133
118
if ( nImages === 0 ) {
134
119
return < div style = { { margin : 8 } } > No data available for { props . manager . entryId } .</ div > ;
135
120
}
0 commit comments