优化代码

This commit is contained in:
yang 2022-10-17 14:52:33 +08:00
parent ea1d0a65f7
commit 2f24113d98
7 changed files with 76 additions and 63 deletions

View File

@ -40,6 +40,7 @@ public class Launcher {
AutoStartFile.toggleExists();
// 设置当程序所有窗口都关闭时JavaFx的UI线程继续运行
Platform.setImplicitExit(false);
// 初始化Toolkit
Platform.startup(() -> {
});
}

View File

@ -23,6 +23,18 @@ public class TrayWindow extends Application {
private final Stage stage = new Stage();
/**
* 单例设计模式
*/
private static TrayWindow trayWindow = null;
private TrayWindow() {}
public static TrayWindow getInstance() {
if(trayWindow == null) {
trayWindow = new TrayWindow();
}
return trayWindow;
}
{
try {
// 加载FXML文件

View File

@ -0,0 +1,34 @@
package org.rococy.roomit.adapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
/**
* @author Rococy
* @date 2022/10/17
*/
public abstract class MouseListenerAdapter implements MouseListener {
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}

View File

@ -27,19 +27,7 @@ public class ConfigurationManager {
static {
try {
initConfiguration(Files.readString(CONFIGURATION_FILE_PATH));
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Map<String, Object> configMap = new HashMap<>(2);
configMap.put("shape", shapeConfiguration);
configMap.put("keyboard", keyBoardConfiguration.getKeyBoardMap());
try {
Files.writeString(CONFIGURATION_FILE_PATH, gson.toJson(configMap));
} catch (IOException e) {
e.printStackTrace();
}
}));
addConfigShutHook();
} catch (IOException e) {
e.printStackTrace();
}
@ -65,6 +53,11 @@ public class ConfigurationManager {
return keyBoardConfiguration;
}
/**
* 解析配置文件的Json字符串
*
* @param jsonStr String
*/
private static void initConfiguration(String jsonStr) {
Gson gson = new Gson();
JsonObject rootObject = JsonParser.parseString(jsonStr).getAsJsonObject();
@ -86,4 +79,21 @@ public class ConfigurationManager {
keyBoardConfiguration = new KeyBoardConfiguration(gson.fromJson(rootObject.get("keyboard"), Map.class));
}
/**
* 程序退出时更新配置文件
*/
private static void addConfigShutHook() {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Map<String, Object> configMap = new HashMap<>(2);
configMap.put("shape", shapeConfiguration);
configMap.put("keyboard", keyBoardConfiguration.getKeyBoardMap());
try {
Files.writeString(CONFIGURATION_FILE_PATH, gson.toJson(configMap));
} catch (IOException e) {
e.printStackTrace();
}
}));
}
}

View File

@ -25,12 +25,4 @@ public class RectangleConfiguration {
public void setBorderWidth(double borderWidth) {
this.borderWidth = borderWidth;
}
@Override
public String toString() {
return "RectangleConfiguration{" +
"borderColor='" + borderColor + '\'' +
", borderWidth=" + borderWidth +
'}';
}
}

View File

@ -3,24 +3,18 @@ package org.rococy.roomit.listener;
import javafx.application.Platform;
import org.rococy.roomit.MainWindow;
import org.rococy.roomit.TrayWindow;
import org.rococy.roomit.adapter.MouseListenerAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
/**
* @author Rococy
* @date 2022/9/25
*/
public class TrayIconMouseListener implements MouseListener {
public class TrayIconMouseListener extends MouseListenerAdapter {
private static final int MOUSE_LEFT_BUTTON = 1, MOUSE_RIGHT_BUTTON = 3;
private TrayWindow trayWindow;
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
switch (e.getButton()) {
@ -28,26 +22,8 @@ public class TrayIconMouseListener implements MouseListener {
case MOUSE_LEFT_BUTTON -> Platform.runLater(MainWindow::toggleDisplay);
// 右键
case MOUSE_RIGHT_BUTTON -> {
Platform.runLater(() -> {
if (trayWindow == null) {
trayWindow = new TrayWindow();
}
trayWindow.show(e.getXOnScreen(), e.getYOnScreen());
});
}
case MOUSE_RIGHT_BUTTON -> Platform.runLater(() -> TrayWindow.getInstance().show(e.getXOnScreen(), e.getYOnScreen()));
}
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}

View File

@ -42,7 +42,7 @@ public class WindowGlobalKeyBoardListener {
int code = info.vkCode;
switch (wParam.intValue()) {
// 键盘按下
case WinUser.WM_KEYDOWN: {
case WinUser.WM_KEYDOWN -> {
if (code == 160) {
keyEvent.isShiftDown = true;
} else if (code == 162) {
@ -55,10 +55,10 @@ public class WindowGlobalKeyBoardListener {
// 触发事件
EVENT_LIST.forEach(c -> c.accept(new KeyEvent(null, null, null, null, null, keyEvent.keyCode, keyEvent.isShiftDown, keyEvent.isCtrlDown, keyEvent.isAltDown, false)));
}
break;
}
// 键盘抬起
case WinUser.WM_KEYUP: {
case WinUser.WM_KEYUP -> {
if (code == 160) {
keyEvent.isShiftDown = false;
}
@ -70,8 +70,6 @@ public class WindowGlobalKeyBoardListener {
if (code == 164) {
keyEvent.isAltDown = false;
}
break;
}
}
}
@ -100,16 +98,6 @@ public class WindowGlobalKeyBoardListener {
private boolean isCtrlDown;
private boolean isAltDown;
private KeyCode keyCode;
@Override
public String toString() {
return "GlobalKeyEvent{" +
"isShiftDown=" + isShiftDown +
", isCtrlDown=" + isCtrlDown +
", isAltDown=" + isAltDown +
", keyCode=" + keyCode +
'}';
}
}
}