From 8dd79523cba301394732f21ffccfb1f4af8611b2 Mon Sep 17 00:00:00 2001 From: Kevin Leung Date: Fri, 9 Nov 2018 23:14:20 +0800 Subject: [PATCH] DRY a bit --- haxe/ui/components/TabBar2.hx | 39 ++++++++++--------------- haxe/ui/containers/ScrollView2.hx | 12 +++++--- haxe/ui/containers/TabView2.hx | 47 +++++++++++++++---------------- 3 files changed, 46 insertions(+), 52 deletions(-) diff --git a/haxe/ui/components/TabBar2.hx b/haxe/ui/components/TabBar2.hx index 5bd881e4d..1b709bbec 100644 --- a/haxe/ui/components/TabBar2.hx +++ b/haxe/ui/components/TabBar2.hx @@ -240,61 +240,52 @@ private class Builder extends CompositeBuilder { } } - private function addTab(child:Component):Component { + private function addTab(child:Component, add:Component->Void):Void { child.addClass("tabbar-button"); - var v = _container.addComponent(child); + add(child); _tabbar.registerInternalEvents(Events, true); if (_tabbar.selectedIndex < 0) { _tabbar.selectedIndex = 0; } - return v; - } - - // TODO: DRY with addTab - private function addTabAt(child:Component, index:Int):Component { - child.addClass("tabbar-button"); - var v = _container.addComponentAt(child, index); - _tabbar.registerInternalEvents(Events, true); - if (_tabbar.selectedIndex < 0) { - _tabbar.selectedIndex = 0; - } - return v; } public override function get_numComponents():Int { return _container.numComponents; } + inline function isInternal(c:Component) { + return c == _container || c == _scrollLeft || c == _scrollRight; + } + public override function addComponent(child:Component):Component { - if (child != _container && child != _scrollLeft && child != _scrollRight) { - return addTab(child); + if (!isInternal(child)) { + addTab(child, _container.addComponent); + return child; } return null; } public override function addComponentAt(child:Component, index:Int):Component { - if (child != _container && child != _scrollLeft && child != _scrollRight) { - return addTabAt(child, index); + if (!isInternal(child)) { + addTab(child, _container.addComponentAt.bind(_, index)); + return child; } return null; } public override function removeComponent(child:Component, dispose:Bool = true, invalidate:Bool = true):Component { - if (child != _container && child != _scrollLeft && child != _scrollRight) { + if (!isInternal(child)) { return _container.removeComponent(child, dispose, invalidate); } return null; } public override function getComponentIndex(child:Component):Int { - if (child != _container && child != _scrollLeft && child != _scrollRight) { - return _container.getComponentIndex(child); - } - return -1; + return _container.getComponentIndex(child); } public override function setComponentIndex(child:Component, index:Int):Component { - if (child != _container && child != _scrollLeft && child != _scrollRight) { + if (!isInternal(child)) { return _container.setComponentIndex(child, index); } return null; diff --git a/haxe/ui/containers/ScrollView2.hx b/haxe/ui/containers/ScrollView2.hx index 8b03a47f2..cfb7d7e1a 100644 --- a/haxe/ui/containers/ScrollView2.hx +++ b/haxe/ui/containers/ScrollView2.hx @@ -623,22 +623,26 @@ class ScrollViewBuilder extends CompositeBuilder { return _contents.numComponents; } + inline function isInternal(c:Component) { + return Std.is(c, HorizontalScroll2) || Std.is(c, VerticalScroll2) || c.hasClass("scrollview-contents"); + } + public override function addComponent(child:Component):Component { - if (Std.is(child, HorizontalScroll2) == false && Std.is(child, VerticalScroll2) == false && child.hasClass("scrollview-contents") == false) { + if (!isInternal(child)) { return _contents.addComponent(child); } return null; } public override function addComponentAt(child:Component, index:Int):Component { - if (Std.is(child, HorizontalScroll2) == false && Std.is(child, VerticalScroll2) == false && child.hasClass("scrollview-contents") == false) { + if (!isInternal(child)) { return _contents.addComponentAt(child, index); } return null; } public override function removeComponent(child:Component, dispose:Bool = true, invalidate:Bool = true):Component { - if (Std.is(child, HorizontalScroll2) == false && Std.is(child, VerticalScroll2) == false && child.hasClass("scrollview-contents") == false) { + if (!isInternal(child)) { return _contents.removeComponent(child, dispose, invalidate); } return null; @@ -649,7 +653,7 @@ class ScrollViewBuilder extends CompositeBuilder { } public override function setComponentIndex(child:Component, index:Int):Component { - if (Std.is(child, HorizontalScroll2) == false && Std.is(child, VerticalScroll2) == false && child.hasClass("scrollview-contents") == false) { + if (!isInternal(child)) { return _contents.setComponentIndex(child, index); } return null; diff --git a/haxe/ui/containers/TabView2.hx b/haxe/ui/containers/TabView2.hx index d9aaddf6b..7cd369290 100644 --- a/haxe/ui/containers/TabView2.hx +++ b/haxe/ui/containers/TabView2.hx @@ -235,46 +235,45 @@ private class Builder extends CompositeBuilder { } } + private function addTab(child:Component, add:Component->Void):Void { + var text:String = child.text; + var icon:String = null; + if (Std.is(child, Box)) { + icon = cast(child, Box).icon; + } + _views.push(child); + var button:Button = new Button(); + button.text = text; + button.icon = icon; + add(button); + } + public override function get_numComponents():Int { return _views.length; } + inline function isInternal(c:Component) { + return c == _content || c == _tabs; + } + public override function addComponent(child:Component):Component { - if (child != _content && child != _tabs) { - var text:String = child.text; - var icon:String = null; - if (Std.is(child, Box)) { - icon = cast(child, Box).icon; - } - _views.push(child); - var button:Button = new Button(); - button.text = text; - button.icon = icon; - _tabs.addComponent(button); + if (!isInternal(child)) { + addTab(child, _tabs.addComponent); return child; } return null; } public override function addComponentAt(child:Component, index:Int):Component { - if (child != _content && child != _tabs) { - var text:String = child.text; - var icon:String = null; - if (Std.is(child, Box)) { - icon = cast(child, Box).icon; - } - _views.insert(index, child); - var button:Button = new Button(); - button.text = text; - button.icon = icon; - _tabs.addComponentAt(button, index); + if (!isInternal(child)) { + addTab(child, _tabs.addComponentAt.bind(_, index)); return child; } return null; } public override function removeComponent(child:Component, dispose:Bool = true, invalidate:Bool = true):Component { - if (child != _content && child != _tabs) { + if (!isInternal(child)) { switch _views.indexOf(child) { case -1: case i: @@ -291,7 +290,7 @@ private class Builder extends CompositeBuilder { } public override function setComponentIndex(child:Component, index:Int):Component { - if (child != _content && child != _tabs) { + if (!isInternal(child)) { switch _views.indexOf(child) { case -1: case i: