forked from qt-creator/qt-creator
Terminal: Switch to std::chrono
QDateTime's interface various between supported Qt Versions, so we use std::chrono instead Change-Id: I5af5ae9950e61c2ed38ff15dc0580f0dc2aa1f57 Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
@@ -29,8 +29,6 @@
|
|||||||
#include <QScrollBar>
|
#include <QScrollBar>
|
||||||
#include <QTextLayout>
|
#include <QTextLayout>
|
||||||
|
|
||||||
#include <chrono>
|
|
||||||
|
|
||||||
Q_LOGGING_CATEGORY(terminalLog, "qtc.terminal", QtWarningMsg)
|
Q_LOGGING_CATEGORY(terminalLog, "qtc.terminal", QtWarningMsg)
|
||||||
|
|
||||||
using namespace Utils;
|
using namespace Utils;
|
||||||
@@ -41,7 +39,7 @@ namespace Terminal {
|
|||||||
using namespace std::chrono_literals;
|
using namespace std::chrono_literals;
|
||||||
|
|
||||||
// Minimum time between two refreshes. (30fps)
|
// Minimum time between two refreshes. (30fps)
|
||||||
static const auto minRefreshInterval = 1s / 30;
|
static constexpr std::chrono::milliseconds minRefreshInterval = 1s / 30;
|
||||||
|
|
||||||
TerminalWidget::TerminalWidget(QWidget *parent, const OpenTerminalParameters &openParameters)
|
TerminalWidget::TerminalWidget(QWidget *parent, const OpenTerminalParameters &openParameters)
|
||||||
: QAbstractScrollArea(parent)
|
: QAbstractScrollArea(parent)
|
||||||
@@ -54,8 +52,8 @@ TerminalWidget::TerminalWidget(QWidget *parent, const OpenTerminalParameters &op
|
|||||||
, m_zoomInAction(Tr::tr("Zoom In"))
|
, m_zoomInAction(Tr::tr("Zoom In"))
|
||||||
, m_zoomOutAction(Tr::tr("Zoom Out"))
|
, m_zoomOutAction(Tr::tr("Zoom Out"))
|
||||||
, m_openParameters(openParameters)
|
, m_openParameters(openParameters)
|
||||||
, m_lastFlush(QDateTime::currentDateTime())
|
, m_lastFlush(std::chrono::system_clock::now())
|
||||||
, m_lastDoubleClick(QDateTime::currentDateTime())
|
, m_lastDoubleClick(std::chrono::system_clock::now())
|
||||||
{
|
{
|
||||||
setupVTerm();
|
setupVTerm();
|
||||||
setupFont();
|
setupFont();
|
||||||
@@ -451,14 +449,24 @@ void TerminalWidget::onReadyRead(bool forceFlush)
|
|||||||
|
|
||||||
void TerminalWidget::flushVTerm(bool force)
|
void TerminalWidget::flushVTerm(bool force)
|
||||||
{
|
{
|
||||||
if (force || QDateTime::currentDateTime() - m_lastFlush > minRefreshInterval) {
|
const std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
|
||||||
m_lastFlush = QDateTime::currentDateTime();
|
const std::chrono::milliseconds timeSinceLastFlush
|
||||||
|
= std::chrono::duration_cast<std::chrono::milliseconds>(now - m_lastFlush);
|
||||||
|
|
||||||
|
const bool shouldFlushImmediately = timeSinceLastFlush > minRefreshInterval;
|
||||||
|
if (force || shouldFlushImmediately) {
|
||||||
|
if (m_flushDelayTimer.isActive())
|
||||||
|
m_flushDelayTimer.stop();
|
||||||
|
|
||||||
|
m_lastFlush = now;
|
||||||
vterm_screen_flush_damage(m_vtermScreen);
|
vterm_screen_flush_damage(m_vtermScreen);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!m_flushDelayTimer.isActive())
|
if (!m_flushDelayTimer.isActive()) {
|
||||||
m_flushDelayTimer.start();
|
const std::chrono::milliseconds timeToNextFlush = (minRefreshInterval - timeSinceLastFlush);
|
||||||
|
m_flushDelayTimer.start(timeToNextFlush.count());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TerminalWidget::setSelection(const std::optional<Selection> &selection)
|
void TerminalWidget::setSelection(const std::optional<Selection> &selection)
|
||||||
@@ -854,7 +862,7 @@ void TerminalWidget::mousePressEvent(QMouseEvent *event)
|
|||||||
m_selectionStartPos = event->pos();
|
m_selectionStartPos = event->pos();
|
||||||
|
|
||||||
if (event->button() == Qt::LeftButton) {
|
if (event->button() == Qt::LeftButton) {
|
||||||
if (QDateTime::currentDateTime() - m_lastDoubleClick < 500ms) {
|
if (std::chrono::system_clock::now() - m_lastDoubleClick < 500ms) {
|
||||||
m_selectLineMode = true;
|
m_selectLineMode = true;
|
||||||
m_selection->start.setX(0);
|
m_selection->start.setX(0);
|
||||||
m_selection->end.setX(viewport()->width());
|
m_selection->end.setX(viewport()->width());
|
||||||
@@ -922,7 +930,7 @@ void TerminalWidget::mouseDoubleClickEvent(QMouseEvent *event)
|
|||||||
std::u32string::size_type chIdx = (clickPosInGrid.x())
|
std::u32string::size_type chIdx = (clickPosInGrid.x())
|
||||||
+ (clickPosInGrid.y()) * m_vtermSize.width();
|
+ (clickPosInGrid.y()) * m_vtermSize.width();
|
||||||
|
|
||||||
if (chIdx >= text.length() || chIdx < 0)
|
if (chIdx >= text.length())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
std::u32string whiteSpaces = U" \t\x00a0";
|
std::u32string whiteSpaces = U" \t\x00a0";
|
||||||
@@ -949,7 +957,7 @@ void TerminalWidget::mouseDoubleClickEvent(QMouseEvent *event)
|
|||||||
|
|
||||||
setSelection(Selection{selectionStart, selectionEnd});
|
setSelection(Selection{selectionStart, selectionEnd});
|
||||||
|
|
||||||
m_lastDoubleClick = QDateTime::currentDateTime();
|
m_lastDoubleClick = std::chrono::system_clock::now();
|
||||||
|
|
||||||
viewport()->update();
|
viewport()->update();
|
||||||
event->accept();
|
event->accept();
|
||||||
|
@@ -15,6 +15,7 @@
|
|||||||
|
|
||||||
#include <vterm.h>
|
#include <vterm.h>
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
namespace Terminal {
|
namespace Terminal {
|
||||||
@@ -169,8 +170,8 @@ private:
|
|||||||
|
|
||||||
Utils::Terminal::OpenTerminalParameters m_openParameters;
|
Utils::Terminal::OpenTerminalParameters m_openParameters;
|
||||||
|
|
||||||
QDateTime m_lastFlush;
|
std::chrono::system_clock::time_point m_lastFlush;
|
||||||
QDateTime m_lastDoubleClick;
|
std::chrono::system_clock::time_point m_lastDoubleClick;
|
||||||
bool m_selectLineMode{false};
|
bool m_selectLineMode{false};
|
||||||
|
|
||||||
std::u32string m_currentLiveText;
|
std::u32string m_currentLiveText;
|
||||||
|
Reference in New Issue
Block a user