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"
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];
19 style_mask |= NSResizableWindowMask;
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;
36 void SetNSWindowCanFullscreen(NSWindow* window, bool allow_fullscreen) {
37 NSWindowCollectionBehavior behavior = [window collectionBehavior];
39 behavior |= NSWindowCollectionBehaviorFullScreenPrimary;
41 behavior &= ~NSWindowCollectionBehaviorFullScreenPrimary;
42 [window setCollectionBehavior:behavior];
45 bool IsNSWindowAlwaysOnTop(NSWindow* window) {
46 return [window level] == AlwaysOnTopWindowLevel();
49 void SetNSWindowAlwaysOnTop(NSWindow* window,
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
57 NSWindowCollectionBehavior behavior =
58 [window collectionBehavior] | NSWindowCollectionBehaviorManaged;
59 [window setCollectionBehavior:behavior];
62 void SetNSWindowVisibleOnAllWorkspaces(NSWindow* window, bool always_visible) {
63 NSWindowCollectionBehavior behavior = [window collectionBehavior];
65 behavior |= NSWindowCollectionBehaviorCanJoinAllSpaces;
67 behavior &= ~NSWindowCollectionBehaviorCanJoinAllSpaces;
68 [window setCollectionBehavior:behavior];
71 void ApplyNSWindowSizeConstraints(NSWindow* window,
72 const gfx::Size& min_size,
73 const gfx::Size& max_size,
75 bool can_fullscreen) {
76 [window setContentMinSize:NSMakeSize(min_size.width(), min_size.height())];
79 max_size.width() == kUnboundedSize ? CGFLOAT_MAX : max_size.width();
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];