Skip to content

Commit

Permalink
X11: Implement putting things into the clipboard (#1851)
Browse files Browse the repository at this point in the history
* x11: Turn Clipboard into a singleton

This commit renames the current Clipboard to ClipboardState and adds a
new Clipboard(Rc<RefCell<ClipboardState>>). This is in preparation for
setting the clipboard contents where we need to keep the clipboard
contents in ClipboardState.

* Preparations for setting the clipboard

Signed-off-by: Uli Schlachter <[email protected]>

* x11: Implement putting things into the clipboard

Fixes: #937
Signed-off-by: Uli Schlachter <[email protected]>

* Add CHANGELOG.md entry

Signed-off-by: Uli Schlachter <[email protected]>
  • Loading branch information
psychon authored Jul 8, 2021
1 parent 387cecb commit 177f3d6
Show file tree
Hide file tree
Showing 3 changed files with 450 additions and 43 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ You can find its changes [documented below](#070---2021-01-01).
- X11: Added support for `get_monitors` ([#1804] by [@psychon])
- x11: Remove some unnecessary casts ([#1851] by [@psychon])
- `has_focus` method on `WidgetPod` ([#1825] by [@ForLoveOfCats])
- x11: Add support for getting clipboard contents ([#1805] by [@psychon])
- x11: Add support for getting and setting clipboard contents ([#1805] and [#1851] by [@psychon])
- Linux extension: primary_clipboard ([#1843] by [@Maan2003])

### Changed
Expand Down
36 changes: 29 additions & 7 deletions druid-shell/src/backend/x11/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ pub(crate) struct Application {
/// The X11 resource database used to query dpi.
pub(crate) rdb: Rc<ResourceDb>,
pub(crate) cursors: Cursors,
/// The clipboard implementation
clipboard: Clipboard,
/// The default screen of the connected display.
///
/// The connected display may also have additional screens.
Expand Down Expand Up @@ -211,6 +213,15 @@ impl Application {
.ok_or_else(|| anyhow!("Couldn't get visual from screen"))?;
let argb_visual_type = util::get_argb_visual_type(&*connection, &screen)?;

let timestamp = Rc::new(Cell::new(x11rb::CURRENT_TIME));
let pending_events = Default::default();
let clipboard = Clipboard::new(
Rc::clone(&connection),
screen_num,
Rc::clone(&pending_events),
Rc::clone(&timestamp),
)?;

Ok(Application {
connection,
rdb,
Expand All @@ -219,14 +230,15 @@ impl Application {
state,
idle_read,
cursors,
clipboard,
idle_write,
present_opcode,
root_visual_type,
argb_visual_type,
pending_events: Default::default(),
marker: std::marker::PhantomData,
render_argb32_pictformat_cursor,
timestamp: Rc::new(Cell::new(x11rb::CURRENT_TIME)),
timestamp,
})
}

Expand Down Expand Up @@ -497,6 +509,21 @@ impl Application {
w.handle_idle_notify(ev)
.context("IDLE_NOTIFY - failed to handle")?;
}
Event::SelectionClear(ev) => {
self.clipboard
.handle_clear(*ev)
.context("SELECTION_CLEAR event handling")?;
}
Event::SelectionRequest(ev) => {
self.clipboard
.handle_request(ev)
.context("SELECTION_REQUEST event handling")?;
}
Event::PropertyNotify(ev) => {
self.clipboard
.handle_property_notify(*ev)
.context("PROPERTY_NOTIFY event handling")?;
}
Event::Error(e) => {
// TODO: if an error is caused by the present extension, disable it and fall back
// to copying pixels. This is blocked on
Expand Down Expand Up @@ -633,12 +660,7 @@ impl Application {
}

pub fn clipboard(&self) -> Clipboard {
Clipboard::new(
Rc::clone(&self.connection),
self.screen_num,
Rc::clone(&self.pending_events),
Rc::clone(&self.timestamp),
)
self.clipboard.clone()
}

pub fn get_locale() -> String {
Expand Down
Loading

0 comments on commit 177f3d6

Please sign in to comment.