mirror of
https://github.com/zed-industries/zed.git
synced 2026-11-19 06:59:22 +01:00
gpui: Normalize clipboard line endings on Windows (#66945)
Windows applications expect CRLF line endings in CF_UNICODETEXT, so convert LF to CRLF when writing to the clipboard. Convert CRLF back to LF when reading from the clipboard, too, to prevent surprises in applications. Release Notes: - N/A - [GPUI] N/A Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use std::{borrow::Cow, sync::LazyLock};
|
||||
use std::sync::LazyLock;
|
||||
|
||||
use anyhow::Result;
|
||||
use collections::FxHashMap;
|
||||
@@ -179,12 +179,13 @@ fn is_image_format(format: u32) -> bool {
|
||||
|
||||
fn write_string(item: &ClipboardString) -> Result<()> {
|
||||
// CF_UNICODETEXT is null-terminated, so replace embedded NUL characters with spaces.
|
||||
let text = if item.text.contains('\0') {
|
||||
Cow::Owned(item.text.replace('\0', " "))
|
||||
} else {
|
||||
Cow::Borrowed(item.text.as_str())
|
||||
};
|
||||
let wide: Vec<u16> = text.encode_utf16().chain(Some(0)).collect_vec();
|
||||
let text = item.text.replace('\0', " ").replace("\r\n", "\n");
|
||||
// Windows applications expect CRLF line endings in CF_UNICODETEXT.
|
||||
let wide: Vec<u16> = text
|
||||
.replace('\n', "\r\n")
|
||||
.encode_utf16()
|
||||
.chain(Some(0))
|
||||
.collect_vec();
|
||||
set_clipboard_bytes(&wide, CF_UNICODETEXT.0 as u32)?;
|
||||
|
||||
if let Some(metadata) = item.metadata.as_ref() {
|
||||
@@ -233,7 +234,8 @@ fn convert_to_png(bytes: &[u8], format: ImageFormat) -> Option<Vec<u8>> {
|
||||
}
|
||||
|
||||
fn read_string() -> Option<ClipboardEntry> {
|
||||
let text = get_clipboard_string(CF_UNICODETEXT.0 as u32)?;
|
||||
// Normalize to LF so that apps the same line endings on every platform.
|
||||
let text = get_clipboard_string(CF_UNICODETEXT.0 as u32)?.replace("\r\n", "\n");
|
||||
let metadata = read_clipboard_metadata(&text);
|
||||
Some(ClipboardEntry::String(ClipboardString { text, metadata }))
|
||||
}
|
||||
@@ -392,3 +394,9 @@ impl Drop for LockedGlobal {
|
||||
unsafe { GlobalUnlock(self.global).ok() };
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn read_raw_clipboard_text() -> Option<String> {
|
||||
let _clip = ClipboardGuard::open()?;
|
||||
get_clipboard_string(CF_UNICODETEXT.0 as u32)
|
||||
}
|
||||
|
||||
@@ -1920,6 +1920,7 @@ unsafe fn username_and_password(credential: &CREDENTIALW) -> Result<(String, Vec
|
||||
mod tests {
|
||||
use std::ffi::{OsStr, OsString};
|
||||
|
||||
use crate::clipboard::read_raw_clipboard_text;
|
||||
use crate::{read_from_clipboard, write_to_clipboard};
|
||||
use gpui::ClipboardItem;
|
||||
use windows::Win32::Security::Credentials::{
|
||||
@@ -2023,5 +2024,22 @@ mod tests {
|
||||
vec![12],
|
||||
)),
|
||||
);
|
||||
|
||||
let item = ClipboardItem::new_string_with_json_metadata("a\nb\nc".to_string(), vec![5]);
|
||||
write_to_clipboard(item.clone());
|
||||
assert_eq!(read_raw_clipboard_text().as_deref(), Some("a\r\nb\r\nc"));
|
||||
assert_eq!(read_from_clipboard(), Some(item));
|
||||
|
||||
let item =
|
||||
ClipboardItem::new_string_with_json_metadata("a\r\nb\nc\rd".to_string(), vec![6]);
|
||||
write_to_clipboard(item);
|
||||
assert_eq!(read_raw_clipboard_text().as_deref(), Some("a\r\nb\r\nc\rd"));
|
||||
assert_eq!(
|
||||
read_from_clipboard(),
|
||||
Some(ClipboardItem::new_string_with_json_metadata(
|
||||
"a\nb\nc\rd".to_string(),
|
||||
vec![6],
|
||||
)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user