Skip to content

Use an SVG image for clipboard instead of unicode character #85118

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 1 commit into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/librustdoc/html/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,20 @@ crate struct Page<'a> {
crate static_extra_scripts: &'a [&'a str],
}

impl<'a> Page<'a> {
crate fn get_static_root_path(&self) -> &str {
self.static_root_path.unwrap_or(self.root_path)
}
}

crate fn render<T: Print, S: Print>(
layout: &Layout,
page: &Page<'_>,
sidebar: S,
t: T,
style_files: &[StylePath],
) -> String {
let static_root_path = page.static_root_path.unwrap_or(page.root_path);
let static_root_path = page.get_static_root_path();
format!(
"<!DOCTYPE html>\
<html lang=\"en\">\
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ impl<'tcx> Context<'tcx> {
&self.shared.layout,
&page,
|buf: &mut _| print_sidebar(self, it, buf),
|buf: &mut _| print_item(self, it, buf),
|buf: &mut _| print_item(self, it, buf, &page),
&self.shared.style_files,
)
} else {
Expand Down
14 changes: 12 additions & 2 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ use crate::formats::{AssocItemRender, Impl, RenderMode};
use crate::html::escape::Escape;
use crate::html::format::{print_abi_with_space, print_where_clause, Buffer, PrintWithSpace};
use crate::html::highlight;
use crate::html::layout::Page;
use crate::html::markdown::MarkdownSummaryLine;

pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer) {
pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer, page: &Page<'_>) {
debug_assert!(!item.is_stripped());
// Write the breadcrumb trail header for the top
buf.write_str("<h1 class=\"fqn\"><span class=\"in-band\">");
Expand Down Expand Up @@ -74,7 +75,16 @@ pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer)
}
}
write!(buf, "<a class=\"{}\" href=\"\">{}</a>", item.type_(), item.name.as_ref().unwrap());
write!(buf, "<button id=\"copy-path\" onclick=\"copy_path(this)\">⎘</button>");
write!(
buf,
"<button id=\"copy-path\" onclick=\"copy_path(this)\">\
<img src=\"{static_root_path}clipboard{suffix}.svg\" \
width=\"19\" height=\"18\" \
alt=\"Copy item import\">\
</button>",
static_root_path = page.get_static_root_path(),
suffix = page.resource_suffix,
);

buf.write_str("</span>"); // in-band
buf.write_str("<span class=\"out-of-band\">");
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/html/render/write_shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ pub(super) fn write_shared(
}
write_toolchain("brush.svg", static_files::BRUSH_SVG)?;
write_toolchain("wheel.svg", static_files::WHEEL_SVG)?;
write_toolchain("clipboard.svg", static_files::CLIPBOARD_SVG)?;
write_toolchain("down-arrow.svg", static_files::DOWN_ARROW_SVG)?;

let mut themes: Vec<&String> = themes.iter().collect();
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/html/static/clipboard.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 18 additions & 2 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1252,15 +1252,31 @@ function hideThemeButtonState() {
document.execCommand('copy');
document.body.removeChild(el);

but.textContent = '✓';
// There is always one children, but multiple childNodes.
but.children[0].style.display = 'none';

var tmp;
if (but.childNodes.length < 2) {
tmp = document.createTextNode('✓');
but.appendChild(tmp);
} else {
onEachLazy(but.childNodes, function(e) {
if (e.nodeType === Node.TEXT_NODE) {
tmp = e;
return true;
}
});
tmp.textContent = '✓';
}

if (reset_button_timeout !== null) {
window.clearTimeout(reset_button_timeout);
}

function reset_button() {
but.textContent = '';
tmp.textContent = '';
reset_button_timeout = null;
but.children[0].style.display = "";
}

reset_button_timeout = window.setTimeout(reset_button, 1000);
Expand Down
9 changes: 5 additions & 4 deletions src/librustdoc/html/static/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -1321,11 +1321,12 @@ h4 > .notable-traits {
}

#copy-path {
height: 30px;
font-size: 18px;
margin-left: 10px;
padding: 0 6px;
width: 28px;
padding: 0;
padding-left: 2px;
}
#copy-path> img {
margin-bottom: 2px;
}

#theme-choices {
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/themes/ayu.css
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ kbd {
color: #fff;
}

#theme-picker > img, #settings-menu > img {
#theme-picker > img, #settings-menu > img, #copy-path > img {
filter: invert(100);
}

Expand Down
3 changes: 3 additions & 0 deletions src/librustdoc/html/static_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ crate static BRUSH_SVG: &[u8] = include_bytes!("static/brush.svg");
/// The file contents of `wheel.svg`, the icon used for the settings button.
crate static WHEEL_SVG: &[u8] = include_bytes!("static/wheel.svg");

/// The file contents of `clipboard.svg`, the icon used for the "copy path" button.
crate static CLIPBOARD_SVG: &[u8] = include_bytes!("static/clipboard.svg");

/// The file contents of `down-arrow.svg`, the icon used for the crate choice combobox.
crate static DOWN_ARROW_SVG: &[u8] = include_bytes!("static/down-arrow.svg");

Expand Down