feat: add new options to tray.displayBalloon() (#19544)
This commit is contained in:
parent
58840f39bb
commit
0fb3c8cb7c
6 changed files with 119 additions and 27 deletions
|
@ -6,15 +6,15 @@
|
|||
|
||||
namespace electron {
|
||||
|
||||
TrayIcon::BalloonOptions::BalloonOptions() = default;
|
||||
|
||||
TrayIcon::TrayIcon() {}
|
||||
|
||||
TrayIcon::~TrayIcon() {}
|
||||
|
||||
void TrayIcon::SetPressedImage(ImageType image) {}
|
||||
|
||||
void TrayIcon::DisplayBalloon(ImageType icon,
|
||||
const base::string16& title,
|
||||
const base::string16& contents) {}
|
||||
void TrayIcon::DisplayBalloon(const BalloonOptions& options) {}
|
||||
|
||||
void TrayIcon::RemoveBalloon() {}
|
||||
|
||||
|
|
|
@ -49,11 +49,27 @@ class TrayIcon {
|
|||
virtual std::string GetTitle() = 0;
|
||||
#endif
|
||||
|
||||
enum class IconType { None, Info, Warning, Error, Custom };
|
||||
|
||||
struct BalloonOptions {
|
||||
IconType icon_type = IconType::Custom;
|
||||
#if defined(OS_WIN)
|
||||
HICON icon = nullptr;
|
||||
#else
|
||||
gfx::Image icon;
|
||||
#endif
|
||||
base::string16 title;
|
||||
base::string16 content;
|
||||
bool large_icon = true;
|
||||
bool no_sound = false;
|
||||
bool respect_quiet_time = false;
|
||||
|
||||
BalloonOptions();
|
||||
};
|
||||
|
||||
// Displays a notification balloon with the specified contents.
|
||||
// Depending on the platform it might not appear by the icon tray.
|
||||
virtual void DisplayBalloon(ImageType icon,
|
||||
const base::string16& title,
|
||||
const base::string16& contents);
|
||||
virtual void DisplayBalloon(const BalloonOptions& options);
|
||||
|
||||
// Removes the notification balloon.
|
||||
virtual void RemoveBalloon();
|
||||
|
|
|
@ -17,6 +17,28 @@
|
|||
#include "ui/views/controls/menu/menu_runner.h"
|
||||
#include "ui/views/widget/widget.h"
|
||||
|
||||
namespace {
|
||||
|
||||
UINT ConvertIconType(electron::TrayIcon::IconType type) {
|
||||
using IconType = electron::TrayIcon::IconType;
|
||||
switch (type) {
|
||||
case IconType::None:
|
||||
return NIIF_NONE;
|
||||
case IconType::Info:
|
||||
return NIIF_INFO;
|
||||
case IconType::Warning:
|
||||
return NIIF_WARNING;
|
||||
case IconType::Error:
|
||||
return NIIF_ERROR;
|
||||
case IconType::Custom:
|
||||
return NIIF_USER;
|
||||
default:
|
||||
NOTREACHED() << "Invalid icon type";
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace electron {
|
||||
|
||||
NotifyIcon::NotifyIcon(NotifyIconHost* host, UINT id, HWND window, UINT message)
|
||||
|
@ -120,18 +142,24 @@ void NotifyIcon::SetToolTip(const std::string& tool_tip) {
|
|||
LOG(WARNING) << "Unable to set tooltip for status tray icon";
|
||||
}
|
||||
|
||||
void NotifyIcon::DisplayBalloon(HICON icon,
|
||||
const base::string16& title,
|
||||
const base::string16& contents) {
|
||||
void NotifyIcon::DisplayBalloon(const BalloonOptions& options) {
|
||||
NOTIFYICONDATA icon_data;
|
||||
InitIconData(&icon_data);
|
||||
icon_data.uFlags |= NIF_INFO;
|
||||
icon_data.dwInfoFlags = NIIF_INFO;
|
||||
wcsncpy_s(icon_data.szInfoTitle, title.c_str(), _TRUNCATE);
|
||||
wcsncpy_s(icon_data.szInfo, contents.c_str(), _TRUNCATE);
|
||||
wcsncpy_s(icon_data.szInfoTitle, options.title.c_str(), _TRUNCATE);
|
||||
wcsncpy_s(icon_data.szInfo, options.content.c_str(), _TRUNCATE);
|
||||
icon_data.uTimeout = 0;
|
||||
icon_data.hBalloonIcon = icon;
|
||||
icon_data.dwInfoFlags = NIIF_USER | NIIF_LARGE_ICON;
|
||||
icon_data.hBalloonIcon = options.icon;
|
||||
icon_data.dwInfoFlags = ConvertIconType(options.icon_type);
|
||||
|
||||
if (options.large_icon)
|
||||
icon_data.dwInfoFlags |= NIIF_LARGE_ICON;
|
||||
|
||||
if (options.no_sound)
|
||||
icon_data.dwInfoFlags |= NIIF_NOSOUND;
|
||||
|
||||
if (options.respect_quiet_time)
|
||||
icon_data.dwInfoFlags |= NIIF_RESPECT_QUIET_TIME;
|
||||
|
||||
BOOL result = Shell_NotifyIcon(NIM_MODIFY, &icon_data);
|
||||
if (!result)
|
||||
|
|
|
@ -58,9 +58,7 @@ class NotifyIcon : public TrayIcon {
|
|||
void SetImage(HICON image) override;
|
||||
void SetPressedImage(HICON image) override;
|
||||
void SetToolTip(const std::string& tool_tip) override;
|
||||
void DisplayBalloon(HICON icon,
|
||||
const base::string16& title,
|
||||
const base::string16& contents) override;
|
||||
void DisplayBalloon(const BalloonOptions& options) override;
|
||||
void RemoveBalloon() override;
|
||||
void PopUpContextMenu(const gfx::Point& pos,
|
||||
AtomMenuModel* menu_model) override;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue