<commit_before>import * as React from 'react'
import * as ReactDOM from 'react-dom'
import OpenFinChrome from '../shell/OpenFinChrome'
import { withDefaultProps } from '../utils/reactTypes'

interface WindowFeatures {
  width: number
  height: number
  left?: number
  top?: number
}

const defaultPortalProps = {
  url: '',
  name: '',
  title: '',
  features: { width: 600, height: 640 } as WindowFeatures,
  center: 'parent' as 'parent' | 'screen',
  copyStyles: true,
  onBlock: null as () => void,
  onUnload: null as () => void,
  onDesktop: false
}

type PortalProps = typeof defaultPortalProps

const initialState = { mounted: false }

type PortalState = Readonly<typeof initialState>

class NewWindow extends React.PureComponent<PortalProps, PortalState> {
  private container: HTMLDivElement
  private window: Window

  constructor(props) {
    super(props)
    this.container = document.createElement('div')
    this.state = {
      mounted: false
    }
  }

  render() {
    if (!this.state.mounted) {
      return false
    }
    if (this.props.onDesktop) {
      return ReactDOM.createPortal(
        <OpenFinChrome
          showHeaderBar={false}
          minimize={() => {
            console.log('Minimize')
          }}
        >
          {this.props.children}
        </OpenFinChrome>,
        this.container
      )
    }

    return ReactDOM.createPortal(this.props.children, this.container)
  }

  componentDidMount() {
    this.openChild()
    this.setState({ mounted: true })
  }

  /**
   * Create the new window when NewWindow component mount.
   */
  openChild() {
    const { url, name, features, center, onDesktop } = this.props

    let left = 0
    let top = 0

    if (center === 'parent') {
      left = window.top.outerWidth / 2 + window.top.screenX - features.width / 2
      top = window.top.outerHeight / 2 + window.top.screenY - features.height / 2
    } else if (center === 'screen') {
      const screenLeft = window.screenLeft
      const screenTop = window.screenTop

      const width = window.innerWidth
        ? window.innerWidth
        : document.documentElement.clientWidth
          ? document.documentElement.clientWidth
          : screen.width
      const height = window.innerHeight
        ? window.innerHeight
        : document.documentElement.clientHeight
          ? document.documentElement.clientHeight
          : screen.height

      left = width / 2 - features.width / 2 + screenLeft
      top = height / 2 - features.height / 2 + screenTop
    }

    if (onDesktop) {
      const win = new fin.desktop.Window(
        {
          name,
          url,
          defaultTop: top,
          defaultLeft: left,
          autoShow: true,
          frame: false
        },
        () => {
          this.window = win.getNativeWindow()
          this.injectIntoWindow()
        },
        error => {
          console.log('Error creating window:', error)
        }
      )
    } else {
      this.window = window.open(
        url,
        name,
        toWindowFeatures({
          ...features,
          left,
          top
        })
      )
      this.injectIntoWindow()
    }
  }

  injectIntoWindow() {
    const { onBlock, title } = this.props
    // Check if the new window was succesfully opened.
    if (this.window) {
      this.window.document.title = title
      this.window.document.body.appendChild(this.container)

      // If specified, copy styles from parent window's document.
      if (this.props.copyStyles) {
        setTimeout(() => copyStyles(document, this.window.document), 0)
      }

      // Release anything bound to this component before the new window unload.
      this.window.addEventListener('beforeunload', () => this.release())
    } else {
      // Handle error on opening of new window.
      if (onBlock) {
        onBlock.call(null)
      } else {
        console.warn('A new window could not be opened. Maybe it was blocked.')
      }<commit_msg>Close window functionality<commit_after>import * as React from 'react'
import * as ReactDOM from 'react-dom'
import OpenFinChrome from '../shell/OpenFinChrome'
import { withDefaultProps } from '../utils/reactTypes'

interface WindowFeatures {
  width: number
  height: number
  left?: number
  top?: number
}

const defaultPortalProps = {
  url: '',
  name: '',
  title: '',
  features: { width: 600, height: 640 } as WindowFeatures,
  center: 'parent' as 'parent' | 'screen',
  copyStyles: true,
  onBlock: null as () => void,
  onUnload: null as () => void,
  onDesktop: false
}

type PortalProps = typeof defaultPortalProps

const initialState = { mounted: false }

type PortalState = Readonly<typeof initialState>

class NewWindow extends React.PureComponent<PortalProps, PortalState> {
  private container: HTMLDivElement
  private window: Window

  constructor(props) {
    super(props)
    this.container = document.createElement('div')
    this.state = {
      mounted: false
    }
  }

  render() {
    if (!this.state.mounted) {
      return false
    }
    if (this.props.onDesktop) {
      return ReactDOM.createPortal(
        <OpenFinChrome showHeaderBar={false} close={this.closeWindow}>
          {this.props.children}
        </OpenFinChrome>,
        this.container
      )
    }

    return ReactDOM.createPortal(this.props.children, this.container)
  }

  componentDidMount() {
    this.openChild()
    this.setState({ mounted: true })
  }

  /**
   * Create the new window when NewWindow component mount.
   */
  openChild() {
    const { url, name, features, center, onDesktop } = this.props

    let left = 0
    let top = 0

    if (center === 'parent') {
      left = window.top.outerWidth / 2 + window.top.screenX - features.width / 2
      top = window.top.outerHeight / 2 + window.top.screenY - features.height / 2
    } else if (center === 'screen') {
      const screenLeft = window.screenLeft
      const screenTop = window.screenTop

      const width = window.innerWidth
        ? window.innerWidth
        : document.documentElement.clientWidth
          ? document.documentElement.clientWidth
          : screen.width
      const height = window.innerHeight
        ? window.innerHeight
        : document.documentElement.clientHeight
          ? document.documentElement.clientHeight
          : screen.height

      left = width / 2 - features.width / 2 + screenLeft
      top = height / 2 - features.height / 2 + screenTop
    }

    if (onDesktop) {
      const win = new fin.desktop.Window(
        {
          name,
          url,
          defaultTop: top,
          defaultLeft: left,
          autoShow: true,
          frame: false
        },
        () => {
          this.window = win.getNativeWindow()
          this.injectIntoWindow()
        },
        error => {
          console.log('Error creating window:', error)
        }
      )
    } else {
      this.window = window.open(
        url,
        name,
        toWindowFeatures({
          ...features,
          left,
          top
        })
      )
      this.injectIntoWindow()
    }
  }

  injectIntoWindow() {
    const { onBlock, title } = this.props
    // Check if the new window was succesfully opened.
    if (this.window) {
      this.window.document.title = title
      this.window.document.body.appendChild(this.container)

      // Update component to allow event handlers
      this.forceUpdate()

      // If specified, copy styles from parent window's document.
      if (this.props.copyStyles) {
        setTimeout(() => copyStyles(document, this.window.document), 0)
      }

      // Release anything bound to this component before the new window unload.
      this.window.addEventListener('beforeunload', () => this.release())
    } else {
      // Handle error on opening of new window.
      if (onBlock) {
        onBlock.call(null)
      } else {
        console.warn('A new window could not be opened. Maybe it was blocked.')
      }
{'fraction_non_alphanumeric': 0.10876705406771096, 'fraction_numerical': 0.004295098534613441, 'mean_word_length': 2.1897663174858986, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 24, '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': '5269547', 'n_tokens_mistral': 2284, 'n_tokens_neox': 2217, 'n_words': 580}