improve margin

This commit is contained in:
lixianjing 2019-05-14 17:29:48 +08:00
parent a6cb009eb8
commit d3fcefa81e
6 changed files with 207 additions and 36 deletions

View File

@ -1,5 +1,8 @@
# 最新动态
* 2019/05/14
* style增加margin\_left/margin\_right/margin\_top/margin\_bottom几个参数用于细化之前的margin参数。 这个只影响icon/text不影响子控件(子控件由布局算法决定)
* 2019/05/13
* 增加widget\_add\_idle。
* 完善window\_manager\_do\_open\_window。

View File

@ -63,7 +63,12 @@ AWTK的主题按控件进行分类每种控件可以有多种不同的风格
* icon\_at 图标的位置取值为left/right/top/bottom。
* x\_offset 在X坐标方向上的偏移(可用来实现按下的效果)。
* y\_offset 在Y坐标方向上的偏移(可用来实现按下的效果)。
* margin 边距。
* margin 边距(边距目前只影响icon/text不影响子控件子控件的边距由布局算法参数决定)。
* margin_top 上边距。
* margin_bottom 下边距。
* margin_left 左边距。
* margin_right 右边距。
* spacer 间距(目前仅用于文本和图标之间)。
* round\_radius 背景和边框的圆角半径(仅在定义WITH\_VGCANVAS时有效)。
> 颜色可使用标准名称,#开头的16进制值和rgba合成的值。

View File

