Skip to content

Commit

Permalink
fix(gui): widget auto size computation bug
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-soft committed Mar 12, 2018
1 parent a40eda2 commit 095f4b8
Show file tree
Hide file tree
Showing 8 changed files with 281 additions and 212 deletions.
22 changes: 17 additions & 5 deletions include/LCUI/gui/widget_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ typedef struct LCUI_WidgetRec_ {
(Widget_CheckStyleType( W, key_width, SCALE ) ||\
Widget_CheckStyleType( W, key_height, SCALE ))

#define Widget_HasParentDependentWidth(W) \
(Widget_CheckStyleType( W, key_width, scale ) &&\
!Widget_HasStaticWidthParent( W ))

/** 部件是否有值为自动(默认)的样式 */
LCUI_API LCUI_BOOL Widget_HasAutoStyle( LCUI_Widget w, int key );

Expand Down Expand Up @@ -313,9 +317,15 @@ LCUI_API void Widget_ComputeBorder( LCUI_Widget w, LCUI_Border *out );
LCUI_API void Widget_PaintBorder( LCUI_Widget w, LCUI_PaintContext paint,
LCUI_WidgetActualStyle style );

LCUI_API float Widget_GetGraphWidth( LCUI_Widget widget );
LCUI_API float Widget_GetCanvasWidth( LCUI_Widget widget );

LCUI_API float Widget_GetCanvasHeight( LCUI_Widget widget );

LCUI_API float Widget_GetLimitedWidth( LCUI_Widget w, float width );

LCUI_API float Widget_GetGraphHeight( LCUI_Widget widget );
LCUI_API float Widget_GetLimitedHeight( LCUI_Widget w, float height );

LCUI_API void Widget_AutoSize( LCUI_Widget w );

/** 根据阴影参数获取部件区域的横向偏移距离 */
LCUI_API float Widget_GetBoxShadowOffsetX( LCUI_Widget w );
Expand All @@ -324,10 +334,10 @@ LCUI_API float Widget_GetBoxShadowOffsetX( LCUI_Widget w );
LCUI_API float Widget_GetBoxShadowOffsetY( LCUI_Widget w );

/** 获取部件在添加阴影后的高度 */
LCUI_API float Widget_GetGraphWidth( LCUI_Widget w );
LCUI_API float Widget_GetCanvasWidth( LCUI_Widget w );

/** 获取部件在添加阴影后的宽度 */
LCUI_API float Widget_GetGraphWidth( LCUI_Widget w );
LCUI_API float Widget_GetCanvasWidth( LCUI_Widget w );

/** 更新部件矩形阴影样式 */
LCUI_API void Widget_UpdateBoxShadow( LCUI_Widget w );
Expand Down Expand Up @@ -426,8 +436,10 @@ LCUI_API float Widget_ComputeMaxContentWidth( LCUI_Widget w );
/** 计算部件的最大可用宽度 */
LCUI_API float Widget_ComputeMaxAvaliableWidth( LCUI_Widget widget );

LCUI_API void Widget_ComputeLimitSize( LCUI_Widget w );

/** 从部件中移除一个状态 */
int Widget_RemoveStatus( LCUI_Widget w, const char *status_name );
LCUI_API int Widget_RemoveStatus( LCUI_Widget w, const char *status_name );

/** 打印部件树 */
LCUI_API void Widget_PrintTree( LCUI_Widget w );
Expand Down
11 changes: 10 additions & 1 deletion src/font/textlayer.c
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,9 @@ int TextLayer_GetWidth( LCUI_TextLayer layer )
int i, row, w, max_w;
TextRow txtrow;

DEBUG_MSG( "rows: %d, font-size: %d\n",
layer->text_rows.length,
layer->text_style.pixel_size );
for( row = 0, max_w = 0; row < layer->text_rows.length; ++row ) {
txtrow = layer->text_rows.rows[row];
for( i = 0, w = 0; i < txtrow->length; ++i ) {
Expand All @@ -921,6 +924,12 @@ int TextLayer_GetWidth( LCUI_TextLayer layer )
continue;
}
w += txtrow->string[i]->bitmap->advance.x;
DEBUG_MSG("[%d/%d] %d %c, width: %d/%d\n",
i, txtrow->length,
txtrow->string[i]->char_code,
txtrow->string[i]->char_code,
txtrow->string[i]->bitmap->advance.x,
w);
}
if( w > max_w ) {
max_w = w;
Expand Down Expand Up @@ -1247,7 +1256,7 @@ static void TextLayer_ValidateArea( LCUI_TextLayer layer, LCUI_Rect *area )
width = layer->width;
}
if( layer->fixed_height > 0 ) {
height = layer->fixed_width;
height = layer->fixed_height;
} else {
height = TextLayer_GetHeight( layer );
}
Expand Down
56 changes: 38 additions & 18 deletions src/gui/widget/textview.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ static void TextView_SetTaskForLineHeight( LCUI_Widget w, int height )
txt->tasks[TASK_UPDATE].is_valid = TRUE;
}

