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

Side by Side Diff: ui/views/controls/scroll_view.cc

Issue 2324043002: Bug Fix: Scroll View Shows Unnecessary Scrollbar (Closed)
Patch Set: Unittest Created 4 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 unified diff | Download patch
OLDNEW
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/scroll_view.h" 5 #include "ui/views/controls/scroll_view.h"
6 6
7 #include "base/feature_list.h" 7 #include "base/feature_list.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "ui/events/event.h" 10 #include "ui/events/event.h"
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 if (!is_bounded()) 287 if (!is_bounded())
288 return View::GetHeightForWidth(width); 288 return View::GetHeightForWidth(width);
289 289
290 gfx::Insets insets = GetInsets(); 290 gfx::Insets insets = GetInsets();
291 width = std::max(0, width - insets.width()); 291 width = std::max(0, width - insets.width());
292 int height = contents()->GetHeightForWidth(width) + insets.height(); 292 int height = contents()->GetHeightForWidth(width) + insets.height();
293 return std::min(std::max(height, min_height_), max_height_); 293 return std::min(std::max(height, min_height_), max_height_);
294 } 294 }
295 295
296 void ScrollView::Layout() { 296 void ScrollView::Layout() {
297 gfx::Rect available_rect = GetContentsBounds();
297 if (is_bounded()) { 298 if (is_bounded()) {
298 int content_width = width(); 299 int content_width = available_rect.width();
299 int content_height = contents()->GetHeightForWidth(content_width); 300 int content_height = contents()->GetHeightForWidth(content_width);
300 if (content_height > height()) { 301 if (content_height > height()) {
301 content_width = std::max(content_width - GetScrollBarWidth(), 0); 302 content_width = std::max(content_width - GetScrollBarWidth(), 0);
302 content_height = contents()->GetHeightForWidth(content_width); 303 content_height = contents()->GetHeightForWidth(content_width);
303 } 304 }
304 if (contents()->bounds().size() != gfx::Size(content_width, content_height)) 305 if (contents()->bounds().size() != gfx::Size(content_width, content_height))
305 contents()->SetBounds(0, 0, content_width, content_height); 306 contents()->SetBounds(0, 0, content_width, content_height);
306 } 307 }
307 308
308 // Most views will want to auto-fit the available space. Most of them want to 309 // Most views will want to auto-fit the available space. Most of them want to
309 // use all available width (without overflowing) and only overflow in 310 // use all available width (without overflowing) and only overflow in
310 // height. Examples are HistoryView, MostVisitedView, DownloadTabView, etc. 311 // height. Examples are HistoryView, MostVisitedView, DownloadTabView, etc.
311 // Other views want to fit in both ways. An example is PrintView. To make both 312 // Other views want to fit in both ways. An example is PrintView. To make both
312 // happy, assume a vertical scrollbar but no horizontal scrollbar. To override 313 // happy, assume a vertical scrollbar but no horizontal scrollbar. To override
313 // this default behavior, the inner view has to calculate the available space, 314 // this default behavior, the inner view has to calculate the available space,
314 // used ComputeScrollBarsVisibility() to use the same calculation that is done 315 // used ComputeScrollBarsVisibility() to use the same calculation that is done
315 // here and sets its bound to fit within. 316 // here and sets its bound to fit within.
316 gfx::Rect viewport_bounds = GetContentsBounds(); 317 gfx::Rect viewport_bounds = available_rect;
317 const int contents_x = viewport_bounds.x(); 318 const int contents_x = viewport_bounds.x();
318 const int contents_y = viewport_bounds.y(); 319 const int contents_y = viewport_bounds.y();
319 if (viewport_bounds.IsEmpty()) { 320 if (viewport_bounds.IsEmpty()) {
320 // There's nothing to layout. 321 // There's nothing to layout.
321 return; 322 return;
322 } 323 }
323 324
324 const int header_height = 325 const int header_height =
325 std::min(viewport_bounds.height(), 326 std::min(viewport_bounds.height(),
326 header_ ? header_->GetPreferredSize().height() : 0); 327 header_ ? header_->GetPreferredSize().height() : 0);
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
584 contents_viewport_->height()); 585 contents_viewport_->height());
585 586
586 ScrollToOffset(gfx::ScrollOffset(new_x, new_y)); 587 ScrollToOffset(gfx::ScrollOffset(new_x, new_y));
587 UpdateScrollBarPositions(); 588 UpdateScrollBarPositions();
588 } 589 }
589 590
590 void ScrollView::ComputeScrollBarsVisibility(const gfx::Size& vp_size, 591 void ScrollView::ComputeScrollBarsVisibility(const gfx::Size& vp_size,
591 const gfx::Size& content_size, 592 const gfx::Size& content_size,
592 bool* horiz_is_shown, 593 bool* horiz_is_shown,
593 bool* vert_is_shown) const { 594 bool* vert_is_shown) const {
595 if (hide_horizontal_scrollbar_) {
596 *horiz_is_shown = false;
597 *vert_is_shown = content_size.height() > vp_size.height();
598 return;
599 }
600
594 // Try to fit both ways first, then try vertical bar only, then horizontal 601 // Try to fit both ways first, then try vertical bar only, then horizontal
595 // bar only, then defaults to both shown. 602 // bar only, then defaults to both shown.
596 if (content_size.width() <= vp_size.width() && 603 if (content_size.width() <= vp_size.width() &&
597 content_size.height() <= vp_size.height()) { 604 content_size.height() <= vp_size.height()) {
598 *horiz_is_shown = false; 605 *horiz_is_shown = false;
599 *vert_is_shown = false; 606 *vert_is_shown = false;
600 } else if (content_size.width() <= vp_size.width() - GetScrollBarWidth()) { 607 } else if (content_size.width() <= vp_size.width() - GetScrollBarWidth()) {
601 *horiz_is_shown = false; 608 *horiz_is_shown = false;
602 *vert_is_shown = true; 609 *vert_is_shown = true;
603 } else if (content_size.height() <= vp_size.height() - GetScrollBarHeight()) { 610 } else if (content_size.height() <= vp_size.height() - GetScrollBarHeight()) {
604 *horiz_is_shown = true; 611 *horiz_is_shown = true;
605 *vert_is_shown = false; 612 *vert_is_shown = false;
606 } else { 613 } else {
607 *horiz_is_shown = true; 614 *horiz_is_shown = true;
608 *vert_is_shown = true; 615 *vert_is_shown = true;
609 } 616 }
610
611 if (hide_horizontal_scrollbar_)
612 *horiz_is_shown = false;
613 } 617 }
614 618
615 // Make sure that a single scrollbar is created and visible as needed 619 // Make sure that a single scrollbar is created and visible as needed
616 void ScrollView::SetControlVisibility(View* control, bool should_show) { 620 void ScrollView::SetControlVisibility(View* control, bool should_show) {
617 if (!control) 621 if (!control)
618 return; 622 return;
619 if (should_show) { 623 if (should_show) {
620 if (!control->visible()) { 624 if (!control->visible()) {
621 AddChildView(control); 625 AddChildView(control);
622 control->SetVisible(true); 626 control->SetVisible(true);
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
754 758
755 VariableRowHeightScrollHelper::RowInfo 759 VariableRowHeightScrollHelper::RowInfo
756 FixedRowHeightScrollHelper::GetRowInfo(int y) { 760 FixedRowHeightScrollHelper::GetRowInfo(int y) {
757 if (y < top_margin_) 761 if (y < top_margin_)
758 return RowInfo(0, top_margin_); 762 return RowInfo(0, top_margin_);
759 return RowInfo((y - top_margin_) / row_height_ * row_height_ + top_margin_, 763 return RowInfo((y - top_margin_) / row_height_ * row_height_ + top_margin_,
760 row_height_); 764 row_height_);
761 } 765 }
762 766
763 } // namespace views 767 } // namespace views
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/desktop_capture/desktop_media_list_view.cc ('k') | ui/views/controls/scroll_view_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698