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 {
    
  11.   Interaction,
    
  12.   MouseDownInteraction,
    
  13.   MouseMoveInteraction,
    
  14.   MouseUpInteraction,
    
  15. } from '../useCanvasInteraction';
    
  16. import type {Rect, Size} from '../geometry';
    
  17. import type {ViewRefs} from '../Surface';
    
  18. 
    
  19. import {BORDER_SIZE, COLORS} from '../../content-views/constants';
    
  20. import {drawText} from '../../content-views/utils/text';
    
  21. import {Surface} from '../Surface';
    
  22. import {View} from '../View';
    
  23. import {rectContainsPoint} from '../geometry';
    
  24. import {noopLayout} from '../layouter';
    
  25. 
    
  26. type ResizeBarState = 'normal' | 'hovered' | 'dragging';
    
  27. 
    
  28. const RESIZE_BAR_DOT_RADIUS = 1;
    
  29. const RESIZE_BAR_DOT_SPACING = 4;
    
  30. const RESIZE_BAR_HEIGHT = 8;
    
  31. const RESIZE_BAR_WITH_LABEL_HEIGHT = 16;
    
  32. 
    
  33. export class ResizeBarView extends View {
    
  34.   _interactionState: ResizeBarState = 'normal';
    
  35.   _label: string;
    
  36. 
    
  37.   showLabel: boolean = false;
    
  38. 
    
  39.   constructor(surface: Surface, frame: Rect, label: string) {
    
  40.     super(surface, frame, noopLayout);
    
  41. 
    
  42.     this._label = label;
    
  43.   }
    
  44. 
    
  45.   desiredSize(): Size {
    
  46.     return this.showLabel
    
  47.       ? {height: RESIZE_BAR_WITH_LABEL_HEIGHT, width: 0}
    
  48.       : {height: RESIZE_BAR_HEIGHT, width: 0};
    
  49.   }
    
  50. 
    
  51.   draw(context: CanvasRenderingContext2D, viewRefs: ViewRefs) {
    
  52.     const {frame} = this;
    
  53.     const {x, y} = frame.origin;
    
  54.     const {width, height} = frame.size;
    
  55. 
    
  56.     const isActive =
    
  57.       this._interactionState === 'dragging' ||
    
  58.       (this._interactionState === 'hovered' && viewRefs.activeView === null);
    
  59. 
    
  60.     context.fillStyle = isActive
    
  61.       ? COLORS.REACT_RESIZE_BAR_ACTIVE
    
  62.       : COLORS.REACT_RESIZE_BAR;
    
  63.     context.fillRect(x, y, width, height);
    
  64. 
    
  65.     context.fillStyle = COLORS.REACT_RESIZE_BAR_BORDER;
    
  66.     context.fillRect(x, y, width, BORDER_SIZE);
    
  67.     context.fillRect(x, y + height - BORDER_SIZE, width, BORDER_SIZE);
    
  68. 
    
  69.     const horizontalCenter = x + width / 2;
    
  70.     const verticalCenter = y + height / 2;
    
  71. 
    
  72.     if (this.showLabel) {
    
  73.       // When the resize view is collapsed entirely,
    
  74.       // rather than showing a resize bar– this view displays a label.
    
  75.       const labelRect: Rect = {
    
  76.         origin: {
    
  77.           x: 0,
    
  78.           y: y + height - RESIZE_BAR_WITH_LABEL_HEIGHT,
    
  79.         },
    
  80.         size: {
    
  81.           width: frame.size.width,
    
  82.           height: RESIZE_BAR_WITH_LABEL_HEIGHT,
    
  83.         },
    
  84.       };
    
  85. 
    
  86.       drawText(this._label, context, labelRect, frame, {
    
  87.         fillStyle: COLORS.REACT_RESIZE_BAR_DOT,
    
  88.         textAlign: 'center',
    
  89.       });
    
  90.     } else {
    
  91.       // Otherwise draw horizontally centered resize bar dots
    
  92.       context.beginPath();
    
  93.       context.fillStyle = COLORS.REACT_RESIZE_BAR_DOT;
    
  94.       context.arc(
    
  95.         horizontalCenter,
    
  96.         verticalCenter,
    
  97.         RESIZE_BAR_DOT_RADIUS,
    
  98.         0,
    
  99.         2 * Math.PI,
    
  100.       );
    
  101.       context.arc(
    
  102.         horizontalCenter + RESIZE_BAR_DOT_SPACING,
    
  103.         verticalCenter,
    
  104.         RESIZE_BAR_DOT_RADIUS,
    
  105.         0,
    
  106.         2 * Math.PI,
    
  107.       );
    
  108.       context.arc(
    
  109.         horizontalCenter - RESIZE_BAR_DOT_SPACING,
    
  110.         verticalCenter,
    
  111.         RESIZE_BAR_DOT_RADIUS,
    
  112.         0,
    
  113.         2 * Math.PI,
    
  114.       );
    
  115.       context.fill();
    
  116.     }
    
  117.   }
    
  118. 
    
  119.   _setInteractionState(state: ResizeBarState) {
    
  120.     if (this._interactionState === state) {
    
  121.       return;
    
  122.     }
    
  123.     this._interactionState = state;
    
  124.     this.setNeedsDisplay();
    
  125.   }
    
  126. 
    
  127.   _handleMouseDown(interaction: MouseDownInteraction, viewRefs: ViewRefs) {
    
  128.     const cursorInView = rectContainsPoint(
    
  129.       interaction.payload.location,
    
  130.       this.frame,
    
  131.     );
    
  132.     if (cursorInView) {
    
  133.       this._setInteractionState('dragging');
    
  134.       viewRefs.activeView = this;
    
  135.     }
    
  136.   }
    
  137. 
    
  138.   _handleMouseMove(interaction: MouseMoveInteraction, viewRefs: ViewRefs) {
    
  139.     const cursorInView = rectContainsPoint(
    
  140.       interaction.payload.location,
    
  141.       this.frame,
    
  142.     );
    
  143. 
    
  144.     if (viewRefs.activeView === this) {
    
  145.       // If we're actively dragging this resize bar,
    
  146.       // show the cursor even if the pointer isn't hovering over this view.
    
  147.       this.currentCursor = 'ns-resize';
    
  148.     } else if (cursorInView) {
    
  149.       if (this.showLabel) {
    
  150.         this.currentCursor = 'pointer';
    
  151.       } else {
    
  152.         this.currentCursor = 'ns-resize';
    
  153.       }
    
  154.     }
    
  155. 
    
  156.     if (cursorInView) {
    
  157.       viewRefs.hoveredView = this;
    
  158.     }
    
  159. 
    
  160.     if (this._interactionState === 'dragging') {
    
  161.       return;
    
  162.     }
    
  163.     this._setInteractionState(cursorInView ? 'hovered' : 'normal');
    
  164.   }
    
  165. 
    
  166.   _handleMouseUp(interaction: MouseUpInteraction, viewRefs: ViewRefs) {
    
  167.     const cursorInView = rectContainsPoint(
    
  168.       interaction.payload.location,
    
  169.       this.frame,
    
  170.     );
    
  171.     if (this._interactionState === 'dragging') {
    
  172.       this._setInteractionState(cursorInView ? 'hovered' : 'normal');
    
  173.     }
    
  174. 
    
  175.     if (viewRefs.activeView === this) {
    
  176.       viewRefs.activeView = null;
    
  177.     }
    
  178.   }
    
  179. 
    
  180.   handleInteraction(interaction: Interaction, viewRefs: ViewRefs) {
    
  181.     switch (interaction.type) {
    
  182.       case 'mousedown':
    
  183.         this._handleMouseDown(interaction, viewRefs);
    
  184.         break;
    
  185.       case 'mousemove':
    
  186.         this._handleMouseMove(interaction, viewRefs);
    
  187.         break;
    
  188.       case 'mouseup':
    
  189.         this._handleMouseUp(interaction, viewRefs);
    
  190.         break;
    
  191.     }
    
  192.   }
    
  193. }