add FXTimiInject

This commit is contained in:
Timi
2026-01-13 17:50:44 +08:00
parent 9e1ec1debc
commit 7119d1175c
12 changed files with 122 additions and 21 deletions

4
.gitignore vendored
View File

@ -1,3 +1,7 @@
/.claude
/CLAUDE.md
/AGENTS.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/

10
pom.xml
View File

@ -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>

View File

@ -295,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);

View File

@ -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;
}
}
}

View File

@ -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 + " 属性转换器");
}
};

View 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 {
* &#64;Inject
* private SomeService service;
*
* &#64;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);
}
}

View File

@ -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();
}
// ---------- 静态构造 ----------

View File

@ -219,6 +219,7 @@ public abstract class RunAsyncScheduled<T> extends ScheduledService<T> {
/** 中断任务 */
public void interrupt() {
isInterrupted = true;
cancel();
}
// ---------- 静态构造 ----------

View File

@ -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);
}
}
}

View File

@ -195,4 +195,4 @@ public class BgFill {
}
return new BgFill(sb.toString()).build();
}
}
}

View File

@ -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()));
}
/**

View File

@ -311,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();
@ -350,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();