Signal-iOS/SignalUI/Views/GradientView.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

96 lines
3.2 KiB
Swift

//
// Copyright 2018 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
public class GradientView: UIView {
public override class var layerClass: AnyClass { CAGradientLayer.self }
public var gradientLayer: CAGradientLayer { layer as! CAGradientLayer }
public var colors: [UIColor] {
didSet {
updateGradientColors()
}
}
public var locations: [CGFloat]? {
didSet {
updateGradientLocations()
}
}
public convenience init(from fromColor: UIColor, to toColor: UIColor) {
self.init(colors: [ fromColor, toColor ])
}
public required init(colors: [UIColor], locations: [CGFloat]? = nil) {
self.colors = colors
self.locations = locations
super.init(frame: .zero)
updateGradientColors()
updateGradientLocations()
}
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func updateGradientColors() {
gradientLayer.colors = colors.map { $0.cgColor }
}
private func updateGradientLocations() {
if let locations = locations, !locations.isEmpty {
gradientLayer.locations = locations.map { NSNumber(value: $0) }
} else {
gradientLayer.locations = nil
}
}
/// Sets the `startPoint` and `endPoint` of the layer to reflect an angle in degrees
/// where 0° starts at 12 o'clock and proceeds in a clockwise direction.
public func setAngle(_ angle: UInt32) {
// While design provides gradients with 0° at 12 o'clock, core animation's
// coordinate system works with 0° at 3 o'clock moving in a counter clockwise
// direction. We need to convert the provided angle accordingly before
// calculating the gradient's start and end points.
let caAngle =
(360 - angle) // Invert to counter clockwise direction
+ 90 // Rotate 90° counter clockwise to shift the start from 3 o'clock to 12 o'clock
let radians = CGFloat(caAngle) * .pi / 180.0
// (x,y) in terms of the signed unit circle
var endPoint = CGPoint(x: cos(radians), y: sin(radians))
// extrapolate to signed unit square
if abs(endPoint.x) > abs(endPoint.y) {
endPoint.x = endPoint.x > 0 ? 1 : -1
endPoint.y = endPoint.x * tan(radians)
} else {
endPoint.y = endPoint.y > 0 ? 1 : -1
endPoint.x = endPoint.y / tan(radians)
}
// The signed unit square is a coordinate space from:
// (-1,-1) to (1,1), but the gradient coordinate space
// ranges from (0,0) to (1,1) with 0 being the top
// left. Convert each point accordingly to calculate
// the final points.
func convertPointToGradientSpace(_ point: CGPoint) -> CGPoint {
return CGPoint(
x: (point.x + 1) * 0.5,
y: 1.0 - (point.y + 1) * 0.5
)
}
// The start point will always be at the opposite side of the signed unit square.
gradientLayer.startPoint = convertPointToGradientSpace(CGPoint(x: -endPoint.x, y: -endPoint.y))
gradientLayer.endPoint = convertPointToGradientSpace(endPoint)
}
}