1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
// *****************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: https://www.gnu.org/licenses/gpl-3.0 *
// * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved *
// *****************************************************************************
#include "tray_icon.h"
#include <zen/i18n.h>
#include <wx/taskbar.h>
#include <wx/menu.h>
#include <wx/icon.h> //req. by Linux
#include <wx+/dc.h>
#include <wx+/image_tools.h>
#include <wx+/image_resources.h>
using namespace zen;
using namespace fff;
namespace
{
void fillRange(wxImage& img, int pixelFirst, int pixelLast, const wxColor& col) //tolerant input range
{
if (img.IsOk())
{
const int width = img.GetWidth ();
const int height = img.GetHeight();
if (width > 0 && height > 0)
{
pixelFirst = std::max(pixelFirst, 0);
pixelLast = std::min(pixelLast, width * height);
if (pixelFirst < pixelLast)
{
unsigned char* const bytesBegin = img.GetData() + pixelFirst * 3;
unsigned char* const bytesEnd = img.GetData() + pixelLast * 3;
for (unsigned char* bytePos = bytesBegin; bytePos < bytesEnd; bytePos += 3)
{
bytePos[0] = col.Red ();
bytePos[1] = col.Green();
bytePos[2] = col.Blue ();
}
if (img.HasAlpha()) //make progress indicator fully opaque:
std::fill(img.GetAlpha() + pixelFirst, img.GetAlpha() + pixelLast, wxIMAGE_ALPHA_OPAQUE);
}
}
}
}
}
//------------------------------------------------------------------------------------------------
//generate icon with progress indicator
class FfsTrayIcon::ProgressIconGenerator
{
public:
ProgressIconGenerator(const wxImage& logo) : logo_(logo) {}
wxIcon get(double fraction);
private:
const wxImage logo_;
wxIcon iconBuf_;
int startPixBuf_ = -1;
};
wxIcon FfsTrayIcon::ProgressIconGenerator::get(double fraction)
{
if (!logo_.IsOk() || logo_.GetWidth() <= 0 || logo_.GetHeight() <= 0)
return wxIcon();
const int pixelCount = logo_.GetWidth() * logo_.GetHeight();
const int startFillPixel = std::clamp<int>(std::round(fraction * pixelCount), 0, pixelCount);
if (startPixBuf_ != startFillPixel)
{
wxImage genImage(logo_.Copy()); //workaround wxWidgets' screwed-up design from hell: their copy-construction implements reference-counting WITHOUT copy-on-write!
//gradually make FFS icon brighter while nearing completion
zen::brighten(genImage, -200 * (1 - fraction));
//fill black border row
if (startFillPixel <= pixelCount - genImage.GetWidth())
{
/* --------
---bbbbb
bbbbSyyy S : start yellow remainder
yyyyyyyy */
int bStart = startFillPixel - genImage.GetWidth();
if (bStart % genImage.GetWidth() != 0) //add one more black pixel, see ascii-art
--bStart;
fillRange(genImage, bStart, startFillPixel, *wxBLACK);
}
else if (startFillPixel < pixelCount)
{
/* special handling for last row:
--------
--------
---bbbbb
---bSyyy S : start yellow remainder */
int bStart = startFillPixel - genImage.GetWidth() - 1;
int bEnd = (bStart / genImage.GetWidth() + 1) * genImage.GetWidth();
fillRange(genImage, bStart, bEnd, *wxBLACK);
fillRange(genImage, startFillPixel - 1, startFillPixel, *wxBLACK);
}
//fill yellow remainder
fillRange(genImage, startFillPixel, pixelCount, wxColor(240, 200, 0));
iconBuf_.CopyFromBitmap(genImage);
startPixBuf_ = startFillPixel;
}
return iconBuf_;
}
class FfsTrayIcon::TaskBarImpl : public wxTaskBarIcon
{
public:
TaskBarImpl(const std::function<void()>& requestResume) : requestResume_(requestResume)
{
Bind(wxEVT_TASKBAR_LEFT_DCLICK, [this](wxTaskBarIconEvent& event) { onDoubleClick(event); });
//Windows User Experience Guidelines: show the context menu rather than doing *nothing* on single left clicks; however:
//MSDN: "Double-clicking the left mouse button actually generates a sequence of four messages: WM_LBUTTONDOWN, WM_LBUTTONUP, WM_LBUTTONDBLCLK, and WM_LBUTTONUP."
//Reference: https://docs.microsoft.com/en-us/windows/win32/inputdev/wm-lbuttondblclk
//=> the only way to distinguish single left click and double-click is to wait wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC) (480ms) which is way too long!
}
void disconnectCallbacks() { requestResume_ = nullptr; }
private:
wxMenu* CreatePopupMenu() override
{
if (!requestResume_)
return nullptr;
wxMenu* contextMenu = new wxMenu;
wxMenuItem* defaultItem = new wxMenuItem(contextMenu, wxID_ANY, _("&Restore"));
//wxWidgets font mess-up:
//1. font must be set *before* wxMenu::Append()!
//2. don't use defaultItem->GetFont(); making it bold creates a huge font size for some reason
contextMenu->Append(defaultItem);
contextMenu->Bind(wxEVT_COMMAND_MENU_SELECTED, [this](wxCommandEvent& event) { if (requestResume_) requestResume_(); }, defaultItem->GetId());
return contextMenu; //ownership transferred to caller
}
void onDoubleClick(wxEvent& event)
{
if (requestResume_)
requestResume_();
}
//void onLeftDownClick(wxEvent& event)
//{
// //copied from wxTaskBarIconBase::OnRightButtonDown()
// if (wxMenu* menu = CreatePopupMenu())
// {
// PopupMenu(menu);
// delete menu;
// }
//}
std::function<void()> requestResume_;
};
FfsTrayIcon::FfsTrayIcon(const std::function<void()>& requestResume) :
trayIcon_(new TaskBarImpl(requestResume)),
iconGenerator_(std::make_unique<ProgressIconGenerator>(loadImage("start_sync", dipToScreen(24))))
{
[[maybe_unused]] const bool rv = trayIcon_->SetIcon(iconGenerator_->get(activeFraction_), activeToolTip_);
assert(rv); //caveat wxTaskBarIcon::SetIcon() can return true, even if not wxTaskBarIcon::IsAvailable()!!!
}
FfsTrayIcon::~FfsTrayIcon()
{
trayIcon_->disconnectCallbacks(); //TaskBarImpl has longer lifetime than FfsTrayIcon: avoid callback!
/* This is not working correctly on OS X! It seems both wxTaskBarIcon::RemoveIcon() and ~wxTaskBarIcon() are broken and do NOT immediately
remove the icon from the system tray! Only some time later in the event loop which called these functions they will be removed.
Maybe some system component has still shared ownership? Objective C auto release pools are freed at the end of the current event loop...
Anyway, wxWidgets fails to disconnect the wxTaskBarIcon event handlers before calling "[m_statusitem release]"!
=> !!!clicking on the icon after ~wxTaskBarIcon ran crashes the application!!!
- if ~wxTaskBarIcon() ran from the SyncProgressDialog::updateGui() event loop (e.g. user manually clicking the icon) => icon removed on return
- if ~wxTaskBarIcon() ran from SyncProgressDialog::closeDirectly() => leaves the icon dangling until user closes this dialog and outter event loop runs!
2021_01-04: yes the system indeed has a reference because wxWidgets forgets to call NSStatusBar::removeStatusItem in wxTaskBarIconCustomStatusItemImpl::RemoveIcon()
=> when this call is added, all these defered deletion shenanigans are NOT NEEDED ANYMORE! (still wxWidgets should really add [m_statusItem setTarget:nil]!)
*/
trayIcon_->RemoveIcon();
//*schedule* for destruction: delete during next idle loop iteration (handle late window messages, e.g. when double-clicking)
trayIcon_->Destroy();
}
void FfsTrayIcon::setToolTip(const wxString& toolTip)
{
activeToolTip_ = toolTip;
trayIcon_->SetIcon(iconGenerator_->get(activeFraction_), activeToolTip_); //another wxWidgets design bug: non-orthogonal method!
}
void FfsTrayIcon::setProgress(double fraction)
{
activeFraction_ = fraction;
trayIcon_->SetIcon(iconGenerator_->get(activeFraction_), activeToolTip_);
}
|