use of com.taobao.weex.dom.WXAttr in project WeexErosFramework by bmfe.
the class BMWXText method updateFontSize.
private void updateFontSize() {
if (mChangeFontSize == null) {
return;
}
WXStyle styles = null;
WXAttr attrs = null;
if (getDomObject() != null) {
styles = getDomObject().getStyles();
attrs = getDomObject().getAttrs();
if ((styles != null && "iconfont".equals(styles.get("fontFamily"))) || (attrs != null && attrs.get("changeFont") != null && !Boolean.valueOf((String) attrs.get("changeFont")))) {
return;
}
}
float scale = 0;
// 获取fontScale字段
if (attrs != null && attrs.get("fontScale") != null) {
float fontScale = Float.valueOf((String) attrs.get("fontScale"));
mCurrentScale = fontScale / mCurrentScale;
}
if (mChangeFontSize.equals(mCurrentFontSize) && mCurrentScale == 1) {
return;
}
// 获取scale字段 在标准字体下不产生变化
if (attrs != null && attrs.get("scale") != null && !(scale > 0)) {
scale = Float.valueOf((String) attrs.get("scale"));
float change = getFixedEnlarge(mChangeFontSize, scale);
float current = getFixedEnlarge(mCurrentFontSize, scale);
scale = change / current;
}
// 根据全局字体配置设置字体大小
if (!(scale > 0)) {
float current = getEnlarge(mCurrentFontSize);
float change = getEnlarge(mChangeFontSize);
scale = change / current * mCurrentScale;
}
if (getDomObject() != null && getDomObject().getStyles() != null) {
WXStyle wxStyle = getDomObject().getStyles();
Object object = wxStyle.get("fontSize");
if (object instanceof Integer) {
int fontSize = (int) object;
int changeFontSize = (int) (fontSize * (scale));
wxStyle.put("fontSize", changeFontSize);
}
// 设置lineHeight
Object lineHeight = wxStyle.get("lineHeight");
if (lineHeight instanceof Integer) {
int target = (int) lineHeight;
wxStyle.put("lineHeight", (int) (target * scale));
}
updateStyle(wxStyle);
}
mCurrentFontSize = mChangeFontSize;
}
use of com.taobao.weex.dom.WXAttr in project incubator-weex by apache.
the class Statements method doRenderComponent.
/**
* @param component component with v-for statement, v-if statement and bind attrs
* @param context execute context
* render component in context, the function do the following work.
* execute component's v-for statement, v-if statement in context,
* and rebuild component's tree with the statement, v-for reuse component execute by pre render.
* if executed, component will be removed, don't remove, just mark it waste;
* may be next render it can be used.
* after statement has executed, render component's binding attrs in context and bind it to component.
*/
private static final int doRenderComponent(WXComponent component, CellRenderContext context, List<WXComponent> updates) {
WXVContainer parent = component.getParent();
WXDomObject domObject = (WXDomObject) component.getDomObject();
WXAttr attrs = domObject.getAttrs();
WXStatement statement = attrs.getStatement();
if (statement != null) {
WXDomObject parentDomObject = (WXDomObject) parent.getDomObject();
Token vif = null;
JSONObject vfor = null;
if (statement.get(WXStatement.WX_IF) instanceof Token) {
vif = (Token) statement.get(WXStatement.WX_IF);
}
if (statement.get(WXStatement.WX_FOR) instanceof JSONObject) {
vfor = (JSONObject) statement.get(WXStatement.WX_FOR);
}
// execute v-for content
if (vfor != null) {
int renderIndex = parent.indexOf(component);
if (vfor.get(WXStatement.WX_FOR_LIST) instanceof Token) {
Token listBlock = (Token) vfor.get(WXStatement.WX_FOR_LIST);
String indexKey = vfor.getString(WXStatement.WX_FOR_INDEX);
String itemKey = vfor.getString(WXStatement.WX_FOR_ITEM);
Object data = null;
if (listBlock != null) {
data = listBlock.execute(context.stack);
}
if ((data instanceof List || data instanceof Map)) {
Collection collection = null;
Map map = null;
if (data instanceof List) {
collection = (List) data;
} else {
map = (Map) data;
collection = map.keySet();
}
Map<String, Object> loop = new HashMap<>();
int index = 0;
for (Object item : collection) {
Object key = null;
Object value = item;
if (map == null) {
key = index;
value = item;
} else {
key = item;
value = map.get(item);
}
if (indexKey != null) {
loop.put(indexKey, key);
}
if (itemKey != null) {
loop.put(itemKey, value);
} else {
context.stack.push(value);
}
if (loop.size() > 0) {
context.stack.push(loop);
}
if (vif != null) {
if (!Operators.isTrue(vif.execute(context.stack))) {
continue;
}
}
// find resuable renderNode
WXComponent renderNode = null;
if (renderIndex < parent.getChildCount()) {
renderNode = parent.getChild(renderIndex);
// check is same statment, if true, it is usabled.
if (!isCreateFromNodeStatement(renderNode, component)) {
renderNode = null;
}
if (renderNode != null) {
if (renderNode.isWaste()) {
renderNode.setWaste(false);
}
}
}
// none resuable render node, create node, add to parent, but clear node's statement
if (renderNode == null) {
long start = System.currentTimeMillis();
renderNode = copyComponentTree(component, parent);
renderNode.setWaste(false);
WXDomObject renderNodeDomObject = (WXDomObject) renderNode.getDomObject();
if (renderNodeDomObject.getAttrs().getStatement() != null) {
renderNodeDomObject.getAttrs().getStatement().remove(WXStatement.WX_FOR);
// clear node's statement
renderNodeDomObject.getAttrs().getStatement().remove(WXStatement.WX_IF);
}
parentDomObject.add(renderNodeDomObject, renderIndex);
parent.addChild(renderNode, renderIndex);
updates.add(renderNode);
if (WXEnvironment.isApkDebugable()) {
WXLogUtils.d(WXRecyclerTemplateList.TAG, Thread.currentThread().getName() + renderNode.getRef() + renderNode.getDomObject().getType() + "statements copy component tree used " + (System.currentTimeMillis() - start));
}
}
doBindingAttrsEventAndRenderChildNode(renderNode, (WXDomObject) renderNode.getDomObject(), context, updates);
renderIndex++;
if (loop.size() > 0) {
context.stack.push(loop);
}
if (itemKey == null) {
context.stack.pop();
}
}
}
} else {
WXLogUtils.e(WXRecyclerTemplateList.TAG, vfor.toJSONString() + " not call vfor block, for pre compile");
}
// after v-for execute, remove component created pre v-for.
for (; renderIndex < parent.getChildCount(); renderIndex++) {
WXComponent wasteNode = parent.getChild(renderIndex);
if (!isCreateFromNodeStatement(wasteNode, component)) {
break;
}
wasteNode.setWaste(true);
}
return renderIndex - parent.indexOf(component);
}
// execute v-if context
if (vif != null) {
if (!Operators.isTrue(vif.execute(context.stack))) {
component.setWaste(true);
return 1;
} else {
component.setWaste(false);
}
}
}
doBindingAttrsEventAndRenderChildNode(component, domObject, context, updates);
return 1;
}
use of com.taobao.weex.dom.WXAttr in project incubator-weex by apache.
the class BasicListComponent method onBindViewHolder.
/**
* Bind the component of the position to the holder. Then flush the view.
*
* @param holder viewHolder, which holds reference to the view
* @param position position of component in list
*/
@Override
public void onBindViewHolder(final ListBaseViewHolder holder, int position) {
if (holder == null)
return;
holder.setComponentUsing(true);
WXComponent component = getChild(position);
if (component == null || (component instanceof WXRefresh) || (component instanceof WXLoading) || (component.getDomObject() != null && component.getDomObject().isFixed())) {
if (WXEnvironment.isApkDebugable()) {
WXLogUtils.d(TAG, "Bind WXRefresh & WXLoading " + holder);
}
if (component instanceof WXBaseRefresh && holder.getView() != null && component.getDomObject() != null && (component.getDomObject().getAttrs().get("holderBackground") != null)) {
Object holderBackground = component.getDomObject().getAttrs().get("holderBackground");
int color = WXResourceUtils.getColor(holderBackground.toString(), Color.WHITE);
holder.getView().setBackgroundColor(color);
holder.getView().setVisibility(View.VISIBLE);
holder.getView().postInvalidate();
}
return;
}
if (holder.getComponent() != null && holder.getComponent() instanceof WXCell) {
if (holder.isRecycled()) {
holder.bindData(component);
component.onRenderFinish(STATE_UI_FINISH);
}
if (mDragHelper == null || !mDragHelper.isDraggable()) {
return;
}
mTriggerType = (mTriggerType == null) ? DEFAULT_TRIGGER_TYPE : mTriggerType;
WXCell cell = (WXCell) holder.getComponent();
boolean isExcluded = DEFAULT_EXCLUDED;
if (cell.getDomObject() != null) {
WXAttr cellAttrs = cell.getDomObject().getAttrs();
isExcluded = WXUtils.getBoolean(cellAttrs.get(EXCLUDED), DEFAULT_EXCLUDED);
}
mDragHelper.setDragExcluded(holder, isExcluded);
// NOTICE: event maybe consumed by other views
if (DragTriggerType.PAN.equals(mTriggerType)) {
mDragHelper.setLongPressDragEnabled(false);
WXComponent anchorComponent = findComponentByAnchorName(cell, DRAG_ANCHOR);
if (anchorComponent != null && anchorComponent.getHostView() != null && !isExcluded) {
View anchor = anchorComponent.getHostView();
anchor.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
mDragHelper.startDrag(holder);
}
return true;
}
});
} else {
if (WXEnvironment.isApkDebugable()) {
if (!isExcluded) {
WXLogUtils.e(TAG, "[error] onBindViewHolder: the anchor component or view is not found");
} else {
WXLogUtils.d(TAG, "onBindViewHolder: position " + position + " is drag excluded");
}
}
}
} else if (DragTriggerType.LONG_PRESS.equals(mTriggerType)) {
mDragHelper.setLongPressDragEnabled(true);
}
}
}
use of com.taobao.weex.dom.WXAttr in project incubator-weex by apache.
the class WXRecyclerTemplateList method initComponentHostView.
@Override
protected BounceRecyclerView initComponentHostView(@NonNull Context context) {
final BounceRecyclerView bounceRecyclerView = new BounceRecyclerView(context, mLayoutType, mColumnCount, mColumnGap, getOrientation());
WXAttr attrs = getDomObject().getAttrs();
String transforms = (String) attrs.get(Constants.Name.TRANSFORM);
if (transforms != null) {
bounceRecyclerView.getInnerView().addItemDecoration(RecyclerTransform.parseTransforms(getOrientation(), transforms));
}
mItemAnimator = bounceRecyclerView.getInnerView().getItemAnimator();
if (attrs.get(NAME_TEMPLATE_CACHE_SIZE) != null) {
templateCacheSize = WXUtils.getInteger(attrs.get(NAME_TEMPLATE_CACHE_SIZE), templateCacheSize);
}
boolean hasFixedSize = false;
int itemViewCacheSize = 2;
if (attrs.get(NAME_ITEM_VIEW_CACHE_SIZE) != null) {
itemViewCacheSize = WXUtils.getNumberInt(getDomObject().getAttrs().get(NAME_ITEM_VIEW_CACHE_SIZE), itemViewCacheSize);
}
if (attrs.get(NAME_HAS_FIXED_SIZE) != null) {
hasFixedSize = WXUtils.getBoolean(attrs.get(NAME_HAS_FIXED_SIZE), hasFixedSize);
}
RecyclerViewBaseAdapter recyclerViewBaseAdapter = new RecyclerViewBaseAdapter<>(this);
recyclerViewBaseAdapter.setHasStableIds(true);
bounceRecyclerView.getInnerView().setItemAnimator(null);
if (itemViewCacheSize != 2) {
bounceRecyclerView.getInnerView().setItemViewCacheSize(itemViewCacheSize);
}
if (bounceRecyclerView.getSwipeLayout() != null) {
if (WXUtils.getBoolean(getDomObject().getAttrs().get("nestedScrollingEnabled"), false)) {
bounceRecyclerView.getSwipeLayout().setNestedScrollingEnabled(true);
}
}
bounceRecyclerView.getInnerView().setHasFixedSize(hasFixedSize);
bounceRecyclerView.setRecyclerViewBaseAdapter(recyclerViewBaseAdapter);
bounceRecyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
bounceRecyclerView.getInnerView().clearOnScrollListeners();
bounceRecyclerView.getInnerView().addOnScrollListener(mViewOnScrollListener);
bounceRecyclerView.getInnerView().addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
List<OnWXScrollListener> listeners = getInstance().getWXScrollListeners();
if (listeners != null && listeners.size() > 0) {
for (OnWXScrollListener listener : listeners) {
if (listener != null) {
View topView = recyclerView.getChildAt(0);
if (topView != null) {
int y = topView.getTop();
listener.onScrollStateChanged(recyclerView, 0, y, newState);
}
}
}
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
List<OnWXScrollListener> listeners = getInstance().getWXScrollListeners();
if (listeners != null && listeners.size() > 0) {
try {
for (OnWXScrollListener listener : listeners) {
if (listener != null) {
if (listener instanceof ICheckBindingScroller) {
if (((ICheckBindingScroller) listener).isNeedScroller(getRef(), null)) {
listener.onScrolled(recyclerView, dx, dy);
}
} else {
listener.onScrolled(recyclerView, dx, dy);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
bounceRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onGlobalLayout() {
BounceRecyclerView view;
if ((view = getHostView()) == null)
return;
mViewOnScrollListener.onScrolled(view.getInnerView(), 0, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
});
listUpdateRunnable = new Runnable() {
@Override
public void run() {
/**
* compute sticky position
*/
if (mStickyHelper != null) {
if (mStickyHelper.getStickyTypes().size() > 0) {
mStickyHelper.getStickyPositions().clear();
if (cellDataManager.listData != null) {
for (int i = 0; i < cellDataManager.listData.size(); i++) {
WXCell cell = getSourceTemplate(i);
if (cell == null) {
continue;
}
if (cell.isSticky()) {
mStickyHelper.getStickyPositions().add(i);
}
}
}
}
}
if (getHostView() != null && getHostView().getRecyclerViewBaseAdapter() != null) {
getHostView().getRecyclerViewBaseAdapter().notifyDataSetChanged();
}
if (WXEnvironment.isOpenDebugLog() && ENABLE_TRACE_LOG) {
WXLogUtils.d(TAG, "WXTemplateList notifyDataSetChanged");
}
}
};
return bounceRecyclerView;
}
use of com.taobao.weex.dom.WXAttr in project WeexErosFramework by bmfe.
the class BMWXText method updateFontSize.
private void updateFontSize() {
if (mChangeFontSize == null) {
return;
}
WXStyle styles = null;
WXAttr attrs = null;
if (getDomObject() != null) {
styles = getDomObject().getStyles();
attrs = getDomObject().getAttrs();
if ((styles != null && "iconfont".equals(styles.get("fontFamily"))) || (attrs != null && attrs.get("changeFont") != null && !Boolean.valueOf((String) attrs.get("changeFont")))) {
return;
}
}
float scale = 0;
// 获取fontScale字段
if (attrs != null && attrs.get("fontScale") != null) {
float fontScale = Float.valueOf((String) attrs.get("fontScale"));
mCurrentScale = fontScale / mCurrentScale;
}
if (mChangeFontSize.equals(mCurrentFontSize) && mCurrentScale == 1) {
return;
}
// 获取scale字段 在标准字体下不产生变化
if (attrs != null && attrs.get("scale") != null && !(scale > 0)) {
scale = Float.valueOf((String) attrs.get("scale"));
float change = getFixedEnlarge(mChangeFontSize, scale);
float current = getFixedEnlarge(mCurrentFontSize, scale);
scale = change / current;
}
// 根据全局字体配置设置字体大小
if (!(scale > 0)) {
float current = getEnlarge(mCurrentFontSize);
float change = getEnlarge(mChangeFontSize);
scale = change / current * mCurrentScale;
}
if (getDomObject() != null && getDomObject().getStyles() != null) {
WXStyle wxStyle = getDomObject().getStyles();
Object object = wxStyle.get("fontSize");
if (object instanceof Integer) {
int fontSize = (int) object;
int changeFontSize = (int) (fontSize * (scale));
wxStyle.put("fontSize", changeFontSize);
}
// 设置lineHeight
Object lineHeight = wxStyle.get("lineHeight");
if (lineHeight instanceof Integer) {
int target = (int) lineHeight;
wxStyle.put("lineHeight", (int) (target * scale));
}
updateStyle(wxStyle);
}
mCurrentFontSize = mChangeFontSize;
}
Aggregations