File tree Expand file tree Collapse file tree 5 files changed +80
-2
lines changed Expand file tree Collapse file tree 5 files changed +80
-2
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -68,4 +68,18 @@ public enum Platform {
68
68
#endif
69
69
}
70
70
}
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
+ }
71
85
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change 9
9
import XCTest
10
10
11
11
final class FirebladePALTests : XCTestCase {
12
- func testPlatformSetup( ) throws {
12
+ override func setUp( ) {
13
+ super. setUp ( )
13
14
Platform . initialize ( )
15
+ }
16
+
17
+ override class func tearDown( ) {
18
+ super. tearDown ( )
19
+ Platform . quit ( )
20
+ }
14
21
22
+ func testPlatformSetup( ) throws {
15
23
#if FRB_ENABLE_PLATFORM_SDL
16
24
XCTAssertEqual ( Platform . implementation, . sdl)
17
25
#endif
18
26
19
27
#if FRB_ENABLE_PLATFORM_APPL
20
28
XCTAssertEqual ( Platform . implementation, . apple)
21
29
#endif
30
+ }
22
31
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! " )
24
36
}
25
37
}
Original file line number Diff line number Diff line change 6
6
// `swift test --generate-linuxmain`
7
7
// to regenerate.
8
8
static let __allTests__FirebladePALTests = [
9
+ ( " testClipboard " , testClipboard) ,
9
10
( " testPlatformSetup " , testPlatformSetup) ,
10
11
]
11
12
}
You can’t perform that action at this time.
0 commit comments