Chromium Code Reviews| 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 "base/macros.h" | |
| 11 #include "ui/gfx/geometry/size.h" | |
| 12 #include "ui/views/layout/layout_manager.h" | |
| 13 | |
| 14 namespace views { | |
| 15 class View; | |
| 16 } // namespace views | |
| 17 | |
| 18 namespace ash { | |
| 19 | |
| 20 // A LayoutManager adapter that allows clients to specify a minimum and/or a | |
| 21 // maximum preferred size. The actual layout will be delegated to the | |
| 22 // LayoutManager owned by this. i.e. this can be used to override the preferred | |
| 23 // size returned by a View. | |
| 24 // | |
| 25 // By default the SizeRangeLayout is configured to own a FillLayout but this can | |
| 26 // be overridden with SetLayoutManager(). | |
| 27 // | |
| 28 // Example use case : | |
| 29 // | |
| 30 // Suppose you wanted to override the preferred size of an ImageView that would | |
| 31 // have a preferred size of (25, 25) to have a size of (50, 50). | |
| 32 // | |
| 33 // Example code: | |
| 34 // | |
| 35 // ImageView* image_view = new ImageView(); | |
| 36 // image_view->SetImage(image); // Where |image| has a size of (25, 25). | |
| 37 // SizeRangeLayout* layout = new SizeRangeLayout(); | |
| 38 // layout->SetSize(gfx::Size(50, 50)); | |
| 39 // image_view->SetLayoutManager(layout); | |
| 40 // | |
| 41 class SizeRangeLayout : public views::LayoutManager { | |
|
sky
2016/10/14 22:11:12
Unless I'm missing something this class seems prob
bruthig
2016/10/17 15:53:12
Ah right, I guess I need to update the example cod
| |
| 42 public: | |
| 43 // The minimum width/height. | |
| 44 static const int kMinSize; | |
| 45 | |
| 46 // The maximum width/height. | |
| 47 static const int kMaxSize; | |
| 48 | |
| 49 // Returns the minimum possible size. Use this with set_min_size() to | |
| 50 // effectively unset the minimum preferred size. | |
| 51 static gfx::Size MinSize(); | |
| 52 | |
| 53 // Returns the maximum possible size. Use this with set_max_size() to | |
| 54 // effectively unset the maximum preferred size. | |
| 55 static gfx::Size MaxSize(); | |
| 56 | |
| 57 // Create a layout with no minimum or maximum preferred size. | |
| 58 SizeRangeLayout(); | |
| 59 | |
| 60 // Create a layout with the given minimum and maximum preferred sizes. | |
| 61 SizeRangeLayout(const gfx::Size& min_size, const gfx::Size& max_size); | |
| 62 | |
| 63 ~SizeRangeLayout() override; | |
| 64 | |
| 65 // Sets both the minimum and maximum preferred size. | |
| 66 void SetSize(const gfx::Size& size); | |
| 67 | |
| 68 void set_min_size(const gfx::Size& size) { min_size_ = size; } | |
| 69 void set_max_size(const gfx::Size& size) { max_size_ = size; } | |
| 70 | |
| 71 // Sets the layout manager that actually performs the layout once the bounds | |
| 72 // have been defined. | |
| 73 void SetLayoutManager(std::unique_ptr<LayoutManager> layout_manager); | |
| 74 | |
| 75 // LayoutManager: | |
| 76 void Installed(views::View* host) override; | |
| 77 void Uninstalled(views::View* host) override; | |
| 78 void Layout(views::View* host) override; | |
| 79 gfx::Size GetPreferredSize(const views::View* host) const override; | |
| 80 int GetPreferredHeightForWidth(const views::View* host, | |
| 81 int width) const override; | |
| 82 void ViewAdded(views::View* host, views::View* view) override; | |
| 83 void ViewRemoved(views::View* host, views::View* view) override; | |
| 84 | |
| 85 private: | |
| 86 // Clamps |size| to be within the minimum and maximum preferred sizes. | |
| 87 void ClampSizeToRange(gfx::Size* size) const; | |
| 88 | |
| 89 // The host View that this has been installed on. | |
| 90 views::View* host_ = nullptr; | |
| 91 | |
| 92 // The layout manager that actually performs the layout. | |
| 93 std::unique_ptr<views::LayoutManager> layout_manager_; | |
| 94 | |
| 95 // The minimum preferred size. | |
| 96 gfx::Size min_size_; | |
| 97 | |
| 98 // The maximum preferred size. | |
| 99 gfx::Size max_size_; | |
| 100 | |
| 101 DISALLOW_COPY_AND_ASSIGN(SizeRangeLayout); | |
| 102 }; | |
| 103 | |
| 104 } // namespace ash | |
| 105 | |
| 106 #endif // ASH_COMMON_SYSTEM_TRAY_SIZE_RANGE_LAYOUT_H_ | |
| OLD | NEW |