<commit_before>'use strict'

import { XdgPositioner } from './protocol/xdg-shell-browser-protocol'
import Point from './math/Point'

const {none, slideX, slideY, flipX, flipY, resizeX, resizeY} = XdgPositioner.ConstraintAdjustment

const inverseY = {
  /**
   * none
   * @return {number}
   */
  0: 0,
  /**
   * top
   * @return {number}
   */
  1: 2,
  /**
   * bottom
   * @return {number}
   */
  2: 1,
  /**
   * left
   * @return {number}
   */
  3: 3,
  /**
   * right
   * @return {number}
   */
  4: 4,
  /**
   * topLeft
   * @return {number}
   */
  5: 6,
  /**
   * bottomLeft
   * @return {number}
   */
  6: 5,
  /**
   * topRight
   * @return {number}
   */
  7: 8,
  /**
   * bottomRight
   * @return {number}
   */
  8: 7
}

const inverseX = {
  /**
   * none
   * @return {number}
   */
  0: 0,
  /**
   * top
   * @return {number}
   */
  1: 1,
  /**
   * bottom
   * @return {number}
   */
  2: 2,
  /**
   * left
   * @return {number}
   */
  3: 4,
  /**
   * right
   * @return {number}
   */
  4: 3,
  /**
   * topLeft
   * @return {number}
   */
  5: 7,
  /**
   * bottomLeft
   * @return {number}
   */
  6: 8,
  /**
   * topRight
   * @return {number}
   */
  7: 5,
  /**
   * bottomRight
   * @return {number}
   */
  8: 6
}

/**
 *
 *      A popup surface is a short-lived, temporary surface. It can be used to
 *      implement for example menus, popovers, tooltips and other similar user
 *      interface concepts.
 *
 *      A popup can be made to take an explicit grab. See xdg_popup.grab for
 *      details.
 *
 *      When the popup is dismissed, a popup_done event will be sent out, and at
 *      the same time the surface will be unmapped. See the xdg_popup.popup_done
 *      event for details.
 *
 *      Explicitly destroying the xdg_popup object will also dismiss the popup and
 *      unmap the surface. Clients that want to dismiss the popup when another
 *      surface of their own is clicked should dismiss the popup using the destroy
 *      request.
 *
 *      The parent surface must have either the xdg_toplevel or xdg_popup surface
 *      role.
 *
 *      A newly created xdg_popup will be stacked on top of all previously created
 *      xdg_popup surfaces associated with the same xdg_toplevel.
 *
 *      The parent of an xdg_popup must be mapped (see the xdg_surface
 *      description) before the xdg_popup itself.
 *
 *      The x and y arguments passed when creating the popup object specify
 *      where the top left of the popup should be placed, relative to the
 *      local surface coordinates of the parent surface. See
 *      xdg_surface.get_popup. An xdg_popup must intersect with or be at least
 *      partially adjacent to its parent surface.
 *
 *      The client must call wl_surface.commit on the corresponding wl_surface
 *      for the xdg_popup state to take effect.
 *
 */
export default class BrowserXdgPopup {
  /**
   * @param {XdgPopup}xdgPopupResource
   * @param {BrowserXdgSurface}browserXdgSurface
   * @param {XdgSurface|null}parent
   * @param {{size: Rect, anchorRect: Rect, anchor: number, gravity: number, constraintAdjustment: number, offset: Point, surfaceSpaceAnchorPoint: (function(BrowserXdgSurface): Point), checkScreenConstraints: (function(BrowserXdgSurface, BrowserSurfaceView): {topViolation: number, rightViolation: number, bottomViolation: number, leftViolation: number})}}positionerState
   * @param {BrowserSession}browserSession
   * @param {BrowserSeat}browserSeat
   * @return {BrowserXdgPopup}
   */
  static create (xdgPopupResource, browserXdgSurface, parent, positionerState, browserSession, browserSeat) {
    const browserXdgPopup = new BrowserXdgPopup(xdgPopupResource, browserXdgSurface, parent, positionerState, browserSession, browserSeat)
    xdgPopupResource.implementation = browserXdgPopup
    browserXdgSurface.grSurfaceResource.implementation.role = browserXdgPopup
    browserXdgPopup.ensureGeometryConstraints(parent, positionerState)
    browserXdgSurface.emitConfigureDone()
    return browserXdgPopup
  }

