-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
423 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
tao: patch | ||
--- | ||
|
||
Add `clipboard` api exposing `read_text` and `write_text`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//! The `Clipboard` struct and associated types. | ||
//! | ||
//! ## Platform-specific | ||
//! | ||
//! - **Android / iOS:** Unsupported | ||
//! | ||
//! ```rust,ignore | ||
//! let mut cliboard = Clipboard::new(); | ||
//! cliboard.write_text("This is injected from tao!!!") | ||
//! let content = cliboard.read_text(); | ||
//! ``` | ||
//! | ||
|
||
use crate::platform_impl::Clipboard as ClipboardPlatform; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
/// Object that allows you to access the `Clipboard` instance. | ||
pub struct Clipboard(ClipboardPlatform); | ||
|
||
impl Clipboard { | ||
/// Creates a new `Clipboard` instance. | ||
/// | ||
/// ## Platform-specific | ||
/// | ||
/// - **Android / iOS:** Unsupported | ||
pub fn new() -> Self { | ||
Self::default() | ||
} | ||
|
||
/// Writes the text into the clipboard as plain text. | ||
/// | ||
/// ## Platform-specific | ||
/// | ||
/// - **Android / iOS:** Unsupported | ||
pub fn write_text(&mut self, s: impl AsRef<str>) { | ||
self.0.write_text(s); | ||
} | ||
|
||
/// The content in the clipboard as plain text. | ||
/// | ||
/// ## Platform-specific | ||
/// | ||
/// - **Android / iOS:** Unsupported | ||
pub fn read_text(&self) -> Option<String> { | ||
self.0.read_text() | ||
} | ||
} | ||
|
||
/// Identifier of a clipboard format. | ||
pub(crate) type FormatId = &'static str; | ||
|
||
/// Object that allows you to access the `ClipboardFormat`. | ||
#[derive(Debug, Clone)] | ||
pub(crate) struct ClipboardFormat { | ||
pub(crate) identifier: FormatId, | ||
pub(crate) data: Vec<u8>, | ||
} | ||
|
||
// todo add more formats | ||
impl ClipboardFormat { | ||
#[cfg(any(target_os = "macos", target_os = "ios"))] | ||
pub const TEXT: &'static str = "public.utf8-plain-text"; | ||
#[cfg(any(target_os = "windows", target_os = "android"))] | ||
pub const TEXT: &'static str = "text/plain"; | ||
#[cfg(target_os = "linux")] | ||
pub const TEXT: &'static str = "UTF8_STRING"; | ||
} | ||
|
||
impl ClipboardFormat { | ||
pub fn new(identifier: FormatId, data: impl Into<Vec<u8>>) -> Self { | ||
let data = data.into(); | ||
ClipboardFormat { identifier, data } | ||
} | ||
} | ||
|
||
impl From<String> for ClipboardFormat { | ||
fn from(src: String) -> ClipboardFormat { | ||
let data = src.into_bytes(); | ||
ClipboardFormat::new(ClipboardFormat::TEXT, data) | ||
} | ||
} | ||
|
||
impl From<&str> for ClipboardFormat { | ||
fn from(src: &str) -> ClipboardFormat { | ||
src.to_string().into() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub struct Clipboard; | ||
impl Clipboard { | ||
pub(crate) fn write_text(&mut self, s: impl AsRef<str>) {} | ||
pub(crate) fn read_text(&self) -> Option<String> { | ||
None | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub struct Clipboard; | ||
impl Clipboard { | ||
pub(crate) fn write_text(&mut self, s: impl AsRef<str>) {} | ||
pub(crate) fn read_text(&self) -> Option<String> { | ||
None | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use gdk::Atom; | ||
use gtk::{TargetEntry, TargetFlags}; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub struct Clipboard; | ||
|
||
const CLIPBOARD_TARGETS: [&str; 5] = [ | ||
"UTF8_STRING", | ||
"TEXT", | ||
"STRING", | ||
"text/plain;charset=utf-8", | ||
"text/plain", | ||
]; | ||
|
||
impl Clipboard { | ||
pub(crate) fn write_text(&mut self, string: impl AsRef<str>) { | ||
let string = string.as_ref().to_string(); | ||
|
||
let display = gdk::Display::get_default().unwrap(); | ||
let clipboard = gtk::Clipboard::get_default(&display).unwrap(); | ||
|
||
let targets: Vec<TargetEntry> = CLIPBOARD_TARGETS | ||
.iter() | ||
.enumerate() | ||
.map(|(i, target)| TargetEntry::new(target, TargetFlags::all(), i as u32)) | ||
.collect(); | ||
|
||
clipboard.set_with_data(&targets, move |_, selection, _| { | ||
selection.set(&selection.get_target(), 8 as i32, string.as_bytes()); | ||
}); | ||
} | ||
|
||
pub(crate) fn read_text(&self) -> Option<String> { | ||
let display = gdk::Display::get_default().unwrap(); | ||
let clipboard = gtk::Clipboard::get_default(&display).unwrap(); | ||
|
||
for target in &CLIPBOARD_TARGETS { | ||
let atom = Atom::intern(target); | ||
if let Some(selection) = clipboard.wait_for_contents(&atom) { | ||
return String::from_utf8(selection.get_data()).ok(); | ||
} | ||
} | ||
|
||
None | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use cocoa::{ | ||
appkit::NSPasteboardTypeString, | ||
base::{id, nil, BOOL, YES}, | ||
foundation::{NSInteger, NSString}, | ||
}; | ||
use objc::{class, msg_send, sel, sel_impl}; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub struct Clipboard; | ||
|
||
impl Clipboard { | ||
pub(crate) fn write_text(&mut self, s: impl AsRef<str>) { | ||
let s = s.as_ref(); | ||
unsafe { | ||
let nsstring = NSString::alloc(nil).init_str(s); | ||
let pasteboard: id = msg_send![class!(NSPasteboard), generalPasteboard]; | ||
let _: NSInteger = msg_send![pasteboard, clearContents]; | ||
let result: BOOL = msg_send![pasteboard, setString: nsstring forType: NSPasteboardTypeString]; | ||
if result != YES { | ||
println!("failed to set clipboard"); | ||
} | ||
} | ||
} | ||
|
||
pub(crate) fn read_text(&self) -> Option<String> { | ||
unsafe { | ||
let pasteboard: id = msg_send![class!(NSPasteboard), generalPasteboard]; | ||
let contents: id = msg_send![pasteboard, stringForType: NSPasteboardTypeString]; | ||
if contents.is_null() { | ||
None | ||
} else { | ||
let slice = std::slice::from_raw_parts(contents.UTF8String() as *const _, contents.len()); | ||
let result = std::str::from_utf8_unchecked(slice); | ||
Some(result.to_string()) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.