gui2/ttext_: Refactor cursor timer setup code

This also makes sure the timer id is set to zero after removing the
timer.
This commit is contained in:
Ignacio R. Morelle 2016-10-12 02:27:27 -03:00
parent 19d6d45800
commit db0b20dae7
2 changed files with 16 additions and 8 deletions

View file

@ -58,14 +58,12 @@ ttext_::ttext_()
connect_signal<event::LOSE_KEYBOARD_FOCUS>( connect_signal<event::LOSE_KEYBOARD_FOCUS>(
std::bind(&ttext_::signal_handler_lose_keyboard_focus, this, _2)); std::bind(&ttext_::signal_handler_lose_keyboard_focus, this, _2));
cursor_timer_ = add_timer(cursor_blink_rate_ms_, std::bind(&ttext_::cursor_timer_callback, this), true); toggle_cursor_timer(true);
} }
ttext_::~ttext_() ttext_::~ttext_()
{ {
if(cursor_timer_) { toggle_cursor_timer(false);
remove_timer(cursor_timer_);
}
} }
void ttext_::set_active(const bool active) void ttext_::set_active(const bool active)
@ -254,6 +252,17 @@ void ttext_::set_state(const tstate state)
} }
} }
void ttext_::toggle_cursor_timer(bool enable)
{
if(cursor_timer_) {
remove_timer(cursor_timer_);
}
cursor_timer_ = enable
? add_timer(cursor_blink_rate_ms_, std::bind(&ttext_::cursor_timer_callback, this), true)
: 0;
}
void ttext_::cursor_timer_callback() void ttext_::cursor_timer_callback()
{ {
switch(state_) { switch(state_) {
@ -283,10 +292,7 @@ void ttext_::reset_cursor_state()
} }
// Restart the blink timer. // Restart the blink timer.
if(cursor_timer_) { toggle_cursor_timer(true);
remove_timer(cursor_timer_);
}
cursor_timer_ = add_timer(cursor_blink_rate_ms_, std::bind(&ttext_::cursor_timer_callback, this), true);
} }
void ttext_::handle_key_left_arrow(SDL_Keymod modifier, bool& handled) void ttext_::handle_key_left_arrow(SDL_Keymod modifier, bool& handled)

View file

@ -264,6 +264,8 @@ private:
void set_state(const tstate state); void set_state(const tstate state);
virtual void toggle_cursor_timer(bool enable);
/** Implements blinking cursor functionality. */ /** Implements blinking cursor functionality. */
virtual void cursor_timer_callback(); virtual void cursor_timer_callback();