[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / ui / gfx / mac / nswindow_frame_controls.mm
blobe1983daa2d25a9c0b7d6def9bcdf6a00b7d32cc3
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import "ui/gfx/mac/nswindow_frame_controls.h"
7 #import "base/mac/mac_util.h"
8 #import "base/mac/sdk_forward_declarations.h"
9 #include "ui/gfx/geometry/size.h"
11 namespace {
13 // The value used to represent an unbounded width or height.
14 const int kUnboundedSize = 0;
16 void SetResizableStyleMask(NSWindow* window, bool resizable) {
17   NSUInteger style_mask = [window styleMask];
18   if (resizable)
19     style_mask |= NSResizableWindowMask;
20   else
21     style_mask &= ~NSResizableWindowMask;
22   [window setStyleMask:style_mask];
25 // Returns the level for windows that are configured to be always on top.
26 // This is not a constant because NSFloatingWindowLevel is a macro defined
27 // as a function call.
28 NSInteger AlwaysOnTopWindowLevel() {
29   return NSFloatingWindowLevel;
32 }  // namespace
34 namespace gfx {
36 void SetNSWindowCanFullscreen(NSWindow* window, bool allow_fullscreen) {
37   NSWindowCollectionBehavior behavior = [window collectionBehavior];
38   if (allow_fullscreen)
39     behavior |= NSWindowCollectionBehaviorFullScreenPrimary;
40   else
41     behavior &= ~NSWindowCollectionBehaviorFullScreenPrimary;
42   [window setCollectionBehavior:behavior];
45 bool IsNSWindowAlwaysOnTop(NSWindow* window) {
46   return [window level] == AlwaysOnTopWindowLevel();
49 void SetNSWindowAlwaysOnTop(NSWindow* window,
50                             bool always_on_top) {
51   [window setLevel:(always_on_top ? AlwaysOnTopWindowLevel()
52                                   : NSNormalWindowLevel)];
53   // Since always-on-top windows have a higher window level than
54   // NSNormalWindowLevel, they will default to
55   // NSWindowCollectionBehaviorTransient. Set the value explicitly here to match
56   // normal windows.
57   NSWindowCollectionBehavior behavior =
58       [window collectionBehavior] | NSWindowCollectionBehaviorManaged;
59   [window setCollectionBehavior:behavior];
62 void SetNSWindowVisibleOnAllWorkspaces(NSWindow* window, bool always_visible) {
63   NSWindowCollectionBehavior behavior = [window collectionBehavior];
64   if (always_visible)
65     behavior |= NSWindowCollectionBehaviorCanJoinAllSpaces;
66   else
67     behavior &= ~NSWindowCollectionBehaviorCanJoinAllSpaces;
68   [window setCollectionBehavior:behavior];
71 void ApplyNSWindowSizeConstraints(NSWindow* window,
72                                   const gfx::Size& min_size,
73                                   const gfx::Size& max_size,
74                                   bool can_resize,
75                                   bool can_fullscreen) {
76   [window setContentMinSize:NSMakeSize(min_size.width(), min_size.height())];
78   CGFloat max_width =
79       max_size.width() == kUnboundedSize ? CGFLOAT_MAX : max_size.width();
80   CGFloat max_height =
81       max_size.height() == kUnboundedSize ? CGFLOAT_MAX : max_size.height();
82   [window setContentMaxSize:NSMakeSize(max_width, max_height)];
84   SetResizableStyleMask(window, can_resize);
85   [window setShowsResizeIndicator:can_resize];
87   // Set the window to participate in Lion Fullscreen mode. Setting this flag
88   // has no effect on Snow Leopard or earlier. UI controls for fullscreen are
89   // only shown for windows that have unbounded size.
90   if (base::mac::IsOSLionOrLater())
91     SetNSWindowCanFullscreen(window, can_fullscreen);
93   [[window standardWindowButton:NSWindowZoomButton] setEnabled:can_fullscreen];
96 }  // namespace gfx