  /**
   * @param {XdgPopup}xdgPopupResource
   * @param {BrowserXdgSurface}browserXdgSurface
   * @param {XdgSurface|null}parent
   * @param {{size: Rect, anchorRect: Rect, anchor: number, gravity: number, constraintAdjustment: number, offset: Point, surfaceSpaceAnchorPoint: (function(BrowserXdgSurface): Point), checkScreenConstraints: (function(BrowserXdgSurface, BrowserSurfaceView): {topViolation: number, rightViolation: number, bottomViolation: number, leftViolation: number})}}positionerState
   * @param {BrowserSession}browserSession
   * @param {BrowserSeat}browserSeat
   */
  constructor (xdgPopupResource, browserXdgSurface, parent, positionerState, browserSession, browserSeat) {
    /**
     * @type {XdgPopup}
     */
    this.resource = xdgPopupResource
    /**
     * @type {BrowserXdgSurface}
     */
    this.browserXdgSurface = browserXdgSurface
    /**
     * @type {XdgSurface|null}
     */
    this.parent = parent
    /**
     * @type {{size: Rect, anchorRect: Rect, anchor: number, gravity: number, constraintAdjustment: number, offset: Point, surfaceSpaceAnchorPoint: (function(BrowserXdgSurface): Point), checkScreenConstraints: (function(BrowserXdgSurface, BrowserSurfaceView): {topViolation: number, rightViolation: number, bottomViolation: number, leftViolation: number})}}
     */
    this.positionerState = positionerState
    /**
     * @type {boolean}
     */
    this.mapped = false
    /**
     * @type {boolean}
     */
    this.dismissed = false
    /**
     * @type {BrowserSession}
     * @private
     */
    this._browserSession = browserSession
    /**
     * @type {BrowserSeat}
     * @private
     */
    this._browserSeat = browserSeat
  }

  /**
   * @return {{windowGeometry: Rect}}
   */
  captureRoleState () {
    return {
      windowGeometry: this.browserXdgSurface.pendingWindowGeometry
    }
  }

  /**
   * @param {{windowGeometry: Rect}}roleState
   */
  setRoleState (roleState) {
    this.browserXdgSurface.updateWindowGeometry(roleState.windowGeometry)
  }

  /**
   * @param {BrowserSurface}browserSurface
   * @param {RenderFrame}renderFrame
   * @param {{bufferContents: {type: string, syncSerial: number, geo: Size, yuvContent: Uint8Array, yuvWidth: number, yuvHeight: number, alphaYuvContent: Uint8Array, alphaYuvWidth: number, alphaYuvHeight: number, pngImage: HTMLImageElement}|null, bufferDamage: Number, opaquePixmanRegion: number, inputPixmanRegion: number, dx: number, dy: number, bufferTransform: number, bufferScale: number, frameCallbacks: BrowserCallback[], roleState:{windowGeometry: Rect}}}newState
   * @return {Promise<void>}
   */
  async onCommit (browserSurface, renderFrame, newState) {
    if (this.dismissed) {
      return
    }

    if (newState.bufferContents) {
      if (!this.mapped) {
        this._map(browserSurface)
      }
    } else if (this.mapped) {
      this._dismiss()
    }

    browserSurface.render(renderFrame, newState)
    renderFrame.fire()
    await renderFrame
    this._browserSession.flush()
  }

  /**
   * @param {BrowserSurface}browserSurface
   * @private
   */
  _map (browserSurface) {
    // TODO check if parent is mapped

    this.mapped = true
    const parentBrowserSurface = this.parent.implementation.grSurfaceResource.implementation

    // set position based on positioner object
    browserSurface.browserSurfaceChildSelf.position = this.positionerState.surfaceSpaceAnchorPoint(this.parent.implementation)
    parentBrowserSurface.addChild(browserSurface.browserSurfaceChildSelf)
  }

  _dismiss () {
    if (!this.dismissed) {
      this.dismissed = true
      if (this._browserSeat.browserPointer) {
        const popupGrab = this._browserSeat.browserPointer.findPopupGrab(this.browserXdgSurface.grSurfaceResource)
        if (popupGrab) {
          popupGrab.resolve()
        }
      }
      this.resource.popupDone()
      const parentBrowserSurface = this.parent.implementation.grSurfaceResource.implementation
      parentBrowserSurface.removeChild(this.browserXdgSurface.grSurfaceResource.implementation.browserSurfaceChildSelf)
      this._browserSeat.browserKeyboard.focusGained(parentBrowserSurface)
    }
  }