@ -134,12 +134,42 @@ BEGIN_C_DECLS
*/
#define STYLE_ID_FG_IMAGE_DRAW_TYPE "fg_image_draw_type"
/**
* @const STYLE_ID_SPACER
*
*/
#define STYLE_ID_SPACER "spacer"
/**
* @const STYLE_ID_MARGIN
*
*/
#define STYLE_ID_MARGIN "margin"
/**
* @const STYLE_ID_MARGIN_LEFT
*
*/
#define STYLE_ID_MARGIN_LEFT "margin_left"
/**
* @const STYLE_ID_MARGIN_RIGHT
*
*/
#define STYLE_ID_MARGIN_RIGHT "margin_right"
/**
* @const STYLE_ID_MARGIN_TOP
*
*/
#define STYLE_ID_MARGIN_TOP "margin_top"
/**
* @const STYLE_ID_MARGIN_BOTTOM
*
*/
#define STYLE_ID_MARGIN_BOTTOM "margin_bottom"
/**
* @const STYLE_ID_ICON_AT
*

View File

@ -733,20 +733,77 @@ ret_t widget_off_by_func(widget_t* widget, uint32_t type, event_func_t on_event,
return emitter_off_by_func(widget->emitter, type, on_event, ctx);
}
ret_t widget_calc_icon_text_rect(const rect_t* ir, int32_t font_size, int32_t icon_at,
int32_t spacer, rect_t* r_text, rect_t* r_icon) {
return_value_if_fail(ir != NULL && (r_text != NULL || r_icon != NULL), RET_BAD_PARAMS);
if (r_icon == NULL) {
*r_text = *ir;
return RET_OK;
}
if (r_text == NULL) {
*r_icon = *ir;
return RET_OK;
}
return_value_if_fail(font_size < ir->h && spacer < ir->h && spacer < ir->w, RET_BAD_PARAMS);
switch (icon_at) {
case ICON_AT_RIGHT: {
*r_icon = rect_init(ir->x + ir->w - ir->h, ir->y, ir->h, ir->h);
*r_text = rect_init(ir->x, ir->y, ir->w - ir->h - spacer, ir->h);
break;
}
case ICON_AT_TOP: {
int32_t icon_h = ir->h - font_size - spacer;
*r_icon = rect_init(ir->x, ir->y, ir->w, icon_h);
*r_text = rect_init(ir->x, icon_h + spacer, ir->w, font_size);
break;
}
case ICON_AT_LEFT:
default: {
*r_icon = rect_init(ir->x, ir->y, ir->h, ir->h);
*r_text = rect_init(ir->x + ir->h + spacer, ir->y, ir->w - ir->h - spacer, ir->h);
break;
}
}
return RET_OK;
}
ret_t widget_draw_icon_text(widget_t* widget, canvas_t* c, const char* icon, wstr_t* text) {
rect_t r;
rect_t ir;
wh_t w = 0;
wh_t h = 0;
bitmap_t img;
rect_t r_icon;
rect_t r_text;
int32_t margin = 0;
int32_t spacer = 0;
int32_t icon_at = 0;
uint16_t font_size = 0;
int32_t margin_left = 0;
int32_t margin_right = 0;
int32_t margin_top = 0;
int32_t margin_bottom = 0;
style_t* style = widget->astyle;
int32_t align_h = ALIGN_H_LEFT;
int32_t align_v = ALIGN_V_MIDDLE;
return_value_if_fail(widget->astyle != NULL, RET_BAD_PARAMS);
margin = style_get_int(style, STYLE_ID_MARGIN, 2);
spacer = style_get_int(style, STYLE_ID_SPACER, 2);
margin = style_get_int(style, STYLE_ID_MARGIN, 0);
margin_top = style_get_int(style, STYLE_ID_MARGIN_TOP, margin);
margin_left = style_get_int(style, STYLE_ID_MARGIN_LEFT, margin);
margin_right = style_get_int(style, STYLE_ID_MARGIN_RIGHT, margin);
margin_bottom = style_get_int(style, STYLE_ID_MARGIN_BOTTOM, margin);
icon_at = style_get_int(style, STYLE_ID_ICON_AT, ICON_AT_AUTO);
wh_t w = widget->w - 2 * margin;
wh_t h = widget->h - 2 * margin;
w = widget->w - margin_left - margin_right;
h = widget->h - margin_top - margin_bottom;
ir = rect_init(margin_left, margin_top, w, h);
if (text == NULL) {
text = &(widget->text);
@ -758,45 +815,35 @@ ret_t widget_draw_icon_text(widget_t* widget, canvas_t* c, const char* icon, wst
widget_prepare_text_style(widget, c);
font_size = style_get_int(style, STYLE_ID_FONT_SIZE, TK_DEFAULT_FONT_SIZE);
if (icon_at == ICON_AT_RIGHT || icon_at == ICON_AT_LEFT) {
align_v = style_get_int(style, STYLE_ID_TEXT_ALIGN_V, ALIGN_V_MIDDLE);
align_h = style_get_int(style, STYLE_ID_TEXT_ALIGN_H, ALIGN_H_LEFT);
} else {
align_v = style_get_int(style, STYLE_ID_TEXT_ALIGN_V, ALIGN_V_MIDDLE);
align_h = style_get_int(style, STYLE_ID_TEXT_ALIGN_H, ALIGN_H_CENTER);
}
canvas_set_text_align(c, align_h, align_v);
if (icon != NULL && widget_load_image(widget, icon, &img) == RET_OK) {
float_t dpr = system_info()->device_pixel_ratio;
if (text != NULL && text->size > 0) {
if ((h > (img.h / dpr + font_size) && icon_at == ICON_AT_AUTO) || icon_at == ICON_AT_TOP) {
int text_h = font_size + margin;
int text_y = widget->h - text_h;
r = rect_init(0, 0, widget->w, text_y);
canvas_draw_icon_in_rect(c, &img, &r);
r = rect_init(margin, text_y, w, text_h);
} else if (icon_at == ICON_AT_RIGHT) {
r = rect_init(widget->x + widget->w - widget->h, 0, widget->h, widget->h);
canvas_draw_icon_in_rect(c, &img, &r);
canvas_set_text_align(c, ALIGN_H_LEFT, ALIGN_V_MIDDLE);
r = rect_init(margin, margin, widget->w - widget->h - margin, h);
} else {
r = rect_init(0, 0, widget->h, widget->h);
canvas_draw_icon_in_rect(c, &img, &r);
canvas_set_text_align(c, ALIGN_H_LEFT, ALIGN_V_MIDDLE);
r = rect_init(widget->h + margin, margin, widget->w - widget->h - 2 * margin, h);
if ((h > (img.h / dpr + font_size) && icon_at == ICON_AT_AUTO)) {
icon_at = ICON_AT_TOP;
}
canvas_draw_text_in_rect(c, text->str, text->size, &r);
widget_calc_icon_text_rect(&ir, font_size, icon_at, spacer, &r_text, &r_icon);
canvas_draw_icon_in_rect(c, &img, &r_icon);
canvas_draw_text_in_rect(c, text->str, text->size, &r_text);
} else {
r = rect_init(0, 0, widget->w, widget->h);
canvas_draw_icon_in_rect(c, &img, &r);
widget_calc_icon_text_rect(&ir, font_size, icon_at, spacer, NULL, &r_icon);
canvas_draw_icon_in_rect(c, &img, &r_icon);
}
} else if (text != NULL && text->size > 0) {
if (icon_at == ICON_AT_RIGHT) {
r = rect_init(margin, margin, w, h);
canvas_set_text_align(c, ALIGN_H_LEFT, ALIGN_V_MIDDLE);
} else {
r = rect_init(margin, margin, w, h);
}
canvas_draw_text_in_rect(c, text->str, text->size, &r);
widget_calc_icon_text_rect(&ir, font_size, icon_at, spacer, &r_text, NULL);
canvas_draw_text_in_rect(c, text->str, text->size, &r_text);
}
return RET_OK;

View File

@ -1889,6 +1889,8 @@ image_manager_t* widget_get_image_manager(widget_t* widget);
assets_manager_t* widget_get_assets_manager(widget_t* widget);
font_manager_t* widget_get_font_manager(widget_t* widget);
uint32_t widget_add_idle(widget_t* widget, idle_func_t on_idle);
ret_t widget_calc_icon_text_rect(const rect_t* ir, int32_t font_size, int32_t icon_at,
int32_t spacer, rect_t* r_text, rect_t* r_icon);
END_C_DECLS

View File

@ -740,3 +740,87 @@ TEST(Widget, load_widget) {
widget_destroy(w);
}
TEST(Widget, calc_icon_text_rect_icon) {
rect_t ir = rect_init(10, 20, 200, 40);
rect_t r_icon;
widget_calc_icon_text_rect(&ir, 10, ICON_AT_TOP, 2, NULL, &r_icon);
ASSERT_EQ(r_icon.x, ir.x);
ASSERT_EQ(r_icon.y, ir.y);
ASSERT_EQ(r_icon.w, ir.w);
ASSERT_EQ(r_icon.h, ir.h);
}
TEST(Widget, calc_icon_text_rect_text) {
rect_t ir = rect_init(10, 20, 200, 40);
rect_t r_text;
widget_calc_icon_text_rect(&ir, 10, ICON_AT_TOP, 2, &r_text, NULL);
ASSERT_EQ(r_text.x, ir.x);
ASSERT_EQ(r_text.y, ir.y);
ASSERT_EQ(r_text.w, ir.w);
ASSERT_EQ(r_text.h, ir.h);
}
TEST(Widget, calc_icon_text_rect_icon_top) {
rect_t r_icon;
rect_t r_text;
int32_t spacer = 2;
int32_t font_size = 20;
rect_t ir = rect_init(10, 20, 200, 80);
widget_calc_icon_text_rect(&ir, font_size, ICON_AT_TOP, spacer, &r_text, &r_icon);
ASSERT_EQ(r_icon.x, ir.x);
ASSERT_EQ(r_icon.y, ir.y);
ASSERT_EQ(r_icon.w, ir.w);
ASSERT_EQ(r_icon.h, ir.h - font_size - spacer);
ASSERT_EQ(r_text.x, ir.x);
ASSERT_EQ(r_text.y, r_icon.h + spacer);
ASSERT_EQ(r_text.w, ir.w);
ASSERT_EQ(r_text.h, font_size);
}
TEST(Widget, calc_icon_text_rect_icon_left) {
rect_t r_icon;
rect_t r_text;
int32_t spacer = 2;
int32_t font_size = 20;
rect_t ir = rect_init(10, 20, 200, 80);
widget_calc_icon_text_rect(&ir, font_size, ICON_AT_LEFT, spacer, &r_text, &r_icon);
ASSERT_EQ(r_icon.x, ir.x);
ASSERT_EQ(r_icon.y, ir.y);
ASSERT_EQ(r_icon.w, ir.h);
ASSERT_EQ(r_icon.h, ir.h);
ASSERT_EQ(r_text.x, ir.x + ir.h + spacer);
ASSERT_EQ(r_text.y, ir.y);
ASSERT_EQ(r_text.w, ir.w - ir.h - spacer);
ASSERT_EQ(r_text.h, ir.h);
}
TEST(Widget, calc_icon_text_rect_icon_right) {
rect_t r_icon;
rect_t r_text;
int32_t spacer = 2;
int32_t font_size = 20;
rect_t ir = rect_init(10, 20, 200, 80);
widget_calc_icon_text_rect(&ir, font_size, ICON_AT_RIGHT, spacer, &r_text, &r_icon);
ASSERT_EQ(r_icon.x, ir.x + ir.w - ir.h);
ASSERT_EQ(r_icon.y, ir.y);
ASSERT_EQ(r_icon.w, ir.h);
ASSERT_EQ(r_icon.h, ir.h);
ASSERT_EQ(r_text.x, ir.x);
ASSERT_EQ(r_text.y, ir.y);
ASSERT_EQ(r_text.w, ir.w - ir.h - spacer);
ASSERT_EQ(r_text.h, ir.h);
}