<commit_before>/* @flow */

import { CompositeDisposable } from 'atom'
import Delegate from './delegate'
import PanelDock from './dock'
import type { LinterMessage } from '../types'

class Panel {
  panel: ?PanelDock;
  element: HTMLElement;
  delegate: Delegate;
  deactivating: boolean;
  subscriptions: CompositeDisposable;
  showPanelConfig: boolean;
  hidePanelWhenEmpty: boolean;
  showPanelStateMessages: boolean;
  constructor() {
    this.panel = null
    this.element = document.createElement('div')
    this.delegate = new Delegate()
    this.deactivating = false
    this.subscriptions = new CompositeDisposable()
    this.showPanelStateMessages = false

    this.subscriptions.add(this.delegate)
    this.subscriptions.add(atom.config.observe('linter-ui-default.hidePanelWhenEmpty', (hidePanelWhenEmpty) => {
      this.hidePanelWhenEmpty = hidePanelWhenEmpty
      this.refresh()
    }))
    this.subscriptions.add(atom.workspace.onDidDestroyPaneItem(({ item: paneItem }) => {
      if (paneItem instanceof PanelDock && !this.deactivating) {
        this.panel = null
        atom.config.set('linter-ui-default.showPanel', false)
      }
    }))
    this.subscriptions.add(atom.config.observe('linter-ui-default.showPanel', (showPanel) => {
      this.showPanelConfig = showPanel
      this.refresh()
    }))
    this.subscriptions.add(atom.workspace.observeActivePaneItem(() => {
      this.showPanelStateMessages = !!this.delegate.filteredMessages.length
      this.refresh()
    }))
    this.activate()
  }
  async activate() {
    if (this.panel) {
      this.panel.dispose()
    }
    this.panel = new PanelDock(this.delegate)
    await atom.workspace.open(this.panel, {
      activatePane: false,
      activateItem: false,
      searchAllPanes: true,
    })
    this.refresh()
  }
  update(messages: Array<LinterMessage>): void {
    this.delegate.update(messages)
    this.showPanelStateMessages = !!this.delegate.filteredMessages.length
    this.refresh()
  }
  async refresh() {
    if (this.panel === null) {
      if (this.showPanelConfig) {
        await this.activate()
      }
      return
    }
    const paneContainer = atom.workspace.paneContainerForItem(this.panel)
    if (!paneContainer || paneContainer.location !== 'bottom') {
      return
    }
    if (
      (this.showPanelConfig) &&
      (!this.hidePanelWhenEmpty || this.showPanelStateMessages)
    ) {
      paneContainer.show()
    } else {
      paneContainer.hide()
    }
  }
  dispose() {
    this.deactivating = true
    if (this.panel) {
      this.panel.dispose()
    }
    this.subscriptions.dispose()
  }
}

module.exports = Panel<commit_msg>:racehorse: Defer panel activation<commit_after>/* @flow */

import { CompositeDisposable } from 'atom'
import Delegate from './delegate'
import PanelDock from './dock'
import type { LinterMessage } from '../types'

class Panel {
  panel: ?PanelDock;
  element: HTMLElement;
  delegate: Delegate;
  messages: Array<LinterMessage>;
  deactivating: boolean;
  subscriptions: CompositeDisposable;
  showPanelConfig: boolean;
  hidePanelWhenEmpty: boolean;
  showPanelStateMessages: boolean;
  activationTimer: number;
  constructor() {
    this.panel = null
    this.element = document.createElement('div')
    this.delegate = new Delegate()
    this.messages = []
    this.deactivating = false
    this.subscriptions = new CompositeDisposable()
    this.showPanelStateMessages = false

    this.subscriptions.add(this.delegate)
    this.subscriptions.add(atom.config.observe('linter-ui-default.hidePanelWhenEmpty', (hidePanelWhenEmpty) => {
      this.hidePanelWhenEmpty = hidePanelWhenEmpty
      this.refresh()
    }))
    this.subscriptions.add(atom.workspace.onDidDestroyPaneItem(({ item: paneItem }) => {
      if (paneItem instanceof PanelDock && !this.deactivating) {
        this.panel = null
        atom.config.set('linter-ui-default.showPanel', false)
      }
    }))
    this.subscriptions.add(atom.config.observe('linter-ui-default.showPanel', (showPanel) => {
      this.showPanelConfig = showPanel
      this.refresh()
    }))
    this.subscriptions.add(atom.workspace.observeActivePaneItem(() => {
      this.showPanelStateMessages = !!this.delegate.filteredMessages.length
      this.refresh()
    }))
    this.activationTimer = window.requestIdleCallback(() => {
      this.activate()
    })
  }
  async activate() {
    if (this.panel) {
      return
    }
    this.panel = new PanelDock(this.delegate)
    await atom.workspace.open(this.panel, {
      activatePane: false,
      activateItem: false,
      searchAllPanes: true,
    })
    this.update()
    this.refresh()
  }
  update(newMessages: ?Array<LinterMessage> = null): void {
    if (newMessages) {
      this.messages = newMessages
    }
    this.delegate.update(this.messages)
    this.showPanelStateMessages = !!this.delegate.filteredMessages.length
    this.refresh()
  }
  async refresh() {
    if (this.panel === null) {
      if (this.showPanelConfig) {
        await this.activate()
      }
      return
    }
    const paneContainer = atom.workspace.paneContainerForItem(this.panel)
    if (!paneContainer || paneContainer.location !== 'bottom') {
      return
    }
    if (
      (this.showPanelConfig) &&
      (!this.hidePanelWhenEmpty || this.showPanelStateMessages)
    ) {
      paneContainer.show()
    } else {
      paneContainer.hide()
    }
  }
  dispose() {
    this.deactivating = true
    if (this.panel) {
      this.panel.dispose()
    }
    this.subscriptions.dispose()
    window.cancelIdleCallback(this.activationTimer)
  }
}

module.exports = Panel
{'fraction_non_alphanumeric': 0.12444206391715765, 'fraction_numerical': 0.0, 'mean_word_length': 3.1191176470588236, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 15, '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': '1955593', 'n_tokens_mistral': 1739, 'n_tokens_neox': 1706, 'n_words': 292}