on xenial help sign in
Fix and refactor DirectionalDragArea::ActiveTouchesInfo
4bdd3eeDaniel d'Andrada
plugins/Ubuntu/Gestures/DirectionalDragArea.cpp+44−12
@@ -602,12 +602,12 @@ bool DirectionalDragArea::isWithinTouchCompositionWindow()
602//************************** ActiveTouchesInfo **************************
602//************************** ActiveTouchesInfo **************************
603
603
604DirectionalDragArea::ActiveTouchesInfo::ActiveTouchesInfo(const SharedTimeSource &timeSource)
604DirectionalDragArea::ActiveTouchesInfo::ActiveTouchesInfo(const SharedTimeSource &timeSource)
605− : m_timeSource(timeSource)
605+ : m_timeSource(timeSource), m_lastUsedIndex(-1)
606{
606{
607 // Estimate of the maximum number of active touches we might reach.
607 // Estimate of the maximum number of active touches we might reach.
608 // Not a problem if it ends up being an underestimate as this is just
608 // Not a problem if it ends up being an underestimate as this is just
609 // an optimization.
609 // an optimization.
610− reserve(10);
610+ m_vector.resize(3);
611}
611}
612
612
613void DirectionalDragArea::ActiveTouchesInfo::update(QTouchEvent *event)
613void DirectionalDragArea::ActiveTouchesInfo::update(QTouchEvent *event)
@@ -630,33 +630,65 @@ void DirectionalDragArea::ActiveTouchesInfo::update(QTouchEvent *event)
630
630
631void DirectionalDragArea::ActiveTouchesInfo::addTouchPoint(const QTouchEvent::TouchPoint &touchPoint)
631void DirectionalDragArea::ActiveTouchesInfo::addTouchPoint(const QTouchEvent::TouchPoint &touchPoint)
632{
632{
633− ActiveTouchInfo activeTouchInfo;
633+ ActiveTouchInfo &activeTouchInfo = getEmptySlot();
634 activeTouchInfo.id = touchPoint.id();
634 activeTouchInfo.id = touchPoint.id();
635 activeTouchInfo.startTime = m_timeSource->msecsSinceReference();
635 activeTouchInfo.startTime = m_timeSource->msecsSinceReference();
636− append(activeTouchInfo);
637}
636}
638
637
639void DirectionalDragArea::ActiveTouchesInfo::removeTouchPoint(const QTouchEvent::TouchPoint &touchPoint)
638void DirectionalDragArea::ActiveTouchesInfo::removeTouchPoint(const QTouchEvent::TouchPoint &touchPoint)
640{
639{
641− for (int i = 0; i < count(); ++i) {
640+ for (int i = 0; i <= m_lastUsedIndex; ++i) {
642− if (touchPoint.id() == at(i).id) {
641+ if (touchPoint.id() == m_vector.at(i).id) {
643− remove(i);
642+ freeSlot(i);
644 return;
643 return;
645 }
644 }
646 }
645 }
647 Q_ASSERT(false); // shouldn't reach this point
646 Q_ASSERT(false); // shouldn't reach this point
648}
647}
649
648
649+DirectionalDragArea::ActiveTouchInfo &DirectionalDragArea::ActiveTouchesInfo::getEmptySlot()
650+{
651+ Q_ASSERT(m_lastUsedIndex < m_vector.size());
652+
653+ // Look for an in-between vacancy first
654+ for (int i = 0; i < m_lastUsedIndex; ++i) {
655+ ActiveTouchInfo &activeTouchInfo = m_vector[i];
656+ if (!activeTouchInfo.isValid()) {
657+ return activeTouchInfo;
658+ }
659+ }
660+
661+ ++m_lastUsedIndex;
662+ if (m_lastUsedIndex >= m_vector.size()) {
663+ m_vector.resize(m_lastUsedIndex + 1);
664+ }
665+
666+ return m_vector[m_lastUsedIndex];
667+}
668+
669+void DirectionalDragArea::ActiveTouchesInfo::freeSlot(int index)
670+{
671+ m_vector[index].reset();
672+ if (index == m_lastUsedIndex) {
673+ do {
674+ --m_lastUsedIndex;
675+ } while (m_lastUsedIndex >= 0 && !m_vector.at(m_lastUsedIndex).isValid());
676+ }
677+}
678+
650qint64 DirectionalDragArea::ActiveTouchesInfo::mostRecentStartTime()
679qint64 DirectionalDragArea::ActiveTouchesInfo::mostRecentStartTime()
651{
680{
652− Q_ASSERT(count() > 0);
681+ Q_ASSERT(m_lastUsedIndex >= 0);
653
682
654− qint64 highestStartTime;
683+ qint64 highestStartTime = m_vector.at(0).startTime;
655− int i = 0;
684+ int i = 1;
656 do {
685 do {
657− highestStartTime = at(i).startTime;
686+ const ActiveTouchInfo &activeTouchInfo = m_vector.at(i);
687+ if (activeTouchInfo.isValid() && activeTouchInfo.startTime > highestStartTime) {
688+ highestStartTime = activeTouchInfo.startTime;
689+ }
658 ++i;
690 ++i;
659− } while (i < count());
691+ } while (i < m_lastUsedIndex);
660
692
661 return highestStartTime;
693 return highestStartTime;
662}
694}
plugins/Ubuntu/Gestures/DirectionalDragArea.h+7−1
@@ -263,19 +263,25 @@ private:
263 struct ActiveTouchInfo {
263 struct ActiveTouchInfo {
264 ActiveTouchInfo() : id(-1), startTime(-1) {}
264 ActiveTouchInfo() : id(-1), startTime(-1) {}
265 bool isValid() const { return id != -1; }
265 bool isValid() const { return id != -1; }
266+ void reset() { id = -1; }
266 int id;
267 int id;
267 qint64 startTime;
268 qint64 startTime;
268 };
269 };
269− class ActiveTouchesInfo : public QVector<struct ActiveTouchInfo> {
270+ class ActiveTouchesInfo {
270 public:
271 public:
271 ActiveTouchesInfo(const UbuntuGestures::SharedTimeSource &timeSource);
272 ActiveTouchesInfo(const UbuntuGestures::SharedTimeSource &timeSource);
272 void update(QTouchEvent *event);
273 void update(QTouchEvent *event);
273 ActiveTouchInfo &touchInfo(int id);
274 ActiveTouchInfo &touchInfo(int id);
274 qint64 mostRecentStartTime();
275 qint64 mostRecentStartTime();
275 UbuntuGestures::SharedTimeSource m_timeSource;
276 UbuntuGestures::SharedTimeSource m_timeSource;
277+ bool isEmpty() const { return m_lastUsedIndex == -1; }
276 private:
278 private:
277 void addTouchPoint(const QTouchEvent::TouchPoint &touchPoint);
279 void addTouchPoint(const QTouchEvent::TouchPoint &touchPoint);
280+ ActiveTouchInfo &getEmptySlot();
281+ void freeSlot(int index);
278 void removeTouchPoint(const QTouchEvent::TouchPoint &touchPoint);
282 void removeTouchPoint(const QTouchEvent::TouchPoint &touchPoint);
283+ QVector<struct ActiveTouchInfo> m_vector;
284+ int m_lastUsedIndex;
279 } m_activeTouches;
285 } m_activeTouches;
280};
286};
281
287