Compare commits
6 Commits
16e11e30ce
...
7119d1175c
| Author | SHA1 | Date | |
|---|---|---|---|
| 7119d1175c | |||
| 9e1ec1debc | |||
| e1b5cefc90 | |||
| f45bb15828 | |||
| f68cbba282 | |||
| 129dac18c5 |
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,3 +1,7 @@
|
||||
/.claude
|
||||
/CLAUDE.md
|
||||
/AGENTS.md
|
||||
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
|
||||
1
.idea/.gitignore
generated
vendored
1
.idea/.gitignore
generated
vendored
@ -1,3 +1,4 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
/CopilotChatHistory.xml
|
||||
|
||||
10
pom.xml
10
pom.xml
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>com.imyeyu.fx</groupId>
|
||||
<artifactId>timi-fx</artifactId>
|
||||
<version>0.0.1</version>
|
||||
<version>0.0.2</version>
|
||||
|
||||
<properties>
|
||||
<fx.version>21.0.2</fx.version>
|
||||
@ -25,7 +25,13 @@
|
||||
<dependency>
|
||||
<groupId>com.imyeyu.config</groupId>
|
||||
<artifactId>timi-config</artifactId>
|
||||
<version>0.0.1</version>
|
||||
<version>0.0.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.imyeyu.inject</groupId>
|
||||
<artifactId>timi-inject</artifactId>
|
||||
<version>0.0.2</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@ -1,10 +1,18 @@
|
||||
package com.imyeyu.fx;
|
||||
|
||||
import com.imyeyu.fx.utils.ScreenFX;
|
||||
import com.imyeyu.java.bean.Callback;
|
||||
import com.imyeyu.java.ref.Ref;
|
||||
import com.imyeyu.utils.Calc;
|
||||
import com.imyeyu.utils.OS;
|
||||
import javafx.application.Application;
|
||||
import javafx.application.Platform;
|
||||
import javafx.beans.Observable;
|
||||
import javafx.beans.binding.Bindings;
|
||||
import javafx.beans.binding.BooleanBinding;
|
||||
import javafx.beans.property.BooleanProperty;
|
||||
import javafx.beans.property.ReadOnlyBooleanProperty;
|
||||
import javafx.beans.property.SimpleBooleanProperty;
|
||||
import javafx.event.EventHandler;
|
||||
import javafx.geometry.Rectangle2D;
|
||||
import javafx.scene.Node;
|
||||
@ -18,10 +26,6 @@ import javafx.stage.Screen;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.stage.Window;
|
||||
import javafx.stage.WindowEvent;
|
||||
import com.imyeyu.fx.utils.ScreenFX;
|
||||
import com.imyeyu.java.ref.Ref;
|
||||
import com.imyeyu.utils.Calc;
|
||||
import com.imyeyu.utils.OS;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
@ -281,7 +285,7 @@ public final class TimiFX {
|
||||
* @param control 控件({@link javafx.scene.control.ListView}, {@link javafx.scene.control.TableView} 等)
|
||||
* @param index 指定项下标
|
||||
*/
|
||||
public static void scrollToCenter(Control control, int index) {
|
||||
public static void scrollToCenter(Control control, int index) {
|
||||
try {
|
||||
if (control.getSkin() == null) {
|
||||
return;
|
||||
@ -291,6 +295,9 @@ public final class TimiFX {
|
||||
if (flow == null) {
|
||||
throw new UnsupportedOperationException("unsupported this control");
|
||||
}
|
||||
if (flow.getCellCount() == 0 || flow.getCell(0) == null) {
|
||||
return;
|
||||
}
|
||||
int offset = Calc.floor(flow.getHeight() * .5 / flow.getCell(0).getHeight());
|
||||
// 滚动
|
||||
Method method = Ref.getMethod(control.getClass(), "scrollTo", int.class);
|
||||
@ -301,7 +308,38 @@ public final class TimiFX {
|
||||
}
|
||||
}
|
||||
|
||||
public static void addScrollFinishedListener(Control control, Callback callback) {
|
||||
BooleanProperty finish = new SimpleBooleanProperty(false);
|
||||
|
||||
control.skinProperty().addListener((obs, o, skin) -> {
|
||||
try {
|
||||
VirtualFlow<?> flow = Ref.getFieldValue(control.getSkin(), "flow", VirtualFlow.class);
|
||||
if (flow == null) {
|
||||
throw new UnsupportedOperationException("unsupported this control");
|
||||
}
|
||||
// 监听总高度和视口高度的变化
|
||||
Observable[] dependencies = {flow.layoutBoundsProperty(), flow.parentProperty()};
|
||||
BooleanBinding binding = Bindings.createBooleanBinding(() -> {
|
||||
double totalHeight = flow.prefHeight(-1);
|
||||
double viewportHeight = flow.getLayoutBounds().getHeight();
|
||||
if (viewportHeight <= 0 || totalHeight <= 0) {
|
||||
return false;
|
||||
}
|
||||
double maxVValue = (totalHeight - viewportHeight) / viewportHeight;
|
||||
double currentVValue = flow.getLayoutY() / viewportHeight;
|
||||
return currentVValue >= (maxVValue - 0.01);
|
||||
}, dependencies);
|
||||
finish.bind(binding);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
finish.addListener((obs, o, isFinished) -> {
|
||||
if (isFinished) {
|
||||
callback.handler();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 重启程序,命令需自定
|
||||
|
||||
@ -68,7 +68,7 @@ public enum Interpolates {
|
||||
*/
|
||||
public double[][] buildBezierPoint(double boost, double precision) {
|
||||
if (precision < 4) {
|
||||
throw new IllegalArgumentException("precision can not lessthan 4: " + precision);
|
||||
throw new IllegalArgumentException("precision can not less than 4: " + precision);
|
||||
}
|
||||
precision = 1D / precision;
|
||||
|
||||
@ -176,4 +176,4 @@ public enum Interpolates {
|
||||
public SplineInterpolator getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -155,6 +155,9 @@ public final class BindingsConfig {
|
||||
|
||||
@Override
|
||||
protected String serialize(Field field, ObjectProperty<Color> property) {
|
||||
if (property.getValue() == null) {
|
||||
return null;
|
||||
}
|
||||
return TimiFX.toHexString(property.getValue());
|
||||
}
|
||||
|
||||
@ -173,7 +176,14 @@ public final class BindingsConfig {
|
||||
|
||||
@Override
|
||||
protected String serialize(Field field, ObjectProperty<Paint> property) {
|
||||
return TimiFX.toHexString(Color.valueOf(property.toString()));
|
||||
Paint paint = property.getValue();
|
||||
if (paint == null) {
|
||||
return null;
|
||||
}
|
||||
if (paint instanceof Color color) {
|
||||
return TimiFX.toHexString(color);
|
||||
}
|
||||
return paint.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -191,6 +201,9 @@ public final class BindingsConfig {
|
||||
|
||||
@Override
|
||||
protected String serialize(Field field, ObjectProperty<Background> property) {
|
||||
if (property.getValue() == null || property.getValue().getFills().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return TimiFX.toHexString(Color.valueOf(property.getValue().getFills().getFirst().getFill().toString()));
|
||||
}
|
||||
|
||||
@ -225,7 +238,7 @@ public final class BindingsConfig {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("not found " + field.getType() + " property converter");
|
||||
throw new RuntimeException("未找到 " + field.getType() + " 属性转换器");
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@ -249,7 +262,7 @@ public final class BindingsConfig {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("not found " + genericType + " property converter");
|
||||
throw new RuntimeException("未找到 " + genericType + " 属性转换器");
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
74
src/main/java/com/imyeyu/fx/inject/FXTimiInject.java
Normal file
74
src/main/java/com/imyeyu/fx/inject/FXTimiInject.java
Normal file
@ -0,0 +1,74 @@
|
||||
package com.imyeyu.fx.inject;
|
||||
|
||||
import com.imyeyu.inject.TimiInject;
|
||||
import javafx.application.Application;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
/**
|
||||
* JavaFX 与 TimiInject 集成支持
|
||||
* <p>
|
||||
* 提供 JavaFX Application 和 Stage 对象到依赖注入容器的集成。
|
||||
* 在 Application.start() 方法中调用 {@link #register} 方法即可完成注册和注入。
|
||||
* <p>
|
||||
* 使用示例:
|
||||
* <pre>
|
||||
* public class Main extends Application {
|
||||
* @Inject
|
||||
* private SomeService service;
|
||||
*
|
||||
* @Override
|
||||
* public void start(Stage stage) {
|
||||
* TimiInject inject = FXTimiInject.run(Main.class, this, stage);
|
||||
* // 现在 service 已经被注入,可以使用了
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* @author 夜雨
|
||||
*/
|
||||
public final class FXTimiInject {
|
||||
|
||||
private FXTimiInject() {
|
||||
throw new UnsupportedOperationException("工具类不允许实例化");
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动容器并在初始化前注册 Application 和 Stage
|
||||
*
|
||||
* @param applicationClass 启动类
|
||||
* @param app JavaFX Application 实例
|
||||
* @param stage JavaFX 主舞台(Primary Stage)
|
||||
* @return TimiInject 容器实例
|
||||
*/
|
||||
public static TimiInject run(Class<?> applicationClass, Application app, Stage stage) {
|
||||
TimiInject inject = TimiInject.run(applicationClass, injector -> registerBeans(injector, app, stage));
|
||||
inject.inject(app);
|
||||
return inject;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 JavaFX Application 和 Stage 注册到容器,并对 Application 执行字段注入
|
||||
* <p>
|
||||
* 此方法会:
|
||||
* <ol>
|
||||
* <li>将 Application 对象以 "application" 为名称注册到容器</li>
|
||||
* <li>将 Stage 对象以 "stage" 和 "primaryStage" 为名称注册到容器</li>
|
||||
* <li>对 Application 对象执行字段注入,使其 @Inject 字段生效</li>
|
||||
* </ol>
|
||||
*
|
||||
* @param inject TimiInject 容器实例
|
||||
* @param app JavaFX Application 实例
|
||||
* @param stage JavaFX 主舞台(Primary Stage)
|
||||
*/
|
||||
public static void register(TimiInject inject, Application app, Stage stage) {
|
||||
registerBeans(inject, app, stage);
|
||||
inject.inject(app);
|
||||
}
|
||||
|
||||
private static void registerBeans(TimiInject inject, Application app, Stage stage) {
|
||||
// 注册 Application 和 Stage 到容器
|
||||
inject.registerBean("application", app);
|
||||
inject.registerBean("stage", stage);
|
||||
inject.registerBean("primaryStage", stage);
|
||||
}
|
||||
}
|
||||
18
src/main/java/com/imyeyu/fx/listener/NewValueListener.java
Normal file
18
src/main/java/com/imyeyu/fx/listener/NewValueListener.java
Normal file
@ -0,0 +1,18 @@
|
||||
package com.imyeyu.fx.listener;
|
||||
|
||||
import javafx.beans.value.ChangeListener;
|
||||
import javafx.beans.value.ObservableValue;
|
||||
|
||||
/**
|
||||
* @author 夜雨
|
||||
* @since 2025-07-21 23:22
|
||||
*/
|
||||
public interface NewValueListener<T> extends ChangeListener<T> {
|
||||
|
||||
void newValue(T newValue);
|
||||
|
||||
@Override
|
||||
default void changed(ObservableValue<? extends T> observable, T oldValue, T newValue) {
|
||||
this.newValue(newValue);
|
||||
}
|
||||
}
|
||||
17
src/main/java/com/imyeyu/fx/listener/OldValueListener.java
Normal file
17
src/main/java/com/imyeyu/fx/listener/OldValueListener.java
Normal file
@ -0,0 +1,17 @@
|
||||
package com.imyeyu.fx.listener;
|
||||
|
||||
import javafx.beans.value.ChangeListener;
|
||||
|
||||
/**
|
||||
* @author 夜雨
|
||||
* @since 2025-07-21 23:41
|
||||
*/
|
||||
public interface OldValueListener<T> extends ChangeListener<T> {
|
||||
|
||||
void oldValue(T oldValue);
|
||||
|
||||
@Override
|
||||
default void changed(javafx.beans.value.ObservableValue<? extends T> observable, T oldValue, T newValue) {
|
||||
this.oldValue(oldValue);
|
||||
}
|
||||
}
|
||||
@ -52,12 +52,8 @@ import com.imyeyu.java.bean.CallbackReturn;
|
||||
* </pre>
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since
|
||||
*
|
||||
* @since 2021-02-13 12:56
|
||||
* @param <T> 数据处理返回类型
|
||||
*
|
||||
* @author 夜雨
|
||||
* @version 2021-02-13 12:56
|
||||
*/
|
||||
public abstract class RunAsync<T> extends Service<T> {
|
||||
|
||||
@ -192,6 +188,7 @@ public abstract class RunAsync<T> extends Service<T> {
|
||||
/** 中断任务 */
|
||||
public void interrupt() {
|
||||
isInterrupted = true;
|
||||
cancel();
|
||||
}
|
||||
|
||||
// ---------- 静态构造 ----------
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
package com.imyeyu.fx.task;
|
||||
|
||||
import javafx.concurrent.Task;
|
||||
import com.imyeyu.java.bean.timi.TimiCode;
|
||||
import com.imyeyu.java.bean.timi.TimiException;
|
||||
import javafx.concurrent.Task;
|
||||
|
||||
/**
|
||||
* 异步守护执行,如果 {@link RunAsync} 超过指定时间没有回调,将触发 onTimeout
|
||||
@ -17,6 +17,9 @@ public abstract class RunAsyncDaemon<T> extends RunAsync<T> {
|
||||
/** true 为回调成功 */
|
||||
protected boolean isCallBack;
|
||||
|
||||
/** true 为回调超时 */
|
||||
protected boolean isTimeout;
|
||||
|
||||
/** 默认构造器(执行超时 2 秒) */
|
||||
public RunAsyncDaemon() {
|
||||
this(2000);
|
||||
@ -35,21 +38,23 @@ public abstract class RunAsyncDaemon<T> extends RunAsync<T> {
|
||||
protected Task<T> createTask() {
|
||||
super.createTask();
|
||||
|
||||
isTimeout = false;
|
||||
isCallBack = false;
|
||||
task.setOnSucceeded(e -> {
|
||||
isCallBack = true;
|
||||
onFinish();
|
||||
onFinish(lastT);
|
||||
onFinally();
|
||||
isCallBack = true;
|
||||
});
|
||||
task.setOnFailed(e -> {
|
||||
onFinally();
|
||||
isCallBack = true;
|
||||
onFinally();
|
||||
});
|
||||
task.setOnRunning(e -> new Thread(() -> {
|
||||
try {
|
||||
Thread.sleep(timeout);
|
||||
if (!isCallBack) {
|
||||
isTimeout = true;
|
||||
onTimeout();
|
||||
}
|
||||
} catch (InterruptedException ex) {
|
||||
|
||||
@ -219,6 +219,7 @@ public abstract class RunAsyncScheduled<T> extends ScheduledService<T> {
|
||||
/** 中断任务 */
|
||||
public void interrupt() {
|
||||
isInterrupted = true;
|
||||
cancel();
|
||||
}
|
||||
|
||||
// ---------- 静态构造 ----------
|
||||
|
||||
@ -326,7 +326,7 @@ public class AnimationRenderer {
|
||||
* 一次性动画
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2023-05-16 16:02
|
||||
* @since 2023-05-16 16:02
|
||||
*/
|
||||
public static class Animation {
|
||||
|
||||
@ -341,7 +341,7 @@ public class AnimationRenderer {
|
||||
* 一次性动画回调
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2023-05-14 10:10
|
||||
* @since 2023-05-14 10:10
|
||||
*/
|
||||
public interface AnimationCallback {
|
||||
|
||||
@ -353,4 +353,4 @@ public class AnimationRenderer {
|
||||
*/
|
||||
void handler(double deltaSecond, double percent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -195,4 +195,4 @@ public class BgFill {
|
||||
}
|
||||
return new BgFill(sb.toString()).build();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,8 +10,6 @@ import javafx.scene.layout.BackgroundRepeat;
|
||||
import javafx.scene.layout.BackgroundSize;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
/**
|
||||
* 背景图片封装。{@link #build()} 作为导出作用,最后执行
|
||||
@ -49,10 +47,9 @@ public class BgImage {
|
||||
* 背景图构造器
|
||||
*
|
||||
* @param file 图片文件(程序外资源)
|
||||
* @throws FileNotFoundException 找不到文件
|
||||
*/
|
||||
public BgImage(File file) throws FileNotFoundException {
|
||||
this(new Image(new FileInputStream(file)));
|
||||
public BgImage(File file) {
|
||||
this(new Image(file.toURI().toString()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -52,18 +52,6 @@ public class SmoothScroll {
|
||||
|
||||
// ---------- 虚拟面板滚动(列表项需统一高度) ----------
|
||||
|
||||
|
||||
/**
|
||||
* 平滑滚动 ComboBox 的选项列表
|
||||
*
|
||||
* @param comboBox 选择器
|
||||
* @deprecated 1.3.4 过时,1.6.0 移除,请使用 {@link #comboBox(ComboBox)}
|
||||
*/
|
||||
@Deprecated
|
||||
public static void combobox(ComboBox<?> comboBox) {
|
||||
comboBox(comboBox);
|
||||
}
|
||||
|
||||
/**
|
||||
* 平滑滚动 ComboBox 的选项列表
|
||||
*
|
||||
@ -323,6 +311,9 @@ public class SmoothScroll {
|
||||
* @param sizeFunc 高度回调
|
||||
*/
|
||||
public static void scrollPane(ScrollPane scrollPane, DoubleProperty scrollProperty, Function<Bounds, Double> sizeFunc) {
|
||||
if (scrollPane.getContent() == null) {
|
||||
return;
|
||||
}
|
||||
final double[] derivatives = new double[FRICTIONS.length];
|
||||
|
||||
Timeline timeline = new Timeline();
|
||||
@ -362,6 +353,9 @@ public class SmoothScroll {
|
||||
}
|
||||
double dy = derivatives[derivatives.length - 1];
|
||||
double size = sizeFunc.apply(scrollPane.getContent().getLayoutBounds());
|
||||
if (size <= 0) {
|
||||
return;
|
||||
}
|
||||
scrollProperty.set(Math.min(Math.max(scrollProperty.get() + dy / size, 0), 1));
|
||||
if (Math.abs(dy) < .001) {
|
||||
timeline.stop();
|
||||
|
||||
Reference in New Issue
Block a user