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

Side by Side Diff: ui/views/examples/multiline_example.cc

Issue 1953133002: [WIP: not for review] Reduce re-layout Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 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 unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/examples/multiline_example.h" 5 #include "ui/views/examples/multiline_example.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "ui/events/event.h" 11 #include "ui/events/event.h"
12 #include "ui/gfx/render_text.h"
13 #include "ui/views/background.h" 12 #include "ui/views/background.h"
14 #include "ui/views/border.h" 13 #include "ui/views/border.h"
15 #include "ui/views/controls/button/checkbox.h" 14 #include "ui/views/controls/button/checkbox.h"
16 #include "ui/views/controls/label.h" 15 #include "ui/views/controls/label.h"
17 #include "ui/views/controls/textfield/textfield.h" 16 #include "ui/views/controls/textfield/textfield.h"
18 #include "ui/views/layout/grid_layout.h" 17 #include "ui/views/layout/grid_layout.h"
19 #include "ui/views/view.h" 18 #include "ui/views/view.h"
20 19
21 using base::ASCIIToUTF16; 20 using base::ASCIIToUTF16;
22 21
(...skipping 18 matching lines...) Expand all
41 gfx::Size GetPreferredSize() const override { 40 gfx::Size GetPreferredSize() const override {
42 return gfx::Size(50, Label::GetPreferredSize().height()); 41 return gfx::Size(50, Label::GetPreferredSize().height());
43 } 42 }
44 43
45 private: 44 private:
46 DISALLOW_COPY_AND_ASSIGN(PreferredSizeLabel); 45 DISALLOW_COPY_AND_ASSIGN(PreferredSizeLabel);
47 }; 46 };
48 47
49 } // namespace 48 } // namespace
50 49
51 // A simple View that hosts a RenderText object. 50 MultilineExample::RenderTextView::RenderTextView()
52 class MultilineExample::RenderTextView : public View { 51 : render_text_(gfx::RenderText::CreateInstanceForEditing()) {
53 public: 52 render_text_->SetHorizontalAlignment(gfx::ALIGN_TO_HEAD);
54 RenderTextView() : render_text_(gfx::RenderText::CreateInstanceForEditing()) { 53 render_text_->SetColor(SK_ColorBLACK);
55 render_text_->SetHorizontalAlignment(gfx::ALIGN_TO_HEAD); 54 render_text_->SetMultiline(true);
56 render_text_->SetColor(SK_ColorBLACK); 55 SetBorder(Border::CreateSolidBorder(2, SK_ColorGRAY));
57 render_text_->SetMultiline(true);
58 SetBorder(Border::CreateSolidBorder(2, SK_ColorGRAY));
59 }
60
61 void OnPaint(gfx::Canvas* canvas) override {
62 View::OnPaint(canvas);
63 render_text_->Draw(canvas);
64 }
65
66 gfx::Size GetPreferredSize() const override {
67 // Turn off multiline mode to get the single-line text size, which is the
68 // preferred size for this view.
69 render_text_->SetMultiline(false);
70 gfx::Size size(render_text_->GetContentWidth(),
71 render_text_->GetStringSize().height());
72 size.Enlarge(GetInsets().width(), GetInsets().height());
73 render_text_->SetMultiline(true);
74 return size;
75 }
76
77 int GetHeightForWidth(int w) const override {
78 // TODO(ckocagil): Why does this happen?
79 if (w == 0)
80 return View::GetHeightForWidth(w);
81 const gfx::Rect old_rect = render_text_->display_rect();
82 gfx::Rect rect = old_rect;
83 rect.set_width(w - GetInsets().width());
84 render_text_->SetDisplayRect(rect);
85 int height = render_text_->GetStringSize().height() + GetInsets().height();
86 render_text_->SetDisplayRect(old_rect);
87 return height;
88 }
89
90 void SetText(const base::string16& new_contents) {
91 // Color and style the text inside |test_range| to test colors and styles.
92 const size_t range_max = new_contents.length();
93 gfx::Range color_range = ClampRange(gfx::Range(1, 21), range_max);
94 gfx::Range bold_range = ClampRange(gfx::Range(4, 10), range_max);
95 gfx::Range italic_range = ClampRange(gfx::Range(7, 13), range_max);
96
97 render_text_->SetText(new_contents);
98 render_text_->SetColor(SK_ColorBLACK);
99 render_text_->ApplyColor(0xFFFF0000, color_range);
100 render_text_->SetStyle(gfx::DIAGONAL_STRIKE, false);
101 render_text_->ApplyStyle(gfx::DIAGONAL_STRIKE, true, color_range);
102 render_text_->SetStyle(gfx::UNDERLINE, false);
103 render_text_->ApplyStyle(gfx::UNDERLINE, true, color_range);
104 render_text_->ApplyStyle(gfx::BOLD, true, bold_range);
105 render_text_->ApplyStyle(gfx::ITALIC, true, italic_range);
106 InvalidateLayout();
107 }
108
109 private:
110 void OnBoundsChanged(const gfx::Rect& previous_bounds) override {
111 gfx::Rect bounds = GetLocalBounds();
112 bounds.Inset(GetInsets());
113 render_text_->SetDisplayRect(bounds);
114 }
115
116 std::unique_ptr<gfx::RenderText> render_text_;
117
118 DISALLOW_COPY_AND_ASSIGN(RenderTextView);
119 };
120
121 MultilineExample::MultilineExample()
122 : ExampleBase("Multiline RenderText"),
123 render_text_view_(NULL),
124 label_(NULL),
125 textfield_(NULL),
126 label_checkbox_(NULL) {
127 } 56 }
128 57
129 MultilineExample::~MultilineExample() { 58 MultilineExample::RenderTextView::~RenderTextView() {}
59
60 void MultilineExample::RenderTextView::SetText(
61 const base::string16& new_contents) {
62 // Color and style the text inside |test_range| to test colors and styles.
63 const size_t range_max = new_contents.length();
64 gfx::Range color_range = ClampRange(gfx::Range(1, 21), range_max);
65 gfx::Range bold_range = ClampRange(gfx::Range(4, 10), range_max);
66 gfx::Range italic_range = ClampRange(gfx::Range(7, 13), range_max);
67
68 render_text_->SetText(new_contents);
69 render_text_->SetColor(SK_ColorBLACK);
70 render_text_->ApplyColor(0xFFFF0000, color_range);
71 render_text_->SetStyle(gfx::DIAGONAL_STRIKE, false);
72 render_text_->ApplyStyle(gfx::DIAGONAL_STRIKE, true, color_range);
tapted 2016/05/10 08:45:53 One big factor seems to be these 4 "ApplyStyle" ca
73 render_text_->SetStyle(gfx::UNDERLINE, false);
74 render_text_->ApplyStyle(gfx::UNDERLINE, true, color_range);
75 render_text_->ApplyStyle(gfx::BOLD, true, bold_range);
76 render_text_->ApplyStyle(gfx::ITALIC, true, italic_range);
77 InvalidateLayout();
78 }
79
80 void MultilineExample::RenderTextView::OnPaint(gfx::Canvas* canvas) {
81 View::OnPaint(canvas);
82 render_text_->Draw(canvas);
83 }
84
85 gfx::Size MultilineExample::RenderTextView::GetPreferredSize() const {
86 // Turn off multiline mode to get the single-line text size, which is the
87 // preferred size for this view.
88 render_text_->SetMultiline(false);
89 gfx::Size size(render_text_->GetContentWidth(),
90 render_text_->GetStringSize().height());
91 size.Enlarge(GetInsets().width(), GetInsets().height());
92 render_text_->SetMultiline(true);
93 return size;
94 }
95
96 int MultilineExample::RenderTextView::GetHeightForWidth(int w) const {
97 // TODO(ckocagil): Why does this happen?
98 if (w == 0)
99 return View::GetHeightForWidth(w);
100 const gfx::Rect old_rect = render_text_->display_rect();
101 gfx::Rect rect = old_rect;
102 rect.set_width(w - GetInsets().width());
103 render_text_->SetDisplayRect(rect);
104 int height = render_text_->GetStringSize().height() + GetInsets().height();
105 render_text_->SetDisplayRect(old_rect);
106 return height;
107 }
108
109 void MultilineExample::RenderTextView::OnBoundsChanged(
110 const gfx::Rect& previous_bounds) {
111 gfx::Rect bounds = GetLocalBounds();
112 bounds.Inset(GetInsets());
113 render_text_->SetDisplayRect(bounds);
114 }
115
116 MultilineExample::MultilineExample(const char* name)
117 : ExampleBase(name),
118 render_text_view_(nullptr),
119 label_(nullptr),
120 textfield_(nullptr),
121 label_checkbox_(nullptr) {}
122
123 MultilineExample::~MultilineExample() {}
124
125 MultilineExample::RenderTextView* MultilineExample::CreateTextView() {
126 return new RenderTextView();
130 } 127 }
131 128
132 void MultilineExample::CreateExampleView(View* container) { 129 void MultilineExample::CreateExampleView(View* container) {
133 const base::string16 kTestString = base::WideToUTF16(L"qwerty" 130 const base::string16 kTestString = base::WideToUTF16(L"qwerty"
134 L"\x627\x644\x631\x626\x64A\x633\x64A\x629" 131 L"\x627\x644\x631\x626\x64A\x633\x64A\x629"
135 L"asdfgh"); 132 L"asdfgh");
136 133
137 render_text_view_ = new RenderTextView(); 134 render_text_view_ = CreateTextView();
138 render_text_view_->SetText(kTestString); 135 render_text_view_->SetText(kTestString);
139 136
140 label_ = new PreferredSizeLabel(); 137 label_ = new PreferredSizeLabel();
141 label_->SetText(kTestString); 138 label_->SetText(kTestString);
142 label_->SetMultiLine(true); 139 label_->SetMultiLine(true);
143 label_->SetBorder(Border::CreateSolidBorder(2, SK_ColorCYAN)); 140 label_->SetBorder(Border::CreateSolidBorder(2, SK_ColorCYAN));
144 141
145 label_checkbox_ = new Checkbox(ASCIIToUTF16("views::Label:")); 142 label_checkbox_ = new Checkbox(ASCIIToUTF16("views::Label:"));
146 label_checkbox_->SetChecked(true); 143 label_checkbox_->SetChecked(true);
147 label_checkbox_->set_listener(this); 144 label_checkbox_->set_listener(this);
(...skipping 23 matching lines...) Expand all
171 layout->StartRow(0, 0); 168 layout->StartRow(0, 0);
172 layout->AddView(new Label(ASCIIToUTF16("Sample Text:"))); 169 layout->AddView(new Label(ASCIIToUTF16("Sample Text:")));
173 layout->AddView(textfield_); 170 layout->AddView(textfield_);
174 } 171 }
175 172
176 void MultilineExample::ContentsChanged(Textfield* sender, 173 void MultilineExample::ContentsChanged(Textfield* sender,
177 const base::string16& new_contents) { 174 const base::string16& new_contents) {
178 render_text_view_->SetText(new_contents); 175 render_text_view_->SetText(new_contents);
179 if (label_checkbox_->checked()) 176 if (label_checkbox_->checked())
180 label_->SetText(new_contents); 177 label_->SetText(new_contents);
181 container()->Layout(); 178 container()->Layout();
tapted 2016/05/10 08:45:53 about 38% of the CPU time is under this call. This
182 container()->SchedulePaint(); 179 container()->SchedulePaint();
183 } 180 }
184 181
185 void MultilineExample::ButtonPressed(Button* sender, const ui::Event& event) { 182 void MultilineExample::ButtonPressed(Button* sender, const ui::Event& event) {
186 DCHECK_EQ(sender, label_checkbox_); 183 DCHECK_EQ(sender, label_checkbox_);
187 label_->SetText(label_checkbox_->checked() ? textfield_->text() : 184 label_->SetText(label_checkbox_->checked() ? textfield_->text() :
188 base::string16()); 185 base::string16());
189 container()->Layout(); 186 container()->Layout();
190 container()->SchedulePaint(); 187 container()->SchedulePaint();
191 } 188 }
192 189
193 } // namespace examples 190 } // namespace examples
194 } // namespace views 191 } // namespace views
OLDNEW
« ui/gfx/render_text_harfbuzz.cc ('K') | « ui/views/examples/multiline_example.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698