dockview-core
Version:
Zero dependency layout manager supporting tabs, groups, grids and splitviews for vanilla TypeScript
87 lines (86 loc) • 2.91 kB
JavaScript
import { CompositeDisposable, MutableDisposable, } from '../lifecycle';
import { defineModule } from './modules';
const SLOT_OPTION_KEY = {
left: 'createLeftHeaderActionComponent',
right: 'createRightHeaderActionComponent',
prefix: 'createPrefixHeaderActionComponent',
};
export class HeaderActionsService {
constructor(host) {
this._perGroup = new Map();
this._host = host;
}
refresh(group) {
// The headerPosition setter on DockviewGroupPanelModel fires inside
// the model's constructor — before the parent DockviewGroupPanel has
// assigned its `_model` field, and in tests where the parent panel
// may be null. Skip; DockviewGroupPanel.initialize() will refresh
// once construction completes for real groups.
if (!(group === null || group === void 0 ? void 0 : group.model)) {
return;
}
const state = this._ensureState(group);
this._refreshSlot('left', group, state.left);
this._refreshSlot('right', group, state.right);
this._refreshSlot('prefix', group, state.prefix);
}
refreshAll() {
for (const group of this._host.groups) {
this.refresh(group);
}
}
disposeGroup(group) {
const state = this._perGroup.get(group);
if (!state) {
return;
}
state.left.dispose();
state.right.dispose();
state.prefix.dispose();
this._perGroup.delete(group);
}
dispose() {
for (const group of [...this._perGroup.keys()]) {
this.disposeGroup(group);
}
}
_ensureState(group) {
let state = this._perGroup.get(group);
if (!state) {
state = {
left: new MutableDisposable(),
right: new MutableDisposable(),
prefix: new MutableDisposable(),
};
this._perGroup.set(group, state);
}
return state;
}
_refreshSlot(slot, group, disposable) {
const factory = this._host.options[SLOT_OPTION_KEY[slot]];
if (factory) {
const renderer = factory(group);
disposable.value = renderer;
renderer.init({
containerApi: this._host.api,
api: group.api,
group,
});
group.model.attachHeaderAction(slot, renderer.element);
}
else {
disposable.dispose();
group.model.attachHeaderAction(slot, undefined);
}
}
}
export const HeaderActionsModule = defineModule({
name: 'HeaderActions',
serviceKey: 'headerActionsService',
create: (host) => new HeaderActionsService(host),
init: (host, service) => {
return new CompositeDisposable(host.onDidRemoveGroup((group) => {
service.disposeGroup(group);
}));
},
});