|
|
@ -1,9 +1,11 @@ |
|
|
|
package com.fine.theme.icon.svg; |
|
|
|
package com.fine.theme.icon.svg; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.fine.theme.icon.AbstractIconSource; |
|
|
|
import com.fine.theme.icon.DisabledIcon; |
|
|
|
import com.fine.theme.icon.DisabledIcon; |
|
|
|
import com.fine.theme.icon.GraphicsFilter; |
|
|
|
import com.fine.theme.icon.GraphicsFilter; |
|
|
|
import com.fine.theme.icon.IconResource; |
|
|
|
import com.fine.theme.icon.IconResource; |
|
|
|
import com.fine.theme.icon.IconType; |
|
|
|
import com.fine.theme.icon.IconType; |
|
|
|
|
|
|
|
import com.fine.theme.icon.UrlIconResource; |
|
|
|
import com.fine.theme.icon.WhiteIcon; |
|
|
|
import com.fine.theme.icon.WhiteIcon; |
|
|
|
import com.formdev.flatlaf.FlatLaf; |
|
|
|
import com.formdev.flatlaf.FlatLaf; |
|
|
|
import com.formdev.flatlaf.ui.FlatUIUtils; |
|
|
|
import com.formdev.flatlaf.ui.FlatUIUtils; |
|
|
@ -69,9 +71,30 @@ public class SvgIcon implements DisabledIcon, WhiteIcon, Icon { |
|
|
|
g = grayGraphics(g); |
|
|
|
g = grayGraphics(g); |
|
|
|
} |
|
|
|
} |
|
|
|
Object[] oldRenderingHints = FlatUIUtils.setRenderingHints(g); |
|
|
|
Object[] oldRenderingHints = FlatUIUtils.setRenderingHints(g); |
|
|
|
render(c, g, x, y); |
|
|
|
try { |
|
|
|
|
|
|
|
render(c, g, x, y, this::fallbackRender); |
|
|
|
|
|
|
|
} finally { |
|
|
|
FlatUIUtils.resetRenderingHints(g, oldRenderingHints); |
|
|
|
FlatUIUtils.resetRenderingHints(g, oldRenderingHints); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 用于fallback渲染disable图像,这段代码来自异常分支,使用率要保持较低水平 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private void fallbackRender(Component c, Graphics g, int x, int y) { |
|
|
|
|
|
|
|
if (resource instanceof UrlIconResource) { |
|
|
|
|
|
|
|
String path = ((UrlIconResource) resource).getPath(); |
|
|
|
|
|
|
|
String[] names = path.split(AbstractIconSource.ICON_DISABLE_SUFFIX); |
|
|
|
|
|
|
|
if (path.contains(AbstractIconSource.ICON_DISABLE_SUFFIX) && names.length > 1) { |
|
|
|
|
|
|
|
SVGLoader loader = new SVGLoader(); |
|
|
|
|
|
|
|
SVGDocument document = loader.load(new UrlIconResource(names[0] + names[1]).getInputStream()); |
|
|
|
|
|
|
|
if (document != null) { |
|
|
|
|
|
|
|
document.render((JComponent) c, grayGraphics(g), |
|
|
|
|
|
|
|
new ViewBox(x, y, scaleSize.width, scaleSize.height)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private Graphics2D grayGraphics(Graphics g) { |
|
|
|
private Graphics2D grayGraphics(Graphics g) { |
|
|
|
Object grayFilterObj = UIManager.get("Component.grayFilter"); |
|
|
|
Object grayFilterObj = UIManager.get("Component.grayFilter"); |
|
|
@ -92,7 +115,7 @@ public class SvgIcon implements DisabledIcon, WhiteIcon, Icon { |
|
|
|
return scaleSize.height; |
|
|
|
return scaleSize.height; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void render(Component c, Graphics g, int x, int y) { |
|
|
|
private void render(Component c, Graphics g, int x, int y, FallbackRender fallbackRender) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
if (type == IconType.white) { |
|
|
|
if (type == IconType.white) { |
|
|
|
Objects.requireNonNull(whiteSvgDocument.getValue()) |
|
|
|
Objects.requireNonNull(whiteSvgDocument.getValue()) |
|
|
@ -103,6 +126,7 @@ public class SvgIcon implements DisabledIcon, WhiteIcon, Icon { |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
FineLoggerFactory.getLogger().error("SvgIcon from url: " + resource + "can not paint.", e); |
|
|
|
FineLoggerFactory.getLogger().error("SvgIcon from url: " + resource + "can not paint.", e); |
|
|
|
|
|
|
|
fallbackRender.render(c, g, x, y); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -141,4 +165,10 @@ public class SvgIcon implements DisabledIcon, WhiteIcon, Icon { |
|
|
|
public @NotNull SvgIcon disabled() { |
|
|
|
public @NotNull SvgIcon disabled() { |
|
|
|
return new SvgIcon(resource, size, IconType.disable); |
|
|
|
return new SvgIcon(resource, size, IconType.disable); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@FunctionalInterface |
|
|
|
|
|
|
|
interface FallbackRender { |
|
|
|
|
|
|
|
void render(Component c, Graphics g, int x, int y); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|