Skip to content

Commit

Permalink
rework all widget creator functions
Browse files Browse the repository at this point in the history
  • Loading branch information
xianjimli committed Jan 16, 2019
1 parent b04cafe commit c6b9a87
Show file tree
Hide file tree
Showing 60 changed files with 152 additions and 250 deletions.
5 changes: 5 additions & 0 deletions docs/changes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# 最新动态
* 2019/01/16
* 增加darray类,逐步替换以前的array类。
* 增加widget pool用于控件的缓存和重复利用,可以通过WITH\_WIDGET\_POOL启用。
* 增加widget\_create函数,简化子类控件的创建函数,同时让通用控件本身的由widget pool集中管理。

* 2019/01/15
* 整理API文档:progress\_circle/rich\_text/slide\_menu/slide\_view/svg\_image/switch
* 整理API文档:text\_selector/time\_clock/digit\_clock
Expand Down
7 changes: 7 additions & 0 deletions src/base/awtk_config_sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,11 @@
* #define ENABLE_CURSOR 1
*/

/**
* 如果启用控件缓存,请定义本宏
* 缓存内存占用约:200 * WITH_WIDGET_POOL
*
* #define WITH_WIDGET_POOL 1000
*/

#endif /*AWTK_CONFIG_H*/
4 changes: 4 additions & 0 deletions src/base/widget_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ BEGIN_C_DECLS
* * 优点:速度快,内存碎片少。
* * 缺点:占用一点内存空间。
*
* 通过宏WITH\_WIDGET\_POOL来指定最大缓存控件的数量。
*
* 控件可以通过vt->enable\_pool参数决定该类控件是否启用缓存。
*
*/
typedef struct _widget_pool_t {
object_t* cache;
Expand Down
8 changes: 4 additions & 4 deletions src/base/window_base.c
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -186,20 +186,20 @@ ret_t window_base_on_event(widget_t* widget, event_t* e) {
return RET_OK;
}

widget_t* window_base_init(widget_t* widget, widget_t* parent, const widget_vtable_t* vt, xy_t x,
xy_t y, wh_t w, wh_t h) {
widget_t* window_base_create(widget_t* parent, const widget_vtable_t* vt, xy_t x, xy_t y, wh_t w,
wh_t h) {
widget_t* widget = widget_create(NULL, vt, x, y, w, h);
window_base_t* win = WINDOW_BASE(widget);

return_value_if_fail(win != NULL, NULL);

widget_init(widget, NULL, vt, x, y, w, h);
if (parent == NULL) {
parent = window_manager();
}

return_value_if_fail(window_manager_open_window(parent, widget) == RET_OK, NULL);

win->stage = WINDOW_STAGE_NONE;

#ifdef ENABLE_MEM_LEAK_CHECK
tk_mem_dump();
#endif /*ENABLE_MEM_LEAK_CHECK*/
Expand Down
4 changes: 2 additions & 2 deletions src/base/window_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ ret_t window_base_on_paint_end(widget_t* widget, canvas_t* c);
ret_t window_base_on_paint_self(widget_t* widget, canvas_t* c);
ret_t window_base_get_prop(widget_t* widget, const char* name, value_t* v);
ret_t window_base_set_prop(widget_t* widget, const char* name, const value_t* v);
widget_t* window_base_init(widget_t* widget, widget_t* parent, const widget_vtable_t* vt, xy_t x,
xy_t y, wh_t w, wh_t h);
widget_t* window_base_create(widget_t* parent, const widget_vtable_t* vt, xy_t x, xy_t y, wh_t w,
wh_t h);

#define WINDOW_BASE(widget) ((window_base_t*)(widget))

Expand Down
6 changes: 1 addition & 5 deletions src/ext_widgets/canvas_widget/canvas_widget.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ static const widget_vtable_t s_canvas_widget_vtable = {.size = sizeof(canvas_wid
.create = canvas_widget_create};

widget_t* canvas_widget_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
canvas_widget_t* canvas_widget = TKMEM_ZALLOC(canvas_widget_t);
widget_t* widget = WIDGET(canvas_widget);
return_value_if_fail(canvas_widget != NULL, NULL);

return widget_init(widget, parent, &s_canvas_widget_vtable, x, y, w, h);
return widget_create(parent, &s_canvas_widget_vtable, x, y, w, h);
}

widget_t* canvas_widget_cast(widget_t* widget) {
Expand Down
5 changes: 2 additions & 3 deletions src/ext_widgets/color_picker/color_component.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,10 @@ static ret_t color_component_update_h(widget_t* widget) {
}

widget_t* color_component_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
color_component_t* color_component = TKMEM_ZALLOC(color_component_t);
widget_t* widget = WIDGET(color_component);
widget_t* widget = widget_create(parent, &s_color_component_vtable, x, y, w, h);
color_component_t* color_component = COLOR_COMPONENT(widget);
return_value_if_fail(color_component != NULL, NULL);

widget_init(widget, parent, &s_color_component_vtable, x, y, w, h);
color_component_init_image(&(color_component->image), "", w, h);
color_component->c = color_init(0xff, 0xff, 0xff, 0xff);
color_component->need_update = TRUE;
Expand Down
5 changes: 2 additions & 3 deletions src/ext_widgets/color_picker/color_picker.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,11 @@ static ret_t color_picker_on_window_will_open(void* ctx, event_t* e) {
}

widget_t* color_picker_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
color_picker_t* color_picker = TKMEM_ZALLOC(color_picker_t);
widget_t* widget = WIDGET(color_picker);
widget_t* widget = widget_create(parent, &s_color_picker_vtable, x, y, w, h);
color_picker_t* color_picker = COLOR_PICKER(widget);
widget_t* win = widget_get_window(parent);
return_value_if_fail(color_picker != NULL, NULL);

widget_init(widget, parent, &s_color_picker_vtable, x, y, w, h);
widget_on(win, EVT_WINDOW_WILL_OPEN, color_picker_on_window_will_open, color_picker);
color_picker_set_color(widget, "gold");

Expand Down
5 changes: 2 additions & 3 deletions src/ext_widgets/gif_image/gif_image.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,10 @@ static const widget_vtable_t s_gif_image_vtable = {.size = sizeof(gif_image_t),
.get_prop = image_base_get_prop};

widget_t* gif_image_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
gif_image_t* gif_image = TKMEM_ZALLOC(gif_image_t);
widget_t* widget = WIDGET(gif_image);
widget_t* widget = widget_create(parent, &s_gif_image_vtable, x, y, w, h);
gif_image_t* gif_image = GIF_IMAGE(widget);
return_value_if_fail(gif_image != NULL, NULL);

widget_init(widget, parent, &s_gif_image_vtable, x, y, w, h);
image_base_init(widget);

return widget;
Expand Down
6 changes: 3 additions & 3 deletions src/ext_widgets/guage/guage.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ static const widget_vtable_t s_guage_vtable = {.size = sizeof(guage_t),
.on_destroy = guage_on_destroy};

widget_t* guage_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
guage_t* guage = TKMEM_ZALLOC(guage_t);
widget_t* widget = WIDGET(guage);
widget_t* widget = widget_create(parent, &s_guage_vtable, x, y, w, h);
guage_t* guage = GUAGE(widget);

return_value_if_fail(guage != NULL, NULL);

widget_init(widget, parent, &s_guage_vtable, x, y, w, h);
guage->draw_type = IMAGE_DRAW_CENTER;

return widget;
Expand Down
6 changes: 1 addition & 5 deletions src/ext_widgets/guage/guage_pointer.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,7 @@ static const widget_vtable_t s_guage_pointer_vtable = {
.on_destroy = guage_pointer_on_destroy};

widget_t* guage_pointer_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
guage_pointer_t* guage_pointer = TKMEM_ZALLOC(guage_pointer_t);
widget_t* widget = WIDGET(guage_pointer);
return_value_if_fail(guage_pointer != NULL, NULL);

return widget_init(widget, parent, &s_guage_pointer_vtable, x, y, w, h);
return widget_create(parent, &s_guage_pointer_vtable, x, y, w, h);
}

widget_t* guage_pointer_cast(widget_t* widget) {
Expand Down
8 changes: 4 additions & 4 deletions src/ext_widgets/image_animation/image_animation.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,18 @@ static ret_t image_animation_on_open(void* ctx, event_t* e) {

widget_t* image_animation_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
widget_t* win = widget_get_window(parent);
image_animation_t* image_animation = TKMEM_ZALLOC(image_animation_t);
widget_t* widget = WIDGET(image_animation);
widget_t* widget = widget_create(parent, &s_image_animation_vtable, x, y, w, h);
image_animation_t* image_animation = IMAGE_ANIMATION(widget);

return_value_if_fail(image_animation != NULL, NULL);

image_animation->index = 0;
image_animation->interval = 16;
image_animation->loop = TRUE;
image_animation->auto_play = FALSE;

widget_on(win, EVT_WINDOW_WILL_OPEN, image_animation_on_open, image_animation);

return widget_init(widget, parent, &s_image_animation_vtable, x, y, w, h);
return widget;
}

ret_t image_animation_set_loop(widget_t* widget, bool_t loop) {
Expand Down
6 changes: 3 additions & 3 deletions src/ext_widgets/image_value/image_value.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ static const widget_vtable_t s_image_value_vtable = {.size = sizeof(image_value_
.on_paint_self = image_value_on_paint_self};

widget_t* image_value_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
image_value_t* image_value = TKMEM_ZALLOC(image_value_t);
widget_t* widget = WIDGET(image_value);
widget_t* widget = widget_create(parent, &s_image_value_vtable, x, y, w, h);
image_value_t* image_value = IMAGE_VALUE(widget);
return_value_if_fail(image_value != NULL, NULL);

return widget_init(widget, parent, &s_image_value_vtable, x, y, w, h);
return widget;
}

ret_t image_value_set_image(widget_t* widget, const char* image) {
Expand Down
6 changes: 2 additions & 4 deletions src/ext_widgets/keyboard/candidates.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,10 @@ static ret_t candidates_on_im_candidates_event(void* ctx, event_t* e) {
}

widget_t* candidates_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
candidates_t* candidates = TKMEM_ZALLOC(candidates_t);
widget_t* widget = WIDGET(candidates);
widget_t* widget = widget_create(parent, &s_candidates_vtable, x, y, w, h);
candidates_t* candidates = CANDIDATES(widget);
return_value_if_fail(candidates != NULL, NULL);

widget_init(widget, parent, &s_candidates_vtable, x, y, w, h);

candidates->event_id = input_method_on(input_method(), EVT_IM_SHOW_CANDIDATES,
candidates_on_im_candidates_event, candidates);

Expand Down
7 changes: 2 additions & 5 deletions src/ext_widgets/keyboard/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,11 @@ static const widget_vtable_t s_keyboard_vtable = {.size = sizeof(keyboard_t),
.on_destroy = keyboard_on_destroy};

widget_t* keyboard_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
keyboard_t* keyboard = TKMEM_ZALLOC(keyboard_t);
widget_t* widget = WIDGET(keyboard);
widget_t* widget = window_base_create(parent, &s_keyboard_vtable, x, y, w, h);
keyboard_t* keyboard = KEYBOARD(widget);
return_value_if_fail(keyboard != NULL, NULL);

window_base_init(widget, parent, &s_keyboard_vtable, x, y, w, h);

array_init(&(keyboard->action_buttons), 0);

widget_on(widget, EVT_WINDOW_LOAD, keyboard_on_load, widget);
keyboard->action_info_id =
input_method_on(input_method(), EVT_IM_ACTION_INFO, keyboard_on_action_info, widget);
Expand Down
6 changes: 2 additions & 4 deletions src/ext_widgets/progress_circle/progress_circle.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,10 @@ static const widget_vtable_t s_progress_circle_vtable = {
.set_prop = progress_circle_set_prop};

widget_t* progress_circle_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
progress_circle_t* progress_circle = TKMEM_ZALLOC(progress_circle_t);
widget_t* widget = WIDGET(progress_circle);
widget_t* widget = widget_create(parent, &s_progress_circle_vtable, x, y, w, h);
progress_circle_t* progress_circle = PROGRESS_CIRCLE(widget);
return_value_if_fail(progress_circle != NULL, NULL);

widget_init(widget, parent, &s_progress_circle_vtable, x, y, w, h);

progress_circle->max = 100;
progress_circle->line_width = 8;
progress_circle->start_angle = -90;
Expand Down
6 changes: 1 addition & 5 deletions src/ext_widgets/rich_text/rich_text.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,7 @@ static const widget_vtable_t s_rich_text_vtable = {.size = sizeof(rich_text_t),
.on_paint_self = rich_text_on_paint_self};

widget_t* rich_text_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
rich_text_t* rich_text = TKMEM_ZALLOC(rich_text_t);
widget_t* widget = WIDGET(rich_text);
return_value_if_fail(rich_text != NULL, NULL);

return widget_init(widget, parent, &s_rich_text_vtable, x, y, w, h);
return widget_create(parent, &s_rich_text_vtable, x, y, w, h);
}

ret_t rich_text_set_text(widget_t* widget, const char* text) {
Expand Down
5 changes: 2 additions & 3 deletions src/ext_widgets/scroll_view/list_item.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,10 @@ static const widget_vtable_t s_list_item_vtable = {.size = sizeof(list_item_t),
.on_destroy = list_item_on_destroy};

widget_t* list_item_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
list_item_t* list_item = TKMEM_ZALLOC(list_item_t);
widget_t* widget = WIDGET(list_item);
widget_t* widget = widget_create(parent, &s_list_item_vtable, x, y, w, h);
list_item_t* list_item = LIST_ITEM(widget);
return_value_if_fail(list_item != NULL, NULL);

widget_init(widget, parent, &s_list_item_vtable, x, y, w, h);
list_item->timer_id = TK_INVALID_ID;

return widget;
Expand Down
7 changes: 2 additions & 5 deletions src/ext_widgets/scroll_view/list_view.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ static ret_t list_view_on_event(widget_t* widget, event_t* e) {
}

static const widget_vtable_t s_list_view_vtable = {.type = WIDGET_TYPE_LIST_VIEW,
.size = sizeof(list_view_t),
.set_prop = list_view_set_prop,
.get_prop = list_view_get_prop,
.on_event = list_view_on_event,
Expand Down Expand Up @@ -257,11 +258,7 @@ static ret_t list_view_on_add_child(widget_t* widget, widget_t* child) {
}

widget_t* list_view_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
list_view_t* list_view = TKMEM_ZALLOC(list_view_t);
widget_t* widget = WIDGET(list_view);
return_value_if_fail(list_view != NULL, NULL);

return widget_init(widget, parent, &s_list_view_vtable, x, y, w, h);
return widget_create(parent, &s_list_view_vtable, x, y, w, h);
}

ret_t list_view_set_item_height(widget_t* widget, int32_t item_height) {
Expand Down
7 changes: 2 additions & 5 deletions src/ext_widgets/scroll_view/list_view_h.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ static ret_t list_view_h_on_event(widget_t* widget, event_t* e) {
}

static const widget_vtable_t s_list_view_h_vtable = {.type = WIDGET_TYPE_LIST_VIEW_H,
.size = sizeof(list_view_h_t),
.set_prop = list_view_h_set_prop,
.get_prop = list_view_h_get_prop,
.on_event = list_view_h_on_event,
Expand Down Expand Up @@ -137,11 +138,7 @@ static ret_t list_view_h_on_add_child(widget_t* widget, widget_t* child) {
}

widget_t* list_view_h_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
list_view_h_t* list_view_h = TKMEM_ZALLOC(list_view_h_t);
widget_t* widget = WIDGET(list_view_h);
return_value_if_fail(list_view_h != NULL, NULL);

return widget_init(widget, parent, &s_list_view_h_vtable, x, y, w, h);
return widget_create(parent, &s_list_view_h_vtable, x, y, w, h);
}

ret_t list_view_h_set_item_width(widget_t* widget, int32_t item_width) {
Expand Down
5 changes: 2 additions & 3 deletions src/ext_widgets/scroll_view/scroll_bar.c
Original file line number Diff line number Diff line change
Expand Up @@ -541,11 +541,10 @@ ret_t scroll_bar_set_value_only(widget_t* widget, int32_t value) {

static widget_t* scroll_bar_create_internal(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h,
const widget_vtable_t* vt) {
scroll_bar_t* scroll_bar = TKMEM_ZALLOC(scroll_bar_t);
widget_t* widget = WIDGET(scroll_bar);
widget_t* widget = widget_create(parent, vt, x, y, w, h);
scroll_bar_t* scroll_bar = SCROLL_BAR(widget);
return_value_if_fail(scroll_bar != NULL, NULL);

widget_init(widget, parent, vt, x, y, w, h);
scroll_bar->animatable = TRUE;
widget_set_state(widget, WIDGET_STATE_NORMAL);

Expand Down
5 changes: 2 additions & 3 deletions src/ext_widgets/scroll_view/scroll_view.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,11 +434,10 @@ static const widget_vtable_t s_scroll_view_vtable = {
.set_prop = scroll_view_set_prop};

widget_t* scroll_view_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
scroll_view_t* scroll_view = TKMEM_ZALLOC(scroll_view_t);
widget_t* widget = WIDGET(scroll_view);
widget_t* widget = widget_create(parent, &s_scroll_view_vtable, x, y, w, h);
scroll_view_t* scroll_view = SCROLL_VIEW(widget);
return_value_if_fail(scroll_view != NULL, NULL);

widget_init(widget, parent, &s_scroll_view_vtable, x, y, w, h);
scroll_view->fix_end_offset = scroll_view_fix_end_offset_default;

return widget;
Expand Down
6 changes: 3 additions & 3 deletions src/ext_widgets/slide_menu/slide_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -550,15 +550,15 @@ static const widget_vtable_t s_slide_menu_vtable = {
.on_event = slide_menu_on_event};

widget_t* slide_menu_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
slide_menu_t* slide_menu = TKMEM_ZALLOC(slide_menu_t);
widget_t* widget = WIDGET(slide_menu);
widget_t* widget = widget_create(parent, &s_slide_menu_vtable, x, y, w, h);
slide_menu_t* slide_menu = SLIDE_MENU(widget);
return_value_if_fail(slide_menu != NULL, NULL);

slide_menu->value = 1;
slide_menu->min_scale = 0.8f;
slide_menu->align_v = ALIGN_V_BOTTOM;

return widget_init(widget, parent, &s_slide_menu_vtable, x, y, w, h);
return widget;
}

ret_t slide_menu_set_value(widget_t* widget, uint32_t index) {
Expand Down
2 changes: 1 addition & 1 deletion src/ext_widgets/slide_menu/slide_menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ BEGIN_C_DECLS
* ```c
* widget_on(slide_menu, EVT_VALUE_CHANGED, on_current_changed, slide_menu);
* ```
* 可按下面的方法关注当前按钮被点击的事件:
*
* ```c
Expand Down
6 changes: 1 addition & 5 deletions src/ext_widgets/slide_view/slide_view.c
Original file line number Diff line number Diff line change
Expand Up @@ -677,11 +677,7 @@ ret_t slide_view_set_vertical(widget_t* widget, bool_t vertical) {
}

widget_t* slide_view_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
slide_view_t* slide_view = TKMEM_ZALLOC(slide_view_t);
widget_t* widget = WIDGET(slide_view);
return_value_if_fail(slide_view != NULL, NULL);

return widget_init(widget, parent, &s_slide_view_vtable, x, y, w, h);
return widget_create(parent, &s_slide_view_vtable, x, y, w, h);
}

static ret_t slide_view_on_timer_next(const timer_info_t* timer) {
Expand Down
6 changes: 3 additions & 3 deletions src/ext_widgets/svg_image/svg_image.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ static const widget_vtable_t s_svg_image_vtable = {.size = sizeof(svg_image_t),
.get_prop = image_base_get_prop};

widget_t* svg_image_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
svg_image_t* svg_image = TKMEM_ZALLOC(svg_image_t);
widget_t* widget = WIDGET(svg_image);
widget_t* widget = widget_create(parent, &s_svg_image_vtable, x, y, w, h);
svg_image_t* svg_image = SVG_IMAGE(widget);
return_value_if_fail(svg_image != NULL, NULL);

widget_init(widget, parent, &s_svg_image_vtable, x, y, w, h);
widget_create(parent, &s_svg_image_vtable, x, y, w, h);
image_base_init(widget);

return widget;
Expand Down
Loading

0 comments on commit c6b9a87

Please sign in to comment.