OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 ASH_COMMON_SYSTEM_TRAY_SIZE_RANGE_LAYOUT_H_ | |
6 #define ASH_COMMON_SYSTEM_TRAY_SIZE_RANGE_LAYOUT_H_ | |
7 | |
8 #include <memory> | |
9 | |
10 #include "ash/ash_export.h" | |
11 #include "base/macros.h" | |
12 #include "ui/gfx/geometry/size.h" | |
13 #include "ui/views/layout/layout_manager.h" | |
14 | |
15 namespace views { | |
16 class View; | |
17 } // namespace views | |
18 | |
19 namespace ash { | |
20 | |
21 // A LayoutManager adapter that allows clients to specify a minimum and/or a | |
22 // maximum preferred size. The actual layout will be delegated to the | |
23 // LayoutManager owned by this. i.e. |this| can be used to override the | |
24 // preferred size returned by a View. | |
25 // | |
26 // By default the SizeRangeLayout is configured to own a FillLayout but this can | |
27 // be overridden with SetLayoutManager(). | |
28 // | |
29 // Example use case : | |
30 // | |
31 // Suppose you wanted a Label to take up a specific size of (50, 50) even | |
32 // though the label's preferred size was (25, 25). | |
33 // | |
34 // Example code: | |
35 // | |
36 // Label* label = new Label(kSomeDummyText); | |
37 // View* container = new View(); | |
38 // container->AddChildView(label); | |
39 // SizeRangeLayout* layout = new SizeRangeLayout(); | |
40 // layout->SetSize(gfx::Size(50, 50)); | |
41 // container->SetLayoutManager(layout); | |
42 // | |
43 class ASH_EXPORT SizeRangeLayout : public views::LayoutManager { | |
44 public: | |
45 // Create a layout with no minimum or maximum preferred size. | |
46 SizeRangeLayout(); | |
47 | |
48 // Create a layout with the given minimum and maximum preferred sizes. If | |
49 // |max_size| is smaller than |min_size| then |min_size| will be set to the | |
50 // smaller |max_size| value. | |
51 SizeRangeLayout(const gfx::Size& min_size, const gfx::Size& max_size); | |
52 | |
53 ~SizeRangeLayout() override; | |
54 | |
55 // The absolute minimum possible width/height. Use this with SetMinSize() to | |
56 // effectively unset the minimum preferred size. | |
57 static const int kAbsoluteMinSize; | |
58 | |
59 // Tthe absolute maximum possible width/height. Use this with SetMaxSize() to | |
60 // effectively unset the maximum preferred size. | |
61 static const int kAbsoluteMaxSize; | |
62 | |
63 // Sets both the minimum and maximum preferred size. | |
64 void SetSize(const gfx::Size& size); | |
65 void SetSize(int width, int height); | |
66 | |
67 // Set the minimum preferred size that GetPreferredSize() will round up to. If | |
68 // |size| is larger than the current |max_size_| then |max_size_| will set to | |
69 // |size| as well. | |
70 void SetMinSize(const gfx::Size& size); | |
71 void SetMinSize(int width, int height); | |
72 | |
73 gfx::Size min_size() const { return min_size_; } | |
74 | |
75 // Set the minimum preferred size that GetPreferredSize() will round down to. | |
76 // If |size| is smaller than the current |min_size_| then |min_size_| will set | |
77 // to |size| as well. | |
78 void SetMaxSize(const gfx::Size& size); | |
79 | |
80 // Sets the layout manager that actually performs the layout once the bounds | |
81 // have been defined. | |
82 void SetLayoutManager(std::unique_ptr<LayoutManager> layout_manager); | |
83 | |
84 // LayoutManager: | |
85 void Installed(views::View* host) override; | |
86 void Layout(views::View* host) override; | |
87 gfx::Size GetPreferredSize(const views::View* host) const override; | |
88 int GetPreferredHeightForWidth(const views::View* host, | |
89 int width) const override; | |
90 void ViewAdded(views::View* host, views::View* view) override; | |
91 void ViewRemoved(views::View* host, views::View* view) override; | |
92 | |
93 private: | |
94 friend class SizeRangeLayoutTest; | |
95 | |
96 // Clamps |size| to be within the minimum and maximum preferred sizes. | |
97 void ClampSizeToRange(gfx::Size* size) const; | |
98 | |
99 // The host View that this has been installed on. | |
100 views::View* host_ = nullptr; | |
101 | |
102 // The layout manager that actually performs the layout. | |
103 std::unique_ptr<views::LayoutManager> layout_manager_; | |
104 | |
105 // The minimum preferred size. | |
106 gfx::Size min_size_; | |
107 | |
108 // The maximum preferred size. | |
109 gfx::Size max_size_; | |
110 | |
111 DISALLOW_COPY_AND_ASSIGN(SizeRangeLayout); | |
112 }; | |
113 | |
114 } // namespace ash | |
115 | |
116 #endif // ASH_COMMON_SYSTEM_TRAY_SIZE_RANGE_LAYOUT_H_ | |
OLD | NEW |