Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 18 additions & 22 deletions Sources/OpenGestures/Event/Event.swift
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
public import OpenCoreGraphicsShims
//
// Event.swift
// OpenGestures
//
// Audited for 9126.1.5
// Status: Complete

// MARK: - Event

/// A protocol representing an input event.
public protocol Event: Sendable {
public protocol Event: Identifiable {
var id: EventID { get }
var phase: EventPhase { get }
var timestamp: Timestamp { get }
}

/// A spatial event with a location.
public protocol SpatialEvent: Event {
var location: CGPoint { get }
}
// MARK: - Never + Event

/// A scroll event with delta values.
public protocol ScrollEvent: Event {
var delta: CGVector { get }
var acceleratedDelta: CGVector { get }
extension Never: Event {
public var id: EventID { fatalError() }
public var phase: EventPhase { fatalError() }
}

// MARK: - EventID

public struct EventID: Hashable, Sendable, CustomStringConvertible {
public var rawValue: Int

public init(rawValue: Int) {
self.rawValue = rawValue
}
// MARK: - EventPhase

public var description: String {
"EventID(\(rawValue))"
}
public enum EventPhase: Hashable, Sendable {
case began
case active
case ended
case failed
}
20 changes: 20 additions & 0 deletions Sources/OpenGestures/Event/EventID.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// EventID.swift
// OpenGestures
//
// Audited for 9126.1.5
// Status: Complete

// MARK: - EventID

public struct EventID: Hashable, CustomStringConvertible, Sendable {
public let rawValue: Int

public init(rawValue: Int) {
self.rawValue = rawValue
}

public var description: String {
rawValue.description
}
}
9 changes: 0 additions & 9 deletions Sources/OpenGestures/Event/EventPhase.swift

This file was deleted.

37 changes: 27 additions & 10 deletions Sources/OpenGestures/Event/MouseEvent.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
//
// MouseEvent.swift
// OpenGestures
//
// Audited for 9126.1.5
// Status: Complete

public import OpenCoreGraphicsShims

// MARK: - MouseEvent

public struct MouseEvent: SpatialEvent, Sendable {
public var id: EventID
public var phase: EventPhase
public var timestamp: Timestamp
public var location: CGPoint
public var button: Button
public struct MouseEvent: SpatialEvent, NestedCustomStringConvertible, Sendable {
public let id: EventID
public let phase: EventPhase
public let timestamp: Timestamp
public let location: CGPoint
public let button: Button

public init(id: EventID, phase: EventPhase, timestamp: Timestamp, location: CGPoint, button: Button) {
self.id = id
Expand All @@ -17,9 +24,19 @@ public struct MouseEvent: SpatialEvent, Sendable {
self.button = button
}

public enum Button: Hashable, Sendable {
case primary
case secondary
case tertiary
// MARK: - Button

public struct Button: RawRepresentable, Sendable {
public let rawValue: Int

public init(rawValue: Int) {
self.rawValue = rawValue
}

public static let primary = Button(rawValue: 1)

public static let secondary = Button(rawValue: 2)

public static let tertiary = Button(rawValue: 3)
}
}
50 changes: 50 additions & 0 deletions Sources/OpenGestures/Event/ScrollEvent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// ScrollEvent.swift
// OpenGestures
//
// Audited for 9126.1.5
// Status: Complete

public import OpenCoreGraphicsShims

// MARK: - ScrollEvent

/// A scroll event with delta values.
public protocol ScrollEvent: SpatialEvent {
var delta: CGVector { get }
var acceleratedDelta: CGVector { get }
}

// MARK: - Never + ScrollEvent

extension Never: ScrollEvent {
public var delta: CGVector { fatalError() }
public var acceleratedDelta: CGVector { fatalError() }
}

// MARK: - ConcreteScrollEvent

public struct ConcreteScrollEvent: ScrollEvent, NestedCustomStringConvertible, Sendable {
public var id: EventID
public var phase: EventPhase
public var timestamp: Timestamp
public var location: CGPoint
public var delta: CGVector
public var acceleratedDelta: CGVector

public init(
id: EventID,
phase: EventPhase,
timestamp: Timestamp,
location: CGPoint,
delta: CGVector,
acceleratedDelta: CGVector
) {
self.id = id
self.phase = phase
self.timestamp = timestamp
self.location = location
self.delta = delta
self.acceleratedDelta = acceleratedDelta
}
}
15 changes: 15 additions & 0 deletions Sources/OpenGestures/Event/SpatialEvent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// SpatialEvent.swift
// OpenGestures
//
// Audited for 9126.1.5
// Status: Complete

// MARK: - SpatialEvent

/// A spatial event with a location.
public protocol SpatialEvent: Event, LocationContaining {}

// MARK: - Never + SpatialEvent

extension Never: SpatialEvent {}
9 changes: 8 additions & 1 deletion Sources/OpenGestures/Event/TouchEvent.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
//
// TouchEvent.swift
// OpenGestures
//
// Audited for 9126.1.5
// Status: Complete

public import OpenCoreGraphicsShims

// MARK: - TouchEvent

public struct TouchEvent: SpatialEvent, Sendable {
public struct TouchEvent: SpatialEvent, NestedCustomStringConvertible, Sendable {
public var id: EventID
public var phase: EventPhase
public var timestamp: Timestamp
Expand Down
27 changes: 17 additions & 10 deletions Sources/OpenGestures/Util/LocationContaining.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,31 @@
// Audited for 9126.1.5
// Status: Complete

#if canImport(Darwin)
import OpenCoreGraphicsShims
#else
package import OpenCoreGraphicsShims
#endif
public import OpenCoreGraphicsShims

package protocol LocationContaining {
// MARK: - LocationContaining

public protocol LocationContaining {
var location: CGPoint { get }
}

// MARK: - CGPoint + LocationContaining

extension CGPoint: LocationContaining {
package var location: CGPoint {
public var location: CGPoint {
self
}
}

// MARK: - Never + LocationContaining

extension Never: LocationContaining {
package var location: CGPoint {
_openGesturesUnreachableCode()
}
public var location: CGPoint { fatalError() }
}

// MARK: - IdentifiableLocation [WIP]

package struct IdentifiableLocation<ID> where ID: Hashable {
package var id: ID
package var location: CGPoint
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// EventIDCompatibilityTests.swift
// OpenGesturesCompatibilityTests

import Testing

struct EventIDCompatibilityTests {
@Test(arguments: [
(EventID(rawValue: 0), "0"),
(EventID(rawValue: 42), "42"),
(EventID(rawValue: -1), "-1"),
])
func description(_ id: EventID, _ expected: String) {
#expect(id.description == expected)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//
// MouseEventCompatibilityTests.swift
// OpenGesturesCompatibilityTests

import OpenCoreGraphicsShims
import Testing

struct MouseEventCompatibilityTests {
@Test
func initAndProperties() {
let event = MouseEvent(
id: EventID(rawValue: 7),
phase: .began,
timestamp: Timestamp(value: .seconds(1)),
location: CGPoint(x: 10, y: 20),
button: .primary
)
#expect(event.id == EventID(rawValue: 7))
#expect(event.phase == .began)
#expect(event.timestamp == Timestamp(value: .seconds(1)))
#expect(event.location == CGPoint(x: 10, y: 20))
#expect(event.button.rawValue == 1)
}

@Test(arguments: [
(MouseEvent.Button.primary, 1),
(.secondary, 2),
(.tertiary, 3),
])
func buttonRawValues(_ button: MouseEvent.Button, _ expectedRaw: Int) {
#expect(button.rawValue == expectedRaw)
#expect(MouseEvent.Button(rawValue: expectedRaw).rawValue == button.rawValue)
}

@Test(arguments: [
(EventID(rawValue: 42), EventPhase.began, MouseEvent.Button.primary, #"""
MouseEvent <42> { \#("")
id: 42
phase: began
timestamp: 1.0 seconds
location: (10.0, 20.0)
button: Button(rawValue: 1)
}
"""#),
(EventID(rawValue: 7), .active, .secondary, #"""
MouseEvent <7> { \#("")
id: 7
phase: active
timestamp: 1.0 seconds
location: (10.0, 20.0)
button: Button(rawValue: 2)
}
"""#),
(EventID(rawValue: 100), .ended, .tertiary, #"""
MouseEvent <100> { \#("")
id: 100
phase: ended
timestamp: 1.0 seconds
location: (10.0, 20.0)
button: Button(rawValue: 3)
}
"""#),
(EventID(rawValue: -1), .failed, .primary, #"""
MouseEvent <-1> { \#("")
id: -1
phase: failed
timestamp: 1.0 seconds
location: (10.0, 20.0)
button: Button(rawValue: 1)
}
"""#),
])
func description(
_ id: EventID,
_ phase: EventPhase,
_ button: MouseEvent.Button,
_ expected: String
) {
let event = MouseEvent(
id: id,
phase: phase,
timestamp: Timestamp(value: .seconds(1)),
location: CGPoint(x: 10, y: 20),
button: button
)
#expect(String(describing: event) == expected)
}
}
Loading
Loading