Skip to content

Commit

Permalink
Merge pull request Aloxaf#108 from etra0/feature/windows-clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
Aloxaf authored Aug 30, 2020
2 parents 792748e + 3c367a5 commit 6177d60
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
**/*.rs.bk
/.idea
/.idea
.vscode
28 changes: 28 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ log = "0.4.11"
[target.'cfg(target_os = "macos")'.dependencies]
pasteboard = "0.1.1"

[target.'cfg(target_os = "windows")'.dependencies]
clipboard-win = "4.0.2"
image = { version = "0.23", default-features = false, features = ["jpeg", "bmp", "jpeg_rayon"] }

[dependencies.image]
version = "0.23"
default-features = false
Expand Down
26 changes: 25 additions & 1 deletion src/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ use image::DynamicImage;
use structopt::StructOpt;
use syntect::easy::HighlightLines;
use syntect::util::LinesWithEndings;
#[cfg(target_os = "windows")]
use {
clipboard_win::{formats, Clipboard, Setter},
image::ImageOutputFormat,
};
#[cfg(target_os = "macos")]
use {image::ImageOutputFormat, pasteboard::Pasteboard};
#[cfg(target_os = "linux")]
Expand Down Expand Up @@ -49,7 +54,26 @@ pub fn dump_image_to_clipboard(image: &DynamicImage) -> Result<(), Error> {
Ok(())
}

#[cfg(not(any(target_os = "linux", target_os = "macos")))]
#[cfg(target_os = "windows")]
pub fn dump_image_to_clipboard(image: &DynamicImage) -> Result<(), Error> {
let mut temp: Vec<u8> = Vec::new();

// Convert the image to RGB without alpha because the clipboard
// of windows doesn't support it.
let image = DynamicImage::ImageRgb8(image.to_rgb());

image.write_to(&mut temp, ImageOutputFormat::Bmp)?;

let _clip =
Clipboard::new_attempts(10).map_err(|e| format_err!("Couldn't open clipboard: {}", e))?;

formats::Bitmap
.write_clipboard(&temp)
.map_err(|e| format_err!("Failed copy image: {}", e))?;
Ok(())
}

#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
pub fn dump_image_to_clipboard(_image: &DynamicImage) -> Result<(), Error> {
Err(format_err!(
"This feature hasn't been implemented for your system"
Expand Down

0 comments on commit 6177d60

Please sign in to comment.