Skip to content

Commit 417ef36

Browse files
feat(ui): remove percentage logic from usePanel
This unused logic was unnecessarily complicating the hook. It also inadvertently made the default panel size arg a percentage value even if it was actually a pixel value. Cleaned up a couple other little bits.
1 parent 7c53812 commit 417ef36

File tree

4 files changed

+55
-75
lines changed

4 files changed

+55
-75
lines changed

invokeai/frontend/web/src/features/gallery/components/GalleryPanelContent.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,16 @@ const GalleryPanelContent = () => {
2525
const boardSearchText = useAppSelector(selectBoardSearchText);
2626
const dispatch = useAppDispatch();
2727
const boardSearchDisclosure = useDisclosure({ defaultIsOpen: !!boardSearchText.length });
28-
const panelGroupRef = useRef<ImperativePanelGroupHandle>(null);
28+
const imperativePanelGroupRef = useRef<ImperativePanelGroupHandle>(null);
2929
const ref = useRef<HTMLDivElement>(null);
3030
useScopeOnFocus('gallery', ref);
3131

3232
const boardsListPanelOptions = useMemo<UsePanelOptions>(
3333
() => ({
3434
id: 'boards-list-panel',
35-
unit: 'pixels',
3635
minSize: 128,
37-
defaultSize: 20,
38-
panelGroupRef,
36+
defaultSize: 208,
37+
imperativePanelGroupRef,
3938
panelGroupDirection: 'vertical',
4039
}),
4140
[]
@@ -81,7 +80,7 @@ const GalleryPanelContent = () => {
8180
</Flex>
8281
</Flex>
8382

84-
<PanelGroup ref={panelGroupRef} direction="vertical" autoSaveId="boards-list-panel">
83+
<PanelGroup ref={imperativePanelGroupRef} direction="vertical" autoSaveId="boards-list-panel">
8584
<Panel collapsible {...boardsListPanel.panelProps}>
8685
<Flex flexDir="column" w="full" h="full">
8786
<Collapse in={boardSearchDisclosure.isOpen} style={COLLAPSE_STYLES}>

invokeai/frontend/web/src/features/ui/components/AppContent.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ import { selectActiveTab } from 'features/ui/store/uiSelectors';
2121
import {
2222
$isLeftPanelOpen,
2323
$isRightPanelOpen,
24-
LEFT_PANEL_MIN_SIZE_PCT,
2524
LEFT_PANEL_MIN_SIZE_PX,
26-
RIGHT_PANEL_MIN_SIZE_PCT,
2725
RIGHT_PANEL_MIN_SIZE_PX,
2826
selectWithLeftPanel,
2927
selectWithRightPanel,
@@ -45,16 +43,15 @@ export const AppContent = memo(() => {
4543
const ref = useRef<HTMLDivElement>(null);
4644
useScopeOnFocus('gallery', ref);
4745

48-
const panelGroupRef = useRef<ImperativePanelGroupHandle>(null);
46+
const imperativePanelGroupRef = useRef<ImperativePanelGroupHandle>(null);
4947

5048
const withLeftPanel = useAppSelector(selectWithLeftPanel);
5149
const leftPanelUsePanelOptions = useMemo<UsePanelOptions>(
5250
() => ({
5351
id: 'left-panel',
54-
unit: 'pixels',
5552
minSize: LEFT_PANEL_MIN_SIZE_PX,
56-
defaultSize: LEFT_PANEL_MIN_SIZE_PCT,
57-
panelGroupRef,
53+
defaultSize: LEFT_PANEL_MIN_SIZE_PX,
54+
imperativePanelGroupRef,
5855
panelGroupDirection: 'horizontal',
5956
onCollapse: onLeftPanelCollapse,
6057
}),
@@ -73,10 +70,9 @@ export const AppContent = memo(() => {
7370
const rightPanelUsePanelOptions = useMemo<UsePanelOptions>(
7471
() => ({
7572
id: 'right-panel',
76-
unit: 'pixels',
7773
minSize: RIGHT_PANEL_MIN_SIZE_PX,
78-
defaultSize: RIGHT_PANEL_MIN_SIZE_PCT,
79-
panelGroupRef,
74+
defaultSize: RIGHT_PANEL_MIN_SIZE_PX,
75+
imperativePanelGroupRef,
8076
panelGroupDirection: 'horizontal',
8177
onCollapse: onRightPanelCollapse,
8278
}),
@@ -127,7 +123,7 @@ export const AppContent = memo(() => {
127123
<VerticalNavBar />
128124
<Flex position="relative" w="full" h="full" gap={4} minW={0}>
129125
<PanelGroup
130-
ref={panelGroupRef}
126+
ref={imperativePanelGroupRef}
131127
id="app-panel-group"
132128
autoSaveId="app-panel-group"
133129
direction="horizontal"

invokeai/frontend/web/src/features/ui/hooks/usePanel.ts

Lines changed: 44 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,30 @@ import { getPanelGroupElement, getResizeHandleElementsForGroup } from 'react-res
1212

1313
type Direction = 'horizontal' | 'vertical';
1414

15-
export type UsePanelOptions =
16-
| {
17-
id: string;
18-
/**
19-
* The minimum size of the panel as a percentage.
20-
*/
21-
minSize: number;
22-
/**
23-
* The default size of the panel as a percentage.
24-
*/
25-
defaultSize?: number;
26-
/**
27-
* The unit of the minSize
28-
*/
29-
unit: 'percentages';
30-
onCollapse?: (isCollapsed: boolean) => void;
31-
}
32-
| {
33-
id: string;
34-
/**
35-
* The minimum size of the panel in pixels.
36-
*/
37-
minSize: number;
38-
/**
39-
* The default size of the panel in pixels.
40-
*/
41-
defaultSize?: number;
42-
/**
43-
* The unit of the minSize.
44-
*/
45-
unit: 'pixels';
46-
/**
47-
* The direction of the panel group.
48-
* This is required to accurately calculate the available space for the panel, minus the space taken by the handles.
49-
*/
50-
panelGroupDirection: Direction;
51-
/**
52-
* A ref to the panel group.
53-
*/
54-
panelGroupRef: RefObject<ImperativePanelGroupHandle>;
55-
onCollapse?: (isCollapsed: boolean) => void;
56-
};
15+
export type UsePanelOptions = {
16+
id: string;
17+
/**
18+
* The minimum size of the panel in pixels.
19+
*/
20+
minSize: number;
21+
/**
22+
* The default size of the panel in pixels.
23+
*/
24+
defaultSize?: number;
25+
/**
26+
* The direction of the panel group.
27+
* This is required to accurately calculate the available space for the panel, minus the space taken by the handles.
28+
*/
29+
panelGroupDirection: Direction;
30+
/**
31+
* A ref to the panel group.
32+
*/
33+
imperativePanelGroupRef: RefObject<ImperativePanelGroupHandle>;
34+
/**
35+
* Called when the board's collapsed state changes.
36+
*/
37+
onCollapse?: (isCollapsed: boolean) => void;
38+
};
5739

5840
export type UsePanelReturn = {
5941
/**
@@ -80,22 +62,28 @@ export type UsePanelReturn = {
8062
* Resize the panel to the given size in the same units as the minSize.
8163
*/
8264
resize: (size: number) => void;
65+
/**
66+
* The props to apply to the panel.
67+
*/
8368
panelProps: Partial<PanelProps & { ref: RefObject<ImperativePanelHandle> }>;
69+
/**
70+
* The props to apply to the resize handle.
71+
*/
8472
resizeHandleProps: Partial<PanelResizeHandleProps>;
8573
};
8674

8775
export const usePanel = (arg: UsePanelOptions): UsePanelReturn => {
8876
const imperativePanelRef = useRef<ImperativePanelHandle>(null);
89-
const [_minSize, _setMinSize] = useState<number>(arg.unit === 'percentages' ? arg.minSize : 0);
90-
const [_defaultSize, _setDefaultSize] = useState<number>(arg.defaultSize ?? arg.minSize);
77+
const [_minSize, _setMinSize] = useState<number>(0);
78+
const [_defaultSize, _setDefaultSize] = useState<number>(0);
9179

9280
// If the units are pixels, we need to calculate the min size as a percentage of the available space,
9381
// then resize the panel if it is too small.
9482
useLayoutEffect(() => {
95-
if (arg.unit === 'percentages' || !arg.panelGroupRef.current) {
83+
if (!arg.imperativePanelGroupRef.current) {
9684
return;
9785
}
98-
const id = arg.panelGroupRef.current.getId();
86+
const id = arg.imperativePanelGroupRef.current.getId();
9987
const panelGroupElement = getPanelGroupElement(id);
10088
const panelGroupHandleElements = getResizeHandleElementsForGroup(id);
10189
if (!panelGroupElement) {
@@ -106,7 +94,12 @@ export const usePanel = (arg: UsePanelOptions): UsePanelReturn => {
10694
return;
10795
}
10896

109-
const minSizePct = getSizeAsPercentage(arg.minSize, arg.panelGroupRef, arg.panelGroupDirection);
97+
const minSizePct = getSizeAsPercentage(arg.minSize, arg.imperativePanelGroupRef, arg.panelGroupDirection);
98+
const defaultSizePct = getSizeAsPercentage(
99+
arg.defaultSize || arg.minSize,
100+
arg.imperativePanelGroupRef,
101+
arg.panelGroupDirection
102+
);
110103

111104
if (minSizePct > 100) {
112105
// This can happen when the panel is hidden
@@ -115,8 +108,8 @@ export const usePanel = (arg: UsePanelOptions): UsePanelReturn => {
115108

116109
_setMinSize(minSizePct);
117110

118-
if (arg.defaultSize && arg.defaultSize > minSizePct) {
119-
_setDefaultSize(arg.defaultSize);
111+
if (defaultSizePct && defaultSizePct > minSizePct) {
112+
_setDefaultSize(defaultSizePct);
120113
} else {
121114
_setDefaultSize(minSizePct);
122115
}
@@ -181,14 +174,8 @@ export const usePanel = (arg: UsePanelOptions): UsePanelReturn => {
181174

182175
const resize = useCallback(
183176
(size: number) => {
184-
// If we are using percentages, we can just resize to the given size
185-
if (arg.unit === 'percentages') {
186-
imperativePanelRef.current?.resize(size);
187-
return;
188-
}
189-
190-
// If we are using pixels, we need to calculate the size as a percentage of the available space
191-
const sizeAsPct = getSizeAsPercentage(size, arg.panelGroupRef, arg.panelGroupDirection);
177+
// We need to calculate the size as a percentage of the available space
178+
const sizeAsPct = getSizeAsPercentage(size, arg.imperativePanelGroupRef, arg.panelGroupDirection);
192179
imperativePanelRef.current?.resize(sizeAsPct);
193180
},
194181
[arg]

invokeai/frontend/web/src/features/ui/store/uiSlice.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,12 @@ export const uiPersistConfig: PersistConfig<UIState> = {
8282
persistDenylist: ['shouldShowImageDetails'],
8383
};
8484

85-
export const LEFT_PANEL_MIN_SIZE_PX = 400;
86-
export const LEFT_PANEL_MIN_SIZE_PCT = 20;
8785
const TABS_WITH_LEFT_PANEL: TabName[] = ['canvas', 'upscaling', 'workflows'] as const;
86+
export const LEFT_PANEL_MIN_SIZE_PX = 400;
8887
export const $isLeftPanelOpen = atom(true);
8988
export const selectWithLeftPanel = createSelector(selectUiSlice, (ui) => TABS_WITH_LEFT_PANEL.includes(ui.activeTab));
9089

9190
const TABS_WITH_RIGHT_PANEL: TabName[] = ['canvas', 'upscaling', 'workflows'] as const;
9291
export const RIGHT_PANEL_MIN_SIZE_PX = 390;
93-
export const RIGHT_PANEL_MIN_SIZE_PCT = 20;
9492
export const $isRightPanelOpen = atom(true);
9593
export const selectWithRightPanel = createSelector(selectUiSlice, (ui) => TABS_WITH_RIGHT_PANEL.includes(ui.activeTab));

0 commit comments

Comments
 (0)