Skip to content

Commit d4676a3

Browse files
UnGastctreffs
andauthored
Add Clipboard handling (#8)
* add getClipboardText() * Add Clipboard protocol * Provide SDLClipboard implementation * Test clipboard interaction * Remove unused function Co-authored-by: Christian Treffs <ctreffs@gmail.com>
1 parent 7be4c41 commit d4676a3

File tree

5 files changed

+80
-2
lines changed

5 files changed

+80
-2
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// Clipboard.swift
3+
// Fireblade PAL
4+
//
5+
// Copyright © 2018-2021 Fireblade Team. All rights reserved.
6+
// Licensed under MIT License. See LICENSE file for details.
7+
8+
public protocol Clipboard {
9+
/// Get UTF-8 text from the clipboard.
10+
func getText() throws -> String
11+
12+
/// Put UTF-8 text into the clipboard.
13+
func setText(_ text: String) throws
14+
15+
/// Query whether the clipboard exists and contains a non-empty text string.
16+
func hasText() -> Bool
17+
}

Sources/FirebladePAL/Platform.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,18 @@ public enum Platform {
6868
#endif
6969
}
7070
}
71+
72+
public static var clipboard: Clipboard {
73+
switch implementation {
74+
#if FRB_PLATFORM_SDL
75+
case .sdl:
76+
return SDLClipboard()
77+
#endif
78+
79+
#if FRB_PLATFORM_APPL
80+
case .apple:
81+
fatalError("not implemented")
82+
#endif
83+
}
84+
}
7185
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// SDLClipboard.swift
3+
// Fireblade PAL
4+
//
5+
// Copyright © 2018-2021 Fireblade Team. All rights reserved.
6+
// Licensed under MIT License. See LICENSE file for details.
7+
8+
#if FRB_PLATFORM_SDL
9+
10+
@_implementationOnly import SDL2
11+
12+
public struct SDLClipboard: Clipboard {
13+
init() {}
14+
15+
public func getText() throws -> String {
16+
guard let pText = SDL_GetClipboardText() else {
17+
throw SDLError()
18+
}
19+
defer { SDL_free(pText) }
20+
return String(cString: pText)
21+
}
22+
23+
public func setText(_ text: String) throws {
24+
if SDL_SetClipboardText(text) < 0 {
25+
throw SDLError()
26+
}
27+
}
28+
29+
public func hasText() -> Bool {
30+
SDL_HasClipboardText() == SDL_TRUE
31+
}
32+
}
33+
34+
#endif

Tests/FirebladePALTests/FirebladePALTests.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,29 @@
99
import XCTest
1010

1111
final class FirebladePALTests: XCTestCase {
12-
func testPlatformSetup() throws {
12+
override func setUp() {
13+
super.setUp()
1314
Platform.initialize()
15+
}
16+
17+
override class func tearDown() {
18+
super.tearDown()
19+
Platform.quit()
20+
}
1421

22+
func testPlatformSetup() throws {
1523
#if FRB_ENABLE_PLATFORM_SDL
1624
XCTAssertEqual(Platform.implementation, .sdl)
1725
#endif
1826

1927
#if FRB_ENABLE_PLATFORM_APPL
2028
XCTAssertEqual(Platform.implementation, .apple)
2129
#endif
30+
}
2231

23-
Platform.quit()
32+
func testClipboard() throws {
33+
try Platform.clipboard.setText("Hello World!")
34+
XCTAssertTrue(Platform.clipboard.hasText())
35+
XCTAssertEqual(try Platform.clipboard.getText(), "Hello World!")
2436
}
2537
}

Tests/FirebladePALTests/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// `swift test --generate-linuxmain`
77
// to regenerate.
88
static let __allTests__FirebladePALTests = [
9+
("testClipboard", testClipboard),
910
("testPlatformSetup", testPlatformSetup),
1011
]
1112
}

0 commit comments

Comments
 (0)