1 // Copyright (c) 2012 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.
4 
5 #ifndef UI_VIEWS_WINDOW_CUSTOM_FRAME_VIEW_H_
6 #define UI_VIEWS_WINDOW_CUSTOM_FRAME_VIEW_H_
7 
8 #include <memory>
9 
10 #include "base/compiler_specific.h"
11 #include "base/macros.h"
12 #include "ui/gfx/image/image_skia.h"
13 #include "ui/views/controls/button/button.h"
14 #include "ui/views/window/frame_buttons.h"
15 #include "ui/views/window/non_client_view.h"
16 
17 namespace gfx {
18 class FontList;
19 }
20 
21 namespace views {
22 
23 class FrameBackground;
24 class ImageButton;
25 class Widget;
26 
27 ///////////////////////////////////////////////////////////////////////////////
28 //
29 // CustomFrameView
30 //
31 //  A view that provides the non client frame for Windows. This means
32 //  rendering the non-standard window caption, border, and controls.
33 //
34 ////////////////////////////////////////////////////////////////////////////////
35 class VIEWS_EXPORT CustomFrameView : public NonClientFrameView,
36                                      public ButtonListener {
37  public:
38   CustomFrameView();
39   ~CustomFrameView() override;
40 
41   void Init(Widget* frame);
42 
43   // Overridden from NonClientFrameView:
44   gfx::Rect GetBoundsForClientView() const override;
45   gfx::Rect GetWindowBoundsForClientBounds(
46       const gfx::Rect& client_bounds) const override;
47   int NonClientHitTest(const gfx::Point& point) override;
48   void GetWindowMask(const gfx::Size& size, SkPath* window_mask) override;
49   void ResetWindowControls() override;
50   void UpdateWindowIcon() override;
51   void UpdateWindowTitle() override;
52   void SizeConstraintsChanged() override;
53   void PaintAsActiveChanged(bool active) override;
54 
55   // Overridden from View:
56   void OnPaint(gfx::Canvas* canvas) override;
57   void Layout() override;
58   gfx::Size CalculatePreferredSize() const override;
59   gfx::Size GetMinimumSize() const override;
60   gfx::Size GetMaximumSize() const override;
61 
62   // Overridden from ButtonListener:
63   void ButtonPressed(Button* sender, const ui::Event& event) override;
64 
65   // Returns the font list to use in the window's title bar.
66   // TODO(https://crbug.com/968860): Move this into the typography provider.
67   static gfx::FontList GetWindowTitleFontList();
68 
69  private:
70   friend class CustomFrameViewTest;
71 
72   // Returns the thickness of the border that makes up the window frame edges.
73   // This does not include any client edge.
74   int FrameBorderThickness() const;
75 
76   // Returns the thickness of the entire nonclient left, right, and bottom
77   // borders, including both the window frame and any client edge.
78   int NonClientBorderThickness() const;
79 
80   // Returns the height of the entire nonclient top border, including the window
81   // frame, any title area, and any connected client edge.
82   int NonClientTopBorderHeight() const;
83 
84   // Returns the y-coordinate of the caption buttons.
85   int CaptionButtonY() const;
86 
87   // Returns the thickness of the nonclient portion of the 3D edge along the
88   // bottom of the titlebar.
89   int TitlebarBottomThickness() const;
90 
91   // Returns the size of the titlebar icon.  This is used even when the icon is
92   // not shown, e.g. to set the titlebar height.
93   int IconSize() const;
94 
95   // Returns the bounds of the titlebar icon (or where the icon would be if
96   // there was one).
97   gfx::Rect IconBounds() const;
98 
99   // Returns true if the title bar, caption buttons, and frame border should be
100   // drawn. If false, the client view occupies the full area of this view.
101   bool ShouldShowTitleBarAndBorder() const;
102 
103   // Returns true if the client edge should be drawn. This is true if
104   // the window is not maximized.
105   bool ShouldShowClientEdge() const;
106 
107   // Paint various sub-components of this view.
108   void PaintRestoredFrameBorder(gfx::Canvas* canvas);
109   void PaintMaximizedFrameBorder(gfx::Canvas* canvas);
110   void PaintTitleBar(gfx::Canvas* canvas);
111   void PaintRestoredClientEdge(gfx::Canvas* canvas);
112 
113   // Compute aspects of the frame needed to paint the frame background.
114   SkColor GetFrameColor() const;
115   gfx::ImageSkia GetFrameImage() const;
116 
117   // Performs the layout for the window control buttons based on the
118   // configuration specified in WindowButtonOrderProvider. The sizing and
119   // positions of the buttons affects LayoutTitleBar, call this beforehand.
120   void LayoutWindowControls();
121 
122   // Calculations depend on the positions of the window controls. Always call
123   // LayoutWindowControls beforehand.
124   void LayoutTitleBar();
125   void LayoutClientView();
126 
127   // Creates, adds and returns a new window caption button (e.g, minimize,
128   // maximize, restore).
129   ImageButton* InitWindowCaptionButton(int accessibility_string_id,
130                                        int normal_image_id,
131                                        int hot_image_id,
132                                        int pushed_image_id);
133 
134   // Returns the window caption button for the given FrameButton type, if it
135   // should be visible. Otherwise NULL.
136   ImageButton* GetImageButton(views::FrameButton button);
137 
138   // The bounds of the client view, in this view's coordinates.
139   gfx::Rect client_view_bounds_;
140 
141   // The layout rect of the title, if visible.
142   gfx::Rect title_bounds_;
143 
144   // Not owned.
145   Widget* frame_ = nullptr;
146 
147   // The icon of this window. May be NULL.
148   ImageButton* window_icon_ = nullptr;
149 
150   // Window caption buttons.
151   ImageButton* minimize_button_ = nullptr;
152   ImageButton* maximize_button_ = nullptr;
153   ImageButton* restore_button_ = nullptr;
154   ImageButton* close_button_ = nullptr;
155 
156   // Background painter for the window frame.
157   std::unique_ptr<FrameBackground> frame_background_;
158 
159   // The horizontal boundaries for the title bar to layout within. Restricted
160   // by the space used by the leading and trailing buttons.
161   int minimum_title_bar_x_ = 0;
162   int maximum_title_bar_x_ = -1;
163 
164   DISALLOW_COPY_AND_ASSIGN(CustomFrameView);
165 };
166 
167 }  // namespace views
168 
169 #endif  // UI_VIEWS_WINDOW_CUSTOM_FRAME_VIEW_H_
170