Refactoring
This commit is contained in:
101
src/systemtrayicon/macsystemtrayicon.h
Normal file
101
src/systemtrayicon/macsystemtrayicon.h
Normal file
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
*Strawberry Music Player
|
||||
* This file was part of Clementine.
|
||||
* Copyright 2010, David Sansome <me@davidsansome.com>
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Strawberry is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef MACSYSTEMTRAYICON_H
|
||||
#define MACSYSTEMTRAYICON_H
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QUrl>
|
||||
#include <QPixmap>
|
||||
#include <QAction>
|
||||
|
||||
#include "includes/scoped_ptr.h"
|
||||
#include "core/song.h"
|
||||
|
||||
class MacSystemTrayIconPrivate;
|
||||
|
||||
class SystemTrayIcon : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit SystemTrayIcon(QObject *parent = nullptr);
|
||||
~SystemTrayIcon();
|
||||
|
||||
bool isSystemTrayAvailable() const { return true; }
|
||||
bool IsSystemTrayAvailable() const { return true; }
|
||||
|
||||
bool isVisible() const { return true; }
|
||||
void setVisible(const bool) {}
|
||||
|
||||
void SetTrayiconProgress(const bool enabled);
|
||||
|
||||
void SetupMenu(QAction *previous, QAction *play, QAction *stop, QAction *stop_after, QAction *next, QAction *mute, QAction *love, QAction *quit);
|
||||
void ShowPopup(const QString&, const QString&, const int) {}
|
||||
|
||||
bool MuteEnabled() const { return false; }
|
||||
void SetMuteEnabled(const bool) {}
|
||||
void MuteButtonStateChanged(const bool) {}
|
||||
|
||||
void SetPlaying(bool enable_play_pause = false);
|
||||
void SetPaused();
|
||||
void SetStopped();
|
||||
|
||||
void SetNowPlaying(const Song &song, const QUrl&);
|
||||
void ClearNowPlaying();
|
||||
|
||||
void SetProgress(const int percentage);
|
||||
|
||||
void LoveVisibilityChanged(const bool) {}
|
||||
void LoveStateChanged(const bool) {}
|
||||
|
||||
private:
|
||||
void SetupMenuItem(QAction *action);
|
||||
QPixmap CreateIcon(const QPixmap &icon, const QPixmap &grey_icon);
|
||||
void UpdateIcon();
|
||||
|
||||
private Q_SLOTS:
|
||||
void ActionChanged();
|
||||
|
||||
Q_SIGNALS:
|
||||
void ChangeVolume(const int delta);
|
||||
void SeekForward();
|
||||
void SeekBackward();
|
||||
void NextTrack();
|
||||
void PreviousTrack();
|
||||
void ShowHide();
|
||||
void PlayPause();
|
||||
|
||||
private:
|
||||
ScopedPtr<MacSystemTrayIconPrivate> p_;
|
||||
|
||||
QPixmap normal_icon_;
|
||||
QPixmap grey_icon_;
|
||||
QPixmap playing_icon_;
|
||||
QPixmap paused_icon_;
|
||||
QPixmap current_state_icon_;
|
||||
bool trayicon_progress_;
|
||||
int song_progress_;
|
||||
Q_DISABLE_COPY(SystemTrayIcon);
|
||||
};
|
||||
|
||||
#endif // MACSYSTEMTRAYICON_H
|
||||
249
src/systemtrayicon/macsystemtrayicon.mm
Normal file
249
src/systemtrayicon/macsystemtrayicon.mm
Normal file
@@ -0,0 +1,249 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* This file was part of Clementine.
|
||||
* Copyright 2010, David Sansome <me@davidsansome.com>
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Strawberry is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QHash>
|
||||
#include <QString>
|
||||
#include <QUrl>
|
||||
#include <QIcon>
|
||||
#include <QAction>
|
||||
|
||||
#include <AppKit/NSMenu.h>
|
||||
#include <AppKit/NSMenuItem.h>
|
||||
|
||||
#include "macsystemtrayicon.h"
|
||||
|
||||
#include "includes/mac_delegate.h"
|
||||
|
||||
#include "core/song.h"
|
||||
#include "core/iconloader.h"
|
||||
|
||||
using namespace Qt::Literals::StringLiterals;
|
||||
|
||||
@interface Target :NSObject {
|
||||
QAction *action_;
|
||||
}
|
||||
- (id) initWithQAction: (QAction*)action;
|
||||
- (void) clicked;
|
||||
@end
|
||||
|
||||
@implementation Target // <NSMenuValidation>
|
||||
- (id) init {
|
||||
return [super init];
|
||||
}
|
||||
|
||||
- (id) initWithQAction: (QAction*)action {
|
||||
action_ = action;
|
||||
return self;
|
||||
}
|
||||
|
||||
- (BOOL) validateMenuItem: (NSMenuItem*)menuItem {
|
||||
Q_UNUSED(menuItem);
|
||||
// This is called when the menu is shown.
|
||||
return action_->isEnabled();
|
||||
}
|
||||
|
||||
- (void) clicked {
|
||||
action_->trigger();
|
||||
}
|
||||
@end
|
||||
|
||||
class MacSystemTrayIconPrivate {
|
||||
public:
|
||||
MacSystemTrayIconPrivate() {
|
||||
dock_menu_ = [[NSMenu alloc] initWithTitle:@"DockMenu"];
|
||||
|
||||
QString title = QT_TR_NOOP(u"Now Playing"_s);
|
||||
NSString *t = [[NSString alloc] initWithUTF8String:title.toUtf8().constData()];
|
||||
now_playing_ = [[NSMenuItem alloc] initWithTitle:t action:nullptr keyEquivalent:@""];
|
||||
now_playing_artist_ = [[NSMenuItem alloc] initWithTitle:@"Nothing to see here" action:nullptr keyEquivalent:@""];
|
||||
now_playing_title_ = [[NSMenuItem alloc] initWithTitle:@"Nothing to see here" action:nullptr keyEquivalent:@""];
|
||||
|
||||
[dock_menu_ insertItem:now_playing_title_ atIndex:0];
|
||||
[dock_menu_ insertItem:now_playing_artist_ atIndex:0];
|
||||
[dock_menu_ insertItem:now_playing_ atIndex:0];
|
||||
|
||||
// Don't look now.
|
||||
// This must be called after our custom NSApplicationDelegate has been set.
|
||||
[reinterpret_cast<AppDelegate*>([NSApp delegate]) setDockMenu:dock_menu_];
|
||||
|
||||
ClearNowPlaying();
|
||||
}
|
||||
|
||||
void AddMenuItem(QAction *action) {
|
||||
// Strip accelarators from name.
|
||||
QString text = action->text().remove(u'&');
|
||||
NSString *title = [[NSString alloc] initWithUTF8String: text.toUtf8().constData()];
|
||||
// Create an object that can receive user clicks and pass them on to the QAction.
|
||||
Target *target = [[Target alloc] initWithQAction:action];
|
||||
NSMenuItem *item = [[[NSMenuItem alloc] initWithTitle:title action:@selector(clicked) keyEquivalent:@""] autorelease];
|
||||
[item setEnabled:action->isEnabled()];
|
||||
[item setTarget:target];
|
||||
[dock_menu_ addItem:item];
|
||||
actions_[action] = item;
|
||||
}
|
||||
|
||||
void ActionChanged(QAction *action) {
|
||||
NSMenuItem *item = actions_[action];
|
||||
NSString *title = [[NSString alloc] initWithUTF8String: action->text().toUtf8().constData()];
|
||||
[item setTitle:title];
|
||||
}
|
||||
|
||||
void AddSeparator() {
|
||||
NSMenuItem *separator = [NSMenuItem separatorItem];
|
||||
[dock_menu_ addItem:separator];
|
||||
}
|
||||
|
||||
void ShowNowPlaying(const QString &artist, const QString &title) {
|
||||
ClearNowPlaying(); // Makes sure the order is consistent.
|
||||
[now_playing_artist_ setTitle: [[NSString alloc] initWithUTF8String: artist.toUtf8().constData()]];
|
||||
[now_playing_title_ setTitle: [[NSString alloc] initWithUTF8String: title.toUtf8().constData()]];
|
||||
title.isEmpty() ? HideItem(now_playing_title_) : ShowItem(now_playing_title_);
|
||||
artist.isEmpty() ? HideItem(now_playing_artist_) : ShowItem(now_playing_artist_);
|
||||
artist.isEmpty() && title.isEmpty() ? HideItem(now_playing_) : ShowItem(now_playing_);
|
||||
}
|
||||
|
||||
void ClearNowPlaying() {
|
||||
// Hiding doesn't seem to work in the dock menu.
|
||||
HideItem(now_playing_);
|
||||
HideItem(now_playing_artist_);
|
||||
HideItem(now_playing_title_);
|
||||
}
|
||||
|
||||
private:
|
||||
void HideItem(NSMenuItem *item) {
|
||||
if ([dock_menu_ indexOfItem:item] != -1) {
|
||||
[dock_menu_ removeItem:item];
|
||||
}
|
||||
}
|
||||
|
||||
void ShowItem(NSMenuItem *item, int index = 0) {
|
||||
if ([dock_menu_ indexOfItem:item] == -1) {
|
||||
[dock_menu_ insertItem:item atIndex:index];
|
||||
}
|
||||
}
|
||||
|
||||
QHash<QAction*, NSMenuItem*> actions_;
|
||||
|
||||
NSMenu *dock_menu_;
|
||||
NSMenuItem *now_playing_;
|
||||
NSMenuItem *now_playing_artist_;
|
||||
NSMenuItem *now_playing_title_;
|
||||
|
||||
Q_DISABLE_COPY(MacSystemTrayIconPrivate);
|
||||
};
|
||||
|
||||
SystemTrayIcon::SystemTrayIcon(QObject *parent)
|
||||
: QObject(parent),
|
||||
normal_icon_(QPixmap(u":/pictures/strawberry.png"_s).scaled(128, 128, Qt::KeepAspectRatio, Qt::SmoothTransformation)),
|
||||
grey_icon_(QPixmap(u":/pictures/strawberry-grey.png"_s).scaled(128, 128, Qt::KeepAspectRatio, Qt::SmoothTransformation)),
|
||||
playing_icon_(u":/pictures/tiny-play.png"_s),
|
||||
paused_icon_(u":/pictures/tiny-pause.png"_s),
|
||||
trayicon_progress_(false),
|
||||
song_progress_(0) {
|
||||
|
||||
QApplication::setWindowIcon(normal_icon_);
|
||||
|
||||
}
|
||||
|
||||
SystemTrayIcon::~SystemTrayIcon() {}
|
||||
|
||||
void SystemTrayIcon::SetTrayiconProgress(const bool enabled) {
|
||||
|
||||
trayicon_progress_ = enabled;
|
||||
UpdateIcon();
|
||||
|
||||
}
|
||||
|
||||
void SystemTrayIcon::SetupMenu(QAction *previous, QAction *play, QAction *stop, QAction *stop_after, QAction *next, QAction *mute, QAction *love, QAction *quit) {
|
||||
|
||||
p_ = std::make_unique<MacSystemTrayIconPrivate>();
|
||||
|
||||
SetupMenuItem(previous);
|
||||
SetupMenuItem(play);
|
||||
SetupMenuItem(stop);
|
||||
SetupMenuItem(stop_after);
|
||||
SetupMenuItem(next);
|
||||
p_->AddSeparator();
|
||||
SetupMenuItem(mute);
|
||||
p_->AddSeparator();
|
||||
SetupMenuItem(love);
|
||||
Q_UNUSED(quit); // Mac already has a Quit item.
|
||||
|
||||
}
|
||||
|
||||
void SystemTrayIcon::SetupMenuItem(QAction *action) {
|
||||
p_->AddMenuItem(action);
|
||||
QObject::connect(action, &QAction::changed, this, &SystemTrayIcon::ActionChanged);
|
||||
}
|
||||
|
||||
void SystemTrayIcon::UpdateIcon() {
|
||||
|
||||
QApplication::setWindowIcon(CreateIcon(normal_icon_, grey_icon_));
|
||||
|
||||
}
|
||||
|
||||
void SystemTrayIcon::ActionChanged() {
|
||||
|
||||
QAction *action = qobject_cast<QAction*>(sender());
|
||||
p_->ActionChanged(action);
|
||||
|
||||
}
|
||||
|
||||
void SystemTrayIcon::SetPlaying(const bool enable_play_pause) {
|
||||
|
||||
Q_UNUSED(enable_play_pause);
|
||||
|
||||
current_state_icon_ = playing_icon_;
|
||||
UpdateIcon();
|
||||
|
||||
}
|
||||
|
||||
void SystemTrayIcon::SetPaused() {
|
||||
|
||||
current_state_icon_ = paused_icon_;
|
||||
UpdateIcon();
|
||||
|
||||
}
|
||||
|
||||
void SystemTrayIcon::SetStopped() {
|
||||
|
||||
current_state_icon_ = QPixmap();
|
||||
UpdateIcon();
|
||||
|
||||
}
|
||||
|
||||
void SystemTrayIcon::SetProgress(const int percentage) {
|
||||
|
||||
song_progress_ = percentage;
|
||||
if (trayicon_progress_) UpdateIcon();
|
||||
|
||||
}
|
||||
|
||||
void SystemTrayIcon::ClearNowPlaying() {
|
||||
p_->ClearNowPlaying();
|
||||
}
|
||||
|
||||
void SystemTrayIcon::SetNowPlaying(const Song &song, const QUrl&) {
|
||||
p_->ShowNowPlaying(song.artist(), song.PrettyTitle());
|
||||
}
|
||||
210
src/systemtrayicon/qtsystemtrayicon.cpp
Normal file
210
src/systemtrayicon/qtsystemtrayicon.cpp
Normal file
@@ -0,0 +1,210 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* This file was part of Clementine.
|
||||
* Copyright 2010, David Sansome <me@davidsansome.com>
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Strawberry is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QCoreApplication>
|
||||
#include <QSystemTrayIcon>
|
||||
#include <QAction>
|
||||
#include <QMenu>
|
||||
#include <QIcon>
|
||||
#include <QString>
|
||||
#include <QUrl>
|
||||
|
||||
#include "core/song.h"
|
||||
#include "core/iconloader.h"
|
||||
#include "qtsystemtrayicon.h"
|
||||
|
||||
using namespace Qt::Literals::StringLiterals;
|
||||
|
||||
SystemTrayIcon::SystemTrayIcon(QObject *parent)
|
||||
: QSystemTrayIcon(parent),
|
||||
menu_(new QMenu),
|
||||
app_name_(QCoreApplication::applicationName()),
|
||||
pixmap_playing_(u":/pictures/tiny-play.png"_s),
|
||||
pixmap_paused_(u":/pictures/tiny-pause.png"_s),
|
||||
action_play_pause_(nullptr),
|
||||
action_stop_(nullptr),
|
||||
action_stop_after_this_track_(nullptr),
|
||||
action_mute_(nullptr),
|
||||
action_love_(nullptr),
|
||||
available_(false),
|
||||
trayicon_progress_(false),
|
||||
song_progress_(0) {
|
||||
|
||||
app_name_[0] = app_name_[0].toUpper();
|
||||
|
||||
const QIcon icon = IconLoader::Load(u"strawberry"_s);
|
||||
const QIcon icon_grey = IconLoader::Load(u"strawberry-grey"_s);
|
||||
pixmap_normal_ = icon.pixmap(48, QIcon::Normal);
|
||||
if (icon_grey.isNull()) {
|
||||
pixmap_grey_ = icon.pixmap(48, QIcon::Disabled);
|
||||
}
|
||||
else {
|
||||
pixmap_grey_ = icon_grey.pixmap(48, QIcon::Disabled);
|
||||
}
|
||||
|
||||
if (isSystemTrayAvailable()) {
|
||||
available_ = true;
|
||||
setIcon(icon);
|
||||
setToolTip(app_name_);
|
||||
}
|
||||
|
||||
QObject::connect(this, &QSystemTrayIcon::activated, this, &SystemTrayIcon::Clicked);
|
||||
|
||||
}
|
||||
|
||||
SystemTrayIcon::~SystemTrayIcon() {
|
||||
delete menu_;
|
||||
}
|
||||
|
||||
void SystemTrayIcon::SetTrayiconProgress(const bool enabled) {
|
||||
|
||||
trayicon_progress_ = enabled;
|
||||
UpdateIcon();
|
||||
|
||||
}
|
||||
|
||||
void SystemTrayIcon::SetupMenu(QAction *previous, QAction *play, QAction *stop, QAction *stop_after, QAction *next, QAction *mute, QAction *love, QAction *quit) {
|
||||
|
||||
// Creating new actions and connecting them to old ones.
|
||||
// This allows us to use old actions without displaying shortcuts that can not be used when Strawberry's window is hidden
|
||||
menu_->addAction(previous->icon(), previous->text(), previous, &QAction::trigger);
|
||||
action_play_pause_ = menu_->addAction(play->icon(), play->text(), play, &QAction::trigger);
|
||||
action_stop_ = menu_->addAction(stop->icon(), stop->text(), stop, &QAction::trigger);
|
||||
action_stop_after_this_track_ = menu_->addAction(stop_after->icon(), stop_after->text(), stop_after, &QAction::trigger);
|
||||
menu_->addAction(next->icon(), next->text(), next, &QAction::trigger);
|
||||
|
||||
menu_->addSeparator();
|
||||
action_mute_ = menu_->addAction(mute->icon(), mute->text(), mute, &QAction::trigger);
|
||||
action_mute_->setCheckable(true);
|
||||
action_mute_->setChecked(mute->isChecked());
|
||||
|
||||
menu_->addSeparator();
|
||||
action_love_ = menu_->addAction(love->icon(), love->text(), love, &QAction::trigger);
|
||||
action_love_->setVisible(love->isVisible());
|
||||
action_love_->setEnabled(love->isEnabled());
|
||||
menu_->addSeparator();
|
||||
menu_->addAction(quit->icon(), quit->text(), quit, &QAction::trigger);
|
||||
|
||||
if (available_) setContextMenu(menu_);
|
||||
|
||||
}
|
||||
|
||||
void SystemTrayIcon::Clicked(const QSystemTrayIcon::ActivationReason reason) {
|
||||
|
||||
switch (reason) {
|
||||
case QSystemTrayIcon::DoubleClick:
|
||||
case QSystemTrayIcon::Trigger:
|
||||
Q_EMIT ShowHide();
|
||||
break;
|
||||
|
||||
case QSystemTrayIcon::MiddleClick:
|
||||
Q_EMIT PlayPause();
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void SystemTrayIcon::ShowPopup(const QString &summary, const QString &message, const int timeout) {
|
||||
if (available_) showMessage(summary, message, QSystemTrayIcon::NoIcon, timeout);
|
||||
}
|
||||
|
||||
void SystemTrayIcon::UpdateIcon() {
|
||||
|
||||
if (available_) setIcon(CreateIcon(pixmap_normal_, pixmap_grey_));
|
||||
|
||||
}
|
||||
|
||||
void SystemTrayIcon::SetPlaying(bool enable_play_pause) {
|
||||
|
||||
current_state_icon_ = pixmap_playing_;
|
||||
UpdateIcon();
|
||||
|
||||
action_stop_->setEnabled(true);
|
||||
action_stop_after_this_track_->setEnabled(true);
|
||||
action_play_pause_->setIcon(IconLoader::Load(u"media-playback-pause"_s));
|
||||
action_play_pause_->setText(tr("Pause"));
|
||||
action_play_pause_->setEnabled(enable_play_pause);
|
||||
|
||||
}
|
||||
|
||||
void SystemTrayIcon::SetPaused() {
|
||||
|
||||
current_state_icon_ = pixmap_paused_;
|
||||
UpdateIcon();
|
||||
|
||||
action_stop_->setEnabled(true);
|
||||
action_stop_after_this_track_->setEnabled(true);
|
||||
action_play_pause_->setIcon(IconLoader::Load(u"media-playback-start"_s));
|
||||
action_play_pause_->setText(tr("Play"));
|
||||
|
||||
action_play_pause_->setEnabled(true);
|
||||
|
||||
}
|
||||
|
||||
void SystemTrayIcon::SetStopped() {
|
||||
|
||||
current_state_icon_ = QPixmap();
|
||||
UpdateIcon();
|
||||
|
||||
action_stop_->setEnabled(false);
|
||||
action_stop_after_this_track_->setEnabled(false);
|
||||
action_play_pause_->setIcon(IconLoader::Load(u"media-playback-start"_s));
|
||||
action_play_pause_->setText(tr("Play"));
|
||||
|
||||
action_play_pause_->setEnabled(true);
|
||||
|
||||
action_love_->setEnabled(false);
|
||||
|
||||
}
|
||||
|
||||
void SystemTrayIcon::SetProgress(const int percentage) {
|
||||
|
||||
song_progress_ = percentage;
|
||||
UpdateIcon();
|
||||
|
||||
}
|
||||
|
||||
void SystemTrayIcon::MuteButtonStateChanged(const bool value) {
|
||||
if (action_mute_) action_mute_->setChecked(value);
|
||||
}
|
||||
|
||||
void SystemTrayIcon::SetNowPlaying(const Song &song, const QUrl &url) {
|
||||
Q_UNUSED(url)
|
||||
if (available_) setToolTip(song.PrettyTitleWithArtist());
|
||||
}
|
||||
|
||||
void SystemTrayIcon::ClearNowPlaying() {
|
||||
if (available_) setToolTip(app_name_);
|
||||
}
|
||||
|
||||
void SystemTrayIcon::LoveVisibilityChanged(const bool value) {
|
||||
if (action_love_) action_love_->setVisible(value);
|
||||
}
|
||||
|
||||
void SystemTrayIcon::LoveStateChanged(const bool value) {
|
||||
if (action_love_) action_love_->setEnabled(value);
|
||||
}
|
||||
105
src/systemtrayicon/qtsystemtrayicon.h
Normal file
105
src/systemtrayicon/qtsystemtrayicon.h
Normal file
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* This file was part of Clementine.
|
||||
* Copyright 2010, David Sansome <me@davidsansome.com>
|
||||
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Strawberry is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef QTSYSTEMTRAYICON_H
|
||||
#define QTSYSTEMTRAYICON_H
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QSystemTrayIcon>
|
||||
#include <QString>
|
||||
#include <QUrl>
|
||||
#include <QIcon>
|
||||
#include <QPixmap>
|
||||
#include <QAction>
|
||||
#include <QtEvents>
|
||||
|
||||
#include "core/song.h"
|
||||
|
||||
class QMenu;
|
||||
|
||||
class SystemTrayIcon : public QSystemTrayIcon {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit SystemTrayIcon(QObject *parent = nullptr);
|
||||
~SystemTrayIcon() override;
|
||||
|
||||
bool IsSystemTrayAvailable() const { return available_; }
|
||||
|
||||
void SetTrayiconProgress(const bool enabled);
|
||||
|
||||
void SetupMenu(QAction *previous, QAction *play, QAction *stop, QAction *stop_after, QAction *next, QAction *mute, QAction *love, QAction *quit);
|
||||
void ShowPopup(const QString &summary, const QString &message, const int timeout);
|
||||
|
||||
void SetPlaying(const bool enable_play_pause = false);
|
||||
void SetPaused();
|
||||
void SetStopped();
|
||||
void SetProgress(const int percentage);
|
||||
void MuteButtonStateChanged(const bool value);
|
||||
void SetNowPlaying(const Song &song, const QUrl &url);
|
||||
void ClearNowPlaying();
|
||||
void LoveVisibilityChanged(const bool value);
|
||||
void LoveStateChanged(const bool value);
|
||||
|
||||
bool MuteEnabled() const { return action_mute_->isVisible(); }
|
||||
void SetMuteEnabled(const bool enabled) { action_mute_->setVisible(enabled); }
|
||||
|
||||
private:
|
||||
QPixmap CreateIcon(const QPixmap &icon, const QPixmap &grey_icon);
|
||||
void UpdateIcon();
|
||||
|
||||
Q_SIGNALS:
|
||||
void ChangeVolume(const int delta);
|
||||
void SeekForward();
|
||||
void SeekBackward();
|
||||
void NextTrack();
|
||||
void PreviousTrack();
|
||||
void ShowHide();
|
||||
void PlayPause();
|
||||
|
||||
private Q_SLOTS:
|
||||
void Clicked(const QSystemTrayIcon::ActivationReason);
|
||||
|
||||
private:
|
||||
QMenu *menu_;
|
||||
QString app_name_;
|
||||
QPixmap pixmap_normal_;
|
||||
QPixmap pixmap_grey_;
|
||||
QPixmap pixmap_playing_;
|
||||
QPixmap pixmap_paused_;
|
||||
|
||||
QAction *action_play_pause_;
|
||||
QAction *action_stop_;
|
||||
QAction *action_stop_after_this_track_;
|
||||
QAction *action_mute_;
|
||||
QAction *action_love_;
|
||||
|
||||
bool available_;
|
||||
bool trayicon_progress_;
|
||||
int song_progress_;
|
||||
|
||||
QPixmap current_state_icon_;
|
||||
|
||||
};
|
||||
|
||||
#endif // QTSYSTEMTRAYICON_H
|
||||
79
src/systemtrayicon/systemtrayicon.cpp
Normal file
79
src/systemtrayicon/systemtrayicon.cpp
Normal file
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Strawberry Music Player
|
||||
* This file was part of Clementine.
|
||||
* Copyright 2010, David Sansome <me@davidsansome.com>
|
||||
* Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net>
|
||||
*
|
||||
* Strawberry is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Strawberry is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include <QPixmap>
|
||||
#include <QPainter>
|
||||
#include <QPoint>
|
||||
#include <QPolygon>
|
||||
#include <QRect>
|
||||
|
||||
#ifdef Q_OS_MACOS
|
||||
# include "macsystemtrayicon.h"
|
||||
#else
|
||||
# include "qtsystemtrayicon.h"
|
||||
#endif
|
||||
|
||||
QPixmap SystemTrayIcon::CreateIcon(const QPixmap &icon, const QPixmap &grey_icon) {
|
||||
|
||||
QRect rect(icon.rect());
|
||||
|
||||
QPixmap ret(icon);
|
||||
QPainter p(&ret);
|
||||
|
||||
if (trayicon_progress_) {
|
||||
// The angle of the line that's used to cover the icon.
|
||||
// Centered on rect.topLeft()
|
||||
double angle = static_cast<double>(100 - song_progress_) / 100.0 * M_PI_2;
|
||||
double length = sqrt(pow(rect.width(), 2.0) + pow(rect.height(), 2.0));
|
||||
|
||||
QPolygon mask;
|
||||
mask << rect.topLeft();
|
||||
mask << rect.topLeft() + QPoint(static_cast<int>(length * sin(angle)), static_cast<int>(length * cos(angle)));
|
||||
|
||||
if (song_progress_ > 50) mask << rect.bottomRight();
|
||||
|
||||
mask << rect.topRight();
|
||||
mask << rect.topLeft();
|
||||
|
||||
// Draw the grey bit
|
||||
p.setClipRegion(mask);
|
||||
p.drawPixmap(0, 0, grey_icon);
|
||||
p.setClipping(false);
|
||||
}
|
||||
|
||||
// Draw the playing or paused icon in the top-right
|
||||
if (!current_state_icon_.isNull()) {
|
||||
int height = rect.height() / 2;
|
||||
QPixmap scaled(current_state_icon_.scaledToHeight(height, Qt::SmoothTransformation));
|
||||
|
||||
QRect state_rect(rect.width() - scaled.width(), 0, scaled.width(), scaled.height());
|
||||
p.drawPixmap(state_rect, scaled);
|
||||
}
|
||||
|
||||
p.end();
|
||||
|
||||
return ret;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user