1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/scrollbar_geometry_fixed_thumb.h"
8 #include <public/WebRect.h>
9 #include <public/WebScrollbar.h>
11 using WebKit::WebRect
;
12 using WebKit::WebScrollbar
;
13 using WebKit::WebScrollbarThemeGeometry
;
17 scoped_ptr
<ScrollbarGeometryFixedThumb
> ScrollbarGeometryFixedThumb::create(scoped_ptr
<WebScrollbarThemeGeometry
> geometry
)
19 return make_scoped_ptr(new ScrollbarGeometryFixedThumb(geometry
.Pass()));
22 ScrollbarGeometryFixedThumb::~ScrollbarGeometryFixedThumb()
26 void ScrollbarGeometryFixedThumb::update(WebScrollbar
* scrollbar
)
28 int length
= ScrollbarGeometryStub::thumbLength(scrollbar
);
30 if (scrollbar
->orientation() == WebScrollbar::Horizontal
)
31 m_thumbSize
= gfx::Size(length
, scrollbar
->size().height
);
33 m_thumbSize
= gfx::Size(scrollbar
->size().width
, length
);
36 WebScrollbarThemeGeometry
* ScrollbarGeometryFixedThumb::clone() const
38 ScrollbarGeometryFixedThumb
* geometry
= new ScrollbarGeometryFixedThumb(make_scoped_ptr(ScrollbarGeometryStub::clone()));
39 geometry
->m_thumbSize
= m_thumbSize
;
43 int ScrollbarGeometryFixedThumb::thumbLength(WebScrollbar
* scrollbar
)
45 if (scrollbar
->orientation() == WebScrollbar::Horizontal
)
46 return m_thumbSize
.width();
47 return m_thumbSize
.height();
50 int ScrollbarGeometryFixedThumb::thumbPosition(WebScrollbar
* scrollbar
)
52 if (scrollbar
->enabled()) {
53 float size
= scrollbar
->maximum();
56 int value
= std::min(std::max(0, scrollbar
->value()), scrollbar
->maximum());
57 float pos
= (trackLength(scrollbar
) - thumbLength(scrollbar
)) * value
/ size
;
58 return static_cast<int>(floorf((pos
< 1 && pos
> 0) ? 1 : pos
));
62 void ScrollbarGeometryFixedThumb::splitTrack(WebScrollbar
* scrollbar
, const WebRect
& unconstrainedTrackRect
, WebRect
& beforeThumbRect
, WebRect
& thumbRect
, WebRect
& afterThumbRect
)
64 // This is a reimplementation of ScrollbarThemeComposite::splitTrack.
65 // Because the WebScrollbarThemeGeometry functions call down to native
66 // ScrollbarThemeComposite code which uses ScrollbarThemeComposite virtual
67 // helpers, there's no way to override a helper like thumbLength from
68 // the WebScrollbarThemeGeometry level. So, these three functions
69 // (splitTrack, thumbPosition, thumbLength) are copied here so that the
70 // WebScrollbarThemeGeometry helper functions are used instead and
71 // a fixed size thumbLength can be used.
73 WebRect trackRect
= constrainTrackRectToTrackPieces(scrollbar
, unconstrainedTrackRect
);
74 int thickness
= scrollbar
->orientation() == WebScrollbar::Horizontal
? scrollbar
->size().height
: scrollbar
->size().width
;
75 int thumbPos
= thumbPosition(scrollbar
);
76 if (scrollbar
->orientation() == WebScrollbar::Horizontal
) {
77 thumbRect
= WebRect(trackRect
.x
+ thumbPos
, trackRect
.y
+ (trackRect
.height
- thickness
) / 2, thumbLength(scrollbar
), thickness
);
78 beforeThumbRect
= WebRect(trackRect
.x
, trackRect
.y
, thumbPos
+ thumbRect
.width
/ 2, trackRect
.height
);
79 afterThumbRect
= WebRect(trackRect
.x
+ beforeThumbRect
.width
, trackRect
.y
, trackRect
.x
+ trackRect
.width
- beforeThumbRect
.x
- beforeThumbRect
.width
, trackRect
.height
);
81 thumbRect
= WebRect(trackRect
.x
+ (trackRect
.width
- thickness
) / 2, trackRect
.y
+ thumbPos
, thickness
, thumbLength(scrollbar
));
82 beforeThumbRect
= WebRect(trackRect
.x
, trackRect
.y
, trackRect
.width
, thumbPos
+ thumbRect
.height
/ 2);
83 afterThumbRect
= WebRect(trackRect
.x
, trackRect
.y
+ beforeThumbRect
.height
, trackRect
.width
, trackRect
.y
+ trackRect
.height
- beforeThumbRect
.y
- beforeThumbRect
.height
);
87 ScrollbarGeometryFixedThumb::ScrollbarGeometryFixedThumb(scoped_ptr
<WebScrollbarThemeGeometry
> geometry
)
88 : ScrollbarGeometryStub(geometry
.Pass())