Repository navigation
Move clipboard, drag-and-drop, and file opening onto a shared multi-format data transfer type #4945
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bb9cd78
Generalize data transfer systems
timon-schelling 66243ee
Move clipboard items to ingest as application data
timon-schelling 304474e
Carry application data as parsed items
timon-schelling acdaad1
Accept image drags on desktop
timon-schelling 1240c3f
Fix tests
timon-schelling File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,120 @@ | ||
| use std::fs; | ||
| use std::io; | ||
| use std::path::PathBuf; | ||
| use winit::data_transfer::{DataTransferId, DataTransferSend, DataTransferSendBuilder, SendData, TypeHint, TypedData}; | ||
| use winit::error::TransferError; | ||
| use winit::event_loop::{ActiveEventLoop, AsyncRequestSerial}; | ||
|
|
||
| use crate::wrapper::messages::{DataTransfer, DataTransferItem, DataType, IngestAction, RawData}; | ||
|
|
||
| fn type_hint(data_type: DataType) -> Option<TypeHint> { | ||
| match data_type { | ||
| DataType::Text => Some(TypeHint::Plaintext), | ||
| DataType::Svg | DataType::Raster(_) => Some(TypeHint::Image { | ||
| extension_hint: Some(data_type.extensions().first().copied()?), | ||
| }), | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| fn data_type(hint: TypeHint) -> Option<DataType> { | ||
| match hint { | ||
| TypeHint::Plaintext => Some(DataType::Text), | ||
| TypeHint::Image { extension_hint: Some(extension) } => match DataType::from_extension(extension) { | ||
| data_type @ (DataType::Svg | DataType::Raster(_)) => Some(data_type), | ||
| _ => None, | ||
| }, | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn send(content: DataTransfer) -> Box<dyn DataTransferSend> { | ||
| let mut builder = DataTransferSendBuilder::new(()); | ||
| for item in content.0 { | ||
| let Some((data_type, data)) = item.into_raw() else { continue }; | ||
| let Some(hint) = type_hint(data_type) else { continue }; | ||
| let data = match (hint, data) { | ||
| (TypeHint::Plaintext, RawData::Text(text)) => SendData::String(text), | ||
| (TypeHint::Plaintext, RawData::Bytes(_)) => continue, | ||
| (_, data) => SendData::Bytes(data.into_bytes()), | ||
| }; | ||
| builder.add_type(hint, move |_, _| Some(data.clone())); | ||
| } | ||
| builder.build() | ||
| } | ||
|
|
||
| pub(crate) fn read_files(paths: impl IntoIterator<Item = PathBuf>) -> Vec<DataTransferItem> { | ||
| paths | ||
| .into_iter() | ||
| .filter_map(|path| match fs::read(&path) { | ||
| Ok(data) => Some(DataTransferItem::from_file(path, None, data)), | ||
| Err(e) => { | ||
| tracing::error!("Failed to read file {}: {e}", path.display()); | ||
| None | ||
| } | ||
| }) | ||
| .collect() | ||
| } | ||
|
|
||
| #[derive(Default)] | ||
| pub(crate) struct TransferReads(Vec<TransferRead>); | ||
|
|
||
| struct TransferRead { | ||
| action: IngestAction, | ||
| fetches: Vec<(AsyncRequestSerial, TypeHint)>, | ||
| items: Vec<DataTransferItem>, | ||
| } | ||
|
|
||
| impl TransferReads { | ||
| pub(crate) fn start(&mut self, event_loop: &dyn ActiveEventLoop, id: DataTransferId, action: IngestAction) -> Result<(), TransferError> { | ||
| let mut hints = Vec::new(); | ||
| for hint in event_loop.data_transfer(id)?.available_types().iter().filter_map(|transfer_type| transfer_type.hint()) { | ||
| if (hint == TypeHint::UriList || data_type(hint).is_some()) && !hints.contains(&hint) { | ||
| hints.push(hint); | ||
| } | ||
| } | ||
|
|
||
| let mut fetches = Vec::new(); | ||
| for hint in hints { | ||
| fetches.push((event_loop.fetch_data_transfer(id, &hint)?, hint)); | ||
| } | ||
|
|
||
| if !fetches.is_empty() { | ||
| self.0.push(TransferRead { action, fetches, items: Vec::new() }); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| pub(crate) fn receive(&mut self, serial: AsyncRequestSerial, value: &dyn TypedData) -> Option<(DataTransfer, IngestAction)> { | ||
| let (index, fetch) = self | ||
| .0 | ||
| .iter() | ||
| .enumerate() | ||
| .find_map(|(index, read)| Some((index, read.fetches.iter().position(|(fetch, _)| *fetch == serial)?)))?; | ||
| let read = &mut self.0[index]; | ||
| let hint = read.fetches[fetch].1; | ||
|
|
||
| let received = match hint { | ||
| TypeHint::UriList => value.try_as_uris().map(|uris| { | ||
| let paths = uris.iter().filter_map(|uri| url::Url::parse(uri).ok()?.to_file_path().ok()); | ||
| read.items.extend(read_files(paths)); | ||
| }), | ||
| TypeHint::Plaintext => value.try_as_string().map(|text| read.items.extend(DataTransferItem::from_raw(DataType::Text, RawData::Text(text)))), | ||
| _ => value | ||
| .try_as_bytes() | ||
| .map(|bytes| read.items.extend(data_type(hint).and_then(|data_type| DataTransferItem::from_raw(data_type, RawData::Bytes(bytes))))), | ||
| }; | ||
| match received { | ||
| Err(e) if e.kind() == io::ErrorKind::WouldBlock => return None, | ||
| Err(e) => tracing::error!("Failed to read transferred data: {e}"), | ||
| Ok(()) => {} | ||
| } | ||
| read.fetches.remove(fetch); | ||
| if !read.fetches.is_empty() { | ||
| return None; | ||
| } | ||
|
|
||
| let read = self.0.remove(index); | ||
| Some((DataTransfer(read.items), read.action)) | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.