Index: ash/common/system/tray/size_range_layout.h |
diff --git a/ash/common/system/tray/size_range_layout.h b/ash/common/system/tray/size_range_layout.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c56c1f6ad363ba5cf19fada33363d62516e2c54e |
--- /dev/null |
+++ b/ash/common/system/tray/size_range_layout.h |
@@ -0,0 +1,106 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef ASH_COMMON_SYSTEM_TRAY_SIZE_RANGE_LAYOUT_H_ |
+#define ASH_COMMON_SYSTEM_TRAY_SIZE_RANGE_LAYOUT_H_ |
+ |
+#include <memory> |
+ |
+#include "base/macros.h" |
+#include "ui/gfx/geometry/size.h" |
+#include "ui/views/layout/layout_manager.h" |
+ |
+namespace views { |
+class View; |
+} // namespace views |
+ |
+namespace ash { |
+ |
+// A LayoutManager adapter that allows clients to specify a minimum and/or a |
+// maximum preferred size. The actual layout will be delegated to the |
+// LayoutManager owned by this. i.e. this can be used to override the preferred |
+// size returned by a View. |
+// |
+// By default the SizeRangeLayout is configured to own a FillLayout but this can |
+// be overridden with SetLayoutManager(). |
+// |
+// Example use case : |
+// |
+// Suppose you wanted to override the preferred size of an ImageView that would |
+// have a preferred size of (25, 25) to have a size of (50, 50). |
+// |
+// Example code: |
+// |
+// ImageView* image_view = new ImageView(); |
+// image_view->SetImage(image); // Where |image| has a size of (25, 25). |
+// SizeRangeLayout* layout = new SizeRangeLayout(); |
+// layout->SetSize(gfx::Size(50, 50)); |
+// image_view->SetLayoutManager(layout); |
+// |
+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
|
+ public: |
+ // The minimum width/height. |
+ static const int kMinSize; |
+ |
+ // The maximum width/height. |
+ static const int kMaxSize; |
+ |
+ // Returns the minimum possible size. Use this with set_min_size() to |
+ // effectively unset the minimum preferred size. |
+ static gfx::Size MinSize(); |
+ |
+ // Returns the maximum possible size. Use this with set_max_size() to |
+ // effectively unset the maximum preferred size. |
+ static gfx::Size MaxSize(); |
+ |
+ // Create a layout with no minimum or maximum preferred size. |
+ SizeRangeLayout(); |
+ |
+ // Create a layout with the given minimum and maximum preferred sizes. |
+ SizeRangeLayout(const gfx::Size& min_size, const gfx::Size& max_size); |
+ |
+ ~SizeRangeLayout() override; |
+ |
+ // Sets both the minimum and maximum preferred size. |
+ void SetSize(const gfx::Size& size); |
+ |
+ void set_min_size(const gfx::Size& size) { min_size_ = size; } |
+ void set_max_size(const gfx::Size& size) { max_size_ = size; } |
+ |
+ // Sets the layout manager that actually performs the layout once the bounds |
+ // have been defined. |
+ void SetLayoutManager(std::unique_ptr<LayoutManager> layout_manager); |
+ |
+ // LayoutManager: |
+ void Installed(views::View* host) override; |
+ void Uninstalled(views::View* host) override; |
+ void Layout(views::View* host) override; |
+ gfx::Size GetPreferredSize(const views::View* host) const override; |
+ int GetPreferredHeightForWidth(const views::View* host, |
+ int width) const override; |
+ void ViewAdded(views::View* host, views::View* view) override; |
+ void ViewRemoved(views::View* host, views::View* view) override; |
+ |
+ private: |
+ // Clamps |size| to be within the minimum and maximum preferred sizes. |
+ void ClampSizeToRange(gfx::Size* size) const; |
+ |
+ // The host View that this has been installed on. |
+ views::View* host_ = nullptr; |
+ |
+ // The layout manager that actually performs the layout. |
+ std::unique_ptr<views::LayoutManager> layout_manager_; |
+ |
+ // The minimum preferred size. |
+ gfx::Size min_size_; |
+ |
+ // The maximum preferred size. |
+ gfx::Size max_size_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(SizeRangeLayout); |
+}; |
+ |
+} // namespace ash |
+ |
+#endif // ASH_COMMON_SYSTEM_TRAY_SIZE_RANGE_LAYOUT_H_ |