  /**
   * @private
   */
  _updatePopupKeyboardFocus () {
    this._browserKeyboard.focusGained(this.browserXdgSurface.grSurfaceResource.implementation)
    // if the keyboard or focus changes to a different client, we have to dismiss the popup
    this._browserKeyboard.onKeyboardFocusChanged().then(() => {
      if (!this._browserKeyboard.focus || this._browserKeyboard.focus.resource.client !== this.resource.client) {
        this._dismiss()
      }
    })<commit_msg>fix popup pointing to undefined keyboard variable<commit_after>'use strict'

import { XdgPositioner } from './protocol/xdg-shell-browser-protocol'
import Point from './math/Point'

const {none, slideX, slideY, flipX, flipY, resizeX, resizeY} = XdgPositioner.ConstraintAdjustment

const inverseY = {
  /**
   * none
   * @return {number}
   */
  0: 0,
  /**
   * top
   * @return {number}
   */
  1: 2,
  /**
   * bottom
   * @return {number}
   */
  2: 1,
  /**
   * left
   * @return {number}
   */
  3: 3,
  /**
   * right
   * @return {number}
   */
  4: 4,
  /**
   * topLeft
   * @return {number}
   */
  5: 6,
  /**
   * bottomLeft
   * @return {number}
   */
  6: 5,
  /**
   * topRight
   * @return {number}
   */
  7: 8,
  /**
   * bottomRight
   * @return {number}
   */
  8: 7
}

const inverseX = {
  /**
   * none
   * @return {number}
   */
  0: 0,
  /**
   * top
   * @return {number}
   */
  1: 1,
  /**
   * bottom
   * @return {number}
   */
  2: 2,
  /**
   * left
   * @return {number}
   */
  3: 4,
  /**
   * right
   * @return {number}
   */
  4: 3,
  /**
   * topLeft
   * @return {number}
   */
  5: 7,
  /**
   * bottomLeft
   * @return {number}
   */
  6: 8,
  /**
   * topRight
   * @return {number}
   */
  7: 5,
  /**
   * bottomRight
   * @return {number}
   */
  8: 6
}

/**
 *
 *      A popup surface is a short-lived, temporary surface. It can be used to
 *      implement for example menus, popovers, tooltips and other similar user
 *      interface concepts.
 *
 *      A popup can be made to take an explicit grab. See xdg_popup.grab for
 *      details.
 *
 *      When the popup is dismissed, a popup_done event will be sent out, and at
 *      the same time the surface will be unmapped. See the xdg_popup.popup_done
 *      event for details.
 *
 *      Explicitly destroying the xdg_popup object will also dismiss the popup and
 *      unmap the surface. Clients that want to dismiss the popup when another
 *      surface of their own is clicked should dismiss the popup using the destroy
 *      request.
 *
 *      The parent surface must have either the xdg_toplevel or xdg_popup surface
 *      role.
 *
 *      A newly created xdg_popup will be stacked on top of all previously created
 *      xdg_popup surfaces associated with the same xdg_toplevel.
 *
 *      The parent of an xdg_popup must be mapped (see the xdg_surface
 *      description) before the xdg_popup itself.
 *
 *      The x and y arguments passed when creating the popup object specify
 *      where the top left of the popup should be placed, relative to the
 *      local surface coordinates of the parent surface. See
 *      xdg_surface.get_popup. An xdg_popup must intersect with or be at least
 *      partially adjacent to its parent surface.
 *
 *      The client must call wl_surface.commit on the corresponding wl_surface
 *      for the xdg_popup state to take effect.
 *
 */
export default class BrowserXdgPopup {
  /**
   * @param {XdgPopup}xdgPopupResource
   * @param {BrowserXdgSurface}browserXdgSurface
   * @param {XdgSurface|null}parent
   * @param {{size: Rect, anchorRect: Rect, anchor: number, gravity: number, constraintAdjustment: number, offset: Point, surfaceSpaceAnchorPoint: (function(BrowserXdgSurface): Point), checkScreenConstraints: (function(BrowserXdgSurface, BrowserSurfaceView): {topViolation: number, rightViolation: number, bottomViolation: number, leftViolation: number})}}positionerState
   * @param {BrowserSession}browserSession
   * @param {BrowserSeat}browserSeat
   * @return {BrowserXdgPopup}
   */
  static create (xdgPopupResource, browserXdgSurface, parent, positionerState, browserSession, browserSeat) {
    const browserXdgPopup = new BrowserXdgPopup(xdgPopupResource, browserXdgSurface, parent, positionerState, browserSession, browserSeat)
    xdgPopupResource.implementation = browserXdgPopup
    browserXdgSurface.grSurfaceResource.implementation.role = browserXdgPopup
    browserXdgPopup.ensureGeometryConstraints(parent, positionerState)
    browserXdgSurface.emitConfigureDone()
    return browserXdgPopup
  }

  /**
   * @param {XdgPopup}xdgPopupResource
   * @param {BrowserXdgSurface}browserXdgSurface
   * @param {XdgSurface|null}parent
   * @param {{size: Rect, anchorRect: Rect, anchor: number, gravity: number, constraintAdjustment: number, offset: Point, surfaceSpaceAnchorPoint: (function(BrowserXdgSurface): Point), checkScreenConstraints: (function(BrowserXdgSurface, BrowserSurfaceView): {topViolation: number, rightViolation: number, bottomViolation: number, leftViolation: number})}}positionerState
   * @param {BrowserSession}browserSession
   * @param {BrowserSeat}browserSeat
   */
  constructor (xdgPopupResource, browserXdgSurface, parent, positionerState, browserSession, browserSeat) {
    /**
     * @type {XdgPopup}
     */
    this.resource = xdgPopupResource
    /**
     * @type {BrowserXdgSurface}
     */
    this.browserXdgSurface = browserXdgSurface
    /**
     * @type {XdgSurface|null}
     */
    this.parent = parent
    /**
     * @type {{size: Rect, anchorRect: Rect, anchor: number, gravity: number, constraintAdjustment: number, offset: Point, surfaceSpaceAnchorPoint: (function(BrowserXdgSurface): Point), checkScreenConstraints: (function(BrowserXdgSurface, BrowserSurfaceView): {topViolation: number, rightViolation: number, bottomViolation: number, leftViolation: number})}}
     */
    this.positionerState = positionerState
    /**
     * @type {boolean}
     */
    this.mapped = false
    /**
     * @type {boolean}
     */
    this.dismissed = false
    /**
     * @type {BrowserSession}
     * @private
     */
    this._browserSession = browserSession
    /**
     * @type {BrowserSeat}
     * @private
     */
    this._browserSeat = browserSeat
  }

  /**
   * @return {{windowGeometry: Rect}}
   */
  captureRoleState () {
    return {
      windowGeometry: this.browserXdgSurface.pendingWindowGeometry
    }
  }

  /**
   * @param {{windowGeometry: Rect}}roleState
   */
  setRoleState (roleState) {
    this.browserXdgSurface.updateWindowGeometry(roleState.windowGeometry)
  }

  /**
   * @param {BrowserSurface}browserSurface
   * @param {RenderFrame}renderFrame
   * @param {{bufferContents: {type: string, syncSerial: number, geo: Size, yuvContent: Uint8Array, yuvWidth: number, yuvHeight: number, alphaYuvContent: Uint8Array, alphaYuvWidth: number, alphaYuvHeight: number, pngImage: HTMLImageElement}|null, bufferDamage: Number, opaquePixmanRegion: number, inputPixmanRegion: number, dx: number, dy: number, bufferTransform: number, bufferScale: number, frameCallbacks: BrowserCallback[], roleState:{windowGeometry: Rect}}}newState
   * @return {Promise<void>}
   */
  async onCommit (browserSurface, renderFrame, newState) {
    if (this.dismissed) {
      return
    }

    if (newState.bufferContents) {
      if (!this.mapped) {
        this._map(browserSurface)
      }
    } else if (this.mapped) {
      this._dismiss()
    }

    browserSurface.render(renderFrame, newState)
    renderFrame.fire()
    await renderFrame
    this._browserSession.flush()
  }

  /**
   * @param {BrowserSurface}browserSurface
   * @private
   */
  _map (browserSurface) {
    // TODO check if parent is mapped

    this.mapped = true
    const parentBrowserSurface = this.parent.implementation.grSurfaceResource.implementation

    // set position based on positioner object
    browserSurface.browserSurfaceChildSelf.position = this.positionerState.surfaceSpaceAnchorPoint(this.parent.implementation)
    parentBrowserSurface.addChild(browserSurface.browserSurfaceChildSelf)
  }

  _dismiss () {
    if (!this.dismissed) {
      this.dismissed = true
      if (this._browserSeat.browserPointer) {
        const popupGrab = this._browserSeat.browserPointer.findPopupGrab(this.browserXdgSurface.grSurfaceResource)
        if (popupGrab) {
          popupGrab.resolve()
        }
      }
      this.resource.popupDone()
      const parentBrowserSurface = this.parent.implementation.grSurfaceResource.implementation
      parentBrowserSurface.removeChild(this.browserXdgSurface.grSurfaceResource.implementation.browserSurfaceChildSelf)
      this._browserSeat.browserKeyboard.focusGained(parentBrowserSurface)
    }
  }

  /**
   * @private
   */
  _updatePopupKeyboardFocus () {
    this._browserSeat.browserKeyboard.focusGained(this.browserXdgSurface.grSurfaceResource.implementation)
    // if the keyboard or focus changes to a different client, we have to dismiss the popup
    this._browserSeat.browserKeyboard.onKeyboardFocusChanged().then(() => {
      if (!this._browserSeat.browserKeyboard.focus || this._browserSeat.browserKeyboard.focus.resource.client !== this.resource.client) {
        this._dismiss()
      }
    })
{'fraction_non_alphanumeric': 0.10699116060153828, 'fraction_numerical': 0.0043623005395476986, 'mean_word_length': 3.2776822980603977, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6249677', 'n_tokens_mistral': 5426, 'n_tokens_neox': 5254, 'n_words': 1312}