mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-21 15:55:40 +00:00
Add support for Ionicons (symbols) to <l:Icon> and <l:task> components (#6186)
This commit is contained in:
1 parent
731a29786a
commit
81072df4f7
61 files changed
+314
-259
No files matched your search
@@ -18,7 +18,7 @@ import org.kohsuke.accmod.restrictions.NoExternalUse;
|
||||
public class AboutJenkins extends ManagementLink {
|
||||
@Override
|
||||
public String getIconFileName() {
|
||||
return "help.svg";
|
||||
return "symbol-help-circle";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -161,6 +161,7 @@ import org.apache.commons.jelly.XMLOutput;
|
||||
import org.apache.commons.jexl.parser.ASTSizeFunction;
|
||||
import org.apache.commons.jexl.util.Introspector;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.jenkins.ui.icon.Icon;
|
||||
import org.jenkins.ui.icon.IconSet;
|
||||
import org.jvnet.tiger_types.Types;
|
||||
import org.kohsuke.accmod.Restricted;
|
||||
@@ -2290,6 +2291,68 @@ public class Functions {
|
||||
}
|
||||
}
|
||||
|
||||
@Restricted(NoExternalUse.class)
|
||||
public static Icon tryGetIcon(String iconGuess) {
|
||||
// Jenkins Symbols don't have metadata so return null
|
||||
if (iconGuess == null || iconGuess.startsWith("symbol-")) {
|
||||
return null;
|
||||
}
|
||||
|
||||
StaplerRequest currentRequest = Stapler.getCurrentRequest();
|
||||
currentRequest.getWebApp().getDispatchValidator().allowDispatch(currentRequest, Stapler.getCurrentResponse());
|
||||
Icon iconMetadata = IconSet.icons.getIconByClassSpec(iconGuess);
|
||||
|
||||
if (iconMetadata == null) {
|
||||
// Icon could be provided as a simple iconFileName e.g. "settings.png"
|
||||
iconMetadata = IconSet.icons.getIconByClassSpec(IconSet.toNormalizedIconNameClass(iconGuess) + " icon-md");
|
||||
}
|
||||
|
||||
if (iconMetadata == null) {
|
||||
// Icon could be provided as an absolute iconFileName e.g. "/plugin/foo/abc.png"
|
||||
iconMetadata = IconSet.icons.getIconByUrl(iconGuess);
|
||||
}
|
||||
|
||||
return iconMetadata;
|
||||
}
|
||||
|
||||
@Restricted(NoExternalUse.class)
|
||||
public static String tryGetIconPath(String iconGuess, JellyContext context) {
|
||||
if (iconGuess == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (iconGuess.startsWith("symbol-")) {
|
||||
return iconGuess;
|
||||
}
|
||||
|
||||
StaplerRequest currentRequest = Stapler.getCurrentRequest();
|
||||
currentRequest.getWebApp().getDispatchValidator().allowDispatch(currentRequest, Stapler.getCurrentResponse());
|
||||
String rootURL = currentRequest.getContextPath();
|
||||
Icon iconMetadata = tryGetIcon(iconGuess);
|
||||
String iconSource = null;
|
||||
|
||||
if (iconMetadata != null) {
|
||||
iconSource = iconMetadata.getQualifiedUrl(context);
|
||||
}
|
||||
|
||||
if (iconMetadata == null) {
|
||||
if (!iconGuess.startsWith("/")) {
|
||||
iconGuess = "/" + iconGuess;
|
||||
}
|
||||
|
||||
iconSource = rootURL + (iconGuess.startsWith("/images/") || iconGuess.startsWith("/plugin/") ? getResourcePath() : "") + iconGuess;
|
||||
}
|
||||
|
||||
if (iconMetadata != null && iconMetadata.getClassSpec() != null) {
|
||||
String translatedIcon = IconSet.tryTranslateTangoIconToSymbol(iconMetadata.getClassSpec());
|
||||
if (translatedIcon != null) {
|
||||
return translatedIcon;
|
||||
}
|
||||
}
|
||||
|
||||
return iconSource;
|
||||
}
|
||||
|
||||
@SuppressFBWarnings(value = "PREDICTABLE_RANDOM", justification = "True randomness isn't necessary for form item IDs")
|
||||
@Restricted(NoExternalUse.class)
|
||||
public static String generateItemId() {
|
||||
|
||||
@@ -78,15 +78,18 @@ import hudson.Functions;
|
||||
*/
|
||||
public interface Action extends ModelObject {
|
||||
/**
|
||||
* Gets the file name of the icon.
|
||||
* Gets the name of the icon.
|
||||
*
|
||||
* @return
|
||||
* If the icon name is prefixed with "symbol-", a Jenkins Symbol
|
||||
* will be used.
|
||||
* <p>
|
||||
* If just a file name (like "abc.gif") is returned, it will be
|
||||
* interpreted as a file name inside {@code /images/24x24}.
|
||||
* This is useful for using one of the stock images.
|
||||
* <p>
|
||||
* If an absolute file name that starts from '/' is returned (like
|
||||
* "/plugin/foo/abc.gif'), then it will be interpreted as a path
|
||||
* "/plugin/foo/abc.gif"), then it will be interpreted as a path
|
||||
* from the context root of Jenkins. This is useful to pick up
|
||||
* image files from a plugin.
|
||||
* <p>
|
||||
@@ -94,6 +97,7 @@ public interface Action extends ModelObject {
|
||||
* but this can be used for actions that only contribute {@code floatBox.jelly}
|
||||
* and no task list item. The other case where this is useful is
|
||||
* to avoid showing links that require a privilege when the user is anonymous.
|
||||
* @see <a href="https://www.jenkins.io/doc/developer/views/symbols/">Jenkins Symbols</a>
|
||||
* @see Functions#isAnonymous()
|
||||
* @see Functions#getIconFilePath(Action)
|
||||
*/
|
||||
|
||||
@@ -38,7 +38,7 @@ public class ManageJenkinsAction implements RootAction {
|
||||
@Override
|
||||
public String getIconFileName() {
|
||||
if (Jenkins.get().hasAnyPermission(Jenkins.MANAGE, Jenkins.SYSTEM_READ))
|
||||
return "gear.png";
|
||||
return "symbol-settings";
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ public class ConfigureLink extends ManagementLink {
|
||||
|
||||
@Override
|
||||
public String getIconFileName() {
|
||||
return "gear.svg";
|
||||
return "symbol-settings";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -133,6 +133,18 @@ public interface ModelObjectWithContextMenu extends ModelObject {
|
||||
return this;
|
||||
}
|
||||
|
||||
/** @since TODO */
|
||||
public ContextMenu add(String url, String icon, String iconXml, String text, boolean post, boolean requiresConfirmation) {
|
||||
if (text != null && icon != null && url != null) {
|
||||
MenuItem item = new MenuItem(url, icon, text);
|
||||
item.iconXml = iconXml;
|
||||
item.post = post;
|
||||
item.requiresConfirmation = requiresConfirmation;
|
||||
items.add(item);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a header row (no icon, no URL, rendered in header style).
|
||||
*
|
||||
@@ -268,6 +280,11 @@ public interface ModelObjectWithContextMenu extends ModelObject {
|
||||
@SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD", justification = "read by Stapler")
|
||||
public String icon;
|
||||
|
||||
/**
|
||||
* Optional icon XML, if set it's used instead of @icon for the menu item
|
||||
*/
|
||||
private String iconXml;
|
||||
|
||||
/**
|
||||
* True to make a POST request rather than GET.
|
||||
* @since 1.504
|
||||
@@ -300,6 +317,11 @@ public interface ModelObjectWithContextMenu extends ModelObject {
|
||||
@SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD", justification = "read by Stapler")
|
||||
public ContextMenu subMenu;
|
||||
|
||||
@Exported
|
||||
public String getIconXml() {
|
||||
return iconXml;
|
||||
}
|
||||
|
||||
public MenuItem(String url, String icon, String displayName) {
|
||||
withUrl(url).withIcon(icon).withDisplayName(displayName);
|
||||
}
|
||||
|
||||
@@ -35,6 +35,8 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.jelly.JellyContext;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.kohsuke.accmod.Restricted;
|
||||
import org.kohsuke.accmod.restrictions.NoExternalUse;
|
||||
|
||||
/**
|
||||
* An icon set.
|
||||
@@ -45,7 +47,7 @@ public class IconSet {
|
||||
|
||||
|
||||
public static final IconSet icons = new IconSet();
|
||||
private static final Map<String, String> IONICONS = new ConcurrentHashMap<>();
|
||||
private static final Map<String, String> SYMBOLS = new ConcurrentHashMap<>();
|
||||
|
||||
private Map<String, Icon> iconsByCSSSelector = new ConcurrentHashMap<>();
|
||||
private Map<String, Icon> iconsByUrl = new ConcurrentHashMap<>();
|
||||
@@ -73,34 +75,40 @@ public class IconSet {
|
||||
return icon;
|
||||
}
|
||||
|
||||
public static String getIonicon(String name, String title) {
|
||||
if (IONICONS.containsKey(name)) {
|
||||
String icon = IONICONS.get(name);
|
||||
return prependTitleIfRequired(icon, title);
|
||||
// for Jelly
|
||||
@Restricted(NoExternalUse.class)
|
||||
public static String getSymbol(String name, String title, String classes) {
|
||||
if (SYMBOLS.containsKey(name)) {
|
||||
String symbol = SYMBOLS.get(name);
|
||||
symbol = symbol.replaceAll("(class=\")[^&]*?(\")", "$1$2");
|
||||
symbol = symbol.replaceAll("<svg", "<svg class=\"" + classes + "\"");
|
||||
return prependTitleIfRequired(symbol, title);
|
||||
}
|
||||
|
||||
// Load icon if it exists
|
||||
InputStream inputStream = IconSet.class.getResourceAsStream("/images/ionicons/" + name + ".svg");
|
||||
String ionicon = null;
|
||||
// Load symbol if it exists
|
||||
InputStream inputStream = IconSet.class.getResourceAsStream("/images/symbols/" + name + ".svg");
|
||||
String symbol = null;
|
||||
|
||||
try {
|
||||
if (inputStream != null) {
|
||||
ionicon = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
|
||||
symbol = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// ignored
|
||||
}
|
||||
if (ionicon == null) {
|
||||
ionicon = PLACEHOLDER_SVG;
|
||||
if (symbol == null) {
|
||||
symbol = PLACEHOLDER_SVG;
|
||||
}
|
||||
|
||||
ionicon = ionicon.replaceAll("(<title>)[^&]*(</title>)", "$1$2");
|
||||
ionicon = ionicon.replaceAll("<svg", "<svg aria-hidden=\"true\"");
|
||||
ionicon = ionicon.replace("stroke:#000", "stroke:currentColor");
|
||||
symbol = symbol.replaceAll("(<title>)[^&]*(</title>)", "$1$2");
|
||||
symbol = symbol.replaceAll("(class=\")[^&]*?(\")", "$1$2");
|
||||
symbol = symbol.replaceAll("<svg", "<svg aria-hidden=\"true\"");
|
||||
symbol = symbol.replaceAll("<svg", "<svg class=\"" + classes + "\"");
|
||||
symbol = symbol.replace("stroke:#000", "stroke:currentColor");
|
||||
|
||||
IONICONS.put(name, ionicon);
|
||||
SYMBOLS.put(name, symbol);
|
||||
|
||||
return prependTitleIfRequired(ionicon, title);
|
||||
return prependTitleIfRequired(symbol, title);
|
||||
}
|
||||
|
||||
public IconSet addIcon(Icon icon) {
|
||||
@@ -517,24 +525,34 @@ public class IconSet {
|
||||
images.add("warning");
|
||||
images.add("document-properties");
|
||||
|
||||
Map<String, String> materialIcons = new HashMap<>();
|
||||
materialIcons.put("help", "svg-sprite-action-symbol.svg#ic_help_24px");
|
||||
|
||||
for (Map.Entry<String, String> size : sizes.entrySet()) {
|
||||
for (String image : images) {
|
||||
icons.addIcon(new Icon("icon-" + image + " " + size.getKey(),
|
||||
"svgs/" + image + ".svg", size.getValue()));
|
||||
}
|
||||
|
||||
for (Map.Entry<String, String> imageEntry : materialIcons.entrySet()) {
|
||||
icons.addIcon(new Icon(
|
||||
"icon-" + imageEntry.getKey() + " " + size.getKey(),
|
||||
"material-icons/" + imageEntry.getValue(),
|
||||
size.getValue(),
|
||||
IconFormat.EXTERNAL_SVG_SPRITE
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a temporary function to replace Tango icons across Jenkins and plugins with
|
||||
* appropriate Jenkins Symbols
|
||||
*
|
||||
* @param tangoIcon A tango icon in the format 'icon-* size-*', e.g. 'icon-gear icon-lg'
|
||||
* @return a Jenkins Symbol (if one exists) otherwise null
|
||||
*/
|
||||
@Restricted(NoExternalUse.class)
|
||||
public static String tryTranslateTangoIconToSymbol(String tangoIcon) {
|
||||
if (tangoIcon != null) {
|
||||
tangoIcon = tangoIcon.split(" ")[0];
|
||||
}
|
||||
|
||||
Map<String, String> translations = new HashMap<>();
|
||||
translations.put("icon-clock", "symbol-play");
|
||||
translations.put("icon-edit-delete", "symbol-trash");
|
||||
translations.put("icon-gear", "symbol-settings");
|
||||
translations.put("icon-gear2", "symbol-settings");
|
||||
translations.put("icon-plugin", "symbol-plugins");
|
||||
|
||||
return translations.getOrDefault(tangoIcon, null);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user