1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and affiliates.
    
  3.  *
    
  4.  * This source code is licensed under the MIT license found in the
    
  5.  * LICENSE file in the root directory of this source tree.
    
  6.  *
    
  7.  * @flow
    
  8.  */
    
  9. 
    
  10. import type {Rect} from '../geometry';
    
  11. import type {Surface, ViewRefs} from '../Surface';
    
  12. import type {
    
  13.   Interaction,
    
  14.   ClickInteraction,
    
  15.   MouseDownInteraction,
    
  16.   MouseMoveInteraction,
    
  17.   MouseUpInteraction,
    
  18. } from '../useCanvasInteraction';
    
  19. 
    
  20. import {VerticalScrollOverflowView} from './VerticalScrollOverflowView';
    
  21. import {rectContainsPoint, rectEqualToRect} from '../geometry';
    
  22. import {View} from '../View';
    
  23. import {BORDER_SIZE, COLORS} from '../../content-views/constants';
    
  24. 
    
  25. const SCROLL_BAR_SIZE = 14;
    
  26. 
    
  27. const HIDDEN_RECT = {
    
  28.   origin: {
    
  29.     x: 0,
    
  30.     y: 0,
    
  31.   },
    
  32.   size: {
    
  33.     width: 0,
    
  34.     height: 0,
    
  35.   },
    
  36. };
    
  37. 
    
  38. export class VerticalScrollBarView extends View {
    
  39.   _contentHeight: number = 0;
    
  40.   _isScrolling: boolean = false;
    
  41.   _scrollBarRect: Rect = HIDDEN_RECT;
    
  42.   _scrollThumbRect: Rect = HIDDEN_RECT;
    
  43.   _verticalScrollOverflowView: VerticalScrollOverflowView;
    
  44. 
    
  45.   constructor(
    
  46.     surface: Surface,
    
  47.     frame: Rect,
    
  48.     verticalScrollOverflowView: VerticalScrollOverflowView,
    
  49.   ) {
    
  50.     super(surface, frame);
    
  51. 
    
  52.     this._verticalScrollOverflowView = verticalScrollOverflowView;
    
  53.   }
    
  54. 
    
  55.   desiredSize(): {+height: number, +width: number} {
    
  56.     return {
    
  57.       width: SCROLL_BAR_SIZE,
    
  58.       height: 0, // No desired height
    
  59.     };
    
  60.   }
    
  61. 
    
  62.   getMaxScrollThumbY(): number {
    
  63.     const {height} = this.frame.size;
    
  64. 
    
  65.     const maxScrollThumbY = height - this._scrollThumbRect.size.height;
    
  66. 
    
  67.     return maxScrollThumbY;
    
  68.   }
    
  69. 
    
  70.   setContentHeight(contentHeight: number) {
    
  71.     this._contentHeight = contentHeight;
    
  72. 
    
  73.     const {height, width} = this.frame.size;
    
  74. 
    
  75.     const proposedScrollThumbRect = {
    
  76.       origin: {
    
  77.         x: this.frame.origin.x,
    
  78.         y: this._scrollThumbRect.origin.y,
    
  79.       },
    
  80.       size: {
    
  81.         width,
    
  82.         height: height * (height / contentHeight),
    
  83.       },
    
  84.     };
    
  85. 
    
  86.     if (!rectEqualToRect(this._scrollThumbRect, proposedScrollThumbRect)) {
    
  87.       this._scrollThumbRect = proposedScrollThumbRect;
    
  88.       this.setNeedsDisplay();
    
  89.     }
    
  90.   }
    
  91. 
    
  92.   setScrollThumbY(value: number) {
    
  93.     const {height} = this.frame.size;
    
  94. 
    
  95.     const maxScrollThumbY = this.getMaxScrollThumbY();
    
  96.     const newScrollThumbY = Math.max(0, Math.min(maxScrollThumbY, value));
    
  97. 
    
  98.     this._scrollThumbRect = {
    
  99.       ...this._scrollThumbRect,
    
  100.       origin: {
    
  101.         x: this.frame.origin.x,
    
  102.         y: newScrollThumbY,
    
  103.       },
    
  104.     };
    
  105. 
    
  106.     const maxContentOffset = this._contentHeight - height;
    
  107.     const contentScrollOffset =
    
  108.       (newScrollThumbY / maxScrollThumbY) * maxContentOffset * -1;
    
  109. 
    
  110.     this._verticalScrollOverflowView.setScrollOffset(
    
  111.       contentScrollOffset,
    
  112.       maxScrollThumbY,
    
  113.     );
    
  114.   }
    
  115. 
    
  116.   draw(context: CanvasRenderingContext2D, viewRefs: ViewRefs) {
    
  117.     const {x, y} = this.frame.origin;
    
  118.     const {width, height} = this.frame.size;
    
  119. 
    
  120.     // TODO Use real color
    
  121.     context.fillStyle = COLORS.REACT_RESIZE_BAR;
    
  122.     context.fillRect(x, y, width, height);
    
  123. 
    
  124.     // TODO Use real color
    
  125.     context.fillStyle = COLORS.SCROLL_CARET;
    
  126.     context.fillRect(
    
  127.       this._scrollThumbRect.origin.x,
    
  128.       this._scrollThumbRect.origin.y,
    
  129.       this._scrollThumbRect.size.width,
    
  130.       this._scrollThumbRect.size.height,
    
  131.     );
    
  132. 
    
  133.     // TODO Use real color
    
  134.     context.fillStyle = COLORS.REACT_RESIZE_BAR_BORDER;
    
  135.     context.fillRect(x, y, BORDER_SIZE, height);
    
  136.   }
    
  137. 
    
  138.   handleInteraction(interaction: Interaction, viewRefs: ViewRefs) {
    
  139.     switch (interaction.type) {
    
  140.       case 'click':
    
  141.         this._handleClick(interaction, viewRefs);
    
  142.         break;
    
  143.       case 'mousedown':
    
  144.         this._handleMouseDown(interaction, viewRefs);
    
  145.         break;
    
  146.       case 'mousemove':
    
  147.         this._handleMouseMove(interaction, viewRefs);
    
  148.         break;
    
  149.       case 'mouseup':
    
  150.         this._handleMouseUp(interaction, viewRefs);
    
  151.         break;
    
  152.     }
    
  153.   }
    
  154. 
    
  155.   _handleClick(interaction: ClickInteraction, viewRefs: ViewRefs) {
    
  156.     const {location} = interaction.payload;
    
  157.     if (rectContainsPoint(location, this.frame)) {
    
  158.       if (rectContainsPoint(location, this._scrollThumbRect)) {
    
  159.         // Ignore clicks on the track thumb directly.
    
  160.         return;
    
  161.       }
    
  162. 
    
  163.       const currentScrollThumbY = this._scrollThumbRect.origin.y;
    
  164.       const y = location.y;
    
  165. 
    
  166.       const {height} = this.frame.size;
    
  167. 
    
  168.       // Scroll up or down about one viewport worth of content:
    
  169.       const deltaY = (height / this._contentHeight) * height * 0.8;
    
  170. 
    
  171.       this.setScrollThumbY(
    
  172.         y > currentScrollThumbY
    
  173.           ? this._scrollThumbRect.origin.y + deltaY
    
  174.           : this._scrollThumbRect.origin.y - deltaY,
    
  175.       );
    
  176.     }
    
  177.   }
    
  178. 
    
  179.   _handleMouseDown(interaction: MouseDownInteraction, viewRefs: ViewRefs) {
    
  180.     const {location} = interaction.payload;
    
  181.     if (!rectContainsPoint(location, this._scrollThumbRect)) {
    
  182.       return;
    
  183.     }
    
  184.     viewRefs.activeView = this;
    
  185. 
    
  186.     this.currentCursor = 'default';
    
  187. 
    
  188.     this._isScrolling = true;
    
  189.     this.setNeedsDisplay();
    
  190.   }
    
  191. 
    
  192.   _handleMouseMove(interaction: MouseMoveInteraction, viewRefs: ViewRefs) {
    
  193.     const {event, location} = interaction.payload;
    
  194.     if (rectContainsPoint(location, this.frame)) {
    
  195.       if (viewRefs.hoveredView !== this) {
    
  196.         viewRefs.hoveredView = this;
    
  197.       }
    
  198. 
    
  199.       this.currentCursor = 'default';
    
  200.     }
    
  201. 
    
  202.     if (viewRefs.activeView === this) {
    
  203.       this.currentCursor = 'default';
    
  204. 
    
  205.       this.setScrollThumbY(this._scrollThumbRect.origin.y + event.movementY);
    
  206.     }
    
  207.   }
    
  208. 
    
  209.   _handleMouseUp(interaction: MouseUpInteraction, viewRefs: ViewRefs) {
    
  210.     if (viewRefs.activeView === this) {
    
  211.       viewRefs.activeView = null;
    
  212.     }
    
  213. 
    
  214.     if (this._isScrolling) {
    
  215.       this._isScrolling = false;
    
  216.       this.setNeedsDisplay();
    
  217.     }
    
  218.   }
    
  219. }