summaryrefslogblamecommitdiffstats
path: root/winit/src/clipboard.rs
blob: a1496fa310cc23397551ed1f45a2f4a9195a2475 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
                         
 


                                                                   







                                           

                
                                                         
                                                                 

                                                                           
                 

                                           

                           
     
 







                                                                        

                                                               



                                                                 
     
 

                                                            



                                                                            
                                                                      


                                    

         

























                                                                        
 
 
                                           




                                           
                             
     







                                                   
 
//! Access the clipboard.

/// A buffer for short-term storage and transfer within and between
/// applications.
#[allow(missing_debug_implementations)]
pub struct Clipboard {
    state: State,
}

enum State {
    Connected(window_clipboard::Clipboard),
    Unavailable,
}

impl Clipboard {
    /// Creates a new [`Clipboard`] for the given window.
    pub fn connect(window: &winit::window::Window) -> Clipboard {
        #[allow(unsafe_code)]
        let state = unsafe { window_clipboard::Clipboard::connect(window) }
            .ok()
            .map(State::Connected)
            .unwrap_or(State::Unavailable);

        Clipboard { state }
    }

    /// Creates a new [`Clipboard`] that isn't associated with a window.
    /// This clipboard will never contain a copied value.
    pub fn unconnected() -> Clipboard {
        Clipboard {
            state: State::Unavailable,
        }
    }

    /// Reads the current content of the [`Clipboard`] as text.
    pub fn read(&self) -> Option<String> {
        match &self.state {
            State::Connected(clipboard) => clipboard.read().ok(),
            State::Unavailable => None,
        }
    }

    /// Writes the given text contents to the [`Clipboard`].
    pub fn write(&mut self, contents: String) {
        match &mut self.state {
            State::Connected(clipboard) => match clipboard.write(contents) {
                Ok(()) => {}
                Err(error) => {
                    log::warn!("error writing to clipboard: {error}");
                }
            },
            State::Unavailable => {}
        }
    }

    /// Reads the current content of primary as text.
    pub fn read_primary(&self) -> Option<String> {
        match &self.state {
            State::Connected(clipboard) => {
                clipboard.read_primary().and_then(|r| r.ok())
            }
            State::Unavailable => None,
        }
    }

    /// Writes the given text contents to primary.
    pub fn write_primary(&mut self, contents: String) {
        match &mut self.state {
            State::Connected(clipboard) => {
                match clipboard.write_primary(contents) {
                    Some(Ok(())) => {}
                    Some(Err(error)) => {
                        log::warn!("error writing to primary: {error}");
                    }
                    None => {}
                }
            }
            State::Unavailable => {}
        }
    }
}

impl crate::core::Clipboard for Clipboard {
    fn read(&self) -> Option<String> {
        self.read()
    }

    fn write(&mut self, contents: String) {
        self.write(contents);
    }

    fn read_primary(&self) -> Option<String> {
        self.read_primary()
    }

    fn write_primary(&mut self, contents: String) {
        self.write_primary(contents);
    }
}