Skip to content

Commit

Permalink
Merge pull request #21 from p-x9/feature/tiled-layer
Browse files Browse the repository at this point in the history
Support `CAGradientLayer`
  • Loading branch information
p-x9 authored Apr 5, 2024
2 parents 169a8a5 + 39e2d28 commit ba914d4
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// CATiledLayer+ICodable.swift
//
//
// Created by p-x9 on 2024/04/05.
//
//

import QuartzCore

extension CATiledLayer {
public typealias Model = JCATiledLayer

open override class var codableTypeName: String {
String(reflecting: Model.self)
}
}
90 changes: 90 additions & 0 deletions Sources/SDCALayer/Model/Layer/JCATiledLayer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//
// JCATiledLayer.swift
//
//
// Created by p-x9 on 2024/04/05.
//
//

import Foundation
import QuartzCore
import KeyPathValue

open class JCATiledLayer: JCALayer {
public typealias Target = CATiledLayer

private enum CodingKeys: String, CodingKey {
case levelsOfDetail
case levelsOfDetailBias
case tileSize
}

open override class var targetTypeName: String {
String(reflecting: Target.self)
}

static private let propertyMap: PropertyMap<CATiledLayer, JCATiledLayer> = .init([
.init(\.levelsOfDetail, \.levelsOfDetail),
.init(\.levelsOfDetailBias, \.levelsOfDetailBias),
.init(\.tileSize, \.tileSize)
])

public var levelsOfDetail: Int?
public var levelsOfDetailBias: Int?
public var tileSize: CGSize?

public override init() {
super.init()
}

public required init(from decoder: Decoder) throws {
try super.init(from: decoder)

let container = try decoder.container(keyedBy: CodingKeys.self)

levelsOfDetail = try container.decodeIfPresent(Int.self, forKey: .levelsOfDetail)
levelsOfDetailBias = try container.decodeIfPresent(Int.self, forKey: .levelsOfDetailBias)

tileSize = try container.decodeIfPresent(CGSize.self, forKey: .tileSize)
}

public required convenience init(with object: CALayer) {
self.init()

applyProperties(with: object)
}

open override func encode(to encoder: Encoder) throws {
try super.encode(to: encoder)

var container = encoder.container(keyedBy: CodingKeys.self)

try container.encodeIfPresent(levelsOfDetail, forKey: .levelsOfDetail)
try container.encodeIfPresent(levelsOfDetailBias, forKey: .levelsOfDetailBias)
try container.encodeIfPresent(tileSize, forKey: .tileSize)
}

open override func applyProperties(to target: CALayer) {
super.applyProperties(to: target)

guard let target = target as? CATiledLayer else { return }

Self.propertyMap.apply(to: target, from: self)
}

open override func applyProperties(with target: CALayer) {
super.applyProperties(with: target)

guard let target = target as? CATiledLayer else { return }

Self.propertyMap.apply(to: self, from: target)
}

open override func convertToLayer() -> CALayer? {
let layer = CATiledLayer()

self.applyProperties(to: layer)

return layer
}
}

0 comments on commit ba914d4

Please sign in to comment.