refactored disabling (disabled) as a helper to get/set isEnabled

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-07-18 08:46:15 -05:00
parent bc6c783e4e
commit b919cfd92b
8 changed files with 40 additions and 45 deletions

View File

@ -44,17 +44,12 @@ open class Control: UIControl, Handlerable, ViewProtocol, Resettable, UserInfoab
open var surface: Surface = .light { didSet { setNeedsUpdate() } } open var surface: Surface = .light { didSet { setNeedsUpdate() } }
/// Control is disabled or not /// Control is disabled or not
open var disabled: Bool = false { didSet { setNeedsUpdate(); isUserInteractionEnabled = !disabled } } open var disabled: Bool {
get { !isEnabled }
open override var state: UIControl.State { set {
get { if !isEnabled != newValue {
var state = super.state isEnabled = !newValue
if disabled {
state.insert(.disabled)
} else {
state.remove(.disabled)
} }
return state
} }
} }
@ -85,14 +80,7 @@ open class Control: UIControl, Handlerable, ViewProtocol, Resettable, UserInfoab
} }
/// Override to deal with setNeedsUpdate() /// Override to deal with setNeedsUpdate()
open override var isEnabled: Bool { open override var isEnabled: Bool { didSet { setNeedsUpdate(); isUserInteractionEnabled = isEnabled } }
get { !disabled }
set {
if disabled != !newValue {
disabled = !newValue
}
}
}
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Initializers // MARK: - Initializers

View File

