fix: match vanilla screenshot and seed link behavior
This commit is contained in:
@@ -39,14 +39,15 @@ fn clicking_seed_copies_the_raw_value_only_inside_the_link() {
|
||||
let font = font();
|
||||
for seed in [1, -42, i64::MIN, i64::MAX] {
|
||||
let mut chat = Chat::default();
|
||||
add(
|
||||
&mut chat,
|
||||
&font,
|
||||
Text::literal("Seed: ").append(Text::copyable(seed.to_string())),
|
||||
);
|
||||
let seed_text = Text::copyable(seed.to_string()).with_color(text::GOLD);
|
||||
assert_eq!(seed_text.plain(), format!("[{seed}]"));
|
||||
assert_eq!(seed_text.spans[0].style.color, Some(text::GOLD));
|
||||
assert_eq!(seed_text.spans[1].style.color, Some(text::GREEN));
|
||||
assert_eq!(seed_text.spans[2].style.color, Some(text::GOLD));
|
||||
add(&mut chat, &font, Text::literal("Seed: ").append(seed_text));
|
||||
let expected = Some(ClickEvent::CopyToClipboard(seed.to_string()));
|
||||
let start = 4.0 + f64::from(font.width("Seed: "));
|
||||
let end = start + f64::from(font.width(&format!("[{seed}]")));
|
||||
let start = 4.0 + f64::from(font.width("Seed: ["));
|
||||
let end = start + f64::from(font.width(&seed.to_string()));
|
||||
assert_eq!(chat.click_at(&font, 360, start, 315.0), expected);
|
||||
assert_eq!(chat.click_at(&font, 360, end - 0.5, 315.0), expected);
|
||||
for (x, y) in [
|
||||
@@ -64,7 +65,7 @@ fn clicking_seed_copies_the_raw_value_only_inside_the_link() {
|
||||
#[test]
|
||||
fn click_targets_match_rendered_spans_when_scaled_wrapped_and_scrolled() {
|
||||
let font = font();
|
||||
let folder = ClickEvent::OpenFile("/tmp/My screenshots/世界".into());
|
||||
let screenshot = ClickEvent::OpenFile("/tmp/My screenshots/世界.png".into());
|
||||
for scale in [0.5, 0.75, 1.0] {
|
||||
for line_spacing in [0.0, 0.5, 1.0] {
|
||||
let mut chat = Chat::default();
|
||||
@@ -78,7 +79,7 @@ fn click_targets_match_rendered_spans_when_scaled_wrapped_and_scrolled() {
|
||||
Text::literal("Saved screenshot as ").append(Text::styled(
|
||||
"2026-10-14_18.55.14.png",
|
||||
Style {
|
||||
click: Some(folder.clone()),
|
||||
click: Some(screenshot.clone()),
|
||||
bold: Some(true),
|
||||
underlined: Some(true),
|
||||
..Style::default()
|
||||
|
||||
@@ -780,12 +780,11 @@ impl Game<'_> {
|
||||
let message = match result {
|
||||
Ok(_) => {
|
||||
let name = path.file_name().unwrap_or_default().to_string_lossy();
|
||||
let folder = path.parent().unwrap_or(std::path::Path::new("screenshots"));
|
||||
let link = Text::styled(
|
||||
name,
|
||||
Style {
|
||||
underlined: Some(true),
|
||||
click: Some(ClickEvent::OpenFile(folder.to_string_lossy().into_owned())),
|
||||
click: Some(ClickEvent::OpenFile(path.to_string_lossy().into_owned())),
|
||||
..Style::default()
|
||||
},
|
||||
);
|
||||
|
||||
@@ -44,7 +44,8 @@ pub fn named_color(name: &str) -> Option<u32> {
|
||||
pub enum ClickEvent {
|
||||
SuggestCommand(String),
|
||||
CopyToClipboard(String),
|
||||
/// A local file or directory to open with the system handler.
|
||||
/// A local file to open with the system handler.
|
||||
/// Only allow this in locally constructed messages, never server/LAN text.
|
||||
OpenFile(String),
|
||||
}
|
||||
|
||||
@@ -145,11 +146,16 @@ impl Text {
|
||||
/// ComponentUtils.copyOnClickText: a green, bracketed clipboard link.
|
||||
pub fn copyable(s: impl Into<String>) -> Self {
|
||||
let s = s.into();
|
||||
Self::literal(format!("[{s}]")).with_style(&Style {
|
||||
click: Some(ClickEvent::CopyToClipboard(s)),
|
||||
hover: Some(Box::new(Self::translate("chat.copy.click", &[]))),
|
||||
..Style::color(GREEN)
|
||||
})
|
||||
Self::literal("[")
|
||||
.append(Self::styled(
|
||||
s.clone(),
|
||||
Style {
|
||||
click: Some(ClickEvent::CopyToClipboard(s)),
|
||||
hover: Some(Box::new(Self::translate("chat.copy.click", &[]))),
|
||||
..Style::color(GREEN)
|
||||
},
|
||||
))
|
||||
.append("]")
|
||||
}
|
||||
/// LocateCommand's coordinate link: suggest a teleport without executing it.
|
||||
/// Structure and POI results preserve the player's current height (`~`).
|
||||
@@ -565,8 +571,8 @@ mod tests {
|
||||
json!({"action": "copy_to_clipboard", "value": "-9223372036854775808"}),
|
||||
),
|
||||
(
|
||||
ClickEvent::OpenFile("/tmp/My screenshots".into()),
|
||||
json!({"action": "open_file", "path": "/tmp/My screenshots"}),
|
||||
ClickEvent::OpenFile("/tmp/My screenshots/世界.png".into()),
|
||||
json!({"action": "open_file", "path": "/tmp/My screenshots/世界.png"}),
|
||||
),
|
||||
] {
|
||||
let style = Style {
|
||||
@@ -578,11 +584,11 @@ mod tests {
|
||||
assert_eq!(text.spans[0].style.click, Some(event));
|
||||
let child = Text::copyable("42").with_style(&style);
|
||||
assert_eq!(
|
||||
child.spans[0].style.click,
|
||||
child.spans[1].style.click,
|
||||
Some(ClickEvent::CopyToClipboard("42".into()))
|
||||
);
|
||||
assert_eq!(
|
||||
child.component()["click_event"],
|
||||
child.component()["extra"][0]["click_event"],
|
||||
json!({"action": "copy_to_clipboard", "value": "42"})
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user