From 5a5674b8320342f1cba2604b0f426d83589c3fef Mon Sep 17 00:00:00 2001 From: NamedKitten Date: Fri, 30 Nov 2018 08:21:34 +0000 Subject: [PATCH] [UI+Backend] Improved time string events and fixed niconico icons. I made creating time string way more efficient by saving old values and comparing them because they can change multiple times per second but the duration string only needs to update once per second and it also needs to always update when speed changes. TODO: Add speed event to allow speed to be updated when paused. --- src/DirectMpvPlayerBackend.cpp | 33 ++++++++++++++++++++++------- src/DirectMpvPlayerBackend.h | 5 ++++- src/MpvPlayerBackend.cpp | 33 ++++++++++++++++++++++------- src/MpvPlayerBackend.h | 5 ++++- src/qml/icons/Niconico/backward.svg | 3 ++- src/qml/icons/Niconico/forward.svg | 2 +- 6 files changed, 61 insertions(+), 20 deletions(-) diff --git a/src/DirectMpvPlayerBackend.cpp b/src/DirectMpvPlayerBackend.cpp index 3ae5d67..5b122b6 100644 --- a/src/DirectMpvPlayerBackend.cpp +++ b/src/DirectMpvPlayerBackend.cpp @@ -471,13 +471,31 @@ DirectMpvPlayerBackend::on_mpv_events() } void -DirectMpvPlayerBackend::updateDurationString() +DirectMpvPlayerBackend::updateDurationString(int numTime) { - emit durationStringChanged( - QString("%1 / %2 (%3x)") - .arg(Utils::createTimestamp(getProperty("time-pos").toInt()), - totalDurationString, - getProperty("speed").toString())); + QVariant speed = getProperty("speed"); + QMetaMethod metaMethod = sender()->metaObject()->method(senderSignalIndex()); + if (metaMethod.name() == "positionChanged") { + if (speed != lastSpeed) { + lastSpeed = speed.toDouble(); + } else { + if (numTime == lastTime) { + return; + } + } + lastTime = numTime; + lastPositionString = Utils::createTimestamp(lastTime); + } else if (metaMethod.name() == "durationChanged") { + totalDurationString = Utils::createTimestamp(numTime); + } + QString durationString; + durationString += lastPositionString; + durationString += " / "; + durationString += totalDurationString; + if (lastSpeed != 1) { + durationString += " (" + speed.toString() + "x)"; + } + emit durationStringChanged(durationString); } void @@ -518,13 +536,12 @@ DirectMpvPlayerBackend::handle_mpv_event(mpv_event* event) if (prop->format == MPV_FORMAT_DOUBLE) { double time = *(double*)prop->data; emit positionChanged(time); - Utils::ResetScreensaver(); } } else if (strcmp(prop->name, "duration") == 0) { if (prop->format == MPV_FORMAT_DOUBLE) { double time = *(double*)prop->data; - totalDurationString = Utils::createTimestamp(time); emit durationChanged(time); + Utils::ResetScreensaver(); } } else if (strcmp(prop->name, "mute") == 0 || strcmp(prop->name, "volume") == 0) { diff --git a/src/DirectMpvPlayerBackend.h b/src/DirectMpvPlayerBackend.h index c4f8b99..889ff26 100644 --- a/src/DirectMpvPlayerBackend.h +++ b/src/DirectMpvPlayerBackend.h @@ -42,7 +42,10 @@ class DirectMpvPlayerBackend mpv_opengl_cb_context* mpv_gl; MpvRenderer* renderer; bool onTop = false; + int lastTime = 0; + double lastSpeed = 0; QString totalDurationString; + QString lastPositionString; public: static void on_update(void* ctx); @@ -92,7 +95,7 @@ signals: private slots: void doUpdate(); void on_mpv_events(); - void updateDurationString(); + void updateDurationString(int numTime); void handleWindowChanged(QQuickWindow* win); private: diff --git a/src/MpvPlayerBackend.cpp b/src/MpvPlayerBackend.cpp index 62c77a0..52f7403 100644 --- a/src/MpvPlayerBackend.cpp +++ b/src/MpvPlayerBackend.cpp @@ -449,13 +449,31 @@ MpvPlayerBackend::on_mpv_events() } void -MpvPlayerBackend::updateDurationString() +MpvPlayerBackend::updateDurationString(int numTime) { - emit durationStringChanged( - QString("%1 / %2 (%3x)") - .arg(Utils::createTimestamp(getProperty("time-pos").toInt()), - totalDurationString, - getProperty("speed").toString())); + QVariant speed = getProperty("speed"); + QMetaMethod metaMethod = sender()->metaObject()->method(senderSignalIndex()); + if (metaMethod.name() == "positionChanged") { + if (speed != lastSpeed) { + lastSpeed = speed.toDouble(); + } else { + if (numTime == lastTime) { + return; + } + } + lastTime = numTime; + lastPositionString = Utils::createTimestamp(lastTime); + } else if (metaMethod.name() == "durationChanged") { + totalDurationString = Utils::createTimestamp(numTime); + } + QString durationString; + durationString += lastPositionString; + durationString += " / "; + durationString += totalDurationString; + if (lastSpeed != 1) { + durationString += " (" + speed.toString() + "x)"; + } + emit durationStringChanged(durationString); } void @@ -496,13 +514,12 @@ MpvPlayerBackend::handle_mpv_event(mpv_event* event) if (prop->format == MPV_FORMAT_DOUBLE) { double time = *(double*)prop->data; emit positionChanged(time); - Utils::ResetScreensaver(); } } else if (strcmp(prop->name, "duration") == 0) { if (prop->format == MPV_FORMAT_DOUBLE) { double time = *(double*)prop->data; - totalDurationString = Utils::createTimestamp(time); emit durationChanged(time); + Utils::ResetScreensaver(); } } else if (strcmp(prop->name, "mute") == 0 || strcmp(prop->name, "volume") == 0) { diff --git a/src/MpvPlayerBackend.h b/src/MpvPlayerBackend.h index c8161b8..a1501b0 100644 --- a/src/MpvPlayerBackend.h +++ b/src/MpvPlayerBackend.h @@ -27,7 +27,10 @@ class MpvPlayerBackend mpv_handle* mpv; mpv_render_context* mpv_gl; bool onTop = false; + int lastTime = 0; + double lastSpeed = 0; QString totalDurationString; + QString lastPositionString; friend class MpvRenderer; @@ -77,7 +80,7 @@ signals: private slots: void doUpdate(); void on_mpv_events(); - void updateDurationString(); + void updateDurationString(int numTime); private: void handle_mpv_event(mpv_event* event); diff --git a/src/qml/icons/Niconico/backward.svg b/src/qml/icons/Niconico/backward.svg index 633505a..1f4120b 100644 --- a/src/qml/icons/Niconico/backward.svg +++ b/src/qml/icons/Niconico/backward.svg @@ -1 +1,2 @@ - \ No newline at end of file + + diff --git a/src/qml/icons/Niconico/forward.svg b/src/qml/icons/Niconico/forward.svg index 3b31b1b..b6e1557 100644 --- a/src/qml/icons/Niconico/forward.svg +++ b/src/qml/icons/Niconico/forward.svg @@ -1 +1 @@ - \ No newline at end of file +