Skip to content

Commit

Permalink
Allow to use non-native notifications
Browse files Browse the repository at this point in the history
Avoid using native notifications on unsupported Windows 7.
  • Loading branch information
hluk committed Apr 13, 2021
1 parent beb6030 commit 5bde427
Show file tree
Hide file tree
Showing 24 changed files with 1,078 additions and 111 deletions.
15 changes: 15 additions & 0 deletions shared/themes/notification.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#Notification, #Notification QWidget
{
/* Resets notification opacity. It will be set in NotificationDaemon::setNotificationOpacity(). */
;background: ${notification_bg + #000}
}
#Notification QWidget{
;color: ${notification_fg}
;${notification_font}
}
#Notification #NotificationTitle{
;${scale=1.2}${notification_font}${scale=1}
}
#Notification #NotificationTip{
;font-style: italic
}
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ file(GLOB copyq_SOURCES
app/*.cpp
common/*.cpp
gui/*.cpp
gui/notification.h
item/*.cpp
scriptable/*.cpp
scriptable/scriptableproxy.h
Expand Down
29 changes: 29 additions & 0 deletions src/app/clipboardserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,35 @@ void ClipboardServer::loadSettings()
startMonitoring();
}

m_sharedData->notifications->setNativeNotificationsEnabled(
appConfig.option<Config::native_notifications>() );
m_sharedData->notifications->setNotificationOpacity(
m_sharedData->theme.color("notification_bg").alphaF() );
m_sharedData->notifications->setNotificationStyleSheet(
m_sharedData->theme.getNotificationStyleSheet() );

int id = appConfig.option<Config::notification_position>();
NotificationDaemon::Position position;
switch (id) {
case 0: position = NotificationDaemon::Top; break;
case 1: position = NotificationDaemon::Bottom; break;
case 2: position = NotificationDaemon::TopRight; break;
case 3: position = NotificationDaemon::BottomRight; break;
case 4: position = NotificationDaemon::BottomLeft; break;
default: position = NotificationDaemon::TopLeft; break;
}
m_sharedData->notifications->setPosition(position);

const int x = appConfig.option<Config::notification_horizontal_offset>();
const int y = appConfig.option<Config::notification_vertical_offset>();
m_sharedData->notifications->setOffset(x, y);

const int w = appConfig.option<Config::notification_maximum_width>();
const int h = appConfig.option<Config::notification_maximum_height>();
m_sharedData->notifications->setMaximumSize(w, h);

m_sharedData->notifications->updateNotificationWidgets();

m_updateThemeTimer.stop();

COPYQ_LOG("Configuration loaded");
Expand Down
25 changes: 25 additions & 0 deletions src/common/appconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,36 @@ struct item_popup_interval : Config<int> {
static QString name() { return "item_popup_interval"; }
};

struct notification_position : Config<int> {
static QString name() { return "notification_position"; }
static Value defaultValue() { return 3; }
};

struct clipboard_notification_lines : Config<int> {
static QString name() { return "clipboard_notification_lines"; }
static Value value(Value v) { return qBound(0, v, 10000); }
};

struct notification_horizontal_offset : Config<int> {
static QString name() { return "notification_horizontal_offset"; }
static Value defaultValue() { return 10; }
};

struct notification_vertical_offset : Config<int> {
static QString name() { return "notification_vertical_offset"; }
static Value defaultValue() { return 10; }
};

struct notification_maximum_width : Config<int> {
static QString name() { return "notification_maximum_width"; }
static Value defaultValue() { return 300; }
};

struct notification_maximum_height : Config<int> {
static QString name() { return "notification_maximum_height"; }
static Value defaultValue() { return 100; }
};

struct edit_ctrl_return : Config<bool> {
static QString name() { return "edit_ctrl_return"; }
static Value defaultValue() { return true; }
Expand Down Expand Up @@ -412,6 +432,11 @@ struct style : Config<QString> {
static QString name() { return "style"; }
};

struct native_notifications : Config<bool> {
static QString name() { return "native_notifications"; }
static Value defaultValue() { return true; }
};

} // namespace Config

class AppConfig final
Expand Down
1 change: 0 additions & 1 deletion src/gui/actionhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,4 @@ void ActionHandler::showActionErrors(Action *action, const QString &message, ush
notification->setTitle(title);
notification->setMessage(msg, Qt::PlainText);
notification->setIcon(icon);
notification->show();
}
5 changes: 5 additions & 0 deletions src/gui/configurationmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,13 @@ void ConfigurationManager::initOptions()
bind<Config::expire_tab>(m_tabHistory->spinBoxExpireTab);
bind<Config::editor>(m_tabHistory->lineEditEditor);
bind<Config::item_popup_interval>(m_tabNotifications->spinBoxNotificationPopupInterval);
bind<Config::notification_position>(m_tabNotifications->comboBoxNotificationPosition);
bind<Config::clipboard_notification_lines>(m_tabNotifications->spinBoxClipboardNotificationLines);
bind<Config::notification_horizontal_offset>(m_tabNotifications->spinBoxNotificationHorizontalOffset);
bind<Config::notification_vertical_offset>(m_tabNotifications->spinBoxNotificationVerticalOffset);
bind<Config::notification_maximum_width>(m_tabNotifications->spinBoxNotificationMaximumWidth);
bind<Config::notification_maximum_height>(m_tabNotifications->spinBoxNotificationMaximumHeight);
bind<Config::native_notifications>(m_tabNotifications->checkBoxUseNativeNotifications);
bind<Config::edit_ctrl_return>(m_tabHistory->checkBoxEditCtrlReturn);
bind<Config::show_simple_items>(m_tabHistory->checkBoxShowSimpleItems);
bind<Config::number_search>(m_tabHistory->checkBoxNumberSearch);
Expand Down
1 change: 0 additions & 1 deletion src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2305,7 +2305,6 @@ void MainWindow::showError(const QString &msg)
notification->setTitle( tr("CopyQ Error", "Notification error message title") );
notification->setMessage(msg);
notification->setIcon(IconTimesCircle);
notification->show();
}

Notification *MainWindow::createNotification(const QString &id)
Expand Down
70 changes: 16 additions & 54 deletions src/gui/notification.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,75 +17,37 @@
along with CopyQ. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef NOTIFICATION_H
#define NOTIFICATION_H
#pragma once

#include "gui/notificationbutton.h"

#include <QColor>
#include <QTimer>
#include <QObject>
#include <QPixmap>
#include <QPointer>

#include <memory>
#include "gui/notificationbutton.h"

class KNotification;
class QWidget;

class Notification final : public QObject
class Notification : public QObject
{
Q_OBJECT

public:
static void initConfiguration();

explicit Notification(const QColor &iconColor, QObject *parent = nullptr);

~Notification();

void setTitle(const QString &title);
void setMessage(const QString &msg, Qt::TextFormat format = Qt::PlainText);
void setPixmap(const QPixmap &pixmap);
void setIcon(const QString &icon);
void setIcon(ushort icon);
void setIconColor(const QColor &color);
void setInterval(int msec);
void setButtons(const NotificationButtons &buttons);

void show();

void close();
explicit Notification(QObject *parent) : QObject(parent) {}
virtual void setTitle(const QString &title) = 0;
virtual void setMessage(const QString &msg, Qt::TextFormat format = Qt::PlainText) = 0;
virtual void setPixmap(const QPixmap &pixmap) = 0;
virtual void setIcon(const QString &icon) = 0;
virtual void setIcon(ushort icon) = 0;
virtual void setInterval(int msec) = 0;
virtual void setOpacity(qreal opacity) = 0;
virtual void setButtons(const NotificationButtons &buttons) = 0;
virtual void adjust() = 0;
virtual QWidget *widget() = 0;
virtual void show() = 0;
virtual void close() = 0;

signals:
/** Emitted if notification needs to be closed. */
void closeNotification(Notification *self);

void buttonClicked(const NotificationButton &button);

private:
void onButtonClicked(unsigned int id);
void onDestroyed();
void onClosed();
void onIgnored();
void onActivated();
void update();

void notificationLog(const char *message);

KNotification *dropNotification();

QPointer<KNotification> m_notification;
NotificationButtons m_buttons;

QColor m_iconColor;
QTimer m_timer;
int m_intervalMsec = -1;
QString m_title;
QString m_message;
QString m_icon;
ushort m_iconId;
QPixmap m_pixmap;
};

#endif // NOTIFICATION_H
Loading

0 comments on commit 5bde427

Please sign in to comment.