Skip to content

Commit

Permalink
GTK Shell: Manually get/set textual data in clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
ForLoveOfCats committed Apr 6, 2021
1 parent 5138e8b commit 596f711
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
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

0 comments on commit 596f711

Please sign in to comment.