From 034de21cc0ff9d89a67cc0bad8577072d9bb939f Mon Sep 17 00:00:00 2001 From: fbchen Date: Thu, 7 Jul 2022 17:48:53 +0800 Subject: [PATCH 1/5] push some code --- .../constraintlayout/ConstrainedNode.kt | 7 + .../wrappers/constraintlayout/ConstraintId.kt | 2 +- .../constraintlayout/ConstraintLayout.kt | 208 +++++++++++++++++- .../ConstraintLayoutException.kt | 4 + .../constraintlayout/IndexConstraintId.kt | 7 + .../constraintlayout/RelativeConstraintId.kt | 7 + .../weiv/wrappers/constraintlayout/Size.kt | 4 + 7 files changed, 235 insertions(+), 4 deletions(-) create mode 100644 weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/ConstrainedNode.kt create mode 100644 weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/ConstraintLayoutException.kt create mode 100644 weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/IndexConstraintId.kt create mode 100644 weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/RelativeConstraintId.kt create mode 100644 weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/Size.kt diff --git a/weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/ConstrainedNode.kt b/weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/ConstrainedNode.kt new file mode 100644 index 0000000..0072385 --- /dev/null +++ b/weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/ConstrainedNode.kt @@ -0,0 +1,7 @@ +package cn.flutterfirst.weiv.wrappers.constraintlayout + +class ConstrainedNode() { + lateinit var nodeId: ConstraintId + var depth = -1 + var notLaidOut = false +} \ No newline at end of file diff --git a/weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/ConstraintId.kt b/weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/ConstraintId.kt index 6914bb5..ece8839 100644 --- a/weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/ConstraintId.kt +++ b/weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/ConstraintId.kt @@ -77,7 +77,7 @@ open class ConstraintId(var id: String) { ConstraintAlign(this, ConstraintAlignType.center) } - protected fun copy(): ConstraintId { + protected open fun copy(): ConstraintId { return ConstraintId(id) } diff --git a/weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/ConstraintLayout.kt b/weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/ConstraintLayout.kt index f858486..0f60906 100644 --- a/weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/ConstraintLayout.kt +++ b/weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/ConstraintLayout.kt @@ -1,38 +1,63 @@ package cn.flutterfirst.weiv.wrappers.constraintlayout +import android.annotation.SuppressLint import android.content.Context import android.view.View import android.view.ViewGroup +import cn.flutterfirst.weiv.core.others.LayoutParam.Companion.matchParent +import cn.flutterfirst.weiv.core.others.LayoutParam.Companion.wrapContent +import cn.flutterfirst.weiv.core.others.getLayoutParam +import kotlin.math.min + // ConstraintLayout design for weiV class ConstraintLayout(context: Context) : ViewGroup(context) { var childrenList = ArrayList() + // Self config + var showLayoutPerformanceOverlay: Boolean = false + var showHelperWidgets: Boolean = false + var showClickArea: Boolean = false + var showZIndex: Boolean = false + var showChildDepth: Boolean = false + var debugPrintConstraints: Boolean = false + var _width = matchParent + var _height = matchParent + var _size: Int? = null + + // var needsRecalculateConstraints = true var needsLayout = true var needsReorderPaintingOrder = true var needsPaint = true var needsReorderEventOrder = true + var size: Size? = null + val helperNodeMap: Map = HashMap() + init { } override fun addView(child: View?, index: Int, params: LayoutParams?) { super.addView(child, index, params) + fillChildrenList() } override fun removeAllViews() { super.removeAllViews() + fillChildrenList() markNeedsRecalculateConstraints() } override fun removeView(view: View?) { super.removeView(view) + fillChildrenList() markNeedsRecalculateConstraints() } override fun removeViewAt(index: Int) { super.removeViewAt(index) + fillChildrenList() markNeedsRecalculateConstraints() } @@ -43,13 +68,190 @@ class ConstraintLayout(context: Context) : ViewGroup(context) { } } + private fun constrainSize(originSize: Int, measureSpec: Int): Int { + val mode = MeasureSpec.getMode(measureSpec) + val size = MeasureSpec.getSize(measureSpec) + return when (mode) { + MeasureSpec.AT_MOST -> { + min(size, originSize) + } + MeasureSpec.EXACTLY -> { + size + } + else -> { + originSize + } + } + } + + private fun debugCheckIds(): Boolean { + val declaredIdSet = HashSet() + declaredIdSet.add(CL.parent) + + if (helperNodeMap.isNotEmpty()) { + for (key in helperNodeMap.keys) { + if (!declaredIdSet.add(key)) { + throw ConstraintLayoutException("Duplicate id in ConstraintLayout.") + } + } + } + + val referencedIdSet = HashSet() + childrenList.forEach { view -> + val constraintLayoutParam = view.getLayoutParam()!! + + if (constraintLayoutParam.id != null) { + if (!declaredIdSet.add(constraintLayoutParam.id!!)) { + throw ConstraintLayoutException("Duplicate id in ConstraintLayout.") + } + } + + if (constraintLayoutParam.left != null) { + referencedIdSet.add(constraintLayoutParam.left!!.id!!) + } + + if (constraintLayoutParam.top != null) { + referencedIdSet.add(constraintLayoutParam.top!!.id!!) + } + + if (constraintLayoutParam.right != null) { + referencedIdSet.add(constraintLayoutParam.right!!.id!!) + } + + if (constraintLayoutParam.bottom != null) { + referencedIdSet.add(constraintLayoutParam.bottom!!.id!!) + } + + if (constraintLayoutParam.baseline != null) { + referencedIdSet.add(constraintLayoutParam.baseline!!.id!!) + } + + // TODO + } + + // TODO + + // The id used by all constraints must be defined + // TODO + + return true + } + + private fun buildConstrainedNodeTrees(selfSizeConfirmed: Boolean): Map { + val nodesMap = LinkedHashMap() + val parentNode = ConstrainedNode() + parentNode.nodeId = CL.parent + parentNode.depth = if (selfSizeConfirmed) 0 else childCount + 1 + parentNode.notLaidOut = false + + if (!selfSizeConfirmed) { + nodesMap[CL.parent] = parentNode + } + + fun getConstrainedNodeForChild(id: ConstraintId, childIndex: Int? = null): ConstrainedNode { + if (id == CL.parent) { + return parentNode + } + + var localId = id + if (id is RelativeConstraintId) { + val targetIndex = childIndex!! + id.siblingIndexOffset; + localId = IndexConstraintId(targetIndex); + } else if (id is IndexConstraintId) { + if (id.siblingIndex < 0) { + localId = IndexConstraintId(childCount + id.siblingIndex); + } + } + + var node = nodesMap[localId] + if (node == null) { + node = ConstrainedNode() + node.nodeId = localId + nodesMap[localId] = node + } + + return node + } + + // TODO + + return nodesMap + } + + @SuppressLint("Range", "DrawAllocation", "AssertionSideEffect") override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { - getChildAt(0).measure(widthMeasureSpec, heightMeasureSpec) - setMeasuredDimension(getChildAt(0).measuredWidth, getChildAt(0).measuredHeight) + + var resolvedWidth: Int + if (_width>= 0) { + resolvedWidth = constrainSize(_width, widthMeasureSpec) + } else { + resolvedWidth = if (_width == matchParent) { + when (MeasureSpec.getMode(widthMeasureSpec)) { + MeasureSpec.AT_MOST -> { + wrapContent + } + MeasureSpec.EXACTLY -> { + MeasureSpec.getSize(widthMeasureSpec) + } + else -> { + wrapContent + } + } + } else { + wrapContent + } + } + + var resolvedHeight: Int + if (_height>= 0) { + resolvedHeight = constrainSize(_height, heightMeasureSpec) + } else { + resolvedHeight = if (_width == matchParent) { + when (MeasureSpec.getMode(heightMeasureSpec)) { + MeasureSpec.AT_MOST -> { + wrapContent + } + MeasureSpec.EXACTLY -> { + MeasureSpec.getSize(heightMeasureSpec) + } + else -> { + wrapContent + } + } + } else { + wrapContent + } + } + + var selfSizeConfirmed = false + if (resolvedWidth != wrapContent && resolvedHeight != wrapContent) { + size = Size(resolvedWidth, resolvedHeight) + selfSizeConfirmed = true + } else if (resolvedWidth != wrapContent) { + size = Size(resolvedWidth, 0) + } else if (resolvedHeight != wrapContent) { + size = Size(0, resolvedHeight) + } + + if (needsRecalculateConstraints) { + assert(debugCheckIds()) + + } + + childrenList.forEach { view -> + val layoutParam = view.getLayoutParam()!! + val widthSpec = MeasureSpec.makeMeasureSpec(layoutParam.width, MeasureSpec.EXACTLY) + val heightSpec = MeasureSpec.makeMeasureSpec(layoutParam.height, MeasureSpec.EXACTLY) + view.measure(widthSpec, heightSpec) + } + setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), + MeasureSpec.getSize(heightMeasureSpec)) } override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) { - getChildAt(0).layout(0, 0, getChildAt(0).measuredWidth, getChildAt(0).measuredHeight) + childrenList.forEach { view -> + view.layout(300, 300, 300 + view.measuredWidth, 300 + view.measuredHeight) + } } fun markNeedsRecalculateConstraints() { diff --git a/weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/ConstraintLayoutException.kt b/weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/ConstraintLayoutException.kt new file mode 100644 index 0000000..970789c --- /dev/null +++ b/weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/ConstraintLayoutException.kt @@ -0,0 +1,4 @@ +package cn.flutterfirst.weiv.wrappers.constraintlayout + +class ConstraintLayoutException(msg: String) : Exception(msg) { +} \ No newline at end of file diff --git a/weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/IndexConstraintId.kt b/weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/IndexConstraintId.kt new file mode 100644 index 0000000..697085e --- /dev/null +++ b/weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/IndexConstraintId.kt @@ -0,0 +1,7 @@ +package cn.flutterfirst.weiv.wrappers.constraintlayout + +class IndexConstraintId(var siblingIndex: Int) : ConstraintId("parent.children[$siblingIndex]") { + override fun copy(): ConstraintId { + return IndexConstraintId(siblingIndex) + } +} \ No newline at end of file diff --git a/weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/RelativeConstraintId.kt b/weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/RelativeConstraintId.kt new file mode 100644 index 0000000..d07fd4e --- /dev/null +++ b/weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/RelativeConstraintId.kt @@ -0,0 +1,7 @@ +package cn.flutterfirst.weiv.wrappers.constraintlayout + +class RelativeConstraintId(var siblingIndexOffset: Int) : ConstraintId("$siblingIndexOffset") { + override fun copy(): ConstraintId { + return RelativeConstraintId(siblingIndexOffset) + } +} \ No newline at end of file diff --git a/weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/Size.kt b/weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/Size.kt new file mode 100644 index 0000000..363da2f --- /dev/null +++ b/weiV/src/main/java/cn/flutterfirst/weiv/wrappers/constraintlayout/Size.kt @@ -0,0 +1,4 @@ +package cn.flutterfirst.weiv.wrappers.constraintlayout + +class Size(var width: Int, var height: Int) { +} \ No newline at end of file From c619d3334ba82ea062c6f3f06eb536eef9ab6664 Mon Sep 17 00:00:00 2001 From: fangbing chen Date: 2024年8月26日 13:45:19 +0800 Subject: [PATCH 2/5] Update README_CN.md --- README_CN.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README_CN.md b/README_CN.md index 5095b0c..edca9b4 100644 --- a/README_CN.md +++ b/README_CN.md @@ -4,6 +4,10 @@ **我开发了世界上最快的通用排序算法,它平均比快速排序快 3 倍,最多快 20 倍**,[ChenSort](https://github.com/hackware1993/ChenSort) +下文《家有三孩的农村独子该如何面对父母双瘫?》 +https://mp.weixin.qq.com/s/lUYAa6IvGOLdwrTnsTpIPg?token=549085630&lang=zh_CN +是我公众号的原创文章。我正在为当事人筹款,但我没有影响力,所以倍感艰难,希望大家多多支持。数以十万计的 App 从我的代码受益,希望大家也能帮帮我。 + ![wave.webp](https://github.com/hackware1993/weiV/blob/master/wave.webp?raw=true) weiV(发音同 wave),一个基于 Android View 系统的全新声明式 UI 开发框架。核心源文件只有 35 KB。 From 17867a038c0002e5f80ab40b4ebc6c7e8e440572 Mon Sep 17 00:00:00 2001 From: fangbing chen Date: 2024年8月26日 13:46:07 +0800 Subject: [PATCH 3/5] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 14e5b5f..7e3ba8f 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,10 @@ **I have developed the world's fastest general purpose sorting algorithm, which is on average 3 times faster than Quicksort and up to 20 times faster**, [ChenSort](https://github.com/hackware1993/ChenSort) +下文《家有三孩的农村独子该如何面对父母双瘫?》 +https://mp.weixin.qq.com/s/lUYAa6IvGOLdwrTnsTpIPg?token=549085630&lang=zh_CN +是我公众号的原创文章。我正在为当事人筹款,但我没有影响力,所以倍感艰难,希望大家多多支持。数以十万计的 App 从我的代码受益,希望大家也能帮帮我。 + ![wave.webp](https://github.com/hackware1993/weiV/blob/master/wave.webp?raw=true) weiV (pronounced the same as wave), a new declarative UI development framework based on the Android From b1547d3a122a04b8e03f0d7c36c5cf33b0a7fb62 Mon Sep 17 00:00:00 2001 From: fangbing chen Date: Sat, 7 Dec 2024 15:48:44 +0800 Subject: [PATCH 4/5] Update README.md --- README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/README.md b/README.md index 7e3ba8f..14e5b5f 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,6 @@ **I have developed the world's fastest general purpose sorting algorithm, which is on average 3 times faster than Quicksort and up to 20 times faster**, [ChenSort](https://github.com/hackware1993/ChenSort) -下文《家有三孩的农村独子该如何面对父母双瘫?》 -https://mp.weixin.qq.com/s/lUYAa6IvGOLdwrTnsTpIPg?token=549085630&lang=zh_CN -是我公众号的原创文章。我正在为当事人筹款,但我没有影响力,所以倍感艰难,希望大家多多支持。数以十万计的 App 从我的代码受益,希望大家也能帮帮我。 - ![wave.webp](https://github.com/hackware1993/weiV/blob/master/wave.webp?raw=true) weiV (pronounced the same as wave), a new declarative UI development framework based on the Android From ca6fbb35ebf7462ea12edf401d24c4fb8e541713 Mon Sep 17 00:00:00 2001 From: fangbing chen Date: Sat, 7 Dec 2024 15:49:00 +0800 Subject: [PATCH 5/5] Update README_CN.md --- README_CN.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/README_CN.md b/README_CN.md index edca9b4..5095b0c 100644 --- a/README_CN.md +++ b/README_CN.md @@ -4,10 +4,6 @@ **我开发了世界上最快的通用排序算法,它平均比快速排序快 3 倍,最多快 20 倍**,[ChenSort](https://github.com/hackware1993/ChenSort) -下文《家有三孩的农村独子该如何面对父母双瘫?》 -https://mp.weixin.qq.com/s/lUYAa6IvGOLdwrTnsTpIPg?token=549085630&lang=zh_CN -是我公众号的原创文章。我正在为当事人筹款,但我没有影响力,所以倍感艰难,希望大家多多支持。数以十万计的 App 从我的代码受益,希望大家也能帮帮我。 - ![wave.webp](https://github.com/hackware1993/weiV/blob/master/wave.webp?raw=true) weiV(发音同 wave),一个基于 Android View 系统的全新声明式 UI 开发框架。核心源文件只有 35 KB。

AltStyle によって変換されたページ (->オリジナル) /