perf(gui): reduce memory usage for widget style sheets

This commit is contained in:
Liu
2019-03-04 22:48:33 +08:00
parent 5a918dabfe
commit 3790fbd287
13 changed files with 358 additions and 162 deletions
+9 -4
View File
@@ -35,6 +35,8 @@
LCUI_BEGIN_HEADER
/* clang-format on */
enum LCUI_CSSFontStyleKey {
key_color,
key_font_size,
@@ -61,10 +63,13 @@ typedef struct LCUI_CSSFontStyleRec_ {
LCUI_StyleValue white_space;
} LCUI_CSSFontStyleRec, *LCUI_CSSFontStyle;
#define Widget_SetFontStyle(W, K, V, T) do {\
int key = LCUI_GetFontStyleKey( K ); \
SetStyle( (W)->custom_style, key, V, T ); \
} while( 0 )
#define Widget_SetFontStyle(W, K, V, T) \
do { \
int key = LCUI_GetFontStyleKey(K); \
Widget_SetStyle(W, key, V, T); \
} while (0)
/* clang-format off */
int LCUI_GetFontStyleKey(int key);
+37 -12
View File
@@ -147,9 +147,11 @@ enum LCUI_StyleKeyName {
typedef struct LCUI_StyleSheetRec_ {
LCUI_Style sheet;
size_t length;
int length;
} LCUI_StyleSheetRec, *LCUI_StyleSheet;
typedef const LCUI_StyleSheetRec *LCUI_CachedStyleSheet;
typedef LinkedList LCUI_StyleListRec;
typedef LinkedList* LCUI_StyleList;
@@ -180,32 +182,53 @@ typedef struct LCUI_SelectorRec_ {
/* clang-format on */
#define CheckStyleType(S, K, T) (S->sheet[K].is_valid && S->sheet[K].type == LCUI_STYPE_##T)
#define CheckStyleType(S, K, T) \
(S->sheet[K].is_valid && S->sheet[K].type == LCUI_STYPE_##T)
#define SetStyle(S, NAME, VAL, TYPE) S->sheet[NAME].is_valid = TRUE, \
S->sheet[NAME].type = LCUI_STYPE_##TYPE, \
S->sheet[NAME].val_##TYPE = VAL
#define SetStyle(S, NAME, VAL, TYPE) \
S->sheet[NAME].is_valid = TRUE, \
S->sheet[NAME].type = LCUI_STYPE_##TYPE, \
S->sheet[NAME].val_##TYPE = VAL
#define UnsetStyle(S, NAME) S->sheet[NAME].is_valid = FALSE, \
S->sheet[NAME].type = LCUI_STYPE_NONE, \
S->sheet[NAME].val_int = 0
#define UnsetStyle(S, NAME) \
S->sheet[NAME].is_valid = FALSE, \
S->sheet[NAME].type = LCUI_STYPE_NONE, S->sheet[NAME].val_int = 0
#define LCUI_FindStyleSheet(S, L) LCUI_FindStyleSheetFromGroup(0, NULL, S, L)
#define StyleSheet_GetStyle(S, K) &((S)->sheet[K])
LCUI_API LCUI_StyleList StyleList(void);
LCUI_API LCUI_StyleListNode StyleList_GetNode(LCUI_StyleList list, int key);
LCUI_API int StyleList_RemoveNode(LCUI_StyleList list, int key);
LCUI_API LCUI_StyleListNode StyleList_AddNode(LCUI_StyleList list, int key);
LCUI_API void StyleList_Delete(LCUI_StyleList list);
LCUI_API LCUI_StyleSheet StyleSheet(void);
LCUI_API void StyleSheet_Clear(LCUI_StyleSheet ss);
LCUI_API void StyleSheet_Delete(LCUI_StyleSheet ss);
LCUI_API int StyleSheet_Merge(LCUI_StyleSheet dest, LCUI_StyleSheet src);
LCUI_API int StyleSheet_Merge(LCUI_StyleSheet dest,
const LCUI_StyleSheetRec *src);
LCUI_API int StyleSheet_Replace(LCUI_StyleSheet dest, LCUI_StyleSheet src);
LCUI_API int StyleSheet_MergeList(LCUI_StyleSheet ss, LCUI_StyleList list);
LCUI_API int StyleSheet_Replace(LCUI_StyleSheet dest,
const LCUI_StyleSheetRec *src);
LCUI_API LCUI_Selector Selector(const char *selector);
LCUI_API LCUI_Selector Selector_Copy(LCUI_Selector selector);
LCUI_API int Selector_AppendNode(LCUI_Selector selector,
LCUI_SelectorNode node);
LCUI_API void Selector_Update(LCUI_Selector s);
LCUI_API void Selector_Delete(LCUI_Selector s);
@@ -223,8 +246,8 @@ LCUI_API void SelectorNode_Delete(LCUI_SelectorNode node);
LCUI_API LCUI_BOOL SelectorNode_Match(LCUI_SelectorNode sn1,
LCUI_SelectorNode sn2);
LCUI_API int LCUI_PutStyleSheet(LCUI_Selector selector,
LCUI_StyleSheet in_ss, const char *space);
LCUI_API int LCUI_PutStyleSheet(LCUI_Selector selector, LCUI_StyleSheet in_ss,
const char *space);
/**
* 从指定组中查找样式表
@@ -236,6 +259,8 @@ LCUI_API int LCUI_PutStyleSheet(LCUI_Selector selector,
LCUI_API int LCUI_FindStyleSheetFromGroup(int group, const char *name,
LCUI_Selector s, LinkedList *list);
LCUI_API LCUI_CachedStyleSheet LCUI_GetCachedStyleSheet(LCUI_Selector s);
LCUI_API void LCUI_GetStyleSheet(LCUI_Selector s, LCUI_StyleSheet out_ss);
LCUI_API void LCUI_PrintStyleSheetsBySelector(LCUI_Selector s);
+14 -7
View File
@@ -109,7 +109,7 @@ typedef struct LCUI_WidgetBoxModelRec_ {
typedef struct LCUI_WidgetTaskBoxRec_ {
LCUI_BOOL for_self; /**< 标志,指示当前部件是否有待处理的任务 */
LCUI_BOOL for_children; /**< 标志,指示是否有待处理的子级部件 */
LCUI_BOOL buffer[LCUI_WTASK_TOTAL_NUM]; /**< 记录缓存 */
LCUI_BOOL states[LCUI_WTASK_TOTAL_NUM]; /**< 各个任务的状态标记 */
} LCUI_WidgetTaskBoxRec;
/** 部件状态 */
@@ -154,7 +154,7 @@ typedef struct LCUI_WidgetDataEntryRec_ {
} LCUI_WidgetDataEntryRec;
typedef struct LCUI_WidgetData_ {
uint_t length;
unsigned length;
LCUI_WidgetDataEntryRec *list;
} LCUI_WidgetData;
@@ -186,8 +186,8 @@ typedef struct LCUI_WidgetRec_ {
LCUI_Rect2F margin; /**< 外边距框 */
LCUI_WidgetBoxModelRec box; /**< 部件的各个区域信息 */
LCUI_StyleSheet style; /**< 当前完整样式表 */
LCUI_StyleSheet custom_style; /**< 自定义样式表 */
LCUI_StyleSheet inherited_style; /**< 通过继承得到的样式表 */
LCUI_StyleList custom_style; /**< 自定义样式表 */
LCUI_CachedStyleSheet inherited_style; /**< 通过继承得到的样式表 */
LCUI_WidgetStyle computed_style; /**< 已经计算的样式数据 */
LCUI_Widget parent; /**< 父部件 */
LinkedList children; /**< 子部件 */
@@ -205,10 +205,17 @@ typedef struct LCUI_WidgetRec_ {
/* clang-format on */
#define Widget_SetStyle(W, K, V, T) SetStyle((W)->custom_style, K, V, T)
#define Widget_UnsetStyle(W, K) UnsetStyle((W)->custom_style, K)
#define Widget_SetStyle(W, K, VAL, TYPE) \
do { \
LCUI_Style _s; \
_s = Widget_GetStyle(W, K); \
_s->is_valid = TRUE; \
_s->type = LCUI_STYPE_##TYPE; \
_s->val_##TYPE = VAL; \
Widget_AddTaskByStyle(W, K); \
} while (0)
#define Widget_CheckStyleType(W, K, T) CheckStyleType((W)->style, K, T)
#define Widget_CheckStyleValid(W, K) W->style->sheet[K].is_valid
#define Widget_HasAbsolutePosition(W) \
((W)->computed_style.position == SV_ABSOLUTE)
+4
View File
@@ -68,11 +68,15 @@ LCUI_API void Widget_ResizeWithSurface(LCUI_Widget w, float width,
LCUI_API LCUI_Style Widget_GetStyle(LCUI_Widget w, int key);
LCUI_API int Widget_UnsetStyle(LCUI_Widget w, int key);
LCUI_API LCUI_Style Widget_GetInheritedStyle(LCUI_Widget w, int key);
LCUI_API LCUI_BOOL Widget_CheckStyleBooleanValue(LCUI_Widget w, int key,
LCUI_BOOL value);
LCUI_API LCUI_BOOL Widget_CheckStyleValid(LCUI_Widget w, int key) ;
LCUI_API void Widget_SetVisibility(LCUI_Widget w, const char *value);
LCUI_API void Widget_SetVisible(LCUI_Widget w);
+2 -3
View File
@@ -37,9 +37,6 @@ void LCUIWidget_InitStyle(void);
/** 销毁,释放资源 */
void LCUIWidget_FreeStyle(void);
/** 计算部件继承得到的样式表 */
LCUI_API void Widget_GetInheritStyle(LCUI_Widget w, LCUI_StyleSheet out_ss);
/** 打印部件的样式表 */
LCUI_API void Widget_PrintStyleSheets(LCUI_Widget w);
@@ -49,6 +46,8 @@ LCUI_API void Widget_UpdateStyle(LCUI_Widget w, LCUI_BOOL is_refresh_all);
/** 更新当前部件的子级部件样式 */
LCUI_API void Widget_UpdateChildrenStyle(LCUI_Widget w, LCUI_BOOL is_refresh_all);
LCUI_API void Widget_AddTaskByStyle(LCUI_Widget w, int key);
/** 直接更新当前部件的样式 */
LCUI_API void Widget_ExecUpdateStyle(LCUI_Widget w, LCUI_BOOL is_update_all);
+6 -2
View File
@@ -40,7 +40,11 @@ LCUI_API void Widget_UpdateTaskStatus(LCUI_Widget widget);
LCUI_API void Widget_AddTask(LCUI_Widget widget, int task_type);
/** 处理部件中当前积累的任务 */
LCUI_API int Widget_Update(LCUI_Widget w);
LCUI_API size_t Widget_Update(LCUI_Widget w);
/** 根据给定的上下文来处理部件中当前积累的任务 */
LCUI_API size_t Widget_UpdateWithContext(LCUI_Widget w,
LCUI_WidgetTaskContext ctx);
/** 为子级部件添加任务 */
LCUI_API void Widget_AddTaskForChildren(LCUI_Widget widget, int task);
@@ -52,7 +56,7 @@ LCUI_API void LCUIWidget_InitTasks(void);
LCUI_API void LCUIWidget_FreeTasks(void);
/** 处理一次当前积累的部件任务 */
LCUI_API void LCUIWidget_Update(void);
LCUI_API size_t LCUIWidget_Update(void);
/** 刷新所有部件的样式 */
LCUI_API void LCUIWidget_RefreshStyle(void);
+4 -3
View File
@@ -96,6 +96,7 @@ static void DrawBorder(LCUI_PaintContext paint)
void LCUIDisplay_Update(void)
{
size_t count;
LCUI_Surface surface;
LinkedListNode *node;
SurfaceRecord record = NULL;
@@ -104,7 +105,7 @@ void LCUIDisplay_Update(void)
return;
}
LCUICursor_Update();
LCUIWidget_Update();
count = LCUIWidget_Update();
/* 遍历当前的 surface 记录列表 */
for (LinkedList_Each(node, &display.surfaces)) {
record = node->data;
@@ -278,8 +279,8 @@ static void LCUIDisplay_BindSurface(LCUI_Widget widget)
LinkedList_Init(&record->rects);
Surface_SetCaptionW(record->surface, widget->title);
LCUIMetrics_ComputeRectActual(&rect, &widget->box.canvas);
if (widget->style->sheet[key_top].is_valid &&
widget->style->sheet[key_left].is_valid) {
if (Widget_CheckStyleValid(widget, key_top) &&
Widget_CheckStyleValid(widget, key_left)) {
Surface_Move(record->surface, rect.x, rect.y);
}
Surface_Resize(record->surface, rect.width, rect.height);
+103 -19
View File
@@ -478,7 +478,7 @@ static void MergeStyle(LCUI_Style dst, LCUI_Style src)
dst->type = src->type;
}
static DeleteStyleListNode(LCUI_StyleListNode node)
static void DeleteStyleListNode(LCUI_StyleListNode node)
{
DestroyStyle(&node->style);
free(node);
@@ -519,29 +519,70 @@ void StyleSheet_Delete(LCUI_StyleSheet ss)
free(ss);
}
static size_t StyleList_Merge(LCUI_StyleList list, LCUI_StyleSheet sheet)
LCUI_StyleListNode StyleList_GetNode(LCUI_StyleList list, int key)
{
size_t i, count;
LinkedListNode *node;
LCUI_StyleListNode snode;
for (LinkedList_Each(node, list)) {
snode = node->data;
if (snode->key == key) {
return snode;
}
}
return NULL;
}
int StyleList_RemoveNode(LCUI_StyleList list, int key)
{
LinkedListNode *node;
LCUI_StyleListNode snode;
for (LinkedList_Each(node, list)) {
snode = node->data;
if (snode->key == key) {
LinkedList_Unlink(list, node);
DestroyStyle(&snode->style);
free(snode);
return 0;
}
}
return -1;
}
LCUI_StyleListNode StyleList_AddNode(LCUI_StyleList list, int key)
{
LCUI_StyleListNode node;
node = malloc(sizeof(LCUI_StyleListNodeRec));
node->key = key;
node->style.is_valid = FALSE;
node->node.data = node;
LinkedList_AppendNode(list, &node->node);
return node;
}
static unsigned StyleList_Merge(LCUI_StyleList list, const LCUI_StyleSheetRec *sheet)
{
int i, count;
LCUI_StyleListNode node;
for (count = 0, i = 0; i < sheet->length; ++i) {
if (!sheet->sheet[i].is_valid) {
continue;
}
node = malloc(sizeof(LCUI_StyleListNodeRec));
node->key = (int)i;
node = StyleList_AddNode(list, i);
MergeStyle(&node->style, &sheet->sheet[i]);
node->node.data = node;
LinkedList_AppendNode(list, &node->node);
count += 1;
}
return count;
}
int StyleSheet_Merge(LCUI_StyleSheet dest, LCUI_StyleSheet src)
int StyleSheet_Merge(LCUI_StyleSheet dest, const LCUI_StyleSheetRec *src)
{
int i;
size_t size;
LCUI_Style s;
size_t i, size;
if (src->length > dest->length) {
size = sizeof(LCUI_StyleRec) * src->length;
@@ -568,7 +609,8 @@ int StyleSheet_MergeList(LCUI_StyleSheet ss, LCUI_StyleList list)
LCUI_Style s;
LCUI_StyleListNode snode;
LinkedListNode *node;
size_t i = 0, count = 0, size;
size_t size;
int i = 0, count = 0;
for (LinkedList_Each(node, list)) {
snode = node->data;
@@ -584,7 +626,7 @@ int StyleSheet_MergeList(LCUI_StyleSheet ss, LCUI_StyleList list)
ss->sheet = s;
ss->length = snode->key + 1;
}
if (!ss->sheet[snode->key].is_valid) {
if (!ss->sheet[snode->key].is_valid && snode->style.is_valid) {
MergeStyle(&ss->sheet[snode->key], &snode->style);
++count;
}
@@ -592,7 +634,7 @@ int StyleSheet_MergeList(LCUI_StyleSheet ss, LCUI_StyleList list)
return (int)count;
}
int StyleSheet_Replace(LCUI_StyleSheet dest, LCUI_StyleSheet src)
int StyleSheet_Replace(LCUI_StyleSheet dest, const LCUI_StyleSheetRec *src)
{
LCUI_Style s;
size_t i, count, size;
@@ -923,6 +965,26 @@ void Selector_Update(LCUI_Selector s)
s->hash = hash;
}
int Selector_AppendNode(LCUI_Selector selector, LCUI_SelectorNode node)
{
const unsigned char *p;
if (selector->length >= MAX_SELECTOR_DEPTH) {
LOG("[css] warning: the number of nodes in the selector has "
"exceeded "
"the %d limit\n",
MAX_SELECTOR_DEPTH);
return -1;
}
selector->nodes[selector->length++] = node;
selector->nodes[selector->length] = NULL;
p = (unsigned char *)node->fullname;
while (*p) {
selector->hash = ((selector->hash << 5) + selector->hash) + (*p++);
}
return 0;
}
LCUI_Selector Selector(const char *selector)
{
const char *p;
@@ -1044,6 +1106,24 @@ LCUI_Selector Selector(const char *selector)
return s;
}
LCUI_Selector Selector_Copy(LCUI_Selector selector)
{
int i;
LCUI_Selector s;
s = Selector(NULL);
for (i = 0; i < selector->length; ++i) {
s->nodes[i] = NEW(LCUI_SelectorNodeRec, 1);
SelectorNode_Copy(s->nodes[i], selector->nodes[i]);
}
s->nodes[selector->length] = NULL;
s->length = selector->length;
s->hash = selector->hash;
s->rank = selector->rank;
s->batch_num = selector->batch_num;
return s;
}
static void DeleteStyleNode(StyleNode node)
{
if (node->space) {
@@ -1476,20 +1556,16 @@ void LCUI_PrintCSSLibrary(void)
LOG("style library end\n");
}
void LCUI_GetStyleSheet(LCUI_Selector s, LCUI_StyleSheet out_ss)
LCUI_CachedStyleSheet LCUI_GetCachedStyleSheet(LCUI_Selector s)
{
LinkedList list;
LinkedListNode *node;
LCUI_StyleSheet ss;
LinkedList_Init(&list);
StyleSheet_Clear(out_ss);
LCUIMutex_Lock(&library.mutex);
ss = Dict_FetchValue(library.cache, &s->hash);
if (ss) {
StyleSheet_Replace(out_ss, ss);
LCUIMutex_Unlock(&library.mutex);
return;
return ss;
}
ss = StyleSheet();
LCUI_FindStyleSheet(s, &list);
@@ -1499,8 +1575,16 @@ void LCUI_GetStyleSheet(LCUI_Selector s, LCUI_StyleSheet out_ss)
}
LinkedList_Clear(&list, NULL);
Dict_Add(library.cache, &s->hash, ss);
return ss;
}
void LCUI_GetStyleSheet(LCUI_Selector s, LCUI_StyleSheet out_ss)
{
const LCUI_StyleSheetRec *ss;
ss = LCUI_GetCachedStyleSheet(s);
StyleSheet_Clear(out_ss);
StyleSheet_Replace(out_ss, ss);
LCUIMutex_Unlock(&library.mutex);
}
void LCUI_PrintStyleSheetsBySelector(LCUI_Selector s)
+2 -2
View File
@@ -364,7 +364,7 @@ static void ScrollBar_UpdateSize(LCUI_Widget w)
if (size > box_size && box_size > 0) {
n = box_size / size;
}
SetStyle(slider->custom_style, key_width, n, scale);
Widget_SetStyle(slider, key_width, n, scale);
} else {
if (scrollbar->target) {
size = scrollbar->target->box.outer.height;
@@ -375,7 +375,7 @@ static void ScrollBar_UpdateSize(LCUI_Widget w)
if (size > box_size && box_size > 0) {
n = box_size / size;
}
SetStyle(slider->custom_style, key_height, n, scale);
Widget_SetStyle(slider, key_height, n, scale);
}
ScrollBar_SetPosition(w, scrollbar->pos);
Widget_UpdateStyle(slider, FALSE);
-2
View File
@@ -109,8 +109,6 @@ static void Widget_Init(LCUI_Widget widget)
widget->state = LCUI_WSTATE_CREATED;
widget->trigger = EventTrigger();
widget->style = StyleSheet();
widget->custom_style = StyleSheet();
widget->inherited_style = StyleSheet();
widget->computed_style.opacity = 1.0;
widget->computed_style.visible = TRUE;
widget->computed_style.focusable = FALSE;
+33 -2
View File
@@ -120,12 +120,38 @@ void Widget_ResizeWithSurface(LCUI_Widget w, float width, float height)
LCUI_Style Widget_GetStyle(LCUI_Widget w, int key)
{
assert(key >= 0 && key < w->custom_style->length);
return &w->custom_style->sheet[key];
LCUI_StyleListNode node;
if (w->custom_style) {
node = StyleList_GetNode(w->custom_style, key);
if (node) {
return &node->style;
}
} else {
w->custom_style = StyleList();
}
node = StyleList_AddNode(w->custom_style, key);
return &node->style;
}
int Widget_UnsetStyle(LCUI_Widget w, int key)
{
if (!w->custom_style) {
return -1;
}
Widget_AddTaskByStyle(w, key);
return StyleList_RemoveNode(w->custom_style, key);
}
LCUI_Style Widget_GetInheritedStyle(LCUI_Widget w, int key)
{
LCUI_Selector selector;
if (!w->inherited_style) {
selector = Widget_GetSelector(w);
w->inherited_style = LCUI_GetCachedStyleSheet(selector);
Selector_Delete(selector);
}
assert(key >= 0 && key < w->inherited_style->length);
return &w->inherited_style->sheet[key];
}
@@ -138,6 +164,11 @@ LCUI_BOOL Widget_CheckStyleBooleanValue(LCUI_Widget w, int key, LCUI_BOOL value)
s->val_bool == value;
}
LCUI_BOOL Widget_CheckStyleValid(LCUI_Widget w, int key)
{
return w->style && w->style->sheet[key].is_valid;
}
void Widget_SetVisibility(LCUI_Widget w, const char *value)
{
LCUI_Style s = Widget_GetStyle(w, key_visibility);
+31 -46
View File
@@ -41,7 +41,7 @@
#define ARRAY_LEN(ARR) sizeof(ARR) / sizeof(ARR[0])
typedef struct LCUI_TaskCacheStatus {
size_t start, end;
int start, end;
LCUI_WidgetTaskType task;
LCUI_BOOL is_valid;
} LCUI_TaskStatus;
@@ -71,6 +71,7 @@ LCUI_SelectorNode Widget_GetSelectorNode(LCUI_Widget w)
int i;
ASSIGN(sn, LCUI_SelectorNode);
ZEROSET(sn, LCUI_SelectorNode);
if (w->id) {
sn->id = strdup2(w->id);
}
@@ -89,7 +90,7 @@ LCUI_SelectorNode Widget_GetSelectorNode(LCUI_Widget w)
LCUI_Selector Widget_GetSelector(LCUI_Widget w)
{
int ni = 0;
int i = 0;
LinkedList list;
LCUI_Selector s;
LCUI_Widget parent;
@@ -110,13 +111,13 @@ LCUI_Selector Widget_GetSelector(LCUI_Widget w)
}
for (LinkedList_EachReverse(node, &list)) {
parent = node->data;
s->nodes[ni] = Widget_GetSelectorNode(parent);
s->rank += s->nodes[ni]->rank;
ni += 1;
s->nodes[i] = Widget_GetSelectorNode(parent);
s->rank += s->nodes[i]->rank;
i += 1;
}
LinkedList_Clear(&list, NULL);
s->nodes[ni] = NULL;
s->length = ni;
s->nodes[i] = NULL;
s->length = i;
Selector_Update(s);
return s;
}
@@ -191,14 +192,6 @@ int Widget_HandleChildrenStyleChange(LCUI_Widget w, int type, const char *name)
return count;
}
void Widget_GetInheritStyle(LCUI_Widget w, LCUI_StyleSheet out_ss)
{
LCUI_Selector s;
s = Widget_GetSelector(w);
LCUI_GetStyleSheet(s, out_ss);
Selector_Delete(s);
}
void Widget_PrintStyleSheets(LCUI_Widget w)
{
LCUI_Selector s = Widget_GetSelector(w);
@@ -209,7 +202,6 @@ void Widget_PrintStyleSheets(LCUI_Widget w)
void Widget_UpdateStyle(LCUI_Widget w, LCUI_BOOL is_refresh_all)
{
if (is_refresh_all) {
StyleSheet_Clear(w->style);
Widget_AddTask(w, LCUI_WTASK_REFRESH_STYLE);
} else {
Widget_AddTask(w, LCUI_WTASK_UPDATE_STYLE);
@@ -226,11 +218,9 @@ void Widget_UpdateChildrenStyle(LCUI_Widget w, LCUI_BOOL is_refresh_all)
}
}
void Widget_ExecUpdateStyle(LCUI_Widget w, LCUI_BOOL is_update_all)
void Widget_AddTaskByStyle(LCUI_Widget w, int key)
{
size_t i, key;
LCUI_Style s;
LCUI_StyleSheet ss;
size_t i;
LCUI_TaskStatus task_status[] = {
{ key_visibility, key_visibility, LCUI_WTASK_VISIBLE, TRUE },
{ key_display, key_display, LCUI_WTASK_DISPLAY, TRUE },
@@ -258,48 +248,43 @@ void Widget_ExecUpdateStyle(LCUI_Widget w, LCUI_BOOL is_update_all)
{ key_pointer_events, key_focusable, LCUI_WTASK_PROPS, TRUE }
};
for (i = 0; i < ARRAY_LEN(task_status); ++i) {
if (key >= task_status[i].start && key <= task_status[i].end) {
if (!task_status[i].is_valid) {
break;
}
task_status[i].is_valid = FALSE;
Widget_AddTask(w, task_status[i].task);
}
}
}
void Widget_ExecUpdateStyle(LCUI_Widget w, LCUI_BOOL is_update_all)
{
StyleSheet_Clear(w->style);
if (is_update_all) {
/* 刷新该部件的相关数据 */
if (w->proto && w->proto->refresh) {
w->proto->refresh(w);
}
Widget_GetInheritStyle(w, w->inherited_style);
}
ss = w->style;
w->style = StyleSheet();
StyleSheet_Merge(w->style, w->custom_style);
if (w->custom_style) {
StyleSheet_MergeList(w->style, w->custom_style);
}
StyleSheet_Merge(w->style, w->inherited_style);
/* 对比两张样式表,确定哪些需要更新 */
for (key = 0; key < STYLE_KEY_TOTAL; ++key) {
s = &w->style->sheet[key];
if (ss->sheet[key].is_valid == s->is_valid &&
ss->sheet[key].type == s->type &&
ss->sheet[key].value == s->value) {
continue;
}
for (i = 0; i < ARRAY_LEN(task_status); ++i) {
if (key >= task_status[i].start &&
key <= task_status[i].end) {
if (!task_status[i].is_valid) {
break;
}
task_status[i].is_valid = FALSE;
Widget_AddTask(w, task_status[i].task);
}
}
}
if (w->proto && w->proto->update &&
w->style->length > STYLE_KEY_TOTAL) {
/* 扩展部分的样式交给该部件自己处理 */
w->proto->update(w);
}
StyleSheet_Delete(ss);
}
void Widget_DestroyStyleSheets(LCUI_Widget w)
{
StyleSheet_Delete(w->inherited_style);
StyleSheet_Delete(w->custom_style);
w->inherited_style = NULL;
if (w->custom_style) {
StyleList_Delete(w->custom_style);
}
StyleSheet_Delete(w->style);
}
+113 -60
View File
@@ -28,23 +28,26 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <LCUI_Build.h>
#include <LCUI/LCUI.h>
#include <LCUI/gui/widget.h>
/** 部件任务模块数据 */
static struct WidgetTaskModule {
LCUI_WidgetFunction handlers[LCUI_WTASK_TOTAL_NUM]; /**< 任务处理器 */
unsigned int update_count; /**< 刷新次数 */
LCUI_WidgetFunction handlers[LCUI_WTASK_TOTAL_NUM];
unsigned update_count;
} self;
static void HandleRefreshStyle(LCUI_Widget w)
{
int i;
Widget_ExecUpdateStyle(w, TRUE);
w->task.buffer[LCUI_WTASK_UPDATE_STYLE] = FALSE;
for (i = LCUI_WTASK_UPDATE_STYLE + 1; i < LCUI_WTASK_TOTAL_NUM; ++i) {
w->task.states[i] = TRUE;
}
w->task.states[LCUI_WTASK_UPDATE_STYLE] = FALSE;
}
static void HandleUpdateStyle(LCUI_Widget w)
@@ -74,7 +77,7 @@ void Widget_UpdateTaskStatus(LCUI_Widget widget)
{
int i;
for (i = 0; i < LCUI_WTASK_TOTAL_NUM; ++i) {
if (widget->task.buffer[i]) {
if (widget->task.states[i]) {
break;
}
}
@@ -92,6 +95,7 @@ void Widget_AddTaskForChildren(LCUI_Widget widget, int task)
{
LCUI_Widget child;
LinkedListNode *node;
widget->task.for_children = TRUE;
for (LinkedList_Each(node, &widget->children)) {
child = node->data;
@@ -106,7 +110,7 @@ void Widget_AddTask(LCUI_Widget widget, int task)
return;
}
widget->task.for_self = TRUE;
widget->task.buffer[task] = TRUE;
widget->task.states[task] = TRUE;
widget = widget->parent;
/* 向没有标记的父级部件添加标记 */
while (widget && !widget->task.for_children) {
@@ -115,7 +119,7 @@ void Widget_AddTask(LCUI_Widget widget, int task)
}
}
static void MapTaskHandler(void)
void LCUIWidget_InitTasks(void)
{
#define SetHandler(NAME, HANDLER) self.handlers[LCUI_WTASK_##NAME] = HANDLER
SetHandler(VISIBLE, Widget_UpdateVisibility);
@@ -138,72 +142,114 @@ static void MapTaskHandler(void)
SetHandler(PROPS, Widget_UpdateProps);
}
void LCUIWidget_InitTasks(void)
{
MapTaskHandler();
}
void LCUIWidget_FreeTasks(void)
{
LCUIWidget_ClearTrash();
}
int Widget_Update(LCUI_Widget w)
LCUI_WidgetTaskContext Widget_BeginUpdate(LCUI_Widget w,
LCUI_WidgetTaskContext ctx)
{
int i;
LCUI_BOOL *buffer;
LinkedListNode *node, *next;
LCUI_CachedStyleSheet inherited_style;
LCUI_WidgetTaskContext self_ctx;
/* 如果该部件没有任务需要处理 */
if (!w->task.for_self) {
goto proc_children_task;
self_ctx = malloc(sizeof(LCUI_WidgetTaskContextRec));
if (!self_ctx) {
return NULL;
}
w->task.for_self = FALSE;
buffer = w->task.buffer;
/* 如果有用户自定义任务 */
if (buffer[LCUI_WTASK_USER] && w->proto && w->proto->runtask) {
w->proto->runtask(w);
}
for (i = 0; i < LCUI_WTASK_USER; ++i) {
if (buffer[i]) {
buffer[i] = FALSE;
if (self.handlers[i]) {
self.handlers[i](w);
}
} else {
buffer[i] = FALSE;
if (ctx) {
self_ctx->selector = Selector_Copy(ctx->selector);
if (0 != Selector_AppendNode(self_ctx->selector,
Widget_GetSelectorNode(w))) {
return NULL;
}
} else {
self_ctx->selector = Widget_GetSelector(w);
}
Widget_AddState(w, LCUI_WSTATE_UPDATED);
proc_children_task:
if (!w->task.for_children) {
return w->task.for_self;
inherited_style = w->inherited_style;
w->inherited_style = LCUI_GetCachedStyleSheet(self_ctx->selector);
if (w->inherited_style != inherited_style) {
Widget_AddTask(w, LCUI_WTASK_REFRESH_STYLE);
}
/* 如果子级部件中有待处理的部件,则递归进去 */
w->task.for_children = FALSE;
node = w->children.head.next;
while (node) {
LCUI_Widget child = node->data;
/* 如果当前部件有销毁任务,结点空间会连同部件一起被
* 释放,为避免因访问非法空间而出现异常,预先保存下
* 个结点。
*/
next = node->next;
/* 如果该级部件的任务需要留到下次再处理 */
if (Widget_Update(child)) {
w->task.for_children = TRUE;
}
node = next;
}
return w->task.for_self || w->task.for_children;
self_ctx->widget = w;
return self_ctx;
}
void LCUIWidget_Update(void)
void Widget_EndUpdate(LCUI_WidgetTaskContext ctx)
{
int count = 0;
Selector_Delete(ctx->selector);
ctx->selector = NULL;
ctx->widget = NULL;
free(ctx);
}
size_t Widget_UpdateWithContext(LCUI_Widget w, LCUI_WidgetTaskContext ctx)
{
int i;
size_t count = 0;
LCUI_BOOL *states;
LCUI_Widget child;
LCUI_WidgetTaskContext self_ctx;
LinkedListNode *node, *next;
if (!w->task.for_self && !w->task.for_children) {
return 0;
}
self_ctx = Widget_BeginUpdate(w, ctx);
if (w->task.for_self) {
w->task.for_self = FALSE;
states = w->task.states;
/* 如果有用户自定义任务 */
if (states[LCUI_WTASK_USER] && w->proto && w->proto->runtask) {
w->proto->runtask(w);
}
for (i = 0; i < LCUI_WTASK_USER; ++i) {
if (states[i]) {
states[i] = FALSE;
if (self.handlers[i]) {
self.handlers[i](w);
}
} else {
states[i] = FALSE;
}
}
Widget_AddState(w, LCUI_WSTATE_UPDATED);
count += 1;
}
if (w->task.for_children) {
/* 如果子级部件中有待处理的部件,则递归进去 */
w->task.for_children = FALSE;
node = w->children.head.next;
while (node) {
child = node->data;
/* 如果当前部件有销毁任务,结点空间会连同部件一起被
* 释放,为避免因访问非法空间而出现异常,预先保存下
* 个结点。
*/
next = node->next;
/* 如果该级部件的任务需要留到下次再处理 */
count += Widget_UpdateWithContext(child, self_ctx);
if (child->task.for_self || child->task.for_children) {
w->task.for_children = TRUE;
}
node = next;
}
}
Widget_EndUpdate(self_ctx);
return count;
}
size_t Widget_Update(LCUI_Widget w)
{
return Widget_UpdateWithContext(w, NULL);
}
size_t LCUIWidget_Update(void)
{
int i;
size_t total, count;
LCUI_Widget root;
/* 前两次更新需要主动刷新所有部件的样式,主要是为了省去在应用程序里手动调用
* LCUIWidget_RefreshStyle() 的麻烦 */
if (self.update_count < 2) {
@@ -211,8 +257,15 @@ void LCUIWidget_Update(void)
self.update_count += 1;
}
root = LCUIWidget_GetRoot();
while (Widget_Update(root) && count++ < 5);
for (total = 0, i = 0; i < 5; ++i) {
count = Widget_Update(root);
if (count < 1) {
break;
}
total += count;
}
LCUIWidget_ClearTrash();
return total;
}
void LCUIWidget_RefreshStyle(void)