Skip to content
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

Fix for empty texts not showing or showing when it shouldn't #612

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
89 changes: 65 additions & 24 deletions haxe/ui/containers/ScrollView.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1363,29 +1363,38 @@ class ScrollViewBuilder extends CompositeBuilder {
}

var containsEmptyContentsComponent = _scrollview.containsChildComponent(emptyContentsComponent);
if (contentsComponent.numComponents == 0) {
if (!containsEmptyContentsComponent) {
emptyContentsComponent.addClass("empty-contents-component");
_scrollview.addComponent(emptyContentsComponent);
}

if (_scrollview.emptyContentsText != null) {
if ((emptyContentsComponent is Label)) {
emptyContentsComponent.text = _scrollview.emptyContentsText;
} else {
var label = emptyContentsComponent.findComponent(Label, true);
if (label != null) {
label.text = _scrollview.emptyContentsText;
}
}
}

emptyContentsComponent.show();
if (!containsContent(contentsComponent)) {
showEmptyContentsComponent();
} else if (containsEmptyContentsComponent) {
emptyContentsComponent.hide();
}
}

private function showEmptyContentsComponent() {
var emptyContentsComponent:Component = _scrollview.emptyContentsComponent;
if (emptyContentsComponent == null) {
return;
}
var containsEmptyContentsComponent = _scrollview.containsChildComponent(emptyContentsComponent);
if (!containsEmptyContentsComponent) {
emptyContentsComponent.addClass("empty-contents-component");
_scrollview.addComponent(emptyContentsComponent);
}

if (_scrollview.emptyContentsText != null) {
if ((emptyContentsComponent is Label)) {
emptyContentsComponent.text = _scrollview.emptyContentsText;
} else {
var label = emptyContentsComponent.findComponent(Label, true);
if (label != null) {
label.text = _scrollview.emptyContentsText;
}
}
}

emptyContentsComponent.show();
}

private override function get_numComponents():Null<Int> {
return _contents.numComponents;
}
Expand All @@ -1406,21 +1415,43 @@ class ScrollViewBuilder extends CompositeBuilder {
if ((child is HorizontalScroll) == false && (child is VerticalScroll) == false && child.hasClass("scrollview-contents") == false) {
var contentsComponent = null;
if ((child is Box)) {
child.registerEvent(UIEvent.COMPONENT_ADDED, onContentsChanged);
child.registerEvent(UIEvent.COMPONENT_REMOVED, onContentsChanged);
contentsComponent = child;
}
registerEventForChildrenBoxes(child);
var r = _contents.addComponent(child);
checkEmptyContentsComponent(contentsComponent);
return r;
}
return null;
}

private function registerEventForChildrenBoxes(b:Component) {
if ((b is Box)||(b is ScrollView)){
b.registerEvent(UIEvent.COMPONENT_ADDED, onContentsChanged);
b.registerEvent(UIEvent.COMPONENT_REMOVED, onContentsChanged);
for ( c in b.childComponents) {
registerEventForChildrenBoxes(c);
}
}
}

private function containsContent(b:Component) {
if (!(b is Box) && !(b is ScrollView)) {
return true;
} else {
for ( c in b.childComponents) {
if (containsContent(c)) return true;
}
}
return false;
}

public override function addComponentAt(child:Component, index:Int):Component {
if ((child is HorizontalScroll) == false && (child is VerticalScroll) == false && child.hasClass("scrollview-contents") == false) {
var r = _contents.addComponentAt(child, index);
checkEmptyContentsComponent();
if ( _scrollview.emptyContentsComponent != null && !_scrollview.emptyContentsComponent.hidden && containsContent(child)) {
checkEmptyContentsComponent();
}
return r;
}
return null;
Expand All @@ -1432,7 +1463,9 @@ class ScrollViewBuilder extends CompositeBuilder {
}
if ((child is HorizontalScroll) == false && (child is VerticalScroll) == false && child.hasClass("scrollview-contents") == false) {
var r = _contents.removeComponent(child, dispose, invalidate);
checkEmptyContentsComponent();
if (containsContent(child)) {
checkEmptyContentsComponent();
}
return r;
}
return null;
Expand Down Expand Up @@ -1478,8 +1511,16 @@ class ScrollViewBuilder extends CompositeBuilder {
}
}

private function onContentsChanged(event:UIEvent) {
checkEmptyContentsComponent(event.target);
private function onContentsChanged(event:UIEvent) {
var child = event.relatedComponent;
registerEventForChildrenBoxes(child);
if (event.type == UIEvent.COMPONENT_ADDED) {
if ( _scrollview.emptyContentsComponent != null && !_scrollview.emptyContentsComponent.hidden && containsContent(child)) {
_scrollview.emptyContentsComponent.hide();
}
} else if ( _scrollview.emptyContentsComponent != null && _scrollview.emptyContentsComponent.hidden && containsContent(child)) {
checkEmptyContentsComponent();
}
}

private function horizontalConstraintModifier():Float {
Expand Down