From 38b836827af40921694945536571fd00ea5487fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=AF=E6=AD=8C?= Date: Wed, 9 Feb 2022 09:08:16 +0800 Subject: [PATCH] layoutinflater add defaultStyle support. --- src/gui/core/assets.cc | 2 +- src/gui/core/graph_device.cc | 8 +++++--- src/gui/widget/layoutinflater.cc | 34 ++++++++++++++++++++++---------- src/gui/widget/layoutinflater.h | 17 ++++++++++------ src/gui/widget/tablayout.cc | 3 ++- src/gui/widget/textview.cc | 3 ++- src/gui/widget/view.cc | 4 ++-- 7 files changed, 47 insertions(+), 24 deletions(-) diff --git a/src/gui/core/assets.cc b/src/gui/core/assets.cc index 1f0f9109..94695cdb 100755 --- a/src/gui/core/assets.cc +++ b/src/gui/core/assets.cc @@ -178,7 +178,7 @@ ZIPArchive*Assets::getResource(const std::string&fullResId,std::string*relativeR guessExtension(pak,resname); if(relativeResid) *relativeResid=resname; } - LOGV_IF(resname.size(),"resource for [%s::%s:%s] is%s found",package.c_str(),fullResId.c_str(),resname.c_str(),(pak?"":" not")); + LOGV_IF(pak==nullptr&&resname.size(),"resource for [%s] is%s found",fullResId.c_str(),(pak?"":" not")); return pak; } diff --git a/src/gui/core/graph_device.cc b/src/gui/core/graph_device.cc index de695997..e7ff47e9 100755 --- a/src/gui/core/graph_device.cc +++ b/src/gui/core/graph_device.cc @@ -111,9 +111,11 @@ void GraphDevice::composeSurfaces(){ WindowManager::getInstance().enumWindows([&wSurfaces,&wBounds,&wins](Window*w)->bool{ if(w->getVisibility()!=View::VISIBLE||w->mVisibleRgn->empty()==false){ Rect rcw=w->getBound(); - wSurfaces.push_back(w->mAttachInfo->mCanvas); - wBounds.push_back(rcw); - wins.push_back(w); + if(w->mAttachInfo->mCanvas){ + wSurfaces.push_back(w->mAttachInfo->mCanvas); + wBounds.push_back(rcw); + wins.push_back(w); + } }return true; }); primaryContext->set_operator(Cairo::Context::Operator::SOURCE); diff --git a/src/gui/widget/layoutinflater.cc b/src/gui/widget/layoutinflater.cc index 3b1df2a4..d89c9c0b 100755 --- a/src/gui/widget/layoutinflater.cc +++ b/src/gui/widget/layoutinflater.cc @@ -16,24 +16,36 @@ LayoutInflater*LayoutInflater::from(Context*context){ return new LayoutInflater(context); } +LayoutInflater::INFLATERMAPPER& LayoutInflater::getInflaterMap(){ + static LayoutInflater::INFLATERMAPPER mFlateMapper; + return mFlateMapper; +} + +LayoutInflater::STYLEMAPPER& LayoutInflater::getStyleMap(){ + static LayoutInflater::STYLEMAPPER mDefaultStyle; + return mDefaultStyle; +} + +const std::string LayoutInflater::getDefaultStyle(const std::string&name)const{ + LayoutInflater::STYLEMAPPER& maps=getStyleMap(); + auto it=maps.find(name); + return it==maps.end()?std::string():it->second; +} + LayoutInflater::ViewInflater LayoutInflater::getInflater(const std::string&name){ - std::map&maps=LayoutInflater::getMap(); const size_t pt = name.rfind('.'); + LayoutInflater::INFLATERMAPPER &maps =getInflaterMap(); const std::string sname=(pt!=std::string::npos)?name.substr(pt+1):name; auto it=maps.find(sname); return (it!=maps.end())?it->second:nullptr; } -std::map&LayoutInflater::getMap(){ - static std::map maps; - return maps; -} - -bool LayoutInflater::registInflater(const std::string&name,LayoutInflater::ViewInflater inflater){ - std::map&maps=LayoutInflater::getMap(); +bool LayoutInflater::registInflater(const std::string&name,const std::string&defstyle,LayoutInflater::ViewInflater inflater){ + LayoutInflater::INFLATERMAPPER&maps =getInflaterMap(); if(maps.find(name)!=maps.end()) return false; - maps.insert(std::map::value_type(name,inflater)); + maps.insert(INFLATERMAPPER::value_type(name,inflater)); + getStyleMap().insert(std::pair(name,defstyle)); return true; } @@ -72,7 +84,9 @@ static void startElement(void *userData, const XML_Char *name, const XML_Char ** LOGE("Unknown Parser for %s",name); return; } - const std::string stname=atts.getString("style"); + std::string stname=atts.getString("style"); + if(stname.empty()) + stname=LayoutInflater::from(pd->ctx)->getDefaultStyle(name); if(!stname.empty()){ AttributeSet style=pd->ctx->obtainStyledAttributes(stname); atts.inherit(style); diff --git a/src/gui/widget/layoutinflater.h b/src/gui/widget/layoutinflater.h index 9c67815d..3859b143 100755 --- a/src/gui/widget/layoutinflater.h +++ b/src/gui/widget/layoutinflater.h @@ -12,25 +12,30 @@ public: private: Context*mContext; LayoutInflater(Context*ctx); - static std::map&getMap(); + typedef std::mapINFLATERMAPPER; + typedef std::mapSTYLEMAPPER; + static INFLATERMAPPER& getInflaterMap(); + static STYLEMAPPER& getStyleMap(); protected: View* inflate(std::istream&stream,ViewGroup*root,bool attachToRoot); public: static LayoutInflater*from(Context*context); static ViewInflater getInflater(const std::string&); - static bool registInflater(const std::string&name,ViewInflater fun); + static bool registInflater(const std::string&name,const std::string&,ViewInflater fun); + const std::string getDefaultStyle(const std::string&name)const; View* inflate(const std::string&resource,ViewGroup* root, bool attachToRoot=true); }; template class InflaterRegister{ public: - InflaterRegister(const std::string&name){ - LayoutInflater::registInflater(name,[](Context*ctx,const AttributeSet&attr)->View*{return new T(ctx,attr);}); + InflaterRegister(const std::string&name,const std::string&defstyle){ + LayoutInflater::registInflater(name,defstyle,[](Context*ctx,const AttributeSet&attr)->View*{return new T(ctx,attr);}); } }; -#define DECLARE_WIDGET(T) static InflaterRegister widget_inflater_##T(#T); -#define DECLARE_WIDGET2(T,name) static InflaterRegister widget_inflater_##T_##name(#name); +#define DECLARE_WIDGET(T) static InflaterRegister widget_inflater_##T(#T,""); +#define DECLARE_WIDGET2(T,style) static InflaterRegister widget_inflater_##T(#T,style); +#define DECLARE_WIDGET3(T,name,style) static InflaterRegister widget_inflater_##name(#name,style); }//endof namespace #endif diff --git a/src/gui/widget/tablayout.cc b/src/gui/widget/tablayout.cc index cebfe1c9..06ceec85 100755 --- a/src/gui/widget/tablayout.cc +++ b/src/gui/widget/tablayout.cc @@ -766,7 +766,8 @@ int TabLayout::getTabMaxWidth() { /////////////////////////////////////////////////////////////////////////////////////////// -static InflaterRegister widget_inflater_tabitem("TabItem"); +DECLARE_WIDGET3(TabLayout::TabItem,TabItem,"") + TabLayout::TabItem::TabItem():View(0,0){ mIcon=nullptr; } diff --git a/src/gui/widget/textview.cc b/src/gui/widget/textview.cc index a010843d..aa916e49 100755 --- a/src/gui/widget/textview.cc +++ b/src/gui/widget/textview.cc @@ -27,7 +27,7 @@ namespace cdroid { -DECLARE_WIDGET(TextView) +DECLARE_WIDGET2(TextView,"cdroid:style/textViewStyle") class Drawables{ public: @@ -275,6 +275,7 @@ TextView::TextView(Context*ctx,const AttributeSet& attrs) if(attrs.hasAttribute("textAppearance")){ TextAppearanceAttributes attributes; attributes.readTextAppearance(ctx,attrs); + applyTextAppearance(&attributes); } setMarqueeRepeatLimit(attrs.getInt("marqueeRepeatLimit",mMarqueeRepeatLimit)); diff --git a/src/gui/widget/view.cc b/src/gui/widget/view.cc index 4d97fab4..50498e67 100755 --- a/src/gui/widget/view.cc +++ b/src/gui/widget/view.cc @@ -300,7 +300,7 @@ void View::initView(){ mID = NO_ID; mAutofillViewId =NO_ID; mAccessibilityViewId=NO_ID; - mDrawingCacheBackgroundColor =0xFF000000; + mDrawingCacheBackgroundColor =0; mContext = nullptr; mParent = nullptr; mAttachInfo = nullptr; @@ -2635,9 +2635,9 @@ bool View::draw(Canvas&canvas,ViewGroup*parent,long drawingTime){ } } else if (cache != nullptr) { mPrivateFlags &= ~PFLAG_DIRTY_MASK; + cache->flush(); canvas.save(); canvas.reset_clip(); - cache->flush(); canvas.set_source(cache,0,0); if(alpha<1)canvas.paint_with_alpha(alpha); else canvas.paint();