Skip to content

Commit

Permalink
Implement LinuxApplicationExt for gtk backend
Browse files Browse the repository at this point in the history
  • Loading branch information
maan2003 committed Jul 4, 2021
1 parent 13cfe46 commit ce969e5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
12 changes: 11 additions & 1 deletion druid-shell/src/backend/gtk/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,20 @@ impl Application {
}

pub fn clipboard(&self) -> Clipboard {
Clipboard
Clipboard {
selection: gdk::SELECTION_CLIPBOARD,
}
}

pub fn get_locale() -> String {
glib::get_language_names()[0].as_str().into()
}
}

impl crate::platform::linux::LinuxApplicationExt for crate::Application {
fn primary_clipboard(&self) -> crate::Clipboard {
crate::Clipboard(Clipboard {
selection: gdk::SELECTION_PRIMARY,
})
}
}
29 changes: 21 additions & 8 deletions druid-shell/src/backend/gtk/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,29 @@ const CLIPBOARD_TARGETS: [&str; 5] = [
];

/// The system clipboard.
#[derive(Debug, Clone)]
pub struct Clipboard;
#[derive(Clone)]
pub struct Clipboard {
pub(crate) selection: Atom,
}

impl std::fmt::Debug for Clipboard {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let name = match self.selection {
gdk::SELECTION_PRIMARY => "Primary",
gdk::SELECTION_CLIPBOARD => "Clipboard",
_ => "(other)",
};
f.debug_tuple("Clipboard").field(&name).finish()
}
}

impl Clipboard {
/// Put a string onto the system clipboard.
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();
let clipboard = gtk::Clipboard::get_for_display(&display, &self.selection);

let targets: Vec<TargetEntry> = CLIPBOARD_TARGETS
.iter()
Expand All @@ -55,7 +68,7 @@ impl Clipboard {
pub fn put_formats(&mut self, formats: &[ClipboardFormat]) {
let entries = make_entries(formats);
let display = gdk::Display::get_default().unwrap();
let clipboard = gtk::Clipboard::get_default(&display).unwrap();
let clipboard = gtk::Clipboard::get_for_display(&display, &self.selection);
// this is gross: we need to reclone all the data in formats in order
// to move it into the closure. :/
let formats = formats.to_owned();
Expand Down Expand Up @@ -83,7 +96,7 @@ impl Clipboard {
/// Get a string from the system clipboard, if one is available.
pub fn get_string(&self) -> Option<String> {
let display = gdk::Display::get_default().unwrap();
let clipboard = gtk::Clipboard::get_default(&display).unwrap();
let clipboard = gtk::Clipboard::get_for_display(&display, &self.selection);

for target in &CLIPBOARD_TARGETS {
let atom = Atom::intern(target);
Expand All @@ -99,7 +112,7 @@ impl Clipboard {
/// highest priority on the system clipboard, or `None` if no types are supported.
pub fn preferred_format(&self, formats: &[FormatId]) -> Option<FormatId> {
let display = gdk::Display::get_default().unwrap();
let clipboard = gtk::Clipboard::get_default(&display).unwrap();
let clipboard = gtk::Clipboard::get_for_display(&display, &self.selection);
let targets = clipboard.wait_for_targets()?;
let format_atoms = formats
.iter()
Expand All @@ -119,7 +132,7 @@ impl Clipboard {
/// [`Clipboard::preferred_format`]
pub fn get_format(&self, format: FormatId) -> Option<Vec<u8>> {
let display = gdk::Display::get_default().unwrap();
let clipboard = gtk::Clipboard::get_default(&display).unwrap();
let clipboard = gtk::Clipboard::get_for_display(&display, &self.selection);
let atom = Atom::intern(format);
clipboard
.wait_for_contents(&atom)
Expand All @@ -128,7 +141,7 @@ impl Clipboard {

pub fn available_type_names(&self) -> Vec<String> {
let display = gdk::Display::get_default().unwrap();
let clipboard = gtk::Clipboard::get_default(&display).unwrap();
let clipboard = gtk::Clipboard::get_for_display(&display, &self.selection);
let targets = clipboard.wait_for_targets().unwrap_or_default();
targets
.iter()
Expand Down
2 changes: 1 addition & 1 deletion druid-shell/src/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub use crate::backend::clipboard as backend;
/// [MIME types]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types
/// [`ClipboardFormat`]: struct.ClipboardFormat.html
#[derive(Debug, Clone)]
pub struct Clipboard(backend::Clipboard);
pub struct Clipboard(pub(crate) backend::Clipboard);

impl Clipboard {
/// Put a string onto the system clipboard.
Expand Down
12 changes: 12 additions & 0 deletions druid-shell/src/platform/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,15 @@ pub trait LinuxApplicationExt {
/// This is useful for middle mouse paste.
fn primary_clipboard(&self) -> Clipboard;
}

#[cfg(test)]
#[allow(unused_imports)]
mod test {
use crate::Application;

use super::*;
use static_assertions as sa;
// TODO(shell/x11): implement LinuxApplicationExt
#[cfg(not(feature = "x11"))]
sa::assert_impl_all!(Application: LinuxApplicationExt);
}

0 comments on commit ce969e5

Please sign in to comment.