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 // Returns the absolute minimum possible size. Use this with set_min_size() to |
| 46 // effectively unset the minimum preferred size. |
| 47 static gfx::Size AbsoluteMinSize(); |
| 48 |
| 49 // Returns the absolute maximum possible size. Use this with set_max_size() to |
| 50 // effectively unset the maximum preferred size. |
| 51 static gfx::Size AbsoluteMaxSize(); |
| 52 |
| 53 // Create a layout with no minimum or maximum preferred size. |
| 54 SizeRangeLayout(); |
| 55 |
| 56 // Create a layout with the given minimum and maximum preferred sizes. If |
| 57 // |max_size| is smaller than |min_size| then |min_size| will be set to the |
| 58 // smaller |max_size| value. |
| 59 SizeRangeLayout(const gfx::Size& min_size, const gfx::Size& max_size); |
| 60 |
| 61 ~SizeRangeLayout() override; |
| 62 |
| 63 // Sets both the minimum and maximum preferred size. |
| 64 void SetSize(const gfx::Size& size); |
| 65 |
| 66 // Set the minimum preferred size that GetPreferredSize() will round up to. If |
| 67 // |size| is larger than the current |max_size_| then |max_size_| will set to |
| 68 // |size| as well. |
| 69 void SetMinSize(const gfx::Size& size); |
| 70 |
| 71 // Set the minimum preferred size that GetPreferredSize() will round down to. |
| 72 // If |size| is smaller than the current |min_size_| then |min_size_| will set |
| 73 // to |size| as well. |
| 74 void SetMaxSize(const gfx::Size& size); |
| 75 |
| 76 // Sets the layout manager that actually performs the layout once the bounds |
| 77 // have been defined. |
| 78 void SetLayoutManager(std::unique_ptr<LayoutManager> layout_manager); |
| 79 |
| 80 // LayoutManager: |
| 81 void Installed(views::View* host) override; |
| 82 void Layout(views::View* host) override; |
| 83 gfx::Size GetPreferredSize(const views::View* host) const override; |
| 84 int GetPreferredHeightForWidth(const views::View* host, |
| 85 int width) const override; |
| 86 void ViewAdded(views::View* host, views::View* view) override; |
| 87 void ViewRemoved(views::View* host, views::View* view) override; |
| 88 |
| 89 private: |
| 90 friend class SizeRangeLayoutTest; |
| 91 |
| 92 // Clamps |size| to be within the minimum and maximum preferred sizes. |
| 93 void ClampSizeToRange(gfx::Size* size) const; |
| 94 |
| 95 // The host View that this has been installed on. |
| 96 views::View* host_ = nullptr; |
| 97 |
| 98 // The layout manager that actually performs the layout. |
| 99 std::unique_ptr<views::LayoutManager> layout_manager_; |
| 100 |
| 101 // The minimum preferred size. |
| 102 gfx::Size min_size_; |
| 103 |
| 104 // The maximum preferred size. |
| 105 gfx::Size max_size_; |
| 106 |
| 107 DISALLOW_COPY_AND_ASSIGN(SizeRangeLayout); |
| 108 }; |
| 109 |
| 110 } // namespace ash |
| 111 |
| 112 #endif // ASH_COMMON_SYSTEM_TRAY_SIZE_RANGE_LAYOUT_H_ |
OLD | NEW |