Browse Source

REPORT-145112 代码质量优化

fbp/feature
Levy.Xie-解安森 4 months ago
parent
commit
c3beb1631b
  1. 38
      designer-base/src/main/java/com/fine/component/popup/ComponentLayer.java
  2. 12
      designer-base/src/main/java/com/fine/component/popup/GlassPaneChild.java
  3. 18
      designer-base/src/main/java/com/fine/component/popup/GlassPopup.java
  4. 8
      designer-base/src/main/java/com/fine/component/popup/ImageChild.java
  5. 18
      designer-base/src/main/java/com/fine/component/popup/WindowSnapshots.java
  6. 5
      designer-base/src/main/java/com/fine/theme/light/ui/ShadowBorder.java
  7. 6
      designer-base/src/main/java/com/fine/theme/light/ui/ToastPanelUI.java
  8. 101
      designer-base/src/main/java/com/fine/theme/utils/ShadowRenderer.java

38
designer-base/src/main/java/com/fine/component/popup/ComponentLayer.java

@ -128,7 +128,9 @@ public class ComponentLayer extends JPanel {
private void showComponent(GlassPaneChild component) { private void showComponent(GlassPaneChild component) {
showSnapshot = true; showSnapshot = true;
this.nextComponent = component; this.nextComponent = component;
if (!component.isTransparent()) {
image = FineUIUtils.createImage(this.component); image = FineUIUtils.createImage(this.component);
}
if (component.getRoundBorder() > 0) { if (component.getRoundBorder() > 0) {
setOpaque(false); setOpaque(false);
component.setOpaque(false); component.setOpaque(false);
@ -149,7 +151,9 @@ public class ComponentLayer extends JPanel {
} }
simpleSnapshot = true; simpleSnapshot = true;
doLayout(); doLayout();
if (!component.isTransparent()) {
image = FineUIUtils.createImage(component, component.getRoundBorder()); image = FineUIUtils.createImage(component, component.getRoundBorder());
}
component.setVisible(false); component.setVisible(false);
} }
@ -167,6 +171,7 @@ public class ComponentLayer extends JPanel {
@Override @Override
protected void paintComponent(Graphics g) { protected void paintComponent(Graphics g) {
if (!component.isTransparent()) {
if (!isOpaque() && component.getRoundBorder() > 0) { if (!isOpaque() && component.getRoundBorder() > 0) {
Graphics2D g2 = (Graphics2D) g.create(); Graphics2D g2 = (Graphics2D) g.create();
FlatUIUtils.setRenderingHints(g2); FlatUIUtils.setRenderingHints(g2);
@ -175,6 +180,7 @@ public class ComponentLayer extends JPanel {
FlatUIUtils.paintComponentBackground(g2, 0, 0, getWidth(), getHeight(), 0, arc); FlatUIUtils.paintComponentBackground(g2, 0, 0, getWidth(), getHeight(), 0, arc);
g2.dispose(); g2.dispose();
} }
}
super.paintComponent(g); super.paintComponent(g);
} }
@ -193,17 +199,20 @@ public class ComponentLayer extends JPanel {
g2.setColor(getBackground()); g2.setColor(getBackground());
FlatUIUtils.paintComponentBackground(g2, 0, 0, width, height, 0, arc); FlatUIUtils.paintComponentBackground(g2, 0, 0, width, height, 0, arc);
if (image != null) { if (image != null) {
int w = image.getWidth(null); paintImage(g2);
double x; }
if (push) { if (nextImage != null) {
x = -w * animate; paintNextImage(g2);
}
g2.dispose();
g.drawImage(bufferedImage, 0, 0, null);
}
} else { } else {
x = w * animate; super.paint(g);
} }
g2.setComposite(AlphaComposite.SrcAtop.derive(1f - animate));
g2.drawImage(image, (int) x, 0, null);
} }
if (nextImage != null) {
private void paintNextImage(Graphics2D g2) {
int w = nextImage.getWidth(null); int w = nextImage.getWidth(null);
double x; double x;
if (push) { if (push) {
@ -214,12 +223,17 @@ public class ComponentLayer extends JPanel {
g2.setComposite(AlphaComposite.SrcAtop.derive(animate)); g2.setComposite(AlphaComposite.SrcAtop.derive(animate));
g2.drawImage(nextImage, (int) x, 0, null); g2.drawImage(nextImage, (int) x, 0, null);
} }
g2.dispose();
g.drawImage(bufferedImage, 0, 0, null); private void paintImage(Graphics2D g2) {
} int w = image.getWidth(null);
double x;
if (push) {
x = -w * animate;
} else { } else {
super.paint(g); x = w * animate;
} }
g2.setComposite(AlphaComposite.SrcAtop.derive(1f - animate));
g2.drawImage(image, (int) x, 0, null);
} }
public GlassPaneChild getComponent() { public GlassPaneChild getComponent() {

12
designer-base/src/main/java/com/fine/component/popup/GlassPaneChild.java

@ -13,10 +13,21 @@ import javax.swing.JPanel;
*/ */
public class GlassPaneChild extends JPanel { public class GlassPaneChild extends JPanel {
protected boolean transparent;
public int getRoundBorder() { public int getRoundBorder() {
return FlatUIUtils.getUIInt("Component.arc", 6); return FlatUIUtils.getUIInt("Component.arc", 6);
} }
/**
* 组件是否透明
*
* @return 组件透明
*/
protected boolean isTransparent() {
return transparent;
}
/** /**
* 放入组件时的回调方法 * 放入组件时的回调方法
*/ */
@ -35,6 +46,7 @@ public class GlassPaneChild extends JPanel {
* 显示弹窗组件时的回调方法 * 显示弹窗组件时的回调方法
*/ */
public void popupShow() { public void popupShow() {
} }
/** /**

18
designer-base/src/main/java/com/fine/component/popup/GlassPopup.java

@ -59,13 +59,24 @@ public class GlassPopup extends JComponent {
@Override @Override
public void begin() { public void begin() {
prepareAnimation();
}
@Override
public void end() {
finishAnimation();
}
});
animator.setInterpolator(CubicBezierEasing.EASE);
}
private void prepareAnimation() {
componentLayer.showSnapshot(); componentLayer.showSnapshot();
parent.windowSnapshots.createSnapshot(); parent.windowSnapshots.createSnapshot();
parent.contentPane.setVisible(false); parent.contentPane.setVisible(false);
} }
@Override private void finishAnimation() {
public void end() {
componentLayer.hideSnapshot(); componentLayer.hideSnapshot();
if (show) { if (show) {
componentLayer.getComponent().popupShow(); componentLayer.getComponent().popupShow();
@ -75,9 +86,6 @@ public class GlassPopup extends JComponent {
parent.contentPane.setVisible(true); parent.contentPane.setVisible(true);
parent.windowSnapshots.removeSnapshot(); parent.windowSnapshots.removeSnapshot();
} }
});
animator.setInterpolator(CubicBezierEasing.EASE);
}
public void setShowPopup(boolean show) { public void setShowPopup(boolean show) {
if (this.show != show) { if (this.show != show) {

8
designer-base/src/main/java/com/fine/component/popup/ImageChild.java

@ -11,7 +11,7 @@ import java.util.Objects;
* 图片遮罩层支撑静态图片及动态GIF * 图片遮罩层支撑静态图片及动态GIF
* *
* @author Levy.Xie * @author Levy.Xie
* @since 11.0 * @since 12.0
* Created on 2024/12/20 * Created on 2024/12/20
*/ */
public class ImageChild extends GlassPaneChild { public class ImageChild extends GlassPaneChild {
@ -23,6 +23,12 @@ public class ImageChild extends GlassPaneChild {
} }
public ImageChild(String imgPath) { public ImageChild(String imgPath) {
this(imgPath, true);
}
public ImageChild(String imgPath, boolean transparent) {
this.transparent = transparent;
setLayout(new BorderLayout()); setLayout(new BorderLayout());
setBorder(new ScaledEmptyBorder(8, 8, 8, 8)); setBorder(new ScaledEmptyBorder(8, 8, 8, 8));
ImageIcon icon = new ImageIcon(Objects.requireNonNull(getClass().getClassLoader().getResource(imgPath))); ImageIcon icon = new ImageIcon(Objects.requireNonNull(getClass().getClassLoader().getResource(imgPath)));

18
designer-base/src/main/java/com/fine/component/popup/WindowSnapshots.java

@ -1,5 +1,7 @@
package com.fine.component.popup; package com.fine.component.popup;
import org.jetbrains.annotations.NotNull;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.JLayeredPane; import javax.swing.JLayeredPane;
import javax.swing.JRootPane; import javax.swing.JRootPane;
@ -39,7 +41,16 @@ public class WindowSnapshots {
JLayeredPane layeredPane = getLayeredPane(); JLayeredPane layeredPane = getLayeredPane();
if (layeredPane != null) { if (layeredPane != null) {
layeredPane.paint(snapshot.getGraphics()); layeredPane.paint(snapshot.getGraphics());
snapshotLayer = new JComponent() { snapshotLayer = createSnapLayer(snapshot);
snapshotLayer.setSize(layeredPane.getSize());
layeredPane.add(snapshotLayer, Integer.valueOf(JLayeredPane.DRAG_LAYER + 1));
}
}
}
}
private static @NotNull JComponent createSnapLayer(VolatileImage snapshot) {
return new JComponent() {
@Override @Override
public void paint(Graphics g) { public void paint(Graphics g) {
if (snapshot.contentsLost()) { if (snapshot.contentsLost()) {
@ -54,11 +65,6 @@ public class WindowSnapshots {
snapshot.flush(); snapshot.flush();
} }
}; };
snapshotLayer.setSize(layeredPane.getSize());
layeredPane.add(snapshotLayer, Integer.valueOf(JLayeredPane.DRAG_LAYER + 1));
}
}
}
} }
/** /**

5
designer-base/src/main/java/com/fine/theme/light/ui/ShadowBorder.java

@ -5,6 +5,7 @@ import com.formdev.flatlaf.FlatPropertiesLaf;
import com.formdev.flatlaf.ui.FlatStylingSupport.Styleable; import com.formdev.flatlaf.ui.FlatStylingSupport.Styleable;
import com.formdev.flatlaf.ui.FlatUIUtils; import com.formdev.flatlaf.ui.FlatUIUtils;
import com.formdev.flatlaf.util.UIScale; import com.formdev.flatlaf.util.UIScale;
import com.fr.stable.AssistUtils;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
@ -73,7 +74,9 @@ public class ShadowBorder extends EmptyBorder {
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
JComponent com = (JComponent) c; JComponent com = (JComponent) c;
int arc = FlatPropertiesLaf.getStyleableValue(com, "arc"); int arc = FlatPropertiesLaf.getStyleableValue(com, "arc");
if (shadowImage == null || !shadowColor.equals(lastShadowColor) || width != lastWidth || height != lastHeight || shadowSize != lastShadowSize || shadowOpacity != lastShadowOpacity || arc != lastArc) { if (shadowImage == null || !shadowColor.equals(lastShadowColor)
|| width != lastWidth || height != lastHeight || shadowSize != lastShadowSize
|| !AssistUtils.equals(shadowOpacity, lastShadowOpacity) || arc != lastArc) {
shadowImage = createShadowImage(width, height, arc); shadowImage = createShadowImage(width, height, arc);
lastShadowColor = shadowColor; lastShadowColor = shadowColor;
lastWidth = width; lastWidth = width;

6
designer-base/src/main/java/com/fine/theme/light/ui/ToastPanelUI.java

@ -136,24 +136,22 @@ public class ToastPanelUI extends BasicPanelUI implements StyleableUI, PropertyC
@Override @Override
public void propertyChange(PropertyChangeEvent e) { public void propertyChange(PropertyChangeEvent e) {
JPanel c = (JPanel) e.getSource();
switch (e.getPropertyName()) { switch (e.getPropertyName()) {
default: default:
case TOAST_ICON: { case TOAST_ICON: {
JPanel c = (JPanel) e.getSource();
uninstallIconComponent(c); uninstallIconComponent(c);
installIconComponent(c); installIconComponent(c);
refreshUI(c); refreshUI(c);
break; break;
} }
case TOAST_COMPONENT: { case TOAST_COMPONENT: {
JPanel c = (JPanel) e.getSource();
uninstallComponent(c); uninstallComponent(c);
installComponent(c); installComponent(c);
refreshUI(c); refreshUI(c);
break; break;
} }
case TOAST_SHOW_CLOSE_BUTTON: { case TOAST_SHOW_CLOSE_BUTTON: {
JPanel c = (JPanel) e.getSource();
uninstallCloseButton(c); uninstallCloseButton(c);
installCloseButton(c); installCloseButton(c);
refreshUI(c); refreshUI(c);
@ -161,7 +159,6 @@ public class ToastPanelUI extends BasicPanelUI implements StyleableUI, PropertyC
} }
case STYLE: case STYLE:
case STYLE_CLASS: { case STYLE_CLASS: {
JPanel c = (JPanel) e.getSource();
installStyle(c); installStyle(c);
refreshUI(c); refreshUI(c);
break; break;
@ -174,7 +171,6 @@ public class ToastPanelUI extends BasicPanelUI implements StyleableUI, PropertyC
c.repaint(); c.repaint();
} }
private void installIconComponent(JComponent c) { private void installIconComponent(JComponent c) {
iconComponent = clientProperty(c, TOAST_ICON, null, JComponent.class); iconComponent = clientProperty(c, TOAST_ICON, null, JComponent.class);
if (iconComponent != null) { if (iconComponent != null) {

101
designer-base/src/main/java/com/fine/theme/utils/ShadowRenderer.java

@ -1,5 +1,7 @@
package com.fine.theme.utils; package com.fine.theme.utils;
import org.jetbrains.annotations.NotNull;
import java.awt.Color; import java.awt.Color;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Rectangle; import java.awt.Rectangle;
@ -77,54 +79,64 @@ public class ShadowRenderer {
int yStop = dstHeight - right; int yStop = dstHeight - right;
int shadowRgb = color.getRGB() & 0x00FFFFFF; int shadowRgb = color.getRGB() & 0x00FFFFFF;
int[] aHistory = new int[shadowSize]; int[] aHistory = new int[shadowSize];
int historyIdx;
int aSum;
BufferedImage dst = new BufferedImage(dstWidth, dstHeight, BufferedImage.TYPE_INT_ARGB); BufferedImage dst = new BufferedImage(dstWidth, dstHeight, BufferedImage.TYPE_INT_ARGB);
int[] dstBuffer = new int[dstWidth * dstHeight]; int[] dstBuffer = new int[dstWidth * dstHeight];
int[] srcBuffer = new int[srcWidth * srcHeight]; int[] srcBuffer = new int[srcWidth * srcHeight];
getPixels(image, 0, 0, srcWidth, srcHeight, srcBuffer); getPixels(image, 0, 0, srcWidth, srcHeight, srcBuffer);
int lastPixelOffset = right * dstWidth; int lastPixelOffset = right * dstWidth;
float hSumDivider = 1.0f / shadowSize;
float vSumDivider = opacity / shadowSize; int[] hSumLookup = initSumLookupArray(1.0f, shadowSize);
int[] hSumLookup = new int[256 * shadowSize]; int[] vSumLookup = initSumLookupArray(opacity, shadowSize);
for (int i = 0; i < hSumLookup.length; i++) {
hSumLookup[i] = (int) (i * hSumDivider); applyHorizontalShadowBlur(left, dstWidth, srcHeight, shadowSize, aHistory, srcWidth, hSumLookup, dstBuffer, srcBuffer);
applyVerticalShadowBlur(dstWidth, left, aHistory, right, dstBuffer, yStop, vSumLookup, shadowRgb,
lastPixelOffset, shadowSize, dstHeight);
setPixels(dst, 0, 0, dstWidth, dstHeight, dstBuffer);
return dst;
} }
int[] vSumLookup = new int[256 * shadowSize];
for (int i = 0; i < vSumLookup.length; i++) { private static int @NotNull [] initSumLookupArray(float x, int shadowSize) {
vSumLookup[i] = (int) (i * vSumDivider); float sumDivider = x / shadowSize;
int[] sumLookup = new int[256 * shadowSize];
for (int i = 0; i < sumLookup.length; i++) {
sumLookup[i] = (int) (i * sumDivider);
} }
int srcOffset; return sumLookup;
for (int srcY = 0, dstOffset = left * dstWidth; srcY < srcHeight; srcY++) {
for (historyIdx = 0; historyIdx < shadowSize; ) {
aHistory[historyIdx++] = 0;
} }
aSum = 0;
historyIdx = 0; private static void applyVerticalShadowBlur(int dstWidth, int left, int[] aHistory, int right, int[] dstBuffer,
srcOffset = srcY * srcWidth; int yStop, int[] vSumLookup, int shadowRgb, int lastPixelOffset,
for (int srcX = 0; srcX < srcWidth; srcX++) { int shadowSize, int dstHeight) {
int a = hSumLookup[aSum]; for (int x = 0, bufferOffset = 0; x < dstWidth; x++, bufferOffset = x) {
dstBuffer[dstOffset++] = a << 24; int aSum = initializeAlphaSum(dstWidth, left, aHistory, right, dstBuffer, bufferOffset, 0);
bufferOffset = x;
int historyIdx = 0;
for (int y = 0; y < yStop; y++, bufferOffset += dstWidth) {
int a = vSumLookup[aSum];
dstBuffer[bufferOffset] = a << 24 | shadowRgb;
aSum -= aHistory[historyIdx]; aSum -= aHistory[historyIdx];
a = srcBuffer[srcOffset + srcX] >>> 24; a = dstBuffer[bufferOffset + lastPixelOffset] >>> 24;
aHistory[historyIdx] = a; aHistory[historyIdx] = a;
aSum += a; aSum += a;
if (++historyIdx >= shadowSize) { if (++historyIdx >= shadowSize) {
historyIdx -= shadowSize; historyIdx -= shadowSize;
} }
} }
for (int i = 0; i < shadowSize; i++) { for (int y = yStop; y < dstHeight; y++, bufferOffset += dstWidth) {
int a = hSumLookup[aSum]; int a = vSumLookup[aSum];
dstBuffer[dstOffset++] = a << 24; dstBuffer[bufferOffset] = a << 24 | shadowRgb;
aSum -= aHistory[historyIdx]; aSum -= aHistory[historyIdx];
if (++historyIdx >= shadowSize) { if (++historyIdx >= shadowSize) {
historyIdx -= shadowSize; historyIdx -= shadowSize;
} }
} }
} }
}
for (int x = 0, bufferOffset = 0; x < dstWidth; x++, bufferOffset = x) { private static int initializeAlphaSum(int dstWidth, int left, int[] aHistory, int right, int[] dstBuffer, int bufferOffset, int aSum) {
aSum = 0; int historyIdx;
for (historyIdx = 0; historyIdx < left; ) { for (historyIdx = 0; historyIdx < left; ) {
aHistory[historyIdx++] = 0; aHistory[historyIdx++] = 0;
} }
@ -133,30 +145,45 @@ public class ShadowRenderer {
aHistory[historyIdx++] = a; aHistory[historyIdx++] = a;
aSum += a; aSum += a;
} }
bufferOffset = x; return aSum;
}
private static void applyHorizontalShadowBlur(int left, int dstWidth, int srcHeight, int shadowSize,
int[] aHistory, int srcWidth, int[] hSumLookup, int[] dstBuffer, int[] srcBuffer) {
int historyIdx;
for (int srcY = 0, dstOffset = left * dstWidth; srcY < srcHeight; srcY++) {
for (historyIdx = 0; historyIdx < shadowSize; ) {
aHistory[historyIdx++] = 0;
}
int aSum = 0;
historyIdx = 0; historyIdx = 0;
for (int y = 0; y < yStop; y++, bufferOffset += dstWidth) { int srcOffset = srcY * srcWidth;
int a = vSumLookup[aSum]; for (int srcX = 0; srcX < srcWidth; srcX++) {
dstBuffer[bufferOffset] = a << 24 | shadowRgb; int a = hSumLookup[aSum];
dstBuffer[dstOffset++] = a << 24;
aSum -= aHistory[historyIdx]; aSum -= aHistory[historyIdx];
a = dstBuffer[bufferOffset + lastPixelOffset] >>> 24; a = srcBuffer[srcOffset + srcX] >>> 24;
aHistory[historyIdx] = a; aHistory[historyIdx] = a;
aSum += a; aSum += a;
if (++historyIdx >= shadowSize) { if (++historyIdx >= shadowSize) {
historyIdx -= shadowSize; historyIdx -= shadowSize;
} }
} }
for (int y = yStop; y < dstHeight; y++, bufferOffset += dstWidth) { dstOffset = updateDstBufferOffset(shadowSize, aHistory, hSumLookup, dstBuffer, aSum, dstOffset, historyIdx);
int a = vSumLookup[aSum]; }
dstBuffer[bufferOffset] = a << 24 | shadowRgb; }
private static int updateDstBufferOffset(int shadowSize, int[] aHistory, int[] hSumLookup, int[] dstBuffer,
int aSum, int dstOffset, int historyIdx) {
for (int i = 0; i < shadowSize; i++) {
int a = hSumLookup[aSum];
dstBuffer[dstOffset++] = a << 24;
aSum -= aHistory[historyIdx]; aSum -= aHistory[historyIdx];
if (++historyIdx >= shadowSize) { if (++historyIdx >= shadowSize) {
historyIdx -= shadowSize; historyIdx -= shadowSize;
} }
} }
} return dstOffset;
setPixels(dst, 0, 0, dstWidth, dstHeight, dstBuffer);
return dst;
} }
private int[] getPixels(BufferedImage img, int x, int y, int w, int h, int[] pixels) { private int[] getPixels(BufferedImage img, int x, int y, int w, int h, int[] pixels) {

Loading…
Cancel
Save