//——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
//  THIS FILE IS GENERATED BY EASY BINDINGS, DO NOT MODIFY IT
//——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

import Cocoa

//——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
//   AutoLayoutIntSlider
//——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

final class AutoLayoutIntSlider : AutoLayoutBase_NSSlider {

  //····················································································································

  private var mWidth : Int

  //····················································································································

  init (width inWidth : Int, min inMin : Int, max inMax : Int, ticks inTickCount : Int) {
    self.mWidth = inWidth
    super.init (min: inMin, max: inMax, ticks: inTickCount)

    if inWidth < 0 {
      _ = self.expandableWidth ()
    }

    self.allowsTickMarkValuesOnly = true
  }

  //····················································································································

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

  //····················································································································

  override func sendAction (_ action : Selector?, to : Any?) -> Bool {
    let value = Int (self.doubleValue.rounded (.toNearestOrEven))
    _ = self.mValueController?.updateModel (withCandidateValue: value, windowForSheet: self.window)
    let r = super.sendAction (action, to: to)
    flushOutletEvents ()
    return r
  }

  //····················································································································

  override var intrinsicContentSize : NSSize {
    var s = super.intrinsicContentSize
    if self.mWidth >= 0 {
      s.width = CGFloat (self.mWidth)
    }
    return s
  }

  //····················································································································
  //  $value binding
  //····················································································································

  fileprivate func update (from object : EBReadOnlyProperty_Int) {
    switch object.selection {
    case .empty, .multiple :
      self.stringValue = "-"
      self.enable (fromValueBinding: false, self.enabledBindingController)
    case .single (let propertyValue) :
      self.doubleValue = Double (propertyValue)
      self.enable (fromValueBinding: true, self.enabledBindingController)
    }
  }

  //····················································································································

  private var mValueController : EBGenericReadWritePropertyController <Int>? = nil

  //····················································································································

  final func bind_value (_ object : EBReadWriteProperty_Int) -> Self {
    self.mValueController = EBGenericReadWritePropertyController <Int> (
      observedObject: object,
      callBack: { [weak self] in self?.update (from: object) }
    )
    return self
  }

  //····················································································································

  final func unbind_value () {
    self.mValueController?.unregister ()
    self.mValueController = nil
  }

  //····················································································································

}

//——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
