1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//! Clipboard controls.

use std::ffi::CString;

use crate::bind;

/// A text from the clipboard.
#[derive(Debug, PartialEq, Eq)]
pub struct ClipboardText {
    text: String,
}

impl ClipboardText {
    /// Get a clipboard text if exists.
    #[must_use]
    pub fn new() -> Option<Self> {
        let ptr = unsafe { bind::SDL_GetClipboardText() };
        if ptr.is_null() {
            return None;
        }
        let text = unsafe { CString::from_raw(ptr) }.into_string().ok()?;
        unsafe { bind::SDL_free(ptr.cast()) }
        Some(Self { text })
    }

    /// Returns a reference to the clipboard string .
    #[must_use]
    pub fn text(&self) -> &String {
        &self.text
    }

    /// Converts into `String`.
    #[must_use]
    pub fn into_string(self) -> String {
        self.text
    }
}