improve rich text

This commit is contained in:
lixianjing 2019-12-12 15:50:15 +08:00
parent cf8c50d097
commit 33c85bc0fa
4 changed files with 49 additions and 13 deletions

View File

@ -4,9 +4,9 @@
* hscroll label 在文本改变时重置 xoffset。
* 完善 combobox 使用 hscroll label 的处理。
* set key target 时触发 focus 事件。
* 修复圆角巨型在gles的时候设置vg的Scale显示不正常的问题感谢智明提供补丁
* 修改了border_width为0还是绘制边框的问题感谢智明提供补丁
* 修复圆角巨型在 gles 的时候,设置 vg Scale 显示不正常的问题(感谢智明提供补丁)
* 修改了 border_width 0 还是绘制边框的问题(感谢智明提供补丁)
* 完善 rich text 控件(感谢尧燊提供补丁)。
* 2019/12/11
* 增加 WIDGET LOAD 事件和状态。

View File

@ -110,6 +110,16 @@ static ret_t rich_text_ensure_render_node(widget_t* widget, canvas_t* c) {
rich_text_t* rich_text = RICH_TEXT(widget);
return_value_if_fail(widget != NULL && rich_text != NULL, RET_BAD_PARAMS);
if (rich_text->need_reset) {
str_t str;
str_init(&str, widget->text.size * 4 + 1);
str_from_wstr(&str, widget->text.str);
rich_text_reset(widget);
rich_text->node = rich_text_parse(str.str, str.size);
str_reset(&str);
rich_text->need_reset = FALSE;
}
if (rich_text->render_node != NULL) {
return RET_OK;
}
@ -122,8 +132,7 @@ static ret_t rich_text_ensure_render_node(widget_t* widget, canvas_t* c) {
int32_t w = widget->w;
int32_t h = widget->h;
int32_t line_gap = rich_text->line_gap;
style_t* style = widget->astyle;
int32_t margin = style_get_int(style, STYLE_ID_MARGIN, 2);
int32_t margin = rich_text->margin;
rich_text->render_node =
rich_text_render_node_layout(widget, rich_text->node, c, w, h, margin, line_gap);
@ -143,6 +152,8 @@ static ret_t rich_text_on_paint_self(widget_t* widget, canvas_t* c) {
static ret_t rich_text_on_event(widget_t* widget, event_t* e) {
uint16_t type = e->type;
rich_text_t* rich_text = RICH_TEXT(widget);
return_value_if_fail(rich_text != NULL, RET_BAD_PARAMS);
switch (type) {
case EVT_POINTER_DOWN: {
@ -158,6 +169,11 @@ static ret_t rich_text_on_event(widget_t* widget, event_t* e) {
break;
case EVT_POINTER_ENTER:
break;
case EVT_RESIZE:
case EVT_MOVE_RESIZE: {
rich_text->need_reset = TRUE;
break;
}
default:
break;
}
@ -173,6 +189,11 @@ static ret_t rich_text_set_prop(widget_t* widget, const char* name, const value_
return rich_text_set_text(widget, value_str(v));
} else if (tk_str_eq(name, WIDGET_PROP_LINE_GAP)) {
rich_text->line_gap = value_int(v);
rich_text->need_reset = TRUE;
return RET_OK;
} else if (tk_str_eq(name, WIDGET_PROP_MARGIN)) {
rich_text->margin = value_int(v);
rich_text->need_reset = TRUE;
return RET_OK;
}
@ -202,12 +223,8 @@ ret_t rich_text_set_text(widget_t* widget, const char* text) {
rich_text_t* rich_text = RICH_TEXT(widget);
return_value_if_fail(rich_text != NULL, RET_BAD_PARAMS);
rich_text_reset(widget);
rich_text->node = rich_text_parse(text, strlen(text));
value_t v;
value_set_str(&v, text);
wstr_from_value(&(widget->text), &v);
wstr_set_utf8(&(widget->text), text);
rich_text->need_reset = TRUE;
return RET_OK;
}

View File

@ -85,11 +85,25 @@ typedef struct _rich_text_t {
widget_t widget;
/**
* @property {int32_t} line_gap
* @property {uint32_t} line_gap
* @annotation ["set_prop","get_prop","readable","persitent","design","scriptable"]
*
*/
int32_t line_gap;
uint32_t line_gap;
/**
* @property {uint32_t} margin
* @annotation ["set_prop","get_prop","readable","persitent","design","scriptable"]
*
*/
uint32_t margin;
/**
* @property {bool_t} need_reset
* @annotation ["readable"]
*
*/
bool_t need_reset;
/*private*/
rich_text_node_t* node;

View File

@ -209,6 +209,11 @@ rich_text_render_node_t* rich_text_render_node_layout(widget_t* widget, rich_tex
}
if ((x + tw + cw) > right || break_type == LINE_BREAK_MUST) {
// 一行的起始不需要换行,且最少包含一个字符
if (x == margin) {
if (i == start) i = start + 1;
break_type = LINE_BREAK_MUST;
}
if (break_type != LINE_BREAK_MUST) {
if ((i - last_breakable) < 10) {
i = last_breakable;