From c793f8844011960254d7eeba9cc43847427b51f8 Mon Sep 17 00:00:00 2001 From: Josh Tynjala Date: Mon, 9 Oct 2023 15:26:19 -0700 Subject: [PATCH] BaseScrollContainer: fix missing ScrollEvent.SCROLL when automatically restricting current scroll position to min or max --- .../supportClasses/BaseScrollContainer.hx | 25 +++++++---- .../ScrollContainerFixedScrollBarsTest.hx | 41 +++++++++++++++++++ 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/src/feathers/controls/supportClasses/BaseScrollContainer.hx b/src/feathers/controls/supportClasses/BaseScrollContainer.hx index 8409a6dd..e2962fdf 100644 --- a/src/feathers/controls/supportClasses/BaseScrollContainer.hx +++ b/src/feathers/controls/supportClasses/BaseScrollContainer.hx @@ -1232,15 +1232,22 @@ class BaseScrollContainer extends FeathersControl implements IFocusObject { } // by checking for the minimum or maximum changing, ensures that // scrollX/Y can be set out of range on purpose - if (this._prevMinScrollX != this.scroller.minScrollX && this.scroller.scrollX < this.scroller.minScrollX) { - this.scroller.restrictedScrollX = this.scroller.scrollX; - } else if (this._prevMaxScrollX != this.scroller.maxScrollX && this.scroller.scrollX > this.scroller.maxScrollX) { - this.scroller.restrictedScrollX = this.scroller.scrollX; - } - if (this._prevMinScrollY != this.scroller.minScrollY && this.scroller.scrollY < this.scroller.minScrollY) { - this.scroller.restrictedScrollY = this.scroller.scrollY; - } else if (this._prevMaxScrollY != this.scroller.maxScrollY && this.scroller.scrollY > this.scroller.maxScrollY) { - this.scroller.restrictedScrollY = this.scroller.scrollY; + var currentScrollX = this.scroller.scrollX; + var currentScrollY = this.scroller.scrollY; + if (this._prevMinScrollX != this.scroller.minScrollX && currentScrollX < this.scroller.minScrollX) { + this.scroller.restrictedScrollX = currentScrollX; + } else if (this._prevMaxScrollX != this.scroller.maxScrollX && currentScrollX > this.scroller.maxScrollX) { + this.scroller.restrictedScrollX = currentScrollX; + } + if (this._prevMinScrollY != this.scroller.minScrollY && currentScrollY < this.scroller.minScrollY) { + this.scroller.restrictedScrollY = currentScrollY; + } else if (this._prevMaxScrollY != this.scroller.maxScrollY && currentScrollY > this.scroller.maxScrollY) { + this.scroller.restrictedScrollY = currentScrollY; + } + if (currentScrollX != this.scroller.scrollX || currentScrollY != this.scroller.scrollY) { + // the scroll event from the scroller will be ignored at this point + // so we need to manually dispatch it + ScrollEvent.dispatch(this, ScrollEvent.SCROLL, false, false, this.scroller.scrollX, this.scroller.scrollY); } } diff --git a/test/src/feathers/controls/ScrollContainerFixedScrollBarsTest.hx b/test/src/feathers/controls/ScrollContainerFixedScrollBarsTest.hx index 40b1ec29..dd64390e 100644 --- a/test/src/feathers/controls/ScrollContainerFixedScrollBarsTest.hx +++ b/test/src/feathers/controls/ScrollContainerFixedScrollBarsTest.hx @@ -8,6 +8,7 @@ package feathers.controls; +import feathers.events.ScrollEvent; import feathers.controls.HScrollBar; import feathers.controls.LayoutGroup; import feathers.controls.VScrollBar; @@ -618,6 +619,46 @@ class ScrollContainerFixedScrollBarsTest extends Test { this._container.validateNow(); }, IllegalOperationError); } + + public function testAutomaticRestrictScrollXToMaxScrollX():Void { + this._container.width = 100.0; + this._container.height = 100.0; + var child = new LayoutGroup(); + child.width = 200.0; + child.height = 75.0; + this._container.addChild(child); + this._container.scrollX = 50.0; + this._container.validateNow(); + child.width = 50.0; + var dispatchedScrollEvent = false; + this._container.addEventListener(ScrollEvent.SCROLL, function(event:ScrollEvent):Void { + dispatchedScrollEvent = true; + }); + Assert.equals(50.0, this._container.scrollX); + this._container.validateNow(); + Assert.equals(0.0, this._container.scrollX); + Assert.isTrue(dispatchedScrollEvent); + } + + public function testAutomaticRestrictScrollYToMaxScrollY():Void { + this._container.width = 100.0; + this._container.height = 100.0; + var child = new LayoutGroup(); + child.width = 75.0; + child.height = 200.0; + this._container.addChild(child); + this._container.scrollY = 50.0; + this._container.validateNow(); + child.height = 50.0; + var dispatchedScrollEvent = false; + this._container.addEventListener(ScrollEvent.SCROLL, function(event:ScrollEvent):Void { + dispatchedScrollEvent = true; + }); + Assert.equals(50.0, this._container.scrollY); + this._container.validateNow(); + Assert.equals(0.0, this._container.scrollY); + Assert.isTrue(dispatchedScrollEvent); + } } private class ResizingHeightBox extends FeathersControl {