Signal-iOS/SignalUI/Views/OWSFlatButton.swift
Evan Hahn 370ff654e7
Change license to AGPL
Change license to AGPL

This commit:

- Updates the `LICENSE` file

- Start every file with something like:

      // Copyright YEAR_FIRST_PUBLISHED Signal Messenger, LLC
      // SPDX-License-Identifier: AGPL-3.0-only

---

First, I removed existing license headers with this Ruby 3.1.2 script:

    require 'set'

    EXTENSIONS_TO_CHECK = Set['.h', '.hpp', '.cpp', '.m', '.mm', '.pch', '.swift']

    same = 0
    different = 0

    all_files = `git ls-files`.lines.map { |line| line.strip }
    all_files.each do |relative_path|
      if relative_path == 'Pods'
        next
      end

      unless EXTENSIONS_TO_CHECK.include? File.extname(relative_path)
        next
      end

      path = File.expand_path(relative_path)

      contents = File.read(path)
      new_contents = contents.sub(/\/\/\n\/\/  Copyright .*\n\/\/\n\n/, '')

      if contents == new_contents
        same += 1
      else
        different += 1
      end

      File.write(path, new_contents)
    end

    puts "updated #{different} file(s), left #{same} untouched"

I'm sure this script could be improved, but it worked well enough.

Then, I created `Scripts/lint/lint-license-headers` and ran it to auto-
fix a lot of files. This changed the mode of some files, but I think
that's actually desirable. For example,
`SignalServiceKit/src/Util/AppContext.m` previously had a mode of
`0755/-rwxr-xr-x`, and it's now `0644/-rw-r--r--`.

Then I fixed some stragglers and updated the precommit script.

See [a similar change in the Desktop app][0].

[0]: 8bfaf598af
2022-10-13 08:25:37 -05:00

261 lines
7.5 KiB
Swift

//
// Copyright 2017 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import Foundation
import SignalServiceKit
@objc
public class OWSFlatButton: UIView {
public let button: UIButton
private var pressedBlock: (() -> Void)?
private var upColor: UIColor?
private var downColor: UIColor?
@objc
public var cornerRadius: CGFloat {
get {
button.layer.cornerRadius
}
set {
button.layer.cornerRadius = newValue
button.clipsToBounds = newValue > 0
}
}
@objc
public override var accessibilityIdentifier: String? {
didSet {
guard let accessibilityIdentifier = self.accessibilityIdentifier else {
return
}
button.accessibilityIdentifier = "\(accessibilityIdentifier).button"
}
}
override public var backgroundColor: UIColor? {
willSet {
owsFailDebug("Use setBackgroundColors(upColor:) instead.")
}
}
public var titleEdgeInsets: UIEdgeInsets {
get {
return button.titleEdgeInsets
}
set {
button.titleEdgeInsets = newValue
}
}
public var contentEdgeInsets: UIEdgeInsets {
get {
return button.contentEdgeInsets
}
set {
button.contentEdgeInsets = newValue
}
}
public override var tintColor: UIColor! {
get {
return button.tintColor
}
set {
button.tintColor = newValue
}
}
@objc
public init() {
AssertIsOnMainThread()
button = UIButton(type: .custom)
super.init(frame: CGRect.zero)
createContent()
}
@available(*, unavailable, message: "use other constructor instead.")
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func createContent() {
self.addSubview(button)
button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
button.autoPinEdgesToSuperviewEdges()
}
@objc
public class func button(title: String,
font: UIFont,
titleColor: UIColor,
backgroundColor: UIColor,
width: CGFloat,
height: CGFloat,
target: Any,
selector: Selector) -> OWSFlatButton {
let button = OWSFlatButton()
button.setTitle(title: title,
font: font,
titleColor: titleColor )
button.setBackgroundColors(upColor: backgroundColor)
button.useDefaultCornerRadius()
button.setSize(width: width, height: height)
button.addTarget(target: target, selector: selector)
return button
}
@objc
public class func button(title: String,
titleColor: UIColor,
backgroundColor: UIColor,
width: CGFloat,
height: CGFloat,
target: Any,
selector: Selector) -> OWSFlatButton {
return OWSFlatButton.button(title: title,
font: fontForHeight(height),
titleColor: titleColor,
backgroundColor: backgroundColor,
width: width,
height: height,
target: target,
selector: selector)
}
@objc
public class func button(title: String,
font: UIFont,
titleColor: UIColor,
backgroundColor: UIColor,
target: Any,
selector: Selector) -> OWSFlatButton {
let button = OWSFlatButton()
button.setTitle(title: title,
font: font,
titleColor: titleColor )
button.setBackgroundColors(upColor: backgroundColor)
button.useDefaultCornerRadius()
button.addTarget(target: target, selector: selector)
return button
}
@objc
public class func fontForHeight(_ height: CGFloat) -> UIFont {
// Cap the "button height" at 40pt or button text can look
// excessively large.
let fontPointSize = round(min(40, height) * 0.45)
return UIFont.ows_semiboldFont(withSize: fontPointSize)
}
@objc
public class func heightForFont(_ font: UIFont) -> CGFloat {
font.lineHeight * 2.5
}
// MARK: Methods
@objc
public func setTitleColor(_ color: UIColor) {
button.setTitleColor(color, for: .normal)
}
@objc
public func setTitle(title: String? = nil, font: UIFont? = nil, titleColor: UIColor? = nil) {
title.map { button.setTitle($0, for: .normal) }
font.map { button.titleLabel?.font = $0 }
titleColor.map { setTitleColor($0) }
}
@objc
public func setAttributedTitle(_ title: NSAttributedString) {
button.setAttributedTitle(title, for: .normal)
}
@objc
public func setImage(_ image: UIImage) {
button.setImage(image, for: .normal)
}
@objc
public func setBackgroundColors(upColor: UIColor,
downColor: UIColor ) {
button.setBackgroundImage(UIImage(color: upColor), for: .normal)
button.setBackgroundImage(UIImage(color: downColor), for: .highlighted)
}
@objc
public func setBackgroundColors(upColor: UIColor) {
let downColor = upColor == .clear ? upColor : upColor.withAlphaComponent(0.7)
setBackgroundColors(upColor: upColor, downColor: downColor)
}
@objc
public func setSize(width: CGFloat, height: CGFloat) {
button.autoSetDimension(.width, toSize: width)
button.autoSetDimension(.height, toSize: height)
}
@objc
public func useDefaultCornerRadius() {
// To my eye, this radius tends to look right regardless of button size
// (within reason) or device size.
button.layer.cornerRadius = 5
button.clipsToBounds = true
}
@objc
public func setEnabled(_ isEnabled: Bool) {
button.isEnabled = isEnabled
}
@objc
public func addTarget(target: Any,
selector: Selector) {
button.addTarget(target, action: selector, for: .touchUpInside)
}
@objc
public func setPressedBlock(_ pressedBlock: @escaping () -> Void) {
guard self.pressedBlock == nil else { return }
self.pressedBlock = pressedBlock
}
@objc
internal func buttonPressed() {
pressedBlock?()
}
@objc
public func enableMultilineLabel() {
button.titleLabel?.numberOfLines = 0
button.titleLabel?.lineBreakMode = .byWordWrapping
button.titleLabel?.textAlignment = .center
}
@objc
public var font: UIFont? {
return button.titleLabel?.font
}
@objc
public func autoSetHeightUsingFont() {
guard let font = font else {
owsFailDebug("Missing button font.")
return
}
autoSetDimension(.height, toSize: Self.heightForFont(font))
}
override public var intrinsicContentSize: CGSize {
button.intrinsicContentSize
}
}