<commit_before>/**
 * <NAME> - http://taewon.de - @hataewon
 * See LICENSE
 */

import Cocoa
import PureLayout

enum WorkspaceBarLocation {
  case top
  case right
  case bottom
  case left
}

struct MainViewConfig {
  let minimumSize: CGSize
}

class Workspace: NSView {

  private(set) var isBarVisible = true {
    didSet {
      self.relayout()
    }
  }

  private let bars: [WorkspaceBarLocation: WorkspaceBar]

  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

  // MARK: - API
  let mainView: NSView
  let mainViewConfig: MainViewConfig

  init(mainView: NSView, config: MainViewConfig = MainViewConfig(minimumSize: CGSize(width: 100, height: 100))) {
    self.mainView = mainView
    self.mainViewConfig = config

    self.bars = [
      .top: WorkspaceBar(location: .top),
      .right: WorkspaceBar(location: .right),
      .bottom: WorkspaceBar(location: .bottom),
      .left: WorkspaceBar(location: .left)
    ]

    super.init(frame: CGRect.zero)
    self.translatesAutoresizingMaskIntoConstraints = false

    self.relayout()
  }

  func append(tool tool: WorkspaceTool, location: WorkspaceBarLocation) {
    self.bars[location]?.append(tool: tool)
  }

  func toggleAllTools() {
    self.isBarVisible = !self.isBarVisible
  }

  func toggleToolButtons() {
    self.bars.values.forEach { $0.isButtonVisible = !$0.isButtonVisible }
  }
}

// MARK: - Layout
extension Workspace {

  private func relayout() {
    // FIXME: I did not investigate why toggleButtons does not work correctly if we store all constraints in an array
    // and remove them here by self.removeConstraints(${all constraints). The following seems to work...
    self.subviews.forEach { $0.removeAllConstraints() }
    self.removeAllSubviews()

    let mainView = self.mainView
    self.addSubview(mainView)
    mainView.autoSetDimension(.Width, toSize: self.mainViewConfig.minimumSize.width, relation: .GreaterThanOrEqual)
    mainView.autoSetDimension(.Height, toSize: self.mainViewConfig.minimumSize.height, relation: .GreaterThanOrEqual)

    guard self.isBarVisible else {
      mainView.autoPinEdgesToSuperviewEdges()
      return
    }

    let topBar = self.bars[.top]!
    let rightBar = self.bars[.right]!
    let bottomBar = self.bars[.bottom]!
    let leftBar = self.bars[.left]!

    self.addSubview(topBar)
    self.addSubview(rightBar)
    self.addSubview(bottomBar)
    self.addSubview(leftBar)

    topBar.autoPinEdgeToSuperviewEdge(.Top)
    topBar.autoPinEdgeToSuperviewEdge(.Right)
    topBar.autoPinEdgeToSuperviewEdge(.Left)

    rightBar.autoPinEdge(.Top, toEdge: .Bottom, ofView: topBar)
    rightBar.autoPinEdgeToSuperviewEdge(.Right)
    rightBar.autoPinEdge(.Bottom, toEdge: .Top, ofView: bottomBar)
<commit_msg>GH-282 Restructure<commit_after>/**
 * <NAME> - http://taewon.de - @hataewon
 * See LICENSE
 */

import Cocoa
import PureLayout

enum WorkspaceBarLocation {
  case top
  case right
  case bottom
  case left
}

class Workspace: NSView {

  struct Config {
    let mainViewMinimumSize: CGSize
  }

  private(set) var isBarVisible = true {
    didSet {
      self.relayout()
    }
  }

  private let bars: [WorkspaceBarLocation: WorkspaceBar]

  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

  // MARK: - API
  let mainView: NSView
  let config: Config

  init(mainView: NSView, config: Config = Config(mainViewMinimumSize: CGSize(width: 100, height: 100))) {
    self.config = config
    self.mainView = mainView

    self.bars = [
      .top: WorkspaceBar(location: .top),
      .right: WorkspaceBar(location: .right),
      .bottom: WorkspaceBar(location: .bottom),
      .left: WorkspaceBar(location: .left)
    ]

    super.init(frame: CGRect.zero)
    self.translatesAutoresizingMaskIntoConstraints = false

    self.relayout()
  }

  func append(tool tool: WorkspaceTool, location: WorkspaceBarLocation) {
    self.bars[location]?.append(tool: tool)
  }

  func toggleAllTools() {
    self.isBarVisible = !self.isBarVisible
  }

  func toggleToolButtons() {
    self.bars.values.forEach { $0.isButtonVisible = !$0.isButtonVisible }
  }
}

// MARK: - Layout
extension Workspace {

  private func relayout() {
    // FIXME: I did not investigate why toggleButtons does not work correctly if we store all constraints in an array
    // and remove them here by self.removeConstraints(${all constraints). The following seems to work...
    self.subviews.forEach { $0.removeAllConstraints() }
    self.removeAllSubviews()

    let mainView = self.mainView
    self.addSubview(mainView)

    mainView.autoSetDimension(.Width, toSize: self.config.mainViewMinimumSize.width, relation: .GreaterThanOrEqual)
    mainView.autoSetDimension(.Height, toSize: self.config.mainViewMinimumSize.height, relation: .GreaterThanOrEqual)

    guard self.isBarVisible else {
      mainView.autoPinEdgesToSuperviewEdges()
      return
    }

    let topBar = self.bars[.top]!
    let rightBar = self.bars[.right]!
    let bottomBar = self.bars[.bottom]!
    let leftBar = self.bars[.left]!

    self.addSubview(topBar)
    self.addSubview(rightBar)
    self.addSubview(bottomBar)
    self.addSubview(leftBar)

    topBar.autoPinEdgeToSuperviewEdge(.Top)
    topBar.autoPinEdgeToSuperviewEdge(.Right)
    topBar.autoPinEdgeToSuperviewEdge(.Left)

    rightBar.autoPinEdge(.Top, toEdge: .Bottom, ofView: topBar)
    rightBar.autoPinEdgeToSuperviewEdge(.Right)
    rightBar.autoPinEdge(.Bottom, toEdge: .Top, ofView: bottomBar)

{'fraction_non_alphanumeric': 0.10771180304127444, 'fraction_numerical': 0.0038015930485155685, 'mean_word_length': 3.5661157024793386, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5806162', 'n_tokens_mistral': 1839, 'n_tokens_neox': 1759, 'n_words': 387}