@ -270,7 +270,7 @@ open class SelectorItemBase<Selector: SelectorControlable>: Control, Errorable,
} }
open override func updateAccessibilityLabel() { open override func updateAccessibilityLabel() {
if !accessibilityTraits.contains(.notEnabled) && disabled { if !accessibilityTraits.contains(.notEnabled) && !isEnabled {
accessibilityTraits.insert(.notEnabled) accessibilityTraits.insert(.notEnabled)
} else { } else {
accessibilityTraits.remove(.notEnabled) accessibilityTraits.remove(.notEnabled)

View File

@ -34,7 +34,16 @@ open class View: UIView, Handlerable, ViewProtocol, Resettable, UserInfoable {
open var surface: Surface = .light { didSet { setNeedsUpdate() } } open var surface: Surface = .light { didSet { setNeedsUpdate() } }
/// Whether this object is disabled or not /// Whether this object is disabled or not
open var disabled: Bool = false { didSet { setNeedsUpdate(); isUserInteractionEnabled = !disabled } } open var disabled: Bool {
get { !isEnabled }
set {
if !isEnabled != newValue {
isEnabled = !newValue
}
}
}
open var isEnabled: Bool = true { didSet { setNeedsUpdate(); isUserInteractionEnabled = isEnabled } }
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Initializers // MARK: - Initializers

View File

@ -55,8 +55,6 @@ open class ButtonBase: UIButton, Buttonable, Handlerable, ViewProtocol, Resettab
open var surface: Surface = .light { didSet { setNeedsUpdate() }} open var surface: Surface = .light { didSet { setNeedsUpdate() }}
open var disabled: Bool = false { didSet { setNeedsUpdate(); isUserInteractionEnabled = !disabled } }
open var userInfo = [String: Primitive]() open var userInfo = [String: Primitive]()
public var touchUpInsideCount: Int = 0 public var touchUpInsideCount: Int = 0
@ -80,28 +78,18 @@ open class ButtonBase: UIButton, Buttonable, Handlerable, ViewProtocol, Resettab
} }
} }
/// Override to deal with setNeedsUpdate() /// Whether this object is disabled or not
open override var isEnabled: Bool { open var disabled: Bool {
get { !disabled } get { !isEnabled }
set { set {
if disabled != !newValue { if !isEnabled != newValue {
disabled = !newValue isEnabled = !newValue
} }
} }
} }
open override var state: UIControl.State { open override var isEnabled: Bool { didSet { setNeedsUpdate(); isUserInteractionEnabled = isEnabled } }
get {
var state = super.state
if disabled {
state.insert(.disabled)
} else {
state.remove(.disabled)
}
return state
}
}
open var textStyle: TextStyle { .defaultStyle } open var textStyle: TextStyle { .defaultStyle }
open var textColor: UIColor { .black } open var textColor: UIColor { .black }
@ -171,7 +159,7 @@ open class ButtonBase: UIButton, Buttonable, Handlerable, ViewProtocol, Resettab
} }
open func updateAccessibilityLabel() { open func updateAccessibilityLabel() {
if !accessibilityTraits.contains(.notEnabled) && disabled { if !accessibilityTraits.contains(.notEnabled) && !isEnabled {
accessibilityTraits.insert(.notEnabled) accessibilityTraits.insert(.notEnabled)
} else { } else {
accessibilityTraits.remove(.notEnabled) accessibilityTraits.remove(.notEnabled)

View File

@ -29,8 +29,6 @@ open class Label: UILabel, Handlerable, ViewProtocol, Resettable, UserInfoable {
open var surface: Surface = .light { didSet { setNeedsUpdate() }} open var surface: Surface = .light { didSet { setNeedsUpdate() }}
open var disabled: Bool = false { didSet { setNeedsUpdate(); isUserInteractionEnabled = !disabled } }
open var attributes: [any LabelAttributeModel]? { didSet { setNeedsUpdate() }} open var attributes: [any LabelAttributeModel]? { didSet { setNeedsUpdate() }}
open var textStyle: TextStyle = .defaultStyle { didSet { setNeedsUpdate() }} open var textStyle: TextStyle = .defaultStyle { didSet { setNeedsUpdate() }}
@ -46,6 +44,18 @@ open class Label: UILabel, Handlerable, ViewProtocol, Resettable, UserInfoable {
} }
} }
/// Whether this object is disabled or not
open var disabled: Bool {
get { !isEnabled }
set {
if !isEnabled != newValue {
isEnabled = !newValue
}
}
}
open override var isEnabled: Bool { didSet { setNeedsUpdate(); isUserInteractionEnabled = isEnabled } }
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Configuration Properties // MARK: - Configuration Properties
//-------------------------------------------------- //--------------------------------------------------

View File

@ -248,7 +248,7 @@ open class RadioBoxItem: Control, Changeable {
} }
open override func updateAccessibilityLabel() { open override func updateAccessibilityLabel() {
if !accessibilityTraits.contains(.notEnabled) && disabled { if !accessibilityTraits.contains(.notEnabled) && !isEnabled {
accessibilityTraits.insert(.notEnabled) accessibilityTraits.insert(.notEnabled)
} else { } else {
accessibilityTraits.remove(.notEnabled) accessibilityTraits.remove(.notEnabled)

View File

@ -312,7 +312,7 @@ open class Toggle: Control, Changeable {
} }
open override func updateAccessibilityLabel() { open override func updateAccessibilityLabel() {
if !accessibilityTraits.contains(.notEnabled) && disabled { if !accessibilityTraits.contains(.notEnabled) && !isEnabled {
accessibilityTraits.insert(.notEnabled) accessibilityTraits.insert(.notEnabled)
} else { } else {
accessibilityTraits.remove(.notEnabled) accessibilityTraits.remove(.notEnabled)

View File

@ -166,7 +166,7 @@ open class Tooltip: Control, TooltipLaunchable {
} }
open override func updateAccessibilityLabel() { open override func updateAccessibilityLabel() {
if !accessibilityTraits.contains(.notEnabled) && disabled { if !accessibilityTraits.contains(.notEnabled) && !isEnabled {
accessibilityTraits.insert(.notEnabled) accessibilityTraits.insert(.notEnabled)
} else { } else {
accessibilityTraits.remove(.notEnabled) accessibilityTraits.remove(.notEnabled)