Skip to content

Commit

Permalink
feat: simplify event
Browse files Browse the repository at this point in the history
  • Loading branch information
mathuo committed Feb 24, 2024
1 parent fd24604 commit 8533a18
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import { toggleClass } from '../../../dom';
import { DockviewPanel, IDockviewPanel } from '../../dockviewPanel';
import { DockviewComponent } from '../../dockviewComponent';
import { WillShowOverlayEvent } from '../../../dnd/droptarget';
import { DockviewGroupDropLocation } from '../../dockviewGroupPanelModel';
import {
DockviewGroupDropLocation,
WillShowOverlayLocationEvent,
} from '../../dockviewGroupPanelModel';

export interface TabDropIndexEvent {
readonly event: DragEvent;
Expand All @@ -35,10 +38,7 @@ export interface ITabsContainer extends IDisposable {
readonly onDrop: Event<TabDropIndexEvent>;
readonly onTabDragStart: Event<TabDragEvent>;
readonly onGroupDragStart: Event<GroupDragEvent>;
readonly onWillShowOverlay: Event<{
event: WillShowOverlayEvent;
kind: DockviewGroupDropLocation;
}>;
readonly onWillShowOverlay: Event<WillShowOverlayLocationEvent>;
hidden: boolean;
delete(id: string): void;
indexOf(id: string): number;
Expand Down Expand Up @@ -83,14 +83,10 @@ export class TabsContainer
readonly onGroupDragStart: Event<GroupDragEvent> =
this._onGroupDragStart.event;

private readonly _onWillShowOverlay = new Emitter<{
event: WillShowOverlayEvent;
kind: DockviewGroupDropLocation;
}>();
readonly onWillShowOverlay: Event<{
event: WillShowOverlayEvent;
kind: DockviewGroupDropLocation;
}> = this._onWillShowOverlay.event;
private readonly _onWillShowOverlay =
new Emitter<WillShowOverlayLocationEvent>();
readonly onWillShowOverlay: Event<WillShowOverlayLocationEvent> =
this._onWillShowOverlay.event;

get panels(): string[] {
return this.tabs.map((_) => _.value.panel.id);
Expand Down Expand Up @@ -248,7 +244,11 @@ export class TabsContainer
});
}),
this.voidContainer.onWillShowOverlay((event) => {
this._onWillShowOverlay.fire({ event, kind: 'header_space' });
this._onWillShowOverlay.fire(
new WillShowOverlayLocationEvent(event, {
kind: 'header_space',
})
);
}),
addDisposableListener(
this.voidContainer.element,
Expand Down Expand Up @@ -407,7 +407,9 @@ export class TabsContainer
});
}),
tab.onWillShowOverlay((event) => {
this._onWillShowOverlay.fire({ event, kind: 'tab' });
this._onWillShowOverlay.fire(
new WillShowOverlayLocationEvent(event, { kind: 'tab' })
);
})
);

Expand Down
2 changes: 1 addition & 1 deletion packages/dockview-core/src/dockview/dockviewComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2134,7 +2134,7 @@ export class DockviewComponent
}),
view.model.onWillShowOverlay((event) => {
if (this.options.disableDnd) {
event.event.preventDefault();
event.preventDefault();
return;
}

Expand Down
43 changes: 39 additions & 4 deletions packages/dockview-core/src/dockview/dockviewGroupPanelModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
DockviewEvent,
Emitter,
Event,
IDockviewEvent,
} from '../events';
import { IViewSize } from '../gridview/gridview';
import { CompositeDisposable } from '../lifecycle';
Expand Down Expand Up @@ -179,9 +180,39 @@ export type DockviewGroupLocation =
| { type: 'floating' }
| { type: 'popout'; getWindow: () => Window };

export interface WillShowOverlayLocationEvent {
event: WillShowOverlayEvent;
kind: DockviewGroupDropLocation;
type A = typeof WillShowOverlayEvent;

export class WillShowOverlayLocationEvent implements IDockviewEvent {
private _kind: DockviewGroupDropLocation;

get kind(): DockviewGroupDropLocation {
return this._kind;
}

get nativeEvent(): DragEvent {
return this.event.nativeEvent;
}

get position(): Position {
return this.event.position;
}

get defaultPrevented(): boolean {
return this.event.defaultPrevented;
}

preventDefault(): void {
this.event.preventDefault();
}

constructor(
private readonly event: WillShowOverlayEvent,
options: {
kind: DockviewGroupDropLocation;
}
) {
this._kind = options.kind;
}
}

export class DockviewGroupPanelModel
Expand Down Expand Up @@ -411,7 +442,11 @@ export class DockviewGroupPanelModel
this._onWillShowOverlay.fire(event);
}),
this.contentContainer.dropTarget.onWillShowOverlay((event) => {
this._onWillShowOverlay.fire({ event, kind: 'content' });
this._onWillShowOverlay.fire(
new WillShowOverlayLocationEvent(event, {
kind: 'content',
})
);
}),
this._onMove,
this._onDidChange,
Expand Down
7 changes: 6 additions & 1 deletion packages/dockview-core/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ export namespace Event {
};
}

export class DockviewEvent {
export interface IDockviewEvent {
readonly defaultPrevented: boolean;
preventDefault(): void;
}

export class DockviewEvent implements IDockviewEvent {
private _defaultPrevented = false;

get defaultPrevented(): boolean {
Expand Down

0 comments on commit 8533a18

Please sign in to comment.