diff --git a/Sources/MapConductorCore/map/MapCameraPositionBase.swift b/Sources/MapConductorCore/map/MapCameraPositionBase.swift index c185278..322715e 100644 --- a/Sources/MapConductorCore/map/MapCameraPositionBase.swift +++ b/Sources/MapConductorCore/map/MapCameraPositionBase.swift @@ -83,7 +83,8 @@ public final class MapCameraPosition: MapCameraPositionProtocol { /// /// 明示された 0(zoom 0 = 世界全体、真北、真上から)と未指定を区別するために持つ。 /// ``MapViewState/moveCameraTo(cameraPosition:durationMillis:)`` は未指定の - /// プロパティだけを現在のカメラから引き継ぐ(``resolved(onto:)``)。 + /// プロパティだけを現在のカメラから引き継ぐ(``resolved(onto:)``)。`paddings` も + /// 辺ごとに同じ規則で引き継ぐ(``MapPaddings``)。 public let isZoomSpecified: Bool public let isBearingSpecified: Bool public let isTiltSpecified: Bool @@ -109,11 +110,14 @@ public final class MapCameraPosition: MapCameraPositionProtocol { self.fov = fov } - /// 未指定の `zoom` / `bearing` / `tilt` を `current` の値で埋めたカメラを返す。 + /// 未指定の `zoom` / `bearing` / `tilt` と、`paddings` の省略された辺を + /// `current` の値で埋めたカメラを返す。 /// - /// 明示されたプロパティは、0 であってもそのまま使う。 + /// 明示されたプロパティは、0 であってもそのまま使う。`paddings` 自体が nil の + /// ときはそのまま(辺ごとの引き継ぎは ``MapPaddings`` の省略された辺だけ)。 public func resolved(onto current: MapCameraPositionProtocol) -> MapCameraPosition { - if isZoomSpecified && isBearingSpecified && isTiltSpecified { + let partialPaddings = (paddings as? MapPaddings).flatMap { $0.isFullySpecified ? nil : $0 } + if isZoomSpecified && isBearingSpecified && isTiltSpecified && partialPaddings == nil { return self } return MapCameraPosition( @@ -121,7 +125,7 @@ public final class MapCameraPosition: MapCameraPositionProtocol { zoom: isZoomSpecified ? zoom : current.zoom, bearing: isBearingSpecified ? bearing : current.bearing, tilt: isTiltSpecified ? tilt : current.tilt, - paddings: paddings, + paddings: partialPaddings?.resolved(onto: current.paddings) ?? paddings, visibleRegion: visibleRegion, fov: fov ) diff --git a/Sources/MapConductorCore/map/MapPaddings.swift b/Sources/MapConductorCore/map/MapPaddings.swift index e0874d0..80b215e 100644 --- a/Sources/MapConductorCore/map/MapPaddings.swift +++ b/Sources/MapConductorCore/map/MapPaddings.swift @@ -7,26 +7,55 @@ public protocol MapPaddingsProtocol { var right: Double { get } } +/// 地図の端に空ける量(pt)。 +/// +/// 4 辺はそれぞれ省略できる。省略した辺は読むと 0 だが、 +/// ``MapViewState/moveCameraTo(cameraPosition:durationMillis:)`` に渡したときは +/// 状態が今持っているカメラの `paddings` の同じ辺を引き継ぐ +/// (``MapCameraPosition/resolved(onto:)``)。`MapPaddings(top: 300)` は +/// 「上だけ 300 にして、ほかの辺は今のまま」という意味になる。 +/// 明示された 0 は引き継がずにそのまま使う。指定されたかどうかは +/// ``isTopSpecified`` などで区別する。 open class MapPaddings: MapPaddingsProtocol { + /// 指定されなかったときは 0。指定されたかどうかは ``isTopSpecified`` で区別する。 public let top: Double + /// 指定されなかったときは 0。指定されたかどうかは ``isLeftSpecified`` で区別する。 public let left: Double + /// 指定されなかったときは 0。指定されたかどうかは ``isBottomSpecified`` で区別する。 public let bottom: Double + /// 指定されなかったときは 0。指定されたかどうかは ``isRightSpecified`` で区別する。 public let right: Double + public let isTopSpecified: Bool + public let isLeftSpecified: Bool + public let isBottomSpecified: Bool + public let isRightSpecified: Bool + + /// 4 辺すべてが明示されているか。 + public var isFullySpecified: Bool { + isTopSpecified && isLeftSpecified && isBottomSpecified && isRightSpecified + } + public init( - top: Double = 0.0, - left: Double = 0.0, - bottom: Double = 0.0, - right: Double = 0.0 + top: Double? = nil, + left: Double? = nil, + bottom: Double? = nil, + right: Double? = nil ) { - self.top = top - self.left = left - self.bottom = bottom - self.right = right + self.top = top ?? 0.0 + self.left = left ?? 0.0 + self.bottom = bottom ?? 0.0 + self.right = right ?? 0.0 + self.isTopSpecified = top != nil + self.isLeftSpecified = left != nil + self.isBottomSpecified = bottom != nil + self.isRightSpecified = right != nil } - public static let Zeros = MapPaddings() + public static let Zeros = MapPaddings(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0) + /// `MapPaddings` はそのまま返す(指定の有無も保たれる)。それ以外の + /// ``MapPaddingsProtocol`` は 4 辺とも指定されたものとして写す。 public static func from(paddings: MapPaddingsProtocol) -> MapPaddings { if let impl = paddings as? MapPaddings { return impl @@ -39,6 +68,19 @@ open class MapPaddings: MapPaddingsProtocol { ) } + /// 省略された辺を `current` の同じ辺で埋める。`current` が nil なら 0。 + /// + /// 4 辺とも指定済みなら自分自身を返す。 + public func resolved(onto current: MapPaddingsProtocol?) -> MapPaddings { + if isFullySpecified { return self } + return MapPaddings( + top: isTopSpecified ? top : (current?.top ?? 0.0), + left: isLeftSpecified ? left : (current?.left ?? 0.0), + bottom: isBottomSpecified ? bottom : (current?.bottom ?? 0.0), + right: isRightSpecified ? right : (current?.right ?? 0.0) + ) + } + public func hashCode() -> Int { var result = top.hashValue result = 31 * result + left.hashValue diff --git a/Tests/MapConductorCoreTests/PaddingsResolveTests.swift b/Tests/MapConductorCoreTests/PaddingsResolveTests.swift new file mode 100644 index 0000000..02fa2dc --- /dev/null +++ b/Tests/MapConductorCoreTests/PaddingsResolveTests.swift @@ -0,0 +1,84 @@ +import XCTest + +@testable import MapConductorCore + +/// `paddings` の省略された辺だけが現在のカメラの同じ辺から引き継がれ、 +/// 明示された 0 はそのまま使われることを確認する。 +final class PaddingsResolveTests: XCTestCase { + private let tokyo = GeoPoint(latitude: 35.6812, longitude: 139.7671) + private let osaka = GeoPoint(latitude: 34.7025, longitude: 135.4959) + + private var current: MapCameraPosition { + MapCameraPosition( + position: tokyo, zoom: 12, bearing: 30, tilt: 45, + paddings: MapPaddings(top: 10, left: 20, bottom: 30, right: 40) + ) + } + + private func sides(_ paddings: MapPaddingsProtocol?) -> [Double] { + guard let paddings else { return [] } + return [paddings.top, paddings.left, paddings.bottom, paddings.right] + } + + func testUnsetSidesComeFromTheCurrentCamera() { + let resolved = MapCameraPosition(position: osaka, zoom: 15, bearing: 0, tilt: 0, paddings: MapPaddings(top: 300)) + .resolved(onto: current) + XCTAssertEqual(sides(resolved.paddings), [300, 20, 30, 40]) + let paddings = MapPaddings.from(paddings: resolved.paddings!) + XCTAssertTrue(paddings.isFullySpecified) + } + + func testAllSidesUnsetCopiesEverySide() { + let resolved = MapCameraPosition(position: osaka, zoom: 15, bearing: 0, tilt: 0, paddings: MapPaddings()) + .resolved(onto: current) + XCTAssertEqual(sides(resolved.paddings), [10, 20, 30, 40]) + } + + func testExplicitZeroIsHonoured() { + let resolved = MapCameraPosition(position: osaka, zoom: 15, bearing: 0, tilt: 0, paddings: MapPaddings(top: 0, left: 0)) + .resolved(onto: current) + XCTAssertEqual(sides(resolved.paddings), [0, 0, 30, 40]) + } + + func testCurrentWithoutPaddingsFillsZero() { + let bare = MapCameraPosition(position: tokyo, zoom: 12, paddings: nil) + let resolved = MapCameraPosition(position: osaka, paddings: MapPaddings(right: 5)).resolved(onto: bare) + XCTAssertEqual(sides(resolved.paddings), [0, 0, 0, 5]) + } + + func testUnresolvedReadsZeroForUnsetSides() { + let paddings = MapPaddings(top: 300) + XCTAssertEqual(sides(paddings), [300, 0, 0, 0]) + XCTAssertTrue(paddings.isTopSpecified) + XCTAssertFalse(paddings.isLeftSpecified) + XCTAssertFalse(paddings.isFullySpecified) + XCTAssertTrue(MapPaddings.Zeros.isFullySpecified) + } + + func testFromAndCopyKeepUnsetSides() { + let partial = MapPaddings(bottom: 80) + XCTAssertFalse(MapPaddings.from(paddings: partial).isTopSpecified) + let camera = MapCameraPosition(position: osaka, zoom: 15, bearing: 0, tilt: 0, paddings: partial) + let copied = camera.copy(zoom: 16) + XCTAssertEqual(sides(copied.resolved(onto: current).paddings), [10, 20, 80, 40]) + } + + func testFastPathIsNotTakenWithAnUnsetSide() { + let camera = MapCameraPosition(position: osaka, zoom: 15, bearing: 0, tilt: 0, paddings: MapPaddings(top: 1)) + XCTAssertFalse(camera.resolved(onto: current) === camera) + let full = MapCameraPosition(position: osaka, zoom: 15, bearing: 0, tilt: 0, paddings: MapPaddings(top: 1, left: 2, bottom: 3, right: 4)) + XCTAssertTrue(full.resolved(onto: current) === full) + } + + func testDefaultPaddingsAreFullySpecifiedZeros() { + // `paddings` を渡さなければ今までどおり 0 で、引き継がない。 + let resolved = MapCameraPosition(position: osaka, zoom: 15, bearing: 0, tilt: 0).resolved(onto: current) + XCTAssertEqual(sides(resolved.paddings), [0, 0, 0, 0]) + } + + func testMoveCameraToCopiesUnsetSidesFromState() { + let state = MapViewState(initialCameraPosition: current) + state.moveCameraTo(cameraPosition: MapCameraPosition(position: osaka, paddings: MapPaddings(left: 7))) + XCTAssertEqual(sides(state.cameraPosition.paddings), [10, 7, 30, 40]) + } +} diff --git a/api/MapConductorCore.api.swift b/api/MapConductorCore.api.swift index 1f1feec..c662ed6 100644 --- a/api/MapConductorCore.api.swift +++ b/api/MapConductorCore.api.swift @@ -1343,9 +1343,17 @@ open class MapPaddings : MapConductorCore::MapPaddingsProtocol { final public let left: Swift::Double final public let bottom: Swift::Double final public let right: Swift::Double - public init(top: Swift::Double = 0.0, left: Swift::Double = 0.0, bottom: Swift::Double = 0.0, right: Swift::Double = 0.0) + final public let isTopSpecified: Swift::Bool + final public let isLeftSpecified: Swift::Bool + final public let isBottomSpecified: Swift::Bool + final public let isRightSpecified: Swift::Bool + public var isFullySpecified: Swift::Bool { + get + } + public init(top: Swift::Double? = nil, left: Swift::Double? = nil, bottom: Swift::Double? = nil, right: Swift::Double? = nil) public static let Zeros: MapConductorCore::MapPaddings public static func from(paddings: any MapConductorCore::MapPaddingsProtocol) -> MapConductorCore::MapPaddings + public func resolved(onto current: (any MapConductorCore::MapPaddingsProtocol)?) -> MapConductorCore::MapPaddings public func hashCode() -> Swift::Int @objc deinit }