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

GTK Shell: Manually get/set textual data in clipboard #1695

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ You can find its changes [documented below](#070---2021-01-01).
### Fixed
- `Notification`s will not be delivered to the widget that sends them ([#1640] by [@cmyr])
- `TextBox` can handle standard keyboard shortcuts without needing menus ([#1660] by [@cmyr])
- GTK Shell: Prevent mangling of newline characters in clipboard ([#1695] by [@ForLoveOfCats])


- Fixed docs of derived Lens ([#1523] by [@Maan2003])
Expand Down Expand Up @@ -661,6 +662,7 @@ Last release without a changelog :(
[#1662]: https://github.com/linebender/druid/pull/1662
[#1677]: https://github.com/linebender/druid/pull/1677
[#1698]: https://github.com/linebender/druid/pull/1698
[#1695]: https://github.com/linebender/druid/pull/1695

[Unreleased]: https://github.com/linebender/druid/compare/v0.7.0...master
[0.7.0]: https://github.com/linebender/druid/compare/v0.6.0...v0.7.0
Expand Down
34 changes: 31 additions & 3 deletions druid-shell/src/platform/gtk/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,36 @@ use gtk::{TargetEntry, TargetFlags};

use crate::clipboard::{ClipboardFormat, FormatId};

const CLIPBOARD_TARGETS: [&str; 5] = [
"UTF8_STRING",
"TEXT",
"STRING",
"text/plain;charset=utf-8",
"text/plain",
];

/// The system clipboard.
#[derive(Debug, Clone)]
pub struct Clipboard;

impl Clipboard {
/// Put a string onto the system clipboard.
pub fn put_string(&mut self, s: impl AsRef<str>) {
pub fn put_string(&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();
clipboard.set_text(s.as_ref())

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, _| {
const STRIDE_BITS: i32 = 8;
selection.set(&selection.get_target(), STRIDE_BITS, string.as_bytes());
});
}

/// Put multi-format data on the system clipboard.
Expand Down Expand Up @@ -62,7 +82,15 @@ impl Clipboard {
pub fn get_string(&self) -> Option<String> {
let display = gdk::Display::get_default().unwrap();
let clipboard = gtk::Clipboard::get_default(&display).unwrap();
clipboard.wait_for_text().map(|s| s.to_string())

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
}

/// Given a list of supported clipboard types, returns the supported type which has
Expand Down