Skip to content

Commit

Permalink
Add Clipboard handling (#8)
Browse files Browse the repository at this point in the history
* add getClipboardText()

* Add Clipboard protocol

* Provide SDLClipboard implementation

* Test clipboard interaction

* Remove unused function

Co-authored-by: Christian Treffs <[email protected]>
  • Loading branch information
UnGast and ctreffs authored Nov 10, 2021
1 parent 7be4c41 commit d4676a3
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 2 deletions.
17 changes: 17 additions & 0 deletions Sources/FirebladePAL/Clipboard.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Clipboard.swift
// Fireblade PAL
//
// Copyright © 2018-2021 Fireblade Team. All rights reserved.
// Licensed under MIT License. See LICENSE file for details.

public protocol Clipboard {
/// Get UTF-8 text from the clipboard.
func getText() throws -> String

/// Put UTF-8 text into the clipboard.
func setText(_ text: String) throws

/// Query whether the clipboard exists and contains a non-empty text string.
func hasText() -> Bool
}
14 changes: 14 additions & 0 deletions Sources/FirebladePAL/Platform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,18 @@ public enum Platform {
#endif
}
}

public static var clipboard: Clipboard {
switch implementation {
#if FRB_PLATFORM_SDL
case .sdl:
return SDLClipboard()
#endif

#if FRB_PLATFORM_APPL
case .apple:
fatalError("not implemented")
#endif
}
}
}
34 changes: 34 additions & 0 deletions Sources/FirebladePAL/Platform/SDL/SDLClipboard.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// SDLClipboard.swift
// Fireblade PAL
//
// Copyright © 2018-2021 Fireblade Team. All rights reserved.
// Licensed under MIT License. See LICENSE file for details.

#if FRB_PLATFORM_SDL

@_implementationOnly import SDL2

public struct SDLClipboard: Clipboard {
init() {}

public func getText() throws -> String {
guard let pText = SDL_GetClipboardText() else {
throw SDLError()
}
defer { SDL_free(pText) }
return String(cString: pText)
}

public func setText(_ text: String) throws {
if SDL_SetClipboardText(text) < 0 {
throw SDLError()
}
}

public func hasText() -> Bool {
SDL_HasClipboardText() == SDL_TRUE
}
}

#endif
16 changes: 14 additions & 2 deletions Tests/FirebladePALTests/FirebladePALTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,29 @@
import XCTest

final class FirebladePALTests: XCTestCase {
func testPlatformSetup() throws {
override func setUp() {
super.setUp()
Platform.initialize()
}

override class func tearDown() {
super.tearDown()
Platform.quit()
}

func testPlatformSetup() throws {
#if FRB_ENABLE_PLATFORM_SDL
XCTAssertEqual(Platform.implementation, .sdl)
#endif

#if FRB_ENABLE_PLATFORM_APPL
XCTAssertEqual(Platform.implementation, .apple)
#endif
}

Platform.quit()
func testClipboard() throws {
try Platform.clipboard.setText("Hello World!")
XCTAssertTrue(Platform.clipboard.hasText())
XCTAssertEqual(try Platform.clipboard.getText(), "Hello World!")
}
}
1 change: 1 addition & 0 deletions Tests/FirebladePALTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// `swift test --generate-linuxmain`
// to regenerate.
static let __allTests__FirebladePALTests = [
("testClipboard", testClipboard),
("testPlatformSetup", testPlatformSetup),
]
}
Expand Down

0 comments on commit d4676a3

Please sign in to comment.