strawberry-audio-player-win.../src/core/macsystemtrayicon.mm

247 lines
6.9 KiB
Plaintext
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
2021-03-20 21:14:47 +01:00
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
2018-02-27 18:06:05 +01:00
*
* 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/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
2018-10-02 00:38:52 +02:00
2018-02-27 18:06:05 +01:00
#include "config.h"
#include <QApplication>
2021-07-12 08:28:52 +02:00
#include <QHash>
2021-05-20 21:40:08 +02:00
#include <QString>
#include <QUrl>
2021-05-20 21:40:08 +02:00
#include <QIcon>
#include <QAction>
2018-02-27 18:06:05 +01:00
#include <AppKit/NSMenu.h>
#include <AppKit/NSMenuItem.h>
2021-05-20 21:40:08 +02:00
#include "macsystemtrayicon.h"
#include "mac_delegate.h"
#include "song.h"
#include "iconloader.h"
2018-02-27 18:06:05 +01:00
@interface Target :NSObject {
2021-06-12 20:53:23 +02:00
QAction *action_;
2018-02-27 18:06:05 +01:00
}
- (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 {
2020-08-01 03:23:50 +02:00
Q_UNUSED(menuItem);
2018-02-27 18:06:05 +01:00
// 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("Now Playing");
2021-05-20 21:40:08 +02:00
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:@""];
2018-02-27 18:06:05 +01:00
[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_];
2018-02-27 18:06:05 +01:00
2018-07-01 22:26:46 +02:00
ClearNowPlaying();
2018-02-27 18:06:05 +01:00
}
2021-05-20 21:40:08 +02:00
void AddMenuItem(QAction *action) {
2018-02-27 18:06:05 +01:00
// Strip accelarators from name.
QString text = action->text().remove("&");
2021-05-20 21:40:08 +02:00
NSString *title = [[NSString alloc] initWithUTF8String: text.toUtf8().constData()];
2018-02-27 18:06:05 +01:00
// Create an object that can receive user clicks and pass them on to the QAction.
2021-05-20 21:40:08 +02:00
Target *target = [[Target alloc] initWithQAction:action];
NSMenuItem *item = [[[NSMenuItem alloc] initWithTitle:title action:@selector(clicked) keyEquivalent:@""] autorelease];
2018-02-27 18:06:05 +01:00
[item setEnabled:action->isEnabled()];
[item setTarget:target];
[dock_menu_ addItem:item];
actions_[action] = item;
}
2021-05-20 21:40:08 +02:00
void ActionChanged(QAction *action) {
NSMenuItem *item = actions_[action];
NSString *title = [[NSString alloc] initWithUTF8String: action->text().toUtf8().constData()];
2018-02-27 18:06:05 +01:00
[item setTitle:title];
}
void AddSeparator() {
2021-05-20 21:40:08 +02:00
NSMenuItem *separator = [NSMenuItem separatorItem];
2018-02-27 18:06:05 +01:00
[dock_menu_ addItem:separator];
}
2021-06-12 20:53:23 +02:00
void ShowNowPlaying(const QString &artist, const QString &title) {
2018-07-01 22:26:46 +02:00
ClearNowPlaying(); // Makes sure the order is consistent.
2021-05-20 21:40:08 +02:00
[now_playing_artist_ setTitle: [[NSString alloc] initWithUTF8String: artist.toUtf8().constData()]];
[now_playing_title_ setTitle: [[NSString alloc] initWithUTF8String: title.toUtf8().constData()]];
2018-02-27 18:06:05 +01:00
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_);
}
2018-07-01 22:26:46 +02:00
void ClearNowPlaying() {
2018-02-27 18:06:05 +01:00
// Hiding doesn't seem to work in the dock menu.
HideItem(now_playing_);
HideItem(now_playing_artist_);
HideItem(now_playing_title_);
}
private:
2021-05-20 21:40:08 +02:00
void HideItem(NSMenuItem *item) {
2018-02-27 18:06:05 +01:00
if ([dock_menu_ indexOfItem:item] != -1) {
[dock_menu_ removeItem:item];
}
}
2021-05-20 21:40:08 +02:00
void ShowItem(NSMenuItem *item, int index = 0) {
2018-02-27 18:06:05 +01:00
if ([dock_menu_ indexOfItem:item] == -1) {
[dock_menu_ insertItem:item atIndex:index];
}
}
2021-07-12 08:28:52 +02:00
QHash<QAction*, NSMenuItem*> actions_;
2018-02-27 18:06:05 +01:00
2021-05-20 21:40:08 +02:00
NSMenu *dock_menu_;
NSMenuItem *now_playing_;
NSMenuItem *now_playing_artist_;
NSMenuItem *now_playing_title_;
2018-02-27 18:06:05 +01:00
Q_DISABLE_COPY(MacSystemTrayIconPrivate);
};
2021-05-20 21:40:08 +02:00
SystemTrayIcon::SystemTrayIcon(QObject *parent)
: QObject(parent),
normal_icon_(QPixmap(":/pictures/strawberry.png").scaled(128, 128, Qt::KeepAspectRatio, Qt::SmoothTransformation)),
2021-05-20 21:40:08 +02:00
grey_icon_(QPixmap(":/pictures/strawberry-grey.png").scaled(128, 128, Qt::KeepAspectRatio, Qt::SmoothTransformation)),
playing_icon_(":/pictures/tiny-play.png"),
paused_icon_(":/pictures/tiny-pause.png"),
trayicon_progress_(false),
song_progress_(0) {
2019-08-06 20:31:31 +02:00
QApplication::setWindowIcon(normal_icon_);
2021-05-20 21:40:08 +02:00
2018-02-27 18:06:05 +01:00
}
2021-05-20 21:40:08 +02:00
SystemTrayIcon::~SystemTrayIcon() {}
void SystemTrayIcon::SetTrayiconProgress(const bool enabled) {
trayicon_progress_ = enabled;
UpdateIcon();
2018-02-27 18:06:05 +01:00
}
2021-05-20 21:40:08 +02:00
void SystemTrayIcon::SetupMenu(QAction *previous, QAction *play, QAction *stop, QAction *stop_after, QAction *next, QAction *mute, QAction *love, QAction *quit) {
2018-02-27 18:06:05 +01:00
p_ = std::make_unique<MacSystemTrayIconPrivate>();
2018-02-27 18:06:05 +01:00
SetupMenuItem(previous);
SetupMenuItem(play);
SetupMenuItem(stop);
SetupMenuItem(stop_after);
SetupMenuItem(next);
p_->AddSeparator();
SetupMenuItem(mute);
p_->AddSeparator();
2019-06-12 06:34:59 +02:00
SetupMenuItem(love);
2018-02-27 18:06:05 +01:00
Q_UNUSED(quit); // Mac already has a Quit item.
2018-10-02 00:38:52 +02:00
2018-02-27 18:06:05 +01:00
}
2021-05-20 21:40:08 +02:00
void SystemTrayIcon::SetupMenuItem(QAction *action) {
2018-02-27 18:06:05 +01:00
p_->AddMenuItem(action);
2021-05-20 21:40:08 +02:00
QObject::connect(action, &QAction::changed, this, &SystemTrayIcon::ActionChanged);
2018-02-27 18:06:05 +01:00
}
2021-05-20 21:40:08 +02:00
void SystemTrayIcon::UpdateIcon() {
QApplication::setWindowIcon(CreateIcon(normal_icon_, grey_icon_));
2018-02-27 18:06:05 +01:00
}
2021-05-20 21:40:08 +02:00
void SystemTrayIcon::ActionChanged() {
QAction *action = qobject_cast<QAction*>(sender());
2018-02-27 18:06:05 +01:00
p_->ActionChanged(action);
2021-05-20 21:40:08 +02:00
}
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();
2018-02-27 18:06:05 +01:00
}
2021-05-20 21:40:08 +02:00
void SystemTrayIcon::ClearNowPlaying() {
2018-07-01 22:26:46 +02:00
p_->ClearNowPlaying();
2018-02-27 18:06:05 +01:00
}
2021-05-20 21:40:08 +02:00
void SystemTrayIcon::SetNowPlaying(const Song &song, const QUrl&) {
2018-07-01 22:26:46 +02:00
p_->ShowNowPlaying(song.artist(), song.PrettyTitle());
2018-02-27 18:06:05 +01:00
}