Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/controls/tree/tree_view.h" | 5 #include "ui/views/controls/tree/tree_view.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "ui/accessibility/ax_view_state.h" | 11 #include "ui/accessibility/ax_view_state.h" |
| 12 #include "ui/base/ime/input_method.h" | 12 #include "ui/base/ime/input_method.h" |
| 13 #include "ui/base/resource/resource_bundle.h" | 13 #include "ui/base/resource/resource_bundle.h" |
| 14 #include "ui/events/event.h" | 14 #include "ui/events/event.h" |
| 15 #include "ui/events/keycodes/keyboard_codes.h" | 15 #include "ui/events/keycodes/keyboard_codes.h" |
| 16 #include "ui/gfx/canvas.h" | 16 #include "ui/gfx/canvas.h" |
| 17 #include "ui/gfx/geometry/rect.h" | 17 #include "ui/gfx/geometry/rect.h" |
| 18 #include "ui/gfx/geometry/rect_conversions.h" | 18 #include "ui/gfx/geometry/rect_conversions.h" |
| 19 #include "ui/gfx/image/image.h" | 19 #include "ui/gfx/image/image.h" |
| 20 #include "ui/gfx/skia_util.h" | 20 #include "ui/gfx/skia_util.h" |
| 21 #include "ui/native_theme/native_theme.h" | 21 #include "ui/native_theme/native_theme.h" |
| 22 #include "ui/resources/grit/ui_resources.h" | 22 #include "ui/resources/grit/ui_resources.h" |
| 23 #include "ui/views/controls/prefix_selector.h" | 23 #include "ui/views/controls/prefix_selector.h" |
| 24 #include "ui/views/controls/scroll_view.h" | 24 #include "ui/views/controls/scroll_view.h" |
| 25 #include "ui/views/controls/textfield/textfield.h" | 25 #include "ui/views/controls/textfield/textfield.h" |
| 26 #include "ui/views/controls/tree/tree_view_controller.h" | 26 #include "ui/views/controls/tree/tree_view_controller.h" |
| 27 #include "ui/views/resources/grit/views_resources.h" | 27 #include "ui/views/resources/grit/views_resources.h" |
| 28 #include "ui/views/style/platform_style.h" | |
| 28 | 29 |
| 29 using ui::TreeModel; | 30 using ui::TreeModel; |
| 30 using ui::TreeModelNode; | 31 using ui::TreeModelNode; |
| 31 | 32 |
| 32 namespace views { | 33 namespace views { |
| 33 | 34 |
| 34 // Insets around the view. | 35 // Insets around the view. |
| 35 static const int kHorizontalInset = 2; | 36 static const int kHorizontalInset = 2; |
| 36 static const int kVerticalInset = 2; | 37 static const int kVerticalInset = 2; |
| 37 // Padding before/after the image. | 38 // Padding before/after the image. |
| (...skipping 599 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 637 } | 638 } |
| 638 | 639 |
| 639 bool TreeView::OnClickOrTap(const ui::LocatedEvent& event) { | 640 bool TreeView::OnClickOrTap(const ui::LocatedEvent& event) { |
| 640 CommitEdit(); | 641 CommitEdit(); |
| 641 RequestFocus(); | 642 RequestFocus(); |
| 642 | 643 |
| 643 int row = (event.y() - kVerticalInset) / row_height_; | 644 int row = (event.y() - kVerticalInset) / row_height_; |
| 644 int depth = 0; | 645 int depth = 0; |
| 645 InternalNode* node = GetNodeByRow(row, &depth); | 646 InternalNode* node = GetNodeByRow(row, &depth); |
| 646 if (node) { | 647 if (node) { |
| 647 gfx::Rect bounds(GetBoundsForNodeImpl(node, row, depth)); | 648 gfx::Rect bounds(GetBoundsForNodeImpl(node, row, depth)); |
|
tapted
2016/06/09 07:13:37
This should use GetBackgroundBoundsForNode() too,
Elly Fong-Jones
2016/06/09 20:24:37
I reworked this function and ShowContextMenu to be
tapted
2016/06/13 07:03:47
Agree - nice refactoring!
| |
| 648 if (bounds.Contains(event.location())) { | 649 if (bounds.Contains(event.location())) { |
| 649 int relative_x = event.x() - bounds.x(); | 650 int relative_x = event.x() - bounds.x(); |
| 650 if (base::i18n::IsRTL()) | 651 if (base::i18n::IsRTL()) |
| 651 relative_x = bounds.width() - relative_x; | 652 relative_x = bounds.width() - relative_x; |
| 652 if (relative_x < kArrowRegionSize && | 653 if (relative_x < kArrowRegionSize && |
| 653 model_->GetChildCount(node->model_node())) { | 654 model_->GetChildCount(node->model_node())) { |
| 654 if (node->is_expanded()) | 655 if (node->is_expanded()) |
| 655 Collapse(node->model_node()); | 656 Collapse(node->model_node()); |
| 656 else | 657 else |
| 657 Expand(node->model_node()); | 658 Expand(node->model_node()); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 735 -(empty_editor_size_.height() - font_list_.GetHeight()) / 2); | 736 -(empty_editor_size_.height() - font_list_.GetHeight()) / 2); |
| 736 // Give a little extra space for editing. | 737 // Give a little extra space for editing. |
| 737 row_bounds.set_width(row_bounds.width() + 50); | 738 row_bounds.set_width(row_bounds.width() + 50); |
| 738 editor_->SetBoundsRect(row_bounds); | 739 editor_->SetBoundsRect(row_bounds); |
| 739 editor_->Layout(); | 740 editor_->Layout(); |
| 740 } | 741 } |
| 741 | 742 |
| 742 void TreeView::SchedulePaintForNode(InternalNode* node) { | 743 void TreeView::SchedulePaintForNode(InternalNode* node) { |
| 743 if (!node) | 744 if (!node) |
| 744 return; // Explicitly allow NULL to be passed in. | 745 return; // Explicitly allow NULL to be passed in. |
| 745 SchedulePaintInRect(GetBoundsForNode(node)); | 746 SchedulePaintInRect(GetBackgroundBoundsForNode(node)); |
| 746 } | 747 } |
| 747 | 748 |
| 748 void TreeView::PaintRows(gfx::Canvas* canvas, | 749 void TreeView::PaintRows(gfx::Canvas* canvas, |
| 749 int min_row, | 750 int min_row, |
| 750 int max_row, | 751 int max_row, |
| 751 InternalNode* node, | 752 InternalNode* node, |
| 752 int depth, | 753 int depth, |
| 753 int* row) { | 754 int* row) { |
| 754 if (*row >= max_row) | 755 if (*row >= max_row) |
| 755 return; | 756 return; |
| 756 | 757 |
| 757 if (*row >= min_row && *row < max_row) | 758 if (*row >= min_row && *row < max_row) |
| 758 PaintRow(canvas, node, *row, depth); | 759 PaintRow(canvas, node, *row, depth); |
| 759 (*row)++; | 760 (*row)++; |
| 760 if (!node->is_expanded()) | 761 if (!node->is_expanded()) |
| 761 return; | 762 return; |
| 762 depth++; | 763 depth++; |
| 763 for (int i = 0; i < node->child_count() && *row < max_row; ++i) | 764 for (int i = 0; i < node->child_count() && *row < max_row; ++i) |
| 764 PaintRows(canvas, min_row, max_row, node->GetChild(i), depth, row); | 765 PaintRows(canvas, min_row, max_row, node->GetChild(i), depth, row); |
| 765 } | 766 } |
| 766 | 767 |
| 767 void TreeView::PaintRow(gfx::Canvas* canvas, | 768 void TreeView::PaintRow(gfx::Canvas* canvas, |
| 768 InternalNode* node, | 769 InternalNode* node, |
| 769 int row, | 770 int row, |
| 770 int depth) { | 771 int depth) { |
| 771 gfx::Rect bounds(GetBoundsForNodeImpl(node, row, depth)); | 772 gfx::Rect bounds(GetBoundsForNodeImpl(node, row, depth)); |
| 773 const SkColor bg_color = GetNativeTheme()->GetSystemColor( | |
|
tapted
2016/06/09 07:13:37
bg_color -> selected_row_bg_color
Elly Fong-Jones
2016/06/09 20:24:37
Done.
| |
| 774 text_background_color_id(HasFocus())); | |
|
tapted
2016/06/09 07:13:37
I think HasFocus() -> HasFocus() || editing_
Othe
Elly Fong-Jones
2016/06/09 20:24:37
Done.
| |
| 775 | |
| 776 // Paint the row background. | |
| 777 if (PlatformStyle::kTreeViewSelectsEntireRow && selected_node_ == node) | |
| 778 canvas->FillRect(GetBackgroundBoundsForNode(node), bg_color); | |
| 772 | 779 |
| 773 if (model_->GetChildCount(node->model_node())) | 780 if (model_->GetChildCount(node->model_node())) |
| 774 PaintExpandControl(canvas, bounds, node->is_expanded()); | 781 PaintExpandControl(canvas, bounds, node->is_expanded()); |
| 775 | 782 |
| 776 // Paint the icon. | 783 // Paint the icon. |
| 777 gfx::ImageSkia icon; | 784 gfx::ImageSkia icon; |
| 778 int icon_index = model_->GetIconIndex(node->model_node()); | 785 int icon_index = model_->GetIconIndex(node->model_node()); |
| 779 if (icon_index != -1) | 786 if (icon_index != -1) |
| 780 icon = icons_[icon_index]; | 787 icon = icons_[icon_index]; |
| 781 else if (node == selected_node_) | 788 else if (node == selected_node_) |
| 782 icon = open_icon_; | 789 icon = open_icon_; |
| 783 else | 790 else |
| 784 icon = closed_icon_; | 791 icon = closed_icon_; |
| 785 int icon_x = kArrowRegionSize + kImagePadding + | 792 int icon_x = kArrowRegionSize + kImagePadding + |
| 786 (open_icon_.width() - icon.width()) / 2; | 793 (open_icon_.width() - icon.width()) / 2; |
| 787 if (base::i18n::IsRTL()) | 794 if (base::i18n::IsRTL()) |
| 788 icon_x = bounds.right() - icon_x - open_icon_.width(); | 795 icon_x = bounds.right() - icon_x - open_icon_.width(); |
| 789 else | 796 else |
| 790 icon_x += bounds.x(); | 797 icon_x += bounds.x(); |
| 791 canvas->DrawImageInt( | 798 canvas->DrawImageInt( |
| 792 icon, icon_x, | 799 icon, icon_x, |
| 793 bounds.y() + (bounds.height() - icon.height()) / 2); | 800 bounds.y() + (bounds.height() - icon.height()) / 2); |
| 794 | 801 |
| 795 if (!editing_ || node != selected_node_) { | 802 gfx::Rect text_bounds(bounds.x() + text_offset_, bounds.y(), |
| 796 gfx::Rect text_bounds(bounds.x() + text_offset_, bounds.y(), | 803 bounds.width() - text_offset_, bounds.height()); |
| 797 bounds.width() - text_offset_, bounds.height()); | 804 if (base::i18n::IsRTL()) |
| 798 if (base::i18n::IsRTL()) | 805 text_bounds.set_x(bounds.x()); |
| 799 text_bounds.set_x(bounds.x()); | 806 |
| 807 // Paint the text background. In edit mode, the selected node is a separate | |
| 808 // editing control, so it does not need a background painted here. | |
| 809 if ((!editing_ || selected_node_ != node) && | |
| 810 !PlatformStyle::kTreeViewSelectsEntireRow) { | |
| 800 if (node == selected_node_) { | 811 if (node == selected_node_) { |
| 801 const SkColor bg_color = GetNativeTheme()->GetSystemColor( | 812 canvas->FillRect(text_bounds, bg_color); |
| 802 text_background_color_id(HasFocus())); | 813 if (HasFocus()) |
| 803 canvas->FillRect(text_bounds, bg_color); | 814 canvas->DrawFocusRect(text_bounds); |
| 804 if (HasFocus()) | |
| 805 canvas->DrawFocusRect(text_bounds); | |
| 806 } | 815 } |
| 807 const ui::NativeTheme::ColorId color_id = | |
| 808 text_color_id(HasFocus(), node == selected_node_); | |
| 809 const gfx::Rect internal_bounds( | |
| 810 text_bounds.x() + kTextHorizontalPadding, | |
| 811 text_bounds.y() + kTextVerticalPadding, | |
| 812 text_bounds.width() - kTextHorizontalPadding * 2, | |
| 813 text_bounds.height() - kTextVerticalPadding * 2); | |
| 814 canvas->DrawStringRect(node->model_node()->GetTitle(), font_list_, | |
| 815 GetNativeTheme()->GetSystemColor(color_id), | |
| 816 internal_bounds); | |
| 817 } | 816 } |
| 817 | |
| 818 // Paint the text. | |
|
tapted
2016/06/09 07:13:37
Should this still be inside `if ((!editing_ || sel
Elly Fong-Jones
2016/06/09 20:24:37
Done.
| |
| 819 const ui::NativeTheme::ColorId color_id = | |
| 820 text_color_id(HasFocus(), node == selected_node_); | |
| 821 const gfx::Rect internal_bounds( | |
| 822 text_bounds.x() + kTextHorizontalPadding, | |
| 823 text_bounds.y() + kTextVerticalPadding, | |
| 824 text_bounds.width() - kTextHorizontalPadding * 2, | |
| 825 text_bounds.height() - kTextVerticalPadding * 2); | |
| 826 canvas->DrawStringRect(node->model_node()->GetTitle(), font_list_, | |
| 827 GetNativeTheme()->GetSystemColor(color_id), | |
| 828 internal_bounds); | |
| 818 } | 829 } |
| 819 | 830 |
| 820 void TreeView::PaintExpandControl(gfx::Canvas* canvas, | 831 void TreeView::PaintExpandControl(gfx::Canvas* canvas, |
| 821 const gfx::Rect& node_bounds, | 832 const gfx::Rect& node_bounds, |
| 822 bool expanded) { | 833 bool expanded) { |
| 823 int center_x; | 834 int center_x; |
| 824 if (base::i18n::IsRTL()) { | 835 if (base::i18n::IsRTL()) { |
| 825 center_x = node_bounds.right() - kArrowRegionSize + | 836 center_x = node_bounds.right() - kArrowRegionSize + |
| 826 (kArrowRegionSize - 4) / 2; | 837 (kArrowRegionSize - 4) / 2; |
| 827 } else { | 838 } else { |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 858 return NULL; | 869 return NULL; |
| 859 if (!parent_internal_node->loaded_children()) { | 870 if (!parent_internal_node->loaded_children()) { |
| 860 if (create_type == DONT_CREATE_IF_NOT_LOADED) | 871 if (create_type == DONT_CREATE_IF_NOT_LOADED) |
| 861 return NULL; | 872 return NULL; |
| 862 LoadChildren(parent_internal_node); | 873 LoadChildren(parent_internal_node); |
| 863 } | 874 } |
| 864 return parent_internal_node->GetChild( | 875 return parent_internal_node->GetChild( |
| 865 model_->GetIndexOf(parent_internal_node->model_node(), model_node)); | 876 model_->GetIndexOf(parent_internal_node->model_node(), model_node)); |
| 866 } | 877 } |
| 867 | 878 |
| 879 gfx::Rect TreeView::GetBackgroundBoundsForNode(InternalNode* node) { | |
| 880 gfx::Rect row_bounds(GetBoundsForNode(node)); | |
|
tapted
2016/06/09 07:13:38
This rect we want is much simpler - e.g. |depth| i
Elly Fong-Jones
2016/06/09 20:24:37
The reason this uses GetBoundsForNode() and then a
tapted
2016/06/13 07:03:47
Yeah - I think it would look like
if (!Platfor
tapted
2016/06/15 03:17:49
ping? any thoughts on this?
Elly Fong-Jones
2016/06/16 14:42:34
Oop, sorry, missed it. Done.
| |
| 881 if (PlatformStyle::kTreeViewSelectsEntireRow) { | |
| 882 row_bounds.set_x(bounds().x()); | |
| 883 row_bounds.set_width(bounds().width()); | |
| 884 } | |
| 885 return row_bounds; | |
| 886 } | |
| 887 | |
| 868 gfx::Rect TreeView::GetBoundsForNode(InternalNode* node) { | 888 gfx::Rect TreeView::GetBoundsForNode(InternalNode* node) { |
| 869 int row, depth; | 889 int row, depth; |
| 870 row = GetRowForInternalNode(node, &depth); | 890 row = GetRowForInternalNode(node, &depth); |
| 871 return GetBoundsForNodeImpl(node, row, depth); | 891 return GetBoundsForNodeImpl(node, row, depth); |
| 872 } | 892 } |
| 873 | 893 |
| 874 gfx::Rect TreeView::GetBoundsForNodeImpl(InternalNode* node, | 894 gfx::Rect TreeView::GetBoundsForNodeImpl(InternalNode* node, |
| 875 int row, | 895 int row, |
| 876 int depth) { | 896 int depth) { |
| 877 gfx::Rect rect(depth * kIndent + kHorizontalInset, | 897 gfx::Rect rect(depth * kIndent + kHorizontalInset, |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1044 if (!is_expanded_) | 1064 if (!is_expanded_) |
| 1045 return max_width; | 1065 return max_width; |
| 1046 for (int i = 0; i < child_count(); ++i) { | 1066 for (int i = 0; i < child_count(); ++i) { |
| 1047 max_width = std::max(max_width, | 1067 max_width = std::max(max_width, |
| 1048 GetChild(i)->GetMaxWidth(indent, depth + 1)); | 1068 GetChild(i)->GetMaxWidth(indent, depth + 1)); |
| 1049 } | 1069 } |
| 1050 return max_width; | 1070 return max_width; |
| 1051 } | 1071 } |
| 1052 | 1072 |
| 1053 } // namespace views | 1073 } // namespace views |
| OLD | NEW |