add inputing prop for edit/mledit slider

This commit is contained in:
xianjimli 2020-08-04 16:08:50 +08:00
parent ce21747b18
commit 9d3fdf662e
11 changed files with 135 additions and 1 deletions

View File

@ -65,6 +65,12 @@ BEGIN_C_DECLS
*/
#define WIDGET_PROP_H "h"
/**
* @const WIDGET_PROP_INPUTING
* inputing
*/
#define WIDGET_PROP_INPUTING "inputing"
/**
* @const WIDGET_PROP_CARET_X
* caret x

View File

@ -22,6 +22,7 @@
#include "tkc/mem.h"
#include "tkc/utf8.h"
#include "tkc/utils.h"
#include "tkc/time_now.h"
#include "base/events.h"
#include "mledit/mledit.h"
#include "mledit/line_number.h"
@ -176,6 +177,10 @@ static ret_t mledit_get_prop(widget_t* widget, const char* name, value_t* v) {
text_edit_get_state(mledit->model, &state);
value_set_int(v, state.caret.y - state.oy);
return RET_OK;
} else if (tk_str_eq(name, WIDGET_PROP_INPUTING)) {
bool_t inputing = (time_now_ms() - mledit->last_user_action_time) < 1500;
value_set_bool(v, inputing);
return RET_OK;
}
return RET_NOT_FOUND;
@ -432,6 +437,7 @@ static ret_t mledit_on_event(widget_t* widget, event_t* e) {
if (widget->target == NULL) {
mledit_request_input_method(widget);
}
mledit->last_user_action_time = e->time;
mledit_update_status(widget);
widget_invalidate(widget, NULL);
break;
@ -449,11 +455,16 @@ static ret_t mledit_on_event(widget_t* widget, event_t* e) {
ret = RET_STOP;
}
}
if (evt.pressed) {
mledit->last_user_action_time = e->time;
}
widget_invalidate(widget, NULL);
break;
}
case EVT_POINTER_UP: {
widget_ungrab(widget->parent, widget);
mledit->last_user_action_time = e->time;
widget_invalidate(widget, NULL);
break;
}
@ -474,6 +485,7 @@ static ret_t mledit_on_event(widget_t* widget, event_t* e) {
mledit_update_status(widget);
ret = RET_STOP;
mledit->last_user_action_time = e->time;
widget_invalidate(widget, NULL);
break;
}
@ -495,18 +507,22 @@ static ret_t mledit_on_event(widget_t* widget, event_t* e) {
}
mledit_commit_str(widget, evt->text);
mledit_update_status(widget);
mledit->last_user_action_time = e->time;
widget_invalidate(widget, NULL);
break;
}
case EVT_IM_PREEDIT: {
mledit->last_user_action_time = e->time;
text_edit_preedit(mledit->model);
break;
}
case EVT_IM_PREEDIT_CONFIRM: {
mledit->last_user_action_time = e->time;
text_edit_preedit_confirm(mledit->model);
break;
}
case EVT_IM_PREEDIT_ABORT: {
mledit->last_user_action_time = e->time;
text_edit_preedit_abort(mledit->model);
break;
}
@ -522,6 +538,7 @@ static ret_t mledit_on_event(widget_t* widget, event_t* e) {
} else {
ret = text_edit_key_up(mledit->model, key_event);
}
mledit->last_user_action_time = e->time;
widget_invalidate(widget, NULL);
break;
}
@ -566,6 +583,7 @@ static ret_t mledit_on_event(widget_t* widget, event_t* e) {
}
}
ret = RET_STOP;
mledit->last_user_action_time = e->time;
widget_invalidate(widget, NULL);
break;
}

View File

@ -134,6 +134,7 @@ typedef struct _mledit_t {
uint32_t timer_id;
wstr_t temp;
uint64_t last_user_action_time;
} mledit_t;
/**

View File

@ -337,6 +337,7 @@ enum { TK_NAME_LEN = 31 };
(((s1) != NULL) && ((s2) != NULL) && *(s1) == *(s2) && wcscmp((s1), (s2)) == 0)
#endif /*WITH_CPPCHECK*/
#define tk_lfequal(f1, f2) (fabs((f1) - (f2)) < 0.0001)
#define tk_fequal(f1, f2) (fabs((f1) - (f2)) < 0.0000001)
#define TK_ROUND_TO(size, round_size) ((((size) + round_size - 1) / round_size) * round_size)

