Chromium Code Reviews| Index: ui/views/widget/widget_mac_utils.mm |
| diff --git a/ui/views/widget/widget_mac_utils.mm b/ui/views/widget/widget_mac_utils.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1664bbe08337f11acf711e389816dfc41c48e493 |
| --- /dev/null |
| +++ b/ui/views/widget/widget_mac_utils.mm |
| @@ -0,0 +1,53 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
|
tapted
2016/03/17 08:18:59
Can this entire file just be moved into the anonym
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ui/views/widget/widget_mac_utils.h" |
| + |
| +#include "base/mac/foundation_util.h" |
| +#include "ui/views/widget/widget.h" |
| + |
| +namespace views { |
| +namespace { |
| + |
| +using RankMap = std::map<NSView*, int>; |
| + |
| +void RankNSViews(View* view, const AssociatedViews& hosts, RankMap* rank) { |
| + auto it = hosts.find(view); |
| + if (it != hosts.end()) |
| + rank->emplace(it->second, rank->size()); |
| + for (int i = 0; i < view->child_count(); ++i) |
| + RankNSViews(view->child_at(i), hosts, rank); |
| +} |
| + |
| +NSComparisonResult SubviewSorter(id lhs, id rhs, void* rank_as_void) { |
| + DCHECK_NE(lhs, rhs); |
| + |
| + const RankMap* rank = static_cast<const RankMap*>(rank_as_void); |
| + auto left_rank = rank->find(lhs); |
| + auto right_rank = rank->find(rhs); |
| + bool left_found = left_rank != rank->end(); |
| + bool right_found = right_rank != rank->end(); |
| + |
| + // Sort unassociated views below associated views. |
| + if (left_found != right_found) |
| + return left_found ? NSOrderedDescending : NSOrderedAscending; |
| + |
| + if (left_found) |
| + return *left_rank < *right_rank ? NSOrderedAscending : NSOrderedDescending; |
| + |
| + // If both are unassociated, consider that order is not important |
| + return NSOrderedSame; |
| +} |
| + |
| +} // namespace |
| + |
| +void ReorderChildNSViews(Widget* widget, |
| + const AssociatedViews& associated_views) { |
| + RankMap rank; |
| + RankNSViews(widget->GetRootView(), associated_views, &rank); |
| + [widget->GetNativeView() sortSubviewsUsingFunction:&SubviewSorter |
| + context:&rank]; |
| +} |
| + |
| +} // namespace views |