Skip to content

[896] Restore Popout content for blocked popouts and emit onDidBlockPopout event #928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5791,6 +5791,54 @@ describe('dockviewComponent', () => {
]);
});

describe('when browsers block popups', () => {
let container: HTMLDivElement;
let dockview: DockviewComponent;
let panel: DockviewPanel;

beforeEach(() => {
jest.spyOn(window, 'open').mockReturnValue(null);

container = document.createElement('div');

dockview = new DockviewComponent(container, {
createComponent(options) {
switch (options.name) {
case 'default':
return new PanelContentPartTest(
options.id,
options.name
);
default:
throw new Error(`unsupported`);
}
},
});

dockview.layout(1000, 500);

panel = dockview.addPanel({
id: 'panel_1',
component: 'default',
});
});

test('onDidBlockPopout event is emitted', async () => {
const onDidBlockPopoutHandler = jest.fn();
dockview.onDidBlockPopout(onDidBlockPopoutHandler);

await dockview.addPopoutGroup(panel.group);

expect(onDidBlockPopoutHandler).toHaveBeenCalledTimes(1);
});

test('popout group is restored to its original position', async () => {
await dockview.addPopoutGroup(panel.group);

expect(panel.group.api.location.type).toBe('grid');
});
});

test('dispose of dockview instance when popup is open', async () => {
const container = document.createElement('div');

Expand Down
4 changes: 4 additions & 0 deletions packages/dockview-core/src/api/component.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,10 @@ export class DockviewApi implements CommonApi<SerializedDockview> {
return this.component.onDidPopoutGroupPositionChange;
}

get onDidBlockPopout(): Event<void> {
return this.component.onDidBlockPopout;
}

/**
* All panel objects.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ export class Tabs extends CompositeDisposable {
delete(id: string): void {
const index = this.indexOf(id);
const tabToRemove = this._tabs.splice(index, 1)[0];
if (!tabToRemove) {
return;
}

const { value, disposable } = tabToRemove;

Expand Down
56 changes: 39 additions & 17 deletions packages/dockview-core/src/dockview/dockviewComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ export interface IDockviewComponent extends IBaseGrid<DockviewGroupPanel> {
readonly onDidMaximizedGroupChange: Event<DockviewMaximizedGroupChanged>;
readonly onDidPopoutGroupSizeChange: Event<PopoutGroupChangeSizeEvent>;
readonly onDidPopoutGroupPositionChange: Event<PopoutGroupChangePositionEvent>;
readonly onDidBlockPopout: Event<void>;
readonly options: DockviewComponentOptions;
updateOptions(options: DockviewOptions): void;
moveGroupOrPanel(options: MoveGroupOrPanelOptions): void;
Expand Down Expand Up @@ -319,6 +320,9 @@ export class DockviewComponent
readonly onDidPopoutGroupPositionChange: Event<PopoutGroupChangePositionEvent> =
this._onDidPopoutGroupPositionChange.event;

private readonly _onDidBlockPopout = new Emitter<void>();
readonly onDidBlockPopout: Event<void> = this._onDidBlockPopout.event;

private readonly _onDidLayoutFromJSON = new Emitter<void>();
readonly onDidLayoutFromJSON: Event<void> = this._onDidLayoutFromJSON.event;

Expand Down Expand Up @@ -505,6 +509,7 @@ export class DockviewComponent
this._onDidOptionsChange,
this._onDidPopoutGroupSizeChange,
this._onDidPopoutGroupPositionChange,
this._onDidBlockPopout,
this.onDidViewVisibilityChangeMicroTaskQueue(() => {
this.updateWatermark();
}),
Expand Down Expand Up @@ -713,28 +718,13 @@ export class DockviewComponent
return false;
}

if (popoutContainer === null) {
popoutWindowDisposable.dispose();
return false;
}

const gready = document.createElement('div');
gready.className = 'dv-overlay-render-container';

const overlayRenderContainer = new OverlayRenderContainer(
gready,
this
);

const referenceGroup =
itemToPopout instanceof DockviewPanel
? itemToPopout.group
: itemToPopout;

const referenceLocation = itemToPopout.api.location.type;

/**
* The group that is being added doesn't already exist within the DOM, the most likely occurance
* The group that is being added doesn't already exist within the DOM, the most likely occurrence
* of this case is when being called from the `fromJSON(...)` method
*/
const isGroupAddedToDom =
Expand All @@ -748,9 +738,41 @@ export class DockviewComponent
group = options.overridePopoutGroup;
} else {
group = this.createGroup({ id: groupId });
this._onDidAddGroup.fire(group);
if (popoutContainer) {
this._onDidAddGroup.fire(group);
}
}

if (popoutContainer === null) {
popoutWindowDisposable.dispose();
this._onDidBlockPopout.fire();

// if the popout window was blocked, we need to move the group back to the reference group
// and set it to visible
this.movingLock(() =>
moveGroupWithoutDestroying({
from: group,
to: referenceGroup,
})
);

if (!referenceGroup.api.isVisible) {
referenceGroup.api.setVisible(true);
}

return false;
}

const gready = document.createElement('div');
gready.className = 'dv-overlay-render-container';

const overlayRenderContainer = new OverlayRenderContainer(
gready,
this
);

const referenceLocation = itemToPopout.api.location.type;

group.model.renderContainer = overlayRenderContainer;
group.layout(
_window.window!.innerWidth,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ export const App = (props: { theme?: string }) => {

const load = (api: DockviewApi) => {
api.clear();
api.onDidBlockPopout(() => {
console.log('Popout blocked');
});
if (layout) {
try {
api.fromJSON(layout);
Expand Down