Imported as a linear vendor commit so forge rebases do not replay the upstream root tree. git-subtree-dir: third_party/pi git-subtree-split: d981de1229ef899957bbe968bc8dcda02a21f477
34 lines
902 B
TypeScript
34 lines
902 B
TypeScript
import {
|
|
type Component,
|
|
dispatchMouseEvent,
|
|
type TuiMouseDispatchResult,
|
|
type TuiMouseEvent,
|
|
type TuiMouseEventResult,
|
|
} from "../tui.ts";
|
|
|
|
export type MouseRegionHandler = (event: TuiMouseEvent) => TuiMouseEventResult | undefined;
|
|
|
|
/** Adds mouse handling to an existing component without changing its rendering. */
|
|
export class MouseRegion implements Component {
|
|
private readonly child: Component;
|
|
private readonly onMouse: MouseRegionHandler;
|
|
|
|
constructor(child: Component, onMouse: MouseRegionHandler) {
|
|
this.child = child;
|
|
this.onMouse = onMouse;
|
|
}
|
|
|
|
render(width: number): string[] {
|
|
return this.child.render(width);
|
|
}
|
|
|
|
handleMouse(event: TuiMouseEvent): TuiMouseDispatchResult | TuiMouseEventResult | undefined {
|
|
const childResult = dispatchMouseEvent(this.child, event);
|
|
return childResult ?? this.onMouse(event);
|
|
}
|
|
|
|
invalidate(): void {
|
|
this.child.invalidate();
|
|
}
|
|
}
|