Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(971)

Unified Diff: ui/views/view_model_utils.h

Issue 598013003: Added views::ViewModelT<T>, a type-safe template version of ViewModel. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@appsgridview-static-casts
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/views/view_model_utils.h
diff --git a/ui/views/view_model_utils.h b/ui/views/view_model_utils.h
index 2d7eb89e810897854772e8f1c9f70ee8063caa69..de7833ea61a2736e9916e909bb008610f1e86673 100644
--- a/ui/views/view_model_utils.h
+++ b/ui/views/view_model_utils.h
@@ -6,13 +6,12 @@
#define UI_VIEWS_VIEW_MODEL_UTILS_H_
#include "base/basictypes.h"
+#include "ui/views/view.h"
+#include "ui/views/view_model.h"
#include "ui/views/views_export.h"
namespace views {
-class View;
-class ViewModel;
-
class VIEWS_EXPORT ViewModelUtils {
public:
enum Alignment {
@@ -21,13 +20,16 @@ class VIEWS_EXPORT ViewModelUtils {
};
// Sets the bounds of each view to its ideal bounds.
- static void SetViewBoundsToIdealBounds(const ViewModel& model);
+ template <class T>
+ static void SetViewBoundsToIdealBounds(const ViewModel<T>& model);
// Returns true if the Views in |model| are at their ideal bounds.
- static bool IsAtIdealBounds(const ViewModel& model);
+ template <class T>
+ static bool IsAtIdealBounds(const ViewModel<T>& model);
// Returns the index to move |view| to based on a coordinate of |x| and |y|.
- static int DetermineMoveIndex(const ViewModel& model,
+ template <class T>
+ static int DetermineMoveIndex(const ViewModel<T>& model,
View* view,
Alignment alignment,
int x,
@@ -37,6 +39,77 @@ class VIEWS_EXPORT ViewModelUtils {
DISALLOW_IMPLICIT_CONSTRUCTORS(ViewModelUtils);
};
+namespace {
+
+// Used in calculating ideal bounds.
+int primary_axis_coordinate(ViewModelUtils::Alignment alignment,
+ int x,
+ int y) {
+ return alignment == ViewModelUtils::HORIZONTAL ? x : y;
+}
+
+} // namespace
+
+// static
+template <class T>
+void ViewModelUtils::SetViewBoundsToIdealBounds(const ViewModel<T>& model) {
+ for (int i = 0; i < model.view_size(); ++i)
+ model.view_at(i)->SetBoundsRect(model.ideal_bounds(i));
+}
+
+// static
+template <class T>
+bool ViewModelUtils::IsAtIdealBounds(const ViewModel<T>& model) {
+ for (int i = 0; i < model.view_size(); ++i) {
+ View* view = model.view_at(i);
+ if (view->bounds() != model.ideal_bounds(i))
+ return false;
+ }
+ return true;
+}
+
+// static
+template <class T>
+int ViewModelUtils::DetermineMoveIndex(const ViewModel<T>& model,
+ View* view,
+ Alignment alignment,
+ int x,
+ int y) {
+ int value = primary_axis_coordinate(alignment, x, y);
+ int current_index = model.GetIndexOfView(view);
+ DCHECK_NE(-1, current_index);
+ for (int i = 0; i < current_index; ++i) {
+ int mid_point = primary_axis_coordinate(
+ alignment,
+ model.ideal_bounds(i).x() + model.ideal_bounds(i).width() / 2,
+ model.ideal_bounds(i).y() + model.ideal_bounds(i).height() / 2);
+ if (value < mid_point)
+ return i;
+ }
+
+ if (current_index + 1 == model.view_size())
+ return current_index;
+
+ // For indices after the current index ignore the bounds of the view being
+ // dragged. This keeps the view from bouncing around as moved.
+ int delta = primary_axis_coordinate(
+ alignment,
+ model.ideal_bounds(current_index + 1).x() -
+ model.ideal_bounds(current_index).x(),
+ model.ideal_bounds(current_index + 1).y() -
+ model.ideal_bounds(current_index).y());
+ for (int i = current_index + 1; i < model.view_size(); ++i) {
+ const gfx::Rect& bounds(model.ideal_bounds(i));
+ int mid_point = primary_axis_coordinate(
+ alignment,
+ bounds.x() + bounds.width() / 2 - delta,
+ bounds.y() + bounds.height() / 2 - delta);
+ if (value < mid_point)
+ return i - 1;
+ }
+ return model.view_size() - 1;
+}
+
} // namespace views
#endif // UI_VIEWS_VIEW_MODEL_UTILS_H_

Powered by Google App Engine
This is Rietveld 408576698