Skip to content

Commit

Permalink
perf(widget): rename scrollbar widget attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-soft committed Feb 15, 2018
1 parent 30de5b8 commit d397914
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 45 deletions.
2 changes: 1 addition & 1 deletion include/LCUI/gui/widget/scrollbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ enum ScrollBarDirection {

LCUI_API void ScrollBar_BindBox( LCUI_Widget w, LCUI_Widget box );

LCUI_API void ScrollBar_BindLayer( LCUI_Widget w, LCUI_Widget layer );
LCUI_API void ScrollBar_BindTarget( LCUI_Widget w, LCUI_Widget layer );

/** 获取滚动条的位置 */
LCUI_API int ScrollBar_GetPosition( LCUI_Widget w );
Expand Down
91 changes: 47 additions & 44 deletions src/gui/widget/scrollbar.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ typedef struct InertialScrollingRec_ {
/** 滚动条的相关数据 */
typedef struct LCUI_ScrollBarRec_ {
LCUI_Widget box; /**< 容器 */
LCUI_Widget layer; /**< 滚动层 */
LCUI_Widget target; /**< 滚动层 */
LCUI_Widget slider; /**< 滑块 */
LCUI_BOOL is_dragging; /**< 是否处于拖拽状态 */
float slider_x, slider_y; /**< 拖拽开始时的滑块位置 */
Expand Down Expand Up @@ -88,7 +88,7 @@ scrollbar {
background-color: #fafafa;
border: 1px solid #eee;
}
scrollbar .slider {
.scrollbar-slider {
top: 0;
left: 0;
width: 14px;
Expand All @@ -98,13 +98,13 @@ scrollbar .slider {
position: absolute;
background-color: #aaa;
}
scrollbar .slider:hover {
.scrollbar-slider:hover {
background-color: #bbb;
}
scrollbar .slider:active {
.scrollbar-slider:active {
background-color: #999;
}
.scrolllayer {
.scrollbar-target {
top: 0;
left: 0;
position: relative;
Expand Down Expand Up @@ -208,16 +208,16 @@ static void StartInertialScrolling( LCUI_Widget w )
static void Slider_OnMouseMove( LCUI_Widget slider,
LCUI_WidgetEvent e, void *arg )
{
LCUI_Widget layer;
LCUI_Widget target;
LCUI_Widget w = e->data;
LCUI_ScrollBar scrollbar;
float n, box_size, size, layer_pos, x, y;

scrollbar = Widget_GetData( w, self.prototype );
if( !scrollbar->is_dragging || !scrollbar->layer ) {
if( !scrollbar->is_dragging || !scrollbar->target ) {
return;
}
layer = scrollbar->layer;
target = scrollbar->target;
if( scrollbar->direction == SBD_HORIZONTAL ) {
y = 0;
x = scrollbar->slider_x;
Expand All @@ -227,7 +227,7 @@ static void Slider_OnMouseMove( LCUI_Widget slider,
} else {
box_size = w->parent->box.content.width;
}
layer_pos = scrollbar->layer->box.outer.width - box_size;
layer_pos = scrollbar->target->box.outer.width - box_size;
size = w->box.content.width - slider->width;
if( x > size ) {
x = size;
Expand All @@ -242,7 +242,7 @@ static void Slider_OnMouseMove( LCUI_Widget slider,
}
}
layer_pos = layer_pos * n;
SetStyle( layer->custom_style, key_left, -layer_pos, px );
Widget_SetStyle( target, key_left, -layer_pos, px );
} else {
x = 0;
y = scrollbar->slider_y;
Expand All @@ -252,7 +252,7 @@ static void Slider_OnMouseMove( LCUI_Widget slider,
} else {
box_size = w->parent->box.content.height;
}
layer_pos = scrollbar->layer->box.outer.height - box_size;
layer_pos = scrollbar->target->box.outer.height - box_size;
size = w->box.content.height - slider->height;
if( y > size ) {
y = size;
Expand All @@ -267,16 +267,16 @@ static void Slider_OnMouseMove( LCUI_Widget slider,
}
}
layer_pos = layer_pos * n;
SetStyle( layer->custom_style, key_top, -layer_pos, px );
Widget_SetStyle( target, key_top, -layer_pos, px );
}
if( scrollbar->pos != iround( layer_pos ) ) {
LCUI_WidgetEventRec e;
e.type = self.event_id;
e.cancel_bubble = TRUE;
Widget_TriggerEvent( layer, &e, &layer_pos );
Widget_TriggerEvent( target, &e, &layer_pos );
}
scrollbar->pos = iround( layer_pos );
Widget_UpdateStyle( layer, FALSE );
Widget_UpdateStyle( target, FALSE );
Widget_Move( slider, x, y );
}

Expand Down Expand Up @@ -321,16 +321,17 @@ static void ScrollBar_OnInit( LCUI_Widget w )
scrollbar->is_dragging = FALSE;
scrollbar->scroll_step = 64;
scrollbar->slider = slider;
scrollbar->layer = NULL;
scrollbar->target = NULL;
scrollbar->box = NULL;
scrollbar->old_pos = 0;
scrollbar->pos = 0;
scrollbar->touch_point_id = -1;
InitInertialScrolling( &scrollbar->effect );
Widget_BindEvent( slider, "mousedown",
Slider_OnMouseDown, w, NULL );
Widget_AddClass( slider, "slider" );
Widget_AddClass( slider, "scrollbar-slider" );
Widget_Append( w, slider );
ScrollBar_BindBox( w, w->parent );
}

static void ScrollBar_UpdateSize( LCUI_Widget w )
Expand All @@ -342,8 +343,8 @@ static void ScrollBar_UpdateSize( LCUI_Widget w )
return;
}
if( scrollbar->direction == SBD_HORIZONTAL ) {
if( scrollbar->layer ) {
size = scrollbar->layer->box.outer.width;
if( scrollbar->target ) {
size = scrollbar->target->box.outer.width;
} else {
size = 0;
}
Expand All @@ -353,8 +354,8 @@ static void ScrollBar_UpdateSize( LCUI_Widget w )
}
SetStyle( slider->custom_style, key_width, n, scale );
} else {
if( scrollbar->layer ) {
size = scrollbar->layer->box.outer.height;
if( scrollbar->target ) {
size = scrollbar->target->box.outer.height;
} else {
size = 0;
}
Expand All @@ -373,7 +374,7 @@ static void ScrollBar_UpdateSize( LCUI_Widget w )
}
}

static void ScrollLayer_OnWheel( LCUI_Widget layer,
static void ScrollLayer_OnWheel( LCUI_Widget target,
LCUI_WidgetEvent e, void *arg )
{
LCUI_Widget w = e->data;
Expand All @@ -388,7 +389,7 @@ static void ScrollLayer_OnWheel( LCUI_Widget layer,
}

/** 滚动层的触屏事件响应 */
static void ScrollLayer_OnTouch( LCUI_Widget layer,
static void ScrollLayer_OnTouch( LCUI_Widget target,
LCUI_WidgetEvent e, void *arg )
{
uint_t time_delta;
Expand Down Expand Up @@ -428,19 +429,19 @@ static void ScrollLayer_OnTouch( LCUI_Widget layer,
if( scrollbar->is_dragging ) {
return;
}
Widget_SetTouchCapture( layer, point->id );
Widget_SetTouchCapture( target, point->id );
scrollbar->mouse_x = point->x;
scrollbar->mouse_y = point->y;
break;
case WET_TOUCHUP:
Widget_ReleaseTouchCapture( layer, -1 );
Widget_ReleaseTouchCapture( target, -1 );
time_delta = (uint_t)LCUI_GetTimeDelta( scrollbar->timestamp );
if( scrollbar->is_dragging && time_delta < 50 ) {
StartInertialScrolling( w );
}
scrollbar->touch_point_id = -1;
scrollbar->is_dragging = FALSE;
Widget_BlockEvent( layer, FALSE );
Widget_BlockEvent( target, FALSE );
break;
case WET_TOUCHMOVE:
pos = scrollbar->old_pos;
Expand All @@ -455,7 +456,7 @@ static void ScrollLayer_OnTouch( LCUI_Widget layer,
if( !scrollbar->is_dragging ) {
scrollbar->is_dragging = TRUE;
LCUIWidget_ClearEventTarget( NULL );
Widget_BlockEvent( layer, TRUE );
Widget_BlockEvent( target, TRUE );
}
distance = pos - scrollbar->pos;
if( (scrollbar->distance > 0) != (distance > 0) ||
Expand Down Expand Up @@ -497,17 +498,19 @@ void ScrollBar_BindBox( LCUI_Widget w, LCUI_Widget box )
ScrollBar_UpdateSize( w );
}

void ScrollBar_BindLayer( LCUI_Widget w, LCUI_Widget layer )
void ScrollBar_BindTarget( LCUI_Widget w, LCUI_Widget target )
{
LCUI_ScrollBar scrollbar = Widget_GetData( w, self.prototype );
if( scrollbar->layer ) {
Widget_UnbindEvent( scrollbar->layer, "resize",
if( scrollbar->target ) {
Widget_RemoveClass( scrollbar->target, "scrollbar-target" );
Widget_UnbindEvent( scrollbar->target, "resize",
ScrollBar_OnUpdateSize );
}
scrollbar->layer = layer;
Widget_BindEvent( layer, "resize", ScrollBar_OnUpdateSize, w, NULL );
Widget_BindEvent( layer, "mousewheel", ScrollLayer_OnWheel, w, NULL );
Widget_BindEvent( layer, "touch", ScrollLayer_OnTouch, w, NULL );
scrollbar->target = target;
Widget_AddClass( target, "scrollbar-target" );
Widget_BindEvent( target, "resize", ScrollBar_OnUpdateSize, w, NULL );
Widget_BindEvent( target, "mousewheel", ScrollLayer_OnWheel, w, NULL );
Widget_BindEvent( target, "touch", ScrollLayer_OnTouch, w, NULL );
ScrollBar_UpdateSize( w );
}

Expand All @@ -522,16 +525,16 @@ void ScrollBar_SetPosition( LCUI_Widget w, int pos )
float new_pos, box_size, size, slider_pos;
LCUI_ScrollBar scrollbar = Widget_GetData( w, self.prototype );
LCUI_Widget slider = scrollbar->slider;
LCUI_Widget layer = scrollbar->layer;
LCUI_Widget target = scrollbar->target;
LCUI_WidgetEvent e;

if( !layer ) {
if( !target ) {
return;
}
new_pos = 1.0f * pos;
memset( &e, 0, sizeof( e ) );
if( scrollbar->direction == SBD_HORIZONTAL ) {
size = scrollbar->layer->box.outer.width;
size = scrollbar->target->box.outer.width;
if( scrollbar->box ) {
box_size = scrollbar->box->box.content.width;
} else {
Expand All @@ -546,9 +549,9 @@ void ScrollBar_SetPosition( LCUI_Widget w, int pos )
slider_pos = w->box.content.width - slider->width;
slider_pos = slider_pos * new_pos / (size - box_size);
Widget_SetStyle( slider, key_left, slider_pos, px );
Widget_SetStyle( layer, key_left, -new_pos, px );
Widget_SetStyle( target, key_left, -new_pos, px );
} else {
size = scrollbar->layer->box.outer.height;
size = scrollbar->target->box.outer.height;
if( scrollbar->box ) {
box_size = scrollbar->box->box.content.height;
} else {
Expand All @@ -567,18 +570,18 @@ void ScrollBar_SetPosition( LCUI_Widget w, int pos )
slider_pos = slider_pos * new_pos / (size - box_size);
}
Widget_SetStyle( slider, key_top, slider_pos, px );
Widget_SetStyle( layer, key_top, -new_pos, px );
Widget_SetStyle( target, key_top, -new_pos, px );
}
pos = iround( new_pos );
if( scrollbar->pos != pos ) {
LCUI_WidgetEventRec e;
e.type = self.event_id;
e.cancel_bubble = TRUE;
Widget_TriggerEvent( layer, &e, &new_pos );
Widget_TriggerEvent( target, &e, &new_pos );
}
scrollbar->pos = pos;
Widget_UpdateStyle( slider, FALSE );
Widget_UpdateStyle( layer, FALSE );
Widget_UpdateStyle( target, FALSE );
}

void ScrollBar_SetDirection( LCUI_Widget w, int direction )
Expand All @@ -596,15 +599,15 @@ static void ScrollBar_OnSetAttr( LCUI_Widget w,
const char *name, const char *value )
{
LCUI_Widget target;
if( strcmp( name, "data-parent" ) == 0 ) {
if( strcmp( name, "parent" ) == 0 ) {
target = LCUIWidget_GetById( value );
if( target ) {
ScrollBar_BindBox( w, target );
}
} else if( strcmp( name, "data-layer" ) == 0 ) {
} else if( strcmp( name, "target" ) == 0 ) {
target = LCUIWidget_GetById( value );
if( target ) {
ScrollBar_BindLayer( w, target );
ScrollBar_BindTarget( w, target );
}
}
}
Expand Down

0 comments on commit d397914

Please sign in to comment.