File: chrome_browser_window.mm

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (115 lines) | stat: -rw-r--r-- 4,027 bytes parent folder | download
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
// Copyright (c) 2012 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/chrome_browser_window.h"

#include "base/logging.h"
#include "chrome/browser/themes/theme_properties.h"
#import "chrome/browser/ui/cocoa/themed_window.h"
#include "ui/base/theme_provider.h"

namespace {

// Upper and lower bounds for determining if a theme's colors indicate that
// it's a "dark" theme. In Material Design, dark themes have controls that are
// drawn using transparent white instead of a transparent shade of gray.
const CGFloat kDarkThemeToolbarColorUpperBound = 0.55;
const CGFloat kDarkThemeTabTextColorLowerBound = 0.7;

}  // namespace

@interface NSWindow (Private)
- (BOOL)hasKeyAppearance;
@end

@implementation ChromeBrowserWindow

- (const ui::ThemeProvider*)themeProvider {
  id delegate = [self delegate];
  if (![delegate respondsToSelector:@selector(themeProvider)])
    return NULL;
  return [delegate themeProvider];
}

- (ThemedWindowStyle)themedWindowStyle {
  id delegate = [self delegate];
  if (![delegate respondsToSelector:@selector(themedWindowStyle)])
    return THEMED_NORMAL;
  return [delegate themedWindowStyle];
}

- (NSPoint)themeImagePositionForAlignment:(ThemeImageAlignment)alignment {
  id delegate = [self delegate];
  if (![delegate respondsToSelector:@selector(themeImagePositionForAlignment:)])
    return NSZeroPoint;
  return [delegate themeImagePositionForAlignment:alignment];
}

- (BOOL)inIncognitoMode {
  const ui::ThemeProvider* themeProvider = [self themeProvider];
  return themeProvider && themeProvider->InIncognitoMode();
}

- (BOOL)inIncognitoModeWithSystemTheme {
  const ui::ThemeProvider* themeProvider = [self themeProvider];
  return themeProvider && themeProvider->InIncognitoMode() &&
      themeProvider->UsingSystemTheme();
}

- (BOOL)hasDarkTheme {
  // If a system theme, return YES if Incognito.
  const ui::ThemeProvider* themeProvider = [self themeProvider];
  if (!themeProvider) {
    return NO;
  } else if (themeProvider->UsingSystemTheme()) {
    return themeProvider->InIncognitoMode();
  }

  // If the custom theme has a custom toolbar color, return YES if it's
  // dark.
  if (themeProvider->HasCustomColor(ThemeProperties::COLOR_TOOLBAR)) {
    NSColor* theColor =
        themeProvider->GetNSColor(ThemeProperties::COLOR_TOOLBAR);
    theColor =
        [theColor colorUsingColorSpaceName:NSCalibratedWhiteColorSpace];
    if (theColor != nil) {
      // The white componement cutoff is an empirical value.
      return [theColor whiteComponent] < kDarkThemeToolbarColorUpperBound;
    }
  }

  // If the custom theme has a custom tab text color, assume that a light
  // color means a dark tab background image, and therefore a dark theme.
  if (themeProvider->HasCustomColor(ThemeProperties::COLOR_TAB_TEXT)) {
    NSColor* theColor =
        themeProvider->GetNSColor(ThemeProperties::COLOR_TAB_TEXT);
    theColor =
        [theColor colorUsingColorSpaceName:NSCalibratedWhiteColorSpace];
    if (theColor != nil) {
      return [theColor whiteComponent] >= kDarkThemeTabTextColorLowerBound;
    }
  }

  return NO;
}

- (BOOL)hasKeyAppearance {
  // If not key, but a non-main child window without its own traffic lights _is_
  // key, then show this window with key appearance to keep the traffic lights
  // lit. This does not currently handle WebModal dialogs, since they are
  // children of an overlay window. But WebModals also temporarily lose key
  // status while animating closed, so extra logic is needed to avoid flicker.
  // Start with an early exit, since this is called for every mouseMove and
  // every cursor blink in an NSTextField.
  if (![self isKeyWindow]) {
    for (NSWindow* child in [self childWindows]) {
      if ([child isKeyWindow] && ![child isMainWindow] &&
          ([child styleMask] & NSClosableWindowMask) == 0)
        return YES;
    }
  }
  return [super hasKeyAppearance];
}

@end