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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "chrome/browser/ui/cocoa/tabs/alert_indicator_button_cocoa.h"
#include "base/logging.h"
#include "base/mac/foundation_util.h"
#include "base/macros.h"
#include "base/threading/thread_task_runner_handle.h"
#import "chrome/browser/ui/cocoa/tabs/tab_view.h"
#include "content/public/browser/user_metrics.h"
#include "ui/gfx/animation/animation.h"
#include "ui/gfx/animation/animation_delegate.h"
#include "ui/gfx/image/image.h"
namespace {
// The minimum required click-to-select area of an inactive tab before allowing
// the click-to-mute functionality to be enabled. This value is in terms of
// some percentage of the AlertIndicatorButton's width. See comments in the
// updateEnabledForMuteToggle method.
const int kMinMouseSelectableAreaPercent = 250;
} // namespace
@implementation AlertIndicatorButton
class FadeAnimationDelegate : public gfx::AnimationDelegate {
public:
explicit FadeAnimationDelegate(AlertIndicatorButton* button)
: button_(button) {}
~FadeAnimationDelegate() override {}
private:
// gfx::AnimationDelegate implementation.
void AnimationProgressed(const gfx::Animation* animation) override {
[button_ setNeedsDisplay:YES];
}
void AnimationCanceled(const gfx::Animation* animation) override {
AnimationEnded(animation);
}
void AnimationEnded(const gfx::Animation* animation) override {
button_->showingAlertState_ = button_->alertState_;
[button_ setNeedsDisplay:YES];
[button_->animationDoneTarget_
performSelector:button_->animationDoneAction_];
}
AlertIndicatorButton* const button_;
DISALLOW_COPY_AND_ASSIGN(FadeAnimationDelegate);
};
@synthesize showingAlertState = showingAlertState_;
- (id)init {
if ((self = [super initWithFrame:NSZeroRect])) {
alertState_ = TabAlertState::NONE;
showingAlertState_ = TabAlertState::NONE;
isDormant_ = NO;
[self setEnabled:NO];
[super setTarget:self];
[super setAction:@selector(handleClick:)];
}
return self;
}
- (void)removeFromSuperview {
fadeAnimation_.reset();
[super removeFromSuperview];
}
- (void)viewDidMoveToWindow {
// In Material Design, the icon color depends on the theme. When the tab
// is moved into another window, make sure that it updates the theme.
[self updateIconForState:showingAlertState_];
}
- (void)updateIconForState:(TabAlertState)aState {
if (aState != TabAlertState::NONE) {
TabView* const tabView = base::mac::ObjCCast<TabView>([self superview]);
SkColor iconColor = [tabView iconColor];
NSImage* tabIndicatorImage =
chrome::GetTabAlertIndicatorImage(aState, iconColor).ToNSImage();
[self setImage:tabIndicatorImage];
affordanceImage_.reset(
[chrome::GetTabAlertIndicatorAffordanceImage(aState, iconColor)
.ToNSImage() retain]);
}
}
- (void)transitionToAlertState:(TabAlertState)nextState {
if (nextState == alertState_)
return;
[self updateIconForState:nextState];
if ((alertState_ == TabAlertState::AUDIO_PLAYING &&
nextState == TabAlertState::AUDIO_MUTING) ||
(alertState_ == TabAlertState::AUDIO_MUTING &&
nextState == TabAlertState::AUDIO_PLAYING) ||
(alertState_ == TabAlertState::AUDIO_MUTING &&
nextState == TabAlertState::NONE)) {
// Instant user feedback: No fade animation.
showingAlertState_ = nextState;
fadeAnimation_.reset();
} else {
if (nextState == TabAlertState::NONE)
showingAlertState_ = alertState_; // Fading-out indicator.
else
showingAlertState_ = nextState; // Fading-in to next indicator.
// gfx::Animation requires a task runner is available for the current
// thread. Generally, only certain unit tests would not instantiate a task
// runner.
if (base::ThreadTaskRunnerHandle::IsSet()) {
fadeAnimation_ = chrome::CreateTabAlertIndicatorFadeAnimation(nextState);
if (!fadeAnimationDelegate_)
fadeAnimationDelegate_.reset(new FadeAnimationDelegate(self));
fadeAnimation_->set_delegate(fadeAnimationDelegate_.get());
fadeAnimation_->Start();
}
}
alertState_ = nextState;
[self updateEnabledForMuteToggle];
[self setNeedsDisplay:YES];
}
- (void)setTarget:(id)aTarget {
NOTREACHED(); // See class-level comments.
}
- (void)setAction:(SEL)anAction {
NOTREACHED(); // See class-level comments.
}
- (void)setAnimationDoneTarget:(id)target withAction:(SEL)action {
animationDoneTarget_ = target;
animationDoneAction_ = action;
}
- (void)setClickTarget:(id)target withAction:(SEL)action {
clickTarget_ = target;
clickAction_ = action;
}
- (void)mouseDown:(NSEvent*)theEvent {
// Do not handle this left-button mouse event if any modifier keys are being
// held down. Instead, the Tab should react (e.g., selection or drag start).
if ([theEvent modifierFlags] & NSDeviceIndependentModifierFlagsMask ||
isDormant_) {
[self setHoverState:kHoverStateNone]; // Turn off hover.
[[self nextResponder] mouseDown:theEvent];
return;
}
[super mouseDown:theEvent];
}
- (void)mouseEntered:(NSEvent*)theEvent {
// If any modifier keys are being held down, do not turn on hover.
if ([theEvent modifierFlags] & NSDeviceIndependentModifierFlagsMask) {
[self setHoverState:kHoverStateNone];
return;
}
[super mouseEntered:theEvent];
}
- (void)mouseExited:(NSEvent*)theEvent {
[self exitDormantPeriod];
[super mouseExited:theEvent];
}
- (void)mouseMoved:(NSEvent*)theEvent {
// If any modifier keys are being held down, turn off hover.
if ([theEvent modifierFlags] & NSDeviceIndependentModifierFlagsMask) {
[self setHoverState:kHoverStateNone];
return;
}
[super mouseMoved:theEvent];
}
- (void)rightMouseDown:(NSEvent*)theEvent {
// All right-button mouse events should be handled by the Tab.
[self setHoverState:kHoverStateNone]; // Turn off hover.
[[self nextResponder] rightMouseDown:theEvent];
}
- (void)drawRect:(NSRect)dirtyRect {
NSImage* const image = ([self hoverState] == kHoverStateNone ||
![self isEnabled] || isDormant_) ?
[self image] : affordanceImage_.get();
if (!image)
return;
NSRect imageRect = NSZeroRect;
imageRect.size = [image size];
NSRect destRect = [self bounds];
destRect.origin.y =
floor((NSHeight(destRect) / 2) - (NSHeight(imageRect) / 2));
destRect.size = imageRect.size;
double opaqueness = 1.0;
if (fadeAnimation_) {
opaqueness = fadeAnimation_->GetCurrentValue();
if (alertState_ == TabAlertState::NONE)
opaqueness = 1.0 - opaqueness; // Fading out, not in.
} else if (isDormant_) {
opaqueness = 0.5;
}
[image drawInRect:destRect
fromRect:imageRect
operation:NSCompositeSourceOver
fraction:opaqueness
respectFlipped:YES
hints:nil];
}
// When disabled, the superview should receive all mouse events.
- (NSView*)hitTest:(NSPoint)aPoint {
if ([self isEnabled] && !isDormant_ && ![self isHidden])
return [super hitTest:aPoint];
else
return nil;
}
- (void)handleClick:(id)sender {
[self enterDormantPeriod];
// Call |-transitionToAlertState| to change the image, providing the user with
// instant feedback. In the very unlikely event that the mute toggle fails,
// |-transitionToAlertState| will be called again, via another code path, to
// set the image to be consistent with the final outcome.
using base::UserMetricsAction;
if (alertState_ == TabAlertState::AUDIO_PLAYING) {
content::RecordAction(UserMetricsAction("AlertIndicatorButton_Mute"));
[self transitionToAlertState:TabAlertState::AUDIO_MUTING];
} else {
DCHECK(alertState_ == TabAlertState::AUDIO_MUTING);
content::RecordAction(UserMetricsAction("AlertIndicatorButton_Unmute"));
[self transitionToAlertState:TabAlertState::AUDIO_PLAYING];
}
[clickTarget_ performSelector:clickAction_ withObject:self];
}
- (void)updateEnabledForMuteToggle {
const BOOL wasEnabled = [self isEnabled];
BOOL enable = chrome::AreExperimentalMuteControlsEnabled() &&
(alertState_ == TabAlertState::AUDIO_PLAYING ||
alertState_ == TabAlertState::AUDIO_MUTING);
// If the tab is not the currently-active tab, make sure it is wide enough
// before enabling click-to-mute. This ensures that there is enough click
// area for the user to activate a tab rather than unintentionally muting it.
TabView* const tabView = base::mac::ObjCCast<TabView>([self superview]);
if (enable && tabView && ([tabView state] != NSOnState)) {
const int requiredWidth =
NSWidth([self frame]) * kMinMouseSelectableAreaPercent / 100;
enable = ([tabView widthOfLargestSelectableRegion] >= requiredWidth);
}
if (enable == wasEnabled)
return;
[self setEnabled:enable];
// If the button has become enabled, check whether the mouse is currently
// hovering. If it is, enter a dormant period where extra user clicks are
// prevented from having an effect (i.e., before the user has realized the
// button has become enabled underneath their cursor).
if (!wasEnabled && [self hoverState] == kHoverStateMouseOver)
[self enterDormantPeriod];
else if (![self isEnabled])
[self exitDormantPeriod];
}
// Enters a temporary "dormant period" where this button will not trigger on
// clicks. The user is provided a visual affordance during this period. Sets a
// timer to call |-exitDormantPeriod|.
- (void)enterDormantPeriod {
isDormant_ = YES;
[self performSelector:@selector(exitDormantPeriod)
withObject:nil
afterDelay:[NSEvent doubleClickInterval]];
[self setNeedsDisplay:YES];
}
// Leaves the "dormant period," allowing clicks to once again trigger an enabled
// button.
- (void)exitDormantPeriod {
if (!isDormant_)
return;
isDormant_ = NO;
[self setNeedsDisplay:YES];
}
// ThemedWindowDrawing protocol support.
- (void)windowDidChangeTheme {
// Force the alert icon to update because the icon color may change based
// on the current theme.
[self updateIconForState:alertState_];
}
- (void)windowDidChangeActive {
}
@end
|