Skip to content

Commit

Permalink
Add non-raising clipboard getter
Browse files Browse the repository at this point in the history
  • Loading branch information
icy-arctic-fox committed Jul 20, 2019
1 parent 402687e commit 4a9c2bc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
22 changes: 22 additions & 0 deletions src/espresso/window.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,17 @@ module Espresso
String.new(chars)
end

# Retrieves the contents of the system clipboard,
# if it contains or is convertible to a UTF-8 encoded string.
# If the clipboard is empty or if its contents cannot be converted,
# nil is returned..
#
# Possible errors that could be raised are: `NotInitializedError` and `PlatformError`.
def clipboard?
chars = LibGLFW.get_clipboard_string(@pointer)
chars ? String.new(chars) : nil
end

# Sets the contents of the system clipboard,
# to the specified UTF-8 encoded *string*.
#
Expand Down Expand Up @@ -1092,6 +1103,17 @@ module Espresso
ErrorHandling.static_checked { LibGLFW.set_clipboard_string(nil, string) }
end

# Retrieves the contents of the system clipboard,
# if it contains or is convertible to a UTF-8 encoded string.
# If the clipboard is empty or if its contents cannot be converted,
# nil is returned..
#
# Possible errors that could be raised are: `NotInitializedError` and `PlatformError`.
def self.clipboard?
chars = LibGLFW.get_clipboard_string(nil)
chars ? String.new(chars) : nil
end

# Attempts to retrieve the monitor the full screen window is using.
# If the window isn't full screen, then nil is returned.
#
Expand Down
7 changes: 3 additions & 4 deletions tests/clipboard.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@ Espresso.run do
event.window.closing = true
when Espresso::Key::V
if event.mods == MODIFIER
begin
string = event.window.clipboard
if (string = Espresso::Window.clipboard?)
puts "Clipboard contains \"#{string}\""
rescue Espresso::FormatUnavailableError
else
puts "Clipboard does not contain a string"
end
end
when Espresso::Key::C
if event.mods == MODIFIER
string = "Hello GLFW World!"
event.window.clipboard = string
Espresso::Window.clipboard = string
puts "Setting clipboard to \"#{string}\""
end
end
Expand Down

0 comments on commit 4a9c2bc

Please sign in to comment.