File: base_scroll_bar_thumb.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (150 lines) | stat: -rw-r--r-- 4,388 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/views/controls/scrollbar/base_scroll_bar_thumb.h"

#include "base/i18n/rtl.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/views/controls/scrollbar/scroll_bar.h"

namespace {
// The distance the mouse can be dragged outside the bounds of the thumb during
// dragging before the scrollbar will snap back to its regular position.
static constexpr int kScrollThumbDragOutSnap = 100;
}  // namespace

namespace views {

BaseScrollBarThumb::BaseScrollBarThumb(ScrollBar* scroll_bar)
    : scroll_bar_(scroll_bar) {}

BaseScrollBarThumb::~BaseScrollBarThumb() = default;

void BaseScrollBarThumb::SetLength(int length) {
  // Make sure the thumb is never sized smaller than its minimum possible
  // display size.
  gfx::Size size = GetPreferredSize({});
  size.SetToMax(
      gfx::Size(IsHorizontal() ? length : 0, IsHorizontal() ? 0 : length));
  SetSize(size);
}

int BaseScrollBarThumb::GetLength() const {
  if (IsHorizontal()) {
    return width();
  }
  return height();
}

void BaseScrollBarThumb::SetPosition(int position) {
  gfx::Rect thumb_bounds = bounds();
  gfx::Rect track_bounds = scroll_bar_->GetTrackBounds();
  if (IsHorizontal()) {
    thumb_bounds.set_x(track_bounds.x() + position);
  } else {
    thumb_bounds.set_y(track_bounds.y() + position);
  }
  SetBoundsRect(thumb_bounds);
}

int BaseScrollBarThumb::GetPosition() const {
  gfx::Rect track_bounds = scroll_bar_->GetTrackBounds();
  if (IsHorizontal()) {
    return x() - track_bounds.x();
  }
  return y() - track_bounds.y();
}

void BaseScrollBarThumb::SetSnapBackOnDragOutside(bool snap) {
  snap_back_on_drag_outside_ = snap;
}

bool BaseScrollBarThumb::GetSnapBackOnDragOutside() const {
  return snap_back_on_drag_outside_;
}

void BaseScrollBarThumb::OnMouseEntered(const ui::MouseEvent& event) {
  SetState(Button::STATE_HOVERED);
}

void BaseScrollBarThumb::OnMouseExited(const ui::MouseEvent& event) {
  SetState(Button::STATE_NORMAL);
}

bool BaseScrollBarThumb::OnMousePressed(const ui::MouseEvent& event) {
  mouse_offset_ = IsHorizontal() ? event.x() : event.y();
  drag_start_position_ = GetPosition();
  SetState(Button::STATE_PRESSED);
  return true;
}

bool BaseScrollBarThumb::OnMouseDragged(const ui::MouseEvent& event) {
  if (snap_back_on_drag_outside_) {
    // If the user moves the mouse more than |kScrollThumbDragOutSnap| outside
    // the bounds of the thumb, the scrollbar will snap the scroll back to the
    // point it was at before the drag began.
    if (IsHorizontal()) {
      if ((event.y() < y() - kScrollThumbDragOutSnap) ||
          (event.y() > (y() + height() + kScrollThumbDragOutSnap))) {
        scroll_bar_->ScrollToThumbPosition(drag_start_position_, false);
        return true;
      }
    } else {
      if ((event.x() < x() - kScrollThumbDragOutSnap) ||
          (event.x() > (x() + width() + kScrollThumbDragOutSnap))) {
        scroll_bar_->ScrollToThumbPosition(drag_start_position_, false);
        return true;
      }
    }
  }
  if (IsHorizontal()) {
    int thumb_x = event.x() - mouse_offset_;
    if (base::i18n::IsRTL()) {
      thumb_x *= -1;
    }
    scroll_bar_->ScrollToThumbPosition(GetPosition() + thumb_x, false);
  } else {
    int thumb_y = event.y() - mouse_offset_;
    scroll_bar_->ScrollToThumbPosition(GetPosition() + thumb_y, false);
  }
  return true;
}

void BaseScrollBarThumb::OnMouseReleased(const ui::MouseEvent& event) {
  SetState(HitTestPoint(event.location()) ? Button::STATE_HOVERED
                                          : Button::STATE_NORMAL);
}

void BaseScrollBarThumb::OnMouseCaptureLost() {
  SetState(Button::STATE_HOVERED);
}

Button::ButtonState BaseScrollBarThumb::GetState() const {
  return state_;
}

void BaseScrollBarThumb::SetState(Button::ButtonState state) {
  if (state_ == state) {
    return;
  }

  state_ = state;
  OnStateChanged();
}

void BaseScrollBarThumb::OnStateChanged() {
  SchedulePaint();
}

bool BaseScrollBarThumb::IsHorizontal() const {
  return scroll_bar_->GetOrientation() == ScrollBar::Orientation::kHorizontal;
}

BEGIN_METADATA(BaseScrollBarThumb)
ADD_PROPERTY_METADATA(bool, SnapBackOnDragOutside);
END_METADATA

}  // namespace views