Skip to content

Commit

Permalink
stability
Browse files Browse the repository at this point in the history
  • Loading branch information
Odizinne committed Nov 28, 2024
1 parent bca3e08 commit 1c96318
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 138 deletions.
9 changes: 1 addition & 8 deletions MediaFlyout/MediaFlyout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

MediaFlyout::MediaFlyout(QWidget* parent, MediaSessionWorker *worker)
: QWidget(parent)
, isAnimating(false)
, ui(new Ui::MediaFlyout)
, worker(worker)
, currentlyPlaying(false)
Expand Down Expand Up @@ -72,9 +71,6 @@ void MediaFlyout::paintEvent(QPaintEvent *event)

void MediaFlyout::animateIn()
{
if (isAnimating) return;

isAnimating = true;
QRect screenGeometry = QApplication::primaryScreen()->geometry();
int screenCenterX = screenGeometry.center().x();
int margin = 12;
Expand All @@ -101,7 +97,6 @@ void MediaFlyout::animateIn()
if (currentY == targetY) {
animationTimer->stop();
animationTimer->deleteLater();
isAnimating = false;
return;
}

Expand All @@ -112,9 +107,6 @@ void MediaFlyout::animateIn()

void MediaFlyout::animateOut(QRect trayIconGeometry)
{
if (isAnimating) return;

isAnimating = true;
QRect screenGeometry = QApplication::primaryScreen()->geometry();
int screenCenterX = screenGeometry.center().x();
int panelX = screenCenterX - this->width() / 2;
Expand All @@ -137,6 +129,7 @@ void MediaFlyout::animateOut(QRect trayIconGeometry)
if (currentY == targetY) {
animationTimer->stop();
animationTimer->deleteLater();
emit mediaFlyoutClosed();
return;
}

Expand Down
3 changes: 2 additions & 1 deletion MediaFlyout/MediaFlyout.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class MediaFlyout : public QWidget
void updatePauseButton(QString state);
void updateControls(bool prev, bool next);
void updateProgress(int currentTime, int totalTime);
bool isAnimating;

public slots:
void onPrevClicked();
Expand All @@ -43,6 +42,8 @@ public slots:
MediaSessionWorker *worker;
bool currentlyPlaying;

signals:
void mediaFlyoutClosed();
};

#endif // MEDIASFLYOUT_H
162 changes: 88 additions & 74 deletions MediaSessionWorker/MediaSessionWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,107 +32,121 @@ void MediaSessionWorker::process()

task.Completed([this](winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::Media::Control::GlobalSystemMediaTransportControlsSessionManager> const& sender, winrt::Windows::Foundation::AsyncStatus status)
{
if (status != winrt::Windows::Foundation::AsyncStatus::Completed)
if (status == winrt::Windows::Foundation::AsyncStatus::Completed)
{
emit sessionError();
return;
}
try
{
auto sessionManager = sender.GetResults();
currentSession = sessionManager.GetCurrentSession();

try
{
auto sessionManager = sender.GetResults();
currentSession = sessionManager.GetCurrentSession();
if (currentSession)
{
MediaSession session;

if (!currentSession)
{
emit sessionError();
return;
}
auto timelineProperties = currentSession.GetTimelineProperties();

MediaSession session;
// Get the current position (in 100-nanosecond intervals)
auto position = timelineProperties.Position();
qint64 currentTime = position.count(); // In 100-nanosecond intervals
int currentTimeInt = static_cast<int>(currentTime / 10000000); // Convert to milliseconds and cast to int

// Timeline properties
auto timelineProperties = currentSession.GetTimelineProperties();
auto position = timelineProperties.Position();
auto duration = timelineProperties.EndTime();
session.currentTime = static_cast<int>(position.count() / 10000000); // Convert to seconds
session.duration = static_cast<int>(duration.count() / 10000000);
// Get the total duration (in 100-nanosecond intervals)
auto duration = timelineProperties.EndTime();
qint64 totalTime = duration.count(); // In 100-nanosecond intervals
int totalTimeInt = static_cast<int>(totalTime / 10000000); // Convert to milliseconds and cast to int

// Media properties
auto mediaProperties = currentSession.TryGetMediaPropertiesAsync().get();
session.title = QString::fromStdWString(mediaProperties.Title().c_str());
session.artist = QString::fromStdWString(mediaProperties.Artist().c_str());
session.currentTime = currentTimeInt;
session.duration = totalTimeInt;

// Thumbnail
auto thumbnail = mediaProperties.Thumbnail();
if (thumbnail)
{
auto stream = thumbnail.OpenReadAsync().get();
if (stream)
{
auto inputStream = stream.GetInputStreamAt(0);
winrt::Windows::Storage::Streams::DataReader reader(inputStream);
uint32_t size = stream.Size();
reader.LoadAsync(size).get();
// Retrieve media properties from the session
auto mediaProperties = currentSession.TryGetMediaPropertiesAsync().get();
session.title = QString::fromStdWString(mediaProperties.Title().c_str());
session.artist = QString::fromStdWString(mediaProperties.Artist().c_str());

QByteArray imageData;
for (uint32_t i = 0; i < size; ++i)
// Get album art (icon)
auto thumbnail = mediaProperties.Thumbnail();
if (thumbnail)
{
imageData.append(static_cast<char>(reader.ReadByte()));
}
auto stream = thumbnail.OpenReadAsync().get(); // Open the thumbnail stream
if (stream)
{
// Create DataReader from the input stream
auto inputStream = stream.GetInputStreamAt(0);
winrt::Windows::Storage::Streams::DataReader reader(inputStream);
uint32_t size = stream.Size();
reader.LoadAsync(size).get(); // Load data into the reader

// Convert to QByteArray
QByteArray imageData;
for (uint32_t i = 0; i < size; ++i)
{
imageData.append(static_cast<char>(reader.ReadByte()));
}

QPixmap pixmap;
if (pixmap.loadFromData(imageData))
{
session.icon = pixmap;
// Load into QPixmap
QPixmap pixmap;
if (pixmap.loadFromData(imageData))
{
session.icon = pixmap; // Set the icon if loading is successful
}
else
{
session.icon = Utils::getPlaceholderIcon(); // Fallback icon
}
}
else
{
session.icon = Utils::getPlaceholderIcon(); // No stream available
}
}
else
{
session.icon = Utils::getPlaceholderIcon();
session.icon = Utils::getPlaceholderIcon(); // No thumbnail available
}

// Get playback control information
auto playbackInfo = currentSession.GetPlaybackInfo();
auto playbackControls = playbackInfo.Controls();
session.canGoNext = playbackControls.IsNextEnabled();
session.canGoPrevious = playbackControls.IsPreviousEnabled();

// Get the playback state (Playing, Paused, Stopped)
auto playbackStatus = playbackInfo.PlaybackStatus();
switch (playbackStatus)
{
case winrt::Windows::Media::Control::GlobalSystemMediaTransportControlsSessionPlaybackStatus::Playing:
session.playbackState = "Playing";
break;
case winrt::Windows::Media::Control::GlobalSystemMediaTransportControlsSessionPlaybackStatus::Paused:
session.playbackState = "Paused";
break;
case winrt::Windows::Media::Control::GlobalSystemMediaTransportControlsSessionPlaybackStatus::Stopped:
session.playbackState = "Stopped";
break;
default:
session.playbackState = "Unknown";
break;
}

emit sessionReady(session);
}
else
{
session.icon = Utils::getPlaceholderIcon();
emit sessionError();
}
}
else
{
session.icon = Utils::getPlaceholderIcon();
}

// Playback controls and state
auto playbackInfo = currentSession.GetPlaybackInfo();
auto playbackControls = playbackInfo.Controls();
session.canGoNext = playbackControls.IsNextEnabled();
session.canGoPrevious = playbackControls.IsPreviousEnabled();

auto playbackStatus = playbackInfo.PlaybackStatus();
switch (playbackStatus)
catch (const std::exception& ex)
{
case winrt::Windows::Media::Control::GlobalSystemMediaTransportControlsSessionPlaybackStatus::Playing:
session.playbackState = "Playing";
break;
case winrt::Windows::Media::Control::GlobalSystemMediaTransportControlsSessionPlaybackStatus::Paused:
session.playbackState = "Paused";
break;
case winrt::Windows::Media::Control::GlobalSystemMediaTransportControlsSessionPlaybackStatus::Stopped:
session.playbackState = "Stopped";
break;
default:
session.playbackState = "Unknown";
break;
emit sessionError();
}

emit sessionReady(session);
}
catch (const std::exception&)
else
{
emit sessionError();
}
});
}
catch (const std::exception&)
catch (const std::exception& ex)
{
emit sessionError();
}
Expand Down
11 changes: 2 additions & 9 deletions Panel/Panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

Panel::Panel(QWidget *parent)
: QWidget(parent)
, isAnimating(false)
, isAnimating(true)
, ui(new Ui::Panel)
{
ui->setupUi(this);
Expand Down Expand Up @@ -56,8 +56,6 @@ Panel::~Panel()

void Panel::animateIn(QRect trayIconGeometry)
{
if (isAnimating) return;

isAnimating = true;
QPoint trayIconPos = trayIconGeometry.topLeft();
QRect screenGeometry = QApplication::primaryScreen()->geometry();
Expand All @@ -82,13 +80,10 @@ void Panel::animateIn(QRect trayIconGeometry)
double easedT = 1 - pow(1 - t, 3);
int currentY = startY + easedT * (targetY - startY);

if (currentY - targetY == 12) {
Utils::setAlwaysOnTopState(this, true);
}

if (currentY == targetY) {
animationTimer->stop();
animationTimer->deleteLater();
Utils::setAlwaysOnTopState(this, true);
isAnimating = false;
return;
}
Expand All @@ -100,8 +95,6 @@ void Panel::animateIn(QRect trayIconGeometry)

void Panel::animateOut(QRect trayIconGeometry)
{
if (isAnimating) return;

isAnimating = true;
Utils::setAlwaysOnTopState(this, false);

Expand Down
Loading

0 comments on commit 1c96318

Please sign in to comment.