Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a dummy clipboard to fix building for unsupported platforms. #50

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub enum Error {
/// This can be caused by a few conditions:
/// - Using the Primary clipboard with an older Wayland compositor (that doesn't support version 2)
/// - Using the Secondary clipboard on Wayland
/// - Using the clipboard on an unsupported platform
#[error("The selected clipboard is not supported with the current system configuration.")]
ClipboardNotSupported,

Expand Down
44 changes: 44 additions & 0 deletions src/dummy_clipboard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::Error;
#[cfg(feature = "image-data")]
use crate::ImageData;

#[derive(Default, Debug)]
pub struct DummyClipboard {}

impl DummyClipboard {
pub fn new() -> Result<Self, Error> {
Ok(DummyClipboard::default())
}

/// Fetches utf-8 text from the clipboard and returns it.
pub fn get_text(&mut self) -> Result<String, Error> {
Err(Error::ContentNotAvailable)
}

/// Places the text onto the clipboard. Any valid utf-8 string is accepted.
pub fn set_text(&mut self, _text: String) -> Result<(), Error> {
Err(Error::ClipboardNotSupported)
}

/// Fetches image data from the clipboard, and returns the decoded pixels.
///
/// Any image data placed on the clipboard with `set_image` will be possible read back, using
/// this function. However it's of not guaranteed that an image placed on the clipboard by any
/// other application will be of a supported format.
#[cfg(feature = "image-data")]
pub fn get_image(&mut self) -> Result<ImageData<'static>, Error> {
Err(Error::ContentNotAvailable)
}

/// Places an image to the clipboard.
///
/// The chosen output format, depending on the platform is the following:
///
/// - On macOS: `NSImage` object
/// - On Linux: PNG, under the atom `image/png`
/// - On Windows: In order of priority `CF_DIB` and `CF_BITMAP`
#[cfg(feature = "image-data")]
pub fn set_image(&mut self, _image: ImageData) -> Result<(), Error> {
Err(Error::ClipboardNotSupported)
}
}
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,27 @@ pub mod windows_clipboard;
#[cfg(target_os = "macos")]
pub mod osx_clipboard;

#[cfg(not(any(
all(unix, not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))),
windows,
target_os = "macos"
)))]
pub mod dummy_clipboard; // Unsupported platforms

#[cfg(all(unix, not(any(target_os = "macos", target_os = "android", target_os = "emscripten")),))]
type PlatformClipboard = common_linux::LinuxClipboard;
#[cfg(windows)]
type PlatformClipboard = windows_clipboard::WindowsClipboardContext;
#[cfg(target_os = "macos")]
type PlatformClipboard = osx_clipboard::OSXClipboardContext;

#[cfg(not(any(
all(unix, not(any(target_os = "macos", target_os = "android", target_os = "emscripten"))),
windows,
target_os = "macos"
)))]
type PlatformClipboard = dummy_clipboard::DummyClipboard; // Unsupported platforms

#[cfg(all(
unix,
not(any(target_os = "macos", target_os = "android", target_os = "emscripten")),
Expand Down