View File

@ -556,7 +556,7 @@ bool_t value_equal(const value_t* v, const value_t* other) {
return tk_fequal(v->value.f32, other->value.f32);
}
case VALUE_TYPE_DOUBLE: {
return tk_fequal(v->value.f64, other->value.f64);
return tk_lfequal(v->value.f64, other->value.f64);
}
case VALUE_TYPE_STRING: {
return (v->value.str == other->value.str) || tk_str_eq(v->value.str, other->value.str);

View File

@ -28,6 +28,7 @@
#include "base/idle.h"
#include "base/timer.h"
#include "tkc/utils.h"
#include "tkc/time_now.h"
#include "base/enums.h"
#include "base/events.h"
#include "base/clip_board.h"
@ -598,6 +599,7 @@ ret_t edit_on_event(widget_t* widget, event_t* e) {
}
edit_update_status(widget);
widget_invalidate(widget, NULL);
edit->last_user_action_time = e->time;
break;
}
case EVT_POINTER_DOWN_ABORT: {
@ -611,6 +613,10 @@ ret_t edit_on_event(widget_t* widget, event_t* e) {
text_edit_drag(edit->model, evt.x, evt.y);
ret = RET_STOP;
}
if (evt.pressed) {
edit->last_user_action_time = e->time;
}
widget_invalidate(widget, NULL);
break;
}
@ -618,14 +624,17 @@ ret_t edit_on_event(widget_t* widget, event_t* e) {
ret = RET_STOP;
widget_ungrab(widget->parent, widget);
widget_invalidate(widget, NULL);
edit->last_user_action_time = e->time;
break;
}
case EVT_KEY_DOWN: {
edit->last_user_action_time = e->time;
ret = edit_on_key_down(widget, (key_event_t*)e);
edit_update_status(widget);
break;
}
case EVT_KEY_UP: {
edit->last_user_action_time = e->time;
ret = edit_on_key_up(widget, (key_event_t*)e);
widget_invalidate(widget, NULL);
break;
@ -635,6 +644,7 @@ ret_t edit_on_event(widget_t* widget, event_t* e) {
text_edit_get_state(edit->model, &state);
im_commit_event_t* evt = (im_commit_event_t*)e;
edit->last_user_action_time = e->time;
if (state.preedit) {
text_edit_preedit_clear(edit->model);
}
@ -690,6 +700,7 @@ ret_t edit_on_event(widget_t* widget, event_t* e) {
edit_inc(edit);
}
ret = RET_STOP;
edit->last_user_action_time = e->time;
break;
}
case EVT_RESIZE:
@ -1028,6 +1039,10 @@ ret_t edit_get_prop(widget_t* widget, const char* name, value_t* v) {
text_edit_get_state(edit->model, &state);
value_set_int(v, state.caret.y);
return RET_OK;
} else if (tk_str_eq(name, WIDGET_PROP_INPUTING)) {
int64_t delta = (time_now_ms() - edit->last_user_action_time);
value_set_bool(v, delta < 1500);
return RET_OK;
}
return RET_NOT_FOUND;

View File

@ -232,6 +232,7 @@ typedef struct _edit_t {
edit_pre_input_t pre_input;
edit_is_valid_char_t is_valid_char;
edit_is_valid_value_t is_valid_value;
uint64_t last_user_action_time;
} edit_t;
/**

View File

@ -484,6 +484,9 @@ static ret_t slider_get_prop(widget_t* widget, const char* name, value_t* v) {
} else if (tk_str_eq(name, SLIDER_PROP_SLIDE_WITH_BAR)) {
value_set_bool(v, slider->slide_with_bar);
return RET_OK;
} else if (tk_str_eq(name, WIDGET_PROP_INPUTING)) {
value_set_bool(v, slider->dragging);
return RET_OK;
}
return RET_NOT_FOUND;

View File

@ -501,3 +501,42 @@ TEST(Edit, value) {
widget_destroy(w1);
}
TEST(Edit, intputing1) {
value_t v;
pointer_event_t evt;
widget_t* w1 = edit_create(NULL, 10, 20, 30, 40);
pointer_event_init(&evt, EVT_POINTER_DOWN, w1, 0, 0);
ASSERT_EQ(widget_get_prop_bool(w1, WIDGET_PROP_INPUTING, TRUE), FALSE);
widget_dispatch(w1, (event_t*)&evt);
ASSERT_EQ(widget_get_prop_bool(w1, WIDGET_PROP_INPUTING, TRUE), TRUE);
widget_destroy(w1);
}
TEST(Edit, intputing2) {
value_t v;
key_event_t evt;
widget_t* w1 = edit_create(NULL, 10, 20, 30, 40);
key_event_init(&evt, EVT_KEY_DOWN, w1, 0);
ASSERT_EQ(widget_get_prop_bool(w1, WIDGET_PROP_INPUTING, TRUE), FALSE);
widget_dispatch(w1, (event_t*)&evt);
ASSERT_EQ(widget_get_prop_bool(w1, WIDGET_PROP_INPUTING, TRUE), TRUE);
widget_destroy(w1);
}
TEST(Edit, intputing3) {
value_t v;
wheel_event_t evt;
widget_t* w1 = edit_create(NULL, 10, 20, 30, 40);
wheel_event_init(&evt, EVT_WHEEL, w1, 0);
ASSERT_EQ(widget_get_prop_bool(w1, WIDGET_PROP_INPUTING, TRUE), FALSE);
widget_dispatch(w1, (event_t*)&evt);
ASSERT_EQ(widget_get_prop_bool(w1, WIDGET_PROP_INPUTING, TRUE), TRUE);
widget_destroy(w1);
}

View File

@ -73,3 +73,42 @@ TEST(MLEdit, events) {
widget_destroy(b);
}
TEST(MLEdit, intputing1) {
value_t v;
pointer_event_t evt;
widget_t* w1 = mledit_create(NULL, 10, 20, 30, 40);
pointer_event_init(&evt, EVT_POINTER_DOWN, w1, 0, 0);
ASSERT_EQ(widget_get_prop_bool(w1, WIDGET_PROP_INPUTING, TRUE), FALSE);
widget_dispatch(w1, (event_t*)&evt);
ASSERT_EQ(widget_get_prop_bool(w1, WIDGET_PROP_INPUTING, TRUE), TRUE);
widget_destroy(w1);
}
TEST(MLEdit, intputing2) {
value_t v;
key_event_t evt;
widget_t* w1 = mledit_create(NULL, 10, 20, 30, 40);
key_event_init(&evt, EVT_KEY_DOWN, w1, 0);
ASSERT_EQ(widget_get_prop_bool(w1, WIDGET_PROP_INPUTING, TRUE), FALSE);
widget_dispatch(w1, (event_t*)&evt);
ASSERT_EQ(widget_get_prop_bool(w1, WIDGET_PROP_INPUTING, TRUE), TRUE);
widget_destroy(w1);
}
TEST(MLEdit, intputing3) {
value_t v;
wheel_event_t evt;
widget_t* w1 = mledit_create(NULL, 10, 20, 30, 40);
wheel_event_init(&evt, EVT_WHEEL, w1, 0);
ASSERT_EQ(widget_get_prop_bool(w1, WIDGET_PROP_INPUTING, TRUE), FALSE);
widget_dispatch(w1, (event_t*)&evt);
ASSERT_EQ(widget_get_prop_bool(w1, WIDGET_PROP_INPUTING, TRUE), TRUE);
widget_destroy(w1);
}

View File

@ -146,3 +146,14 @@ TEST(Slider, dec_step) {
widget_destroy(w);
}
TEST(Slider, inputing) {
widget_t* w = slider_create(NULL, 0, 0, 100, 100);
slider_t* slider = SLIDER(w);
ASSERT_EQ(widget_get_prop_bool(w, WIDGET_PROP_INPUTING, TRUE), FALSE);
slider->dragging = TRUE;
ASSERT_EQ(widget_get_prop_bool(w, WIDGET_PROP_INPUTING, TRUE), TRUE);
widget_destroy(w);
}