Skip to content

Commit

Permalink
Fix Mac clipboard serialization regression
Browse files Browse the repository at this point in the history
  • Loading branch information
rtfeldman committed Aug 13, 2024
1 parent 222c1f3 commit 4893166
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
14 changes: 8 additions & 6 deletions crates/gpui/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -995,11 +995,12 @@ impl ClipboardItem {
}

/// Create a new ClipboardItem::String with the given text and associated metadata
pub fn new_string_with_metadata<T: Serialize>(text: String, metadata: T) -> Self {
pub fn new_string_with_metadata(text: String, metadata: String) -> Self {
Self {
entries: vec![ClipboardEntry::String(
ClipboardString::new(text).with_metadata(metadata),
)],
entries: vec![ClipboardEntry::String(ClipboardString {
text,
metadata: Some(metadata),
})],
}
}

Expand Down Expand Up @@ -1164,8 +1165,9 @@ impl ClipboardString {
}
}

/// Return a new clipboard item with the metadata replaced by the given metadata
pub fn with_metadata<T: Serialize>(mut self, metadata: T) -> Self {
/// Return a new clipboard item with the metadata replaced by the given metadata,
/// after serializing it as JSON.
pub fn with_json_metadata<T: Serialize>(mut self, metadata: T) -> Self {
self.metadata = Some(serde_json::to_string(&metadata).unwrap());
self
}
Expand Down
8 changes: 5 additions & 3 deletions crates/gpui/src/platform/mac/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,7 @@ impl MacPlatform {
text_bytes: &[u8],
) -> ClipboardItem {
let text = String::from_utf8_lossy(text_bytes).to_string();
let opt_metadata = self
let metadata = self
.read_from_pasteboard(state.pasteboard, state.text_hash_pasteboard_type)
.and_then(|hash_bytes| {
let hash_bytes = hash_bytes.try_into().ok()?;
Expand All @@ -1103,7 +1103,9 @@ impl MacPlatform {
}
});

ClipboardItem::new_string_with_metadata(text, opt_metadata)
ClipboardItem {
entries: vec![ClipboardEntry::String(ClipboardString { text, metadata })],
}
}

unsafe fn write_plaintext_to_clipboard(&self, string: &ClipboardString) {
Expand Down Expand Up @@ -1440,7 +1442,7 @@ mod tests {

let item = ClipboardItem {
entries: vec![ClipboardEntry::String(
ClipboardString::new("2".to_string()).with_metadata(vec![3, 4]),
ClipboardString::new("2".to_string()).with_json_metadata(vec![3, 4]),
)],
};
platform.write_to_clipboard(item.clone());
Expand Down

0 comments on commit 4893166

Please sign in to comment.