Skip to content

Commit

Permalink
Implement issue #20 (#64)
Browse files Browse the repository at this point in the history
* Add CTRL+[ Hotkey to expand/collapse the size of the chat-bar

* Move state variables inside the ResizeAuxBarWidth action

* Remove promise and refactor label name

---------

Co-authored-by: Duke Pan <[email protected]>
  • Loading branch information
2 people authored and nang-dev committed Jan 27, 2025
1 parent e14d17b commit a32bcf6
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions src/vs/workbench/browser/parts/auxiliarybar/auxiliaryBarActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,85 @@ registerAction2(class extends SwitchCompositeViewAction {
}, ViewContainerLocation.AuxiliaryBar, 1);
}
});

export class ResizeAuxiliaryBarWidthAction extends Action2 {
static readonly ID = 'workbench.action.resizeAuxiliaryBarWidth';
static readonly LABEL = localize2('resizeAuxiliaryBarWidth', "Resize Auxiliary Bar Width");

// Tracking the previous width of the aux bar and visibility of the left side bar
static _previousAuxiliaryBarWidth: number | null = null;
static _previousSideBarVisibility: boolean | null = null;

constructor() {
super({
id: ResizeAuxiliaryBarWidthAction.ID,
title: ResizeAuxiliaryBarWidthAction.LABEL,
category: Categories.View,
f1: true,
keybinding: {
weight: KeybindingWeight.WorkbenchContrib,
primary: KeyMod.CtrlCmd | KeyCode.BracketLeft,
linux: {
primary: KeyMod.CtrlCmd | KeyCode.BracketLeft
},
win: {
primary: KeyMod.Alt | KeyCode.BracketLeft
},
mac: {
primary: KeyMod.CtrlCmd | KeyCode.BracketLeft
}
},
});
}

run(accessor: ServicesAccessor): void {
const layoutService = accessor.get(IWorkbenchLayoutService);
// Check if the main window is available
if (!mainWindow) {
return;
}

const auxBarPart = layoutService.getContainer(mainWindow, Parts.AUXILIARYBAR_PART);
const auxBarDimensions = auxBarPart?.getBoundingClientRect();
const isAuxiliaryBarVisible = layoutService.isVisible(Parts.AUXILIARYBAR_PART);

// If the auxiliary bar is not visible, or the dimensions are null, return
if (!auxBarDimensions || !isAuxiliaryBarVisible) {
return;
}

// Save the current width as the previous width if it has not been saved yet
if (ResizeAuxiliaryBarWidthAction._previousAuxiliaryBarWidth === null) {
ResizeAuxiliaryBarWidthAction._previousAuxiliaryBarWidth = auxBarDimensions.width;
ResizeAuxiliaryBarWidthAction._previousSideBarVisibility = layoutService.isVisible(Parts.SIDEBAR_PART);
}

// Set a minimum width for the auxiliary bar, unless its greater than a % of the window width
const PSEUDO_MINIMUM_AUX_BAR_WIDTH = 600;

// Calculate the minimum width for the auxiliary bar
// 70% of the window width is the maximum width
const maxWidth = (0.7 * mainWindow.innerWidth);
// The minimum width is the maximum width, unless it is less than the max of (previous width * 2) or the predetermined minimum width
const minWidth = Math.min(maxWidth, Math.max(ResizeAuxiliaryBarWidthAction._previousAuxiliaryBarWidth * 2, PSEUDO_MINIMUM_AUX_BAR_WIDTH));

// If the current width is less than or equal to the previous width, expand the auxiliary bar
if (auxBarDimensions.width <= ResizeAuxiliaryBarWidthAction._previousAuxiliaryBarWidth) {
// Expand to the calculated minWidth
layoutService.resizePart(Parts.AUXILIARYBAR_PART, (minWidth - auxBarDimensions.width), 0);
// Hide the left side bar if it was previously visible
layoutService.setPartHidden(true, Parts.SIDEBAR_PART);
} else {
// If the current width is greater than the previous width, collapse the auxiliary bar back to the previous width (initial width)
layoutService.resizePart(Parts.AUXILIARYBAR_PART, (auxBarDimensions.width - ResizeAuxiliaryBarWidthAction._previousAuxiliaryBarWidth) * -1, 0);
// Restore the left side bar to the user's previous state
ResizeAuxiliaryBarWidthAction._previousSideBarVisibility ? layoutService.setPartHidden(false, Parts.SIDEBAR_PART) : layoutService.setPartHidden(true, Parts.SIDEBAR_PART);
// Reset the previous width to null after collapsing
ResizeAuxiliaryBarWidthAction._previousAuxiliaryBarWidth = null;
}

return;
}
}

registerAction2(ResizeAuxiliaryBarWidthAction);

0 comments on commit a32bcf6

Please sign in to comment.