1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 14:38:11 +00:00

LibGUI: Fix broken doubleclick detection due to uninitialized GElapsedTimer.

This commit is contained in:
Andreas Kling 2019-04-01 22:03:32 +02:00
parent ee4d7c18c8
commit c9b0d87927
4 changed files with 17 additions and 3 deletions

View file

@ -1,8 +1,10 @@
#include <LibGUI/GElapsedTimer.h>
#include <AK/Assertions.h>
#include <sys/time.h>
void GElapsedTimer::start()
{
m_valid = true;
gettimeofday(&m_start_time, nullptr);
}
@ -18,6 +20,7 @@ inline void timersub(const struct timeval* a, const struct timeval* b, struct ti
int GElapsedTimer::elapsed() const
{
ASSERT(is_valid());
struct timeval now;
gettimeofday(&now, nullptr);
struct timeval diff;

View file

@ -6,9 +6,11 @@ class GElapsedTimer {
public:
GElapsedTimer() { }
bool is_valid() const { return m_valid; }
void start();
int elapsed() const;
private:
bool m_valid { false };
struct timeval m_start_time { 0, 0 };
};

View file

@ -67,9 +67,7 @@ void GWidget::event(GEvent& event)
case GEvent::MouseMove:
return mousemove_event(static_cast<GMouseEvent&>(event));
case GEvent::MouseDown:
if (accepts_focus())
set_focus(true);
return mousedown_event(static_cast<GMouseEvent&>(event));
return handle_mousedown_event(static_cast<GMouseEvent&>(event));
case GEvent::MouseUp:
return handle_mouseup_event(static_cast<GMouseEvent&>(event));
case GEvent::Enter:
@ -154,6 +152,7 @@ void GWidget::handle_mouseup_event(GMouseEvent& event)
// It's a click.. but is it a doubleclick?
// FIXME: This needs improvement.
int elapsed_since_last_click = m_click_clock.elapsed();
dbgprintf("Click clock elapsed: %d\n", m_click_clock.elapsed());
if (elapsed_since_last_click < 250) {
doubleclick_event(event);
} else {
@ -161,6 +160,15 @@ void GWidget::handle_mouseup_event(GMouseEvent& event)
}
}
void GWidget::handle_mousedown_event(GMouseEvent& event)
{
if (accepts_focus())
set_focus(true);
if (!m_click_clock.is_valid())
m_click_clock.start();
mousedown_event(event);
}
void GWidget::click_event(GMouseEvent&)
{
}

View file

@ -147,6 +147,7 @@ private:
void handle_paint_event(GPaintEvent&);
void handle_resize_event(GResizeEvent&);
void handle_mousedown_event(GMouseEvent&);
void handle_mouseup_event(GMouseEvent&);
void do_layout();