2010-02-08 06:38:14 +03:00
|
|
|
/* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-02-08 06:38:14 +03:00
|
|
|
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
2013-03-05 08:21:12 +04:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2010-02-08 06:38:14 +03:00
|
|
|
#include "nsMacDockSupport.h"
|
2010-03-05 03:10:42 +03:00
|
|
|
#include "nsObjCExceptions.h"
|
2010-02-08 06:38:14 +03:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsMacDockSupport, nsIMacDockSupport, nsITaskbarProgress)
|
2013-03-03 14:58:00 +04:00
|
|
|
|
|
|
|
nsMacDockSupport::nsMacDockSupport()
|
|
|
|
: mAppIcon(nil)
|
|
|
|
, mProgressBackground(nil)
|
|
|
|
, mProgressState(STATE_NO_PROGRESS)
|
|
|
|
, mProgressFraction(0.0)
|
|
|
|
{
|
2017-10-16 09:12:54 +03:00
|
|
|
mProgressTimer = NS_NewTimer();
|
2013-03-03 14:58:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsMacDockSupport::~nsMacDockSupport()
|
|
|
|
{
|
|
|
|
if (mAppIcon) {
|
|
|
|
[mAppIcon release];
|
|
|
|
mAppIcon = nil;
|
|
|
|
}
|
|
|
|
if (mProgressBackground) {
|
|
|
|
[mProgressBackground release];
|
|
|
|
mProgressBackground = nil;
|
|
|
|
}
|
|
|
|
if (mProgressTimer) {
|
|
|
|
mProgressTimer->Cancel();
|
|
|
|
mProgressTimer = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2010-02-08 06:38:14 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMacDockSupport::GetDockMenu(nsIStandaloneNativeMenu ** aDockMenu)
|
|
|
|
{
|
2016-04-05 17:46:43 +03:00
|
|
|
nsCOMPtr<nsIStandaloneNativeMenu> dockMenu(mDockMenu);
|
|
|
|
dockMenu.forget(aDockMenu);
|
2010-02-08 06:38:14 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMacDockSupport::SetDockMenu(nsIStandaloneNativeMenu * aDockMenu)
|
|
|
|
{
|
2016-04-05 17:46:43 +03:00
|
|
|
mDockMenu = aDockMenu;
|
|
|
|
return NS_OK;
|
2010-02-08 06:38:14 +03:00
|
|
|
}
|
2010-03-05 03:10:42 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsMacDockSupport::ActivateApplication(bool aIgnoreOtherApplications)
|
2010-03-05 03:10:42 +03:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
|
|
|
[[NSApplication sharedApplication] activateIgnoringOtherApps:aIgnoreOtherApplications];
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
}
|
2011-12-05 13:57:45 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMacDockSupport::SetBadgeText(const nsAString& aBadgeText)
|
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
|
|
|
NSDockTile *tile = [[NSApplication sharedApplication] dockTile];
|
|
|
|
mBadgeText = aBadgeText;
|
|
|
|
if (aBadgeText.IsEmpty())
|
|
|
|
[tile setBadgeLabel: nil];
|
|
|
|
else
|
2013-10-10 20:59:40 +04:00
|
|
|
[tile setBadgeLabel:[NSString stringWithCharacters:reinterpret_cast<const unichar*>(mBadgeText.get())
|
|
|
|
length:mBadgeText.Length()]];
|
2011-12-05 13:57:45 +04:00
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMacDockSupport::GetBadgeText(nsAString& aBadgeText)
|
|
|
|
{
|
|
|
|
aBadgeText = mBadgeText;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-03-03 14:58:00 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMacDockSupport::SetProgressState(nsTaskbarProgressState aState,
|
2013-04-03 04:59:27 +04:00
|
|
|
uint64_t aCurrentValue,
|
|
|
|
uint64_t aMaxValue)
|
2013-03-03 14:58:00 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_RANGE(aState, 0, STATE_PAUSED);
|
|
|
|
if (aState == STATE_NO_PROGRESS || aState == STATE_INDETERMINATE) {
|
|
|
|
NS_ENSURE_TRUE(aCurrentValue == 0, NS_ERROR_INVALID_ARG);
|
|
|
|
NS_ENSURE_TRUE(aMaxValue == 0, NS_ERROR_INVALID_ARG);
|
|
|
|
}
|
|
|
|
if (aCurrentValue > aMaxValue) {
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mProgressState = aState;
|
|
|
|
if (aMaxValue == 0) {
|
|
|
|
mProgressFraction = 0;
|
|
|
|
} else {
|
|
|
|
mProgressFraction = (double)aCurrentValue / aMaxValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mProgressState == STATE_NORMAL || mProgressState == STATE_INDETERMINATE) {
|
2013-04-05 16:50:50 +04:00
|
|
|
int perSecond = 8; // Empirically determined, see bug 848792
|
2017-06-29 22:13:25 +03:00
|
|
|
mProgressTimer->InitWithNamedFuncCallback(RedrawIconCallback, this, 1000 / perSecond,
|
|
|
|
nsITimer::TYPE_REPEATING_SLACK,
|
|
|
|
"nsMacDockSupport::RedrawIconCallback");
|
2013-03-03 14:58:00 +04:00
|
|
|
return NS_OK;
|
|
|
|
} else {
|
|
|
|
mProgressTimer->Cancel();
|
|
|
|
return RedrawIcon();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void nsMacDockSupport::RedrawIconCallback(nsITimer* aTimer, void* aClosure)
|
|
|
|
{
|
|
|
|
static_cast<nsMacDockSupport*>(aClosure)->RedrawIcon();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return whether to draw progress
|
|
|
|
bool nsMacDockSupport::InitProgress()
|
|
|
|
{
|
|
|
|
if (mProgressState != STATE_NORMAL && mProgressState != STATE_INDETERMINATE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mAppIcon) {
|
2017-10-16 09:12:54 +03:00
|
|
|
mProgressTimer = NS_NewTimer();
|
2013-03-03 14:58:00 +04:00
|
|
|
mAppIcon = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
|
|
|
|
mProgressBackground = [mAppIcon copyWithZone:nil];
|
2013-04-17 10:51:52 +04:00
|
|
|
mTheme = new nsNativeThemeCocoa();
|
2013-03-03 14:58:00 +04:00
|
|
|
|
|
|
|
NSSize sz = [mProgressBackground size];
|
2013-04-17 10:51:52 +04:00
|
|
|
mProgressBounds = CGRectMake(sz.width * 1/32, sz.height * 3/32,
|
|
|
|
sz.width * 30/32, sz.height * 4/32);
|
2013-03-03 14:58:00 +04:00
|
|
|
[mProgressBackground lockFocus];
|
|
|
|
[[NSColor whiteColor] set];
|
2013-04-17 10:51:52 +04:00
|
|
|
NSRectFill(NSRectFromCGRect(mProgressBounds));
|
2013-03-03 14:58:00 +04:00
|
|
|
[mProgressBackground unlockFocus];
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsMacDockSupport::RedrawIcon()
|
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
|
|
|
if (InitProgress()) {
|
2013-04-17 10:51:52 +04:00
|
|
|
// TODO: - Implement ERROR and PAUSED states?
|
2013-03-03 14:58:00 +04:00
|
|
|
NSImage *icon = [mProgressBackground copyWithZone:nil];
|
|
|
|
bool isIndeterminate = (mProgressState != STATE_NORMAL);
|
|
|
|
|
|
|
|
[icon lockFocus];
|
|
|
|
CGContextRef ctx = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
|
2013-04-17 10:51:52 +04:00
|
|
|
mTheme->DrawProgress(ctx, mProgressBounds, isIndeterminate,
|
|
|
|
true, mProgressFraction, 1.0, NULL);
|
2013-03-03 14:58:00 +04:00
|
|
|
[icon unlockFocus];
|
|
|
|
[NSApp setApplicationIconImage:icon];
|
|
|
|
[icon release];
|
|
|
|
} else {
|
|
|
|
[NSApp setApplicationIconImage:mAppIcon];
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
}
|