static void TextView_SetTaskForTextStyle( LCUI_Widget w, LCUI_TextStyle style )
static void TextView_SetTaskForTextStyle( LCUI_Widget w,
LCUI_TextStyle style )
{
LCUI_TextView txt = GetData( w );
TextStyle_Copy( &txt->tasks[TASK_SET_TEXT_STYLE].style, style );
Expand Down Expand Up @@ -196,19 +197,16 @@ static void TextView_UpdateStyle( LCUI_Widget w )
TextStyle_Destroy( &ts );
}

static void TextView_UpdateSize( LCUI_Widget w )
static void TextView_UpdateLayerSize( LCUI_Widget w )
{
LCUI_RectF rect;
LCUI_TextView txt;
LinkedList rects;
LinkedListNode *node;
int width = 0, height = 0;
float scale, max_width = 0, max_height = 0;

txt = GetData( w );
LinkedList_Init( &rects );
scale = LCUIMetrics_GetScale();
if( Widget_HasFitContentWidth( w ) ) {
if( Widget_HasFitContentWidth( w ) ||
Widget_HasParentDependentWidth( w ) ) {
max_width = Widget_ComputeMaxContentWidth( w );
} else {
max_width = w->box.content.width;
Expand All @@ -223,6 +221,20 @@ static void TextView_UpdateSize( LCUI_Widget w )
width = LCUIMetrics_ComputeActual( max_width, SVT_PX );
height = LCUIMetrics_ComputeActual( max_height, SVT_PX );
TextLayer_SetMaxSize( txt->layer, width, height );
}

static void TextView_OnResize( LCUI_Widget w, LCUI_WidgetEvent e, void *arg )
{
float scale;
LCUI_RectF rect;
LCUI_TextView txt;
LinkedList rects;
LinkedListNode *node;

txt = GetData( w );
LinkedList_Init( &rects );
scale = LCUIMetrics_GetScale();
TextView_UpdateLayerSize( w );
TextLayer_Update( txt->layer, &rects );
for( LinkedList_Each( node, &rects ) ) {
LCUIRect_ToRectF( node->data, &rect, 1.0f / scale );
Expand All @@ -232,11 +244,6 @@ static void TextView_UpdateSize( LCUI_Widget w )
TextLayer_ClearInvalidRect( txt->layer );
}

static void TextView_OnResize( LCUI_Widget w, LCUI_WidgetEvent e, void *arg )
{
TextView_UpdateSize( w );
}

/** 初始化 TextView 部件数据 */
static void TextView_OnInit( LCUI_Widget w )
{
Expand Down Expand Up @@ -287,14 +294,22 @@ static void TextView_OnDestroy( LCUI_Widget w )

static void TextView_AutoSize( LCUI_Widget w, float *width, float *height )
{
float max_width;
int fixed_w, fixed_h;
LCUI_TextView txt = GetData( w );
float scale = LCUIMetrics_GetScale();

TextView_UpdateLayerSize( w );
fixed_w = txt->layer->fixed_width;
fixed_h = txt->layer->fixed_height;
if( Widget_HasFitContentWidth( w ) ||
!Widget_HasStaticWidthParent( w ) ) {
int fixed_w = txt->layer->fixed_width;
int fixed_h = txt->layer->fixed_height;
/* 解除固定宽高设置,以计算最大宽高 */
TextLayer_SetFixedSize( txt->layer, (int)(*width * scale), 0 );
if( Widget_HasParentDependentWidth( w ) ) {
max_width = scale * Widget_ComputeMaxContentWidth( w );
TextLayer_SetMaxSize( txt->layer, (int)max_width, 0 );
}
TextLayer_Update( txt->layer, NULL );
if( *width <= 0 ) {
*width = TextLayer_GetWidth( txt->layer ) / scale;
Expand Down Expand Up @@ -379,8 +394,12 @@ static void TextView_OnTask( LCUI_Widget w )
}
i = TASK_UPDATE_SIZE;
if( txt->tasks[i].is_valid ) {
TextView_UpdateSize( w );
TextView_UpdateLayerSize( w );
if( Widget_HasFitContentWidth( w ) ) {
Widget_AddTask( w, LCUI_WTASK_RESIZE );
}
txt->tasks[i].is_valid = FALSE;
txt->tasks[TASK_UPDATE].is_valid = TRUE;
}
i = TASK_UPDATE;
if( !txt->tasks[i].is_valid ) {
Expand All @@ -396,9 +415,10 @@ static void TextView_OnTask( LCUI_Widget w )
}
RectList_Clear( &rects );
TextLayer_ClearInvalidRect( txt->layer );
if( w->style->sheet[key_width].type == SVT_AUTO
|| w->style->sheet[key_height].type == SVT_AUTO ) {
Widget_AddTask( w, LCUI_WTASK_RESIZE );
if( Widget_HasParentDependentWidth( w ) ||
Widget_HasAutoStyle( w, key_width ) ||
Widget_HasAutoStyle( w, key_height ) ) {
Widget_AddTask( w->parent, LCUI_WTASK_RESIZE );
}
}

Expand Down
Loading

0 comments on commit 095f4b8

Please sign in to comment.