Skip to content

Commit

Permalink
Deactive sliders when canvas loses focus (BabylonJS#8728)
Browse files Browse the repository at this point in the history
* allow controls to handle canvas blur

* update whatsnew

* reword what's new

* linting fix

* only handle sliders

* fix color pickers
  • Loading branch information
DarraghBurkeMS authored Aug 11, 2020
1 parent 55602a3 commit 5ebbbe1
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions dist/what's new.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@
- Added `ScrollViewer.freezeControls` property to speed up rendering ([Popov72](https://github.com/Popov72))
- Added `ImageScrollBar.num90RotationInVerticalMode` property to let the user rotate the pictures when in vertical mode ([Popov72](https://github.com/Popov72))
- Modified isPointerBlocker to block mouse wheel scroll events. ScrollViewer mouse scroll no longer dependent on scene. ([lockphase](https://github.com/lockphase/))
- Added `_onCanvasBlur` event for controls to detect when the canvas loses focus. Fixes bug where sliders and color pickers would become stuck when canvas lost focus. ([DarraghBurkeMS](https://github.com/DarraghBurkeMS))

### Particles

Expand Down
14 changes: 14 additions & 0 deletions gui/src/2D/advancedDynamicTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export class AdvancedDynamicTexture extends DynamicTexture {
private _pointerMoveObserver: Nullable<Observer<PointerInfoPre>>;
private _pointerObserver: Nullable<Observer<PointerInfo>>;
private _canvasPointerOutObserver: Nullable<Observer<PointerEvent>>;
private _canvasBlurObserver: Nullable<Observer<Engine>>;
private _background: string;
/** @hidden */
public _rootContainer = new Container("root");
Expand Down Expand Up @@ -480,6 +481,9 @@ export class AdvancedDynamicTexture extends DynamicTexture {
if (this._canvasPointerOutObserver) {
scene.getEngine().onCanvasPointerOutObservable.remove(this._canvasPointerOutObserver);
}
if (this._canvasBlurObserver) {
scene.getEngine().onCanvasBlurObservable.remove(this._canvasBlurObserver);
}
if (this._layerToDispose) {
this._layerToDispose.texture = null;
this._layerToDispose.dispose();
Expand Down Expand Up @@ -741,6 +745,7 @@ export class AdvancedDynamicTexture extends DynamicTexture {
}
});
this._attachToOnPointerOut(scene);
this._attachToOnBlur(scene);
}
/** @hidden */
private onClipboardCopy = (rawEvt: Event) => {
Expand Down Expand Up @@ -838,6 +843,7 @@ export class AdvancedDynamicTexture extends DynamicTexture {
});
mesh.enablePointerMoveEvents = supportPointerMove;
this._attachToOnPointerOut(scene);
this._attachToOnBlur(scene);
}
/**
* Move the focus to a specific control
Expand Down Expand Up @@ -876,6 +882,14 @@ export class AdvancedDynamicTexture extends DynamicTexture {
}
});
}
private _attachToOnBlur(scene: Scene): void {
this._canvasBlurObserver = scene.getEngine().onCanvasBlurObservable.add((pointerEvent) => {
Object.entries(this._lastControlDown).forEach(([key, value]) => {
value._onCanvasBlur();
});
this._lastControlDown = {};
});
}
// Statics
/**
* Creates a new AdvancedDynamicTexture in projected mode (ie. attached to a mesh)
Expand Down
5 changes: 5 additions & 0 deletions gui/src/2D/controls/colorpicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,11 @@ export class ColorPicker extends Control {
super._onPointerUp(target, coordinates, pointerId, buttonIndex, notifyClick);
}

public _onCanvasBlur() {
this._forcePointerUp();
super._onCanvasBlur();
}

/**
* This function expands the color picker by creating a color picker dialog with manual
* color value input and the ability to save colors into an array to be used later in
Expand Down
3 changes: 3 additions & 0 deletions gui/src/2D/controls/control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1853,6 +1853,9 @@ export class Control {
if (canNotify && this.parent != null) { this.parent._onWheelScroll(deltaX, deltaY); }
}

/** @hidden */
public _onCanvasBlur(): void {}

/** @hidden */
public _processObservables(type: number, x: number, y: number, pointerId: number, buttonIndex: number, deltaX?: number, deltaY?: number): boolean {
if (!this._isEnabled) {
Expand Down
6 changes: 6 additions & 0 deletions gui/src/2D/controls/sliders/baseSlider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,4 +326,10 @@ export class BaseSlider extends Control {
delete this._host._capturingControl[pointerId];
super._onPointerUp(target, coordinates, pointerId, buttonIndex, notifyClick);
}

public _onCanvasBlur(): void {
this._forcePointerUp();
super._onCanvasBlur();
}

}

0 comments on commit 5ebbbe1

Please sign in to comment.