完成window开机自启

This commit is contained in:
yang 2022-10-13 12:43:56 +08:00
parent 7b3fb90865
commit 3b59fc631f
15 changed files with 296 additions and 91 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
.idea
.gradle
build
build
config

View File

@ -25,7 +25,7 @@ tasks.withType(JavaCompile) {
application {
mainModule = 'org.rococy.roomit'
mainClass = 'org.rococy.roomit.MainWindow'
mainClass = 'org.rococy.roomit.Launcher'
}
javafx {
@ -49,7 +49,7 @@ jar {
manifest {
attributes "Implementation-Title": project.name
attributes "Implementation-Version": '1.0'
attributes 'Main-Class': 'org.rococy.roomit.MainWindow'
attributes 'Main-Class': 'org.rococy.roomit.Launcher'
}
}

View File

@ -1,5 +1,8 @@
{
"shape": {
"basic": {
"selfStarting": true
},
"brush": {
"transparent": false,
"strokeColor": "linear-gradient(to bottom, #f64f59, #c471ed, #12c2e9)",

View File

@ -0,0 +1,47 @@
package org.rococy.roomit;
import javafx.application.Platform;
import org.rococy.roomit.constant.GlobalConsts;
import org.rococy.roomit.listener.TrayIconMouseListener;
import org.rococy.roomit.util.AutoStartFile;
import org.rococy.roomit.util.ResourceUtils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.io.IOException;
/**
* @author Rococy
* @date 2022/10/12
*/
public class Launcher {
public static void main(String[] args) throws IOException, AWTException {
new Launcher().init();
}
private void init() throws IOException, AWTException {
initPreConfig();
initTrayIcon();
}
private void initTrayIcon() throws IOException, AWTException {
// 获取系统托盘
SystemTray systemTray = SystemTray.getSystemTray();
// 加载托盘图片 16*16的png图片
TrayIcon trayIcon = new TrayIcon(ImageIO.read(ResourceUtils.getResource(GlobalConsts.LOGO_PATH)), "RoomIt");
// 托盘图片自适应大小
trayIcon.setImageAutoSize(true);
trayIcon.addMouseListener(new TrayIconMouseListener());
systemTray.add(trayIcon);
}
private void initPreConfig() {
AutoStartFile.toggleExists();
// 设置当程序所有窗口都关闭时JavaFx的UI线程继续运行
Platform.setImplicitExit(false);
Platform.startup(() -> {
});
}
}

View File

@ -11,14 +11,11 @@ import org.rococy.roomit.config.ConfigurationManager;
import org.rococy.roomit.constant.GlobalConsts;
import org.rococy.roomit.constant.KeyBoardConsts;
import org.rococy.roomit.domain.KeyBoardConfiguration;
import org.rococy.roomit.listener.TrayIconMouseListener;
import org.rococy.roomit.listener.WindowGlobalKeyBoardListener;
import org.rococy.roomit.util.KeyBoardUtils;
import org.rococy.roomit.util.ResourceUtils;
import org.rococy.roomit.util.ScreenUtils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.io.IOException;
/**
@ -26,27 +23,19 @@ import java.io.IOException;
*/
public class MainWindow extends Application {
private static Stage stage;
private static Stage stage = new Stage();
{
try {
// 设置全局快捷键
KeyBoardConfiguration keyBoardConfiguration = ConfigurationManager.getKeyboardConfiguration();
WindowGlobalKeyBoardListener.addEventListener(event -> {
String keyBoardText = KeyBoardUtils.parse(event);
String type = keyBoardConfiguration.getType(keyBoardText);
// 关闭或启动窗口
if (type.equals(KeyBoardConsts.OPEN_OR_CLOSE)) {
Platform.runLater(MainWindow::toggleDisplay);
}
});
// 初始化托盘图标
initTrayIcon();
// 设置当程序所有窗口都关闭时JavaFx的UI线程继续运行
Platform.setImplicitExit(false);
} catch (IOException | AWTException e) {
e.printStackTrace();
}
static {
// 设置全局快捷键
KeyBoardConfiguration keyBoardConfiguration = ConfigurationManager.getKeyboardConfiguration();
WindowGlobalKeyBoardListener.addEventListener(event -> {
String keyBoardText = KeyBoardUtils.parse(event);
String type = keyBoardConfiguration.getType(keyBoardText);
// 关闭或启动窗口
if (type.equals(KeyBoardConsts.OPEN_OR_CLOSE)) {
Platform.runLater(MainWindow::toggleDisplay);
}
});
}
@Override
@ -93,19 +82,4 @@ public class MainWindow extends Application {
}
}
private void initTrayIcon() throws IOException, AWTException {
// 获取系统托盘
SystemTray systemTray = SystemTray.getSystemTray();
// 加载托盘图片 16*16的png图片
TrayIcon trayIcon = new TrayIcon(ImageIO.read(ResourceUtils.getResource(GlobalConsts.LOGO_PATH)), "RoomIt");
// 托盘图片自适应大小
trayIcon.setImageAutoSize(true);
trayIcon.addMouseListener(new TrayIconMouseListener());
systemTray.add(trayIcon);
}
public static void main(String[] args) {
launch(args);
}
}

View File

@ -45,6 +45,10 @@ public class ConfigurationManager {
}
}
public static BasicConfiguration getBasicConfiguration() {
return shapeConfiguration.getBasic();
}
public static BrushConfiguration getBrushConfiguration() {
return shapeConfiguration.getBrush();
}
@ -68,10 +72,12 @@ public class ConfigurationManager {
// 形状配置
shapeConfiguration = new ShapeConfiguration();
BasicConfiguration basicConfiguration = gson.fromJson(shapeObject.get("basic"), BasicConfiguration.class);
BrushConfiguration brushConfiguration = gson.fromJson(shapeObject.get("brush"), BrushConfiguration.class);
RectangleConfiguration rectangleConfiguration = gson.fromJson(shapeObject.get("rectangle"), RectangleConfiguration.class);
EllipseConfiguration ellipseConfiguration = gson.fromJson(shapeObject.get("ellipse"), EllipseConfiguration.class);
shapeConfiguration.setBasic(basicConfiguration);
shapeConfiguration.setBrush(brushConfiguration);
shapeConfiguration.setRectangle(rectangleConfiguration);
shapeConfiguration.setEllipse(ellipseConfiguration);

View File

@ -1,5 +1,7 @@
package org.rococy.roomit.constant;
import org.rococy.roomit.util.ResourceUtils;
/**
* @author Rococy
* @date 2022/9/19
@ -24,6 +26,6 @@ public class GlobalConsts {
/**
* 配置文件路径
*/
public static final String CONFIGURATION_FILE_PATH = System.getProperty("user.dir") + "\\config.json";
public static final String CONFIGURATION_FILE_PATH = ResourceUtils.getResourceByProject("config/config.json");
}

View File

@ -14,9 +14,11 @@ import org.rococy.roomit.config.ConfigurationManager;
import org.rococy.roomit.constant.GlobalConsts;
import org.rococy.roomit.constant.KeyBoardConsts;
import org.rococy.roomit.control.RoomItSlider;
import org.rococy.roomit.domain.BasicConfiguration;
import org.rococy.roomit.domain.BrushConfiguration;
import org.rococy.roomit.domain.EllipseConfiguration;
import org.rococy.roomit.domain.RectangleConfiguration;
import org.rococy.roomit.util.AutoStartFile;
import org.rococy.roomit.util.ColorUtils;
import java.awt.*;
@ -31,6 +33,7 @@ import java.util.StringJoiner;
*/
public class SettingWindowController {
private final BasicConfiguration basicConfiguration = ConfigurationManager.getBasicConfiguration();
private final BrushConfiguration brushConfiguration = ConfigurationManager.getBrushConfiguration();
private final RectangleConfiguration rectangleConfiguration = ConfigurationManager.getRectangleConfiguration();
private final EllipseConfiguration ellipseConfiguration = ConfigurationManager.getEllipseConfiguration();
@ -42,7 +45,7 @@ public class SettingWindowController {
* 左边导航区域
*/
@FXML
private Button shapeBtn;
private Button basicBtn;
private Button preSelectedBtn;
/**
@ -58,6 +61,13 @@ public class SettingWindowController {
*/
@FXML
private ToggleGroup transparentGroup;
/**
* 是否透明
*/
@FXML
private RadioButton transparentRadioBtn;
@FXML
private RadioButton notTransparentRadioBtn;
@FXML
private ColorPicker brushStrokeColorPicker;
@FXML
@ -79,10 +89,15 @@ public class SettingWindowController {
@FXML
private RoomItSlider ellipseBorderWidthSlider;
/**
* 是否开机自启
*/
@FXML
private RadioButton transparentRadioBtn;
private ToggleGroup selfStartingGroup;
@FXML
private RadioButton notTransparentRadioBtn;
private RadioButton selfStartingBtn;
@FXML
private RadioButton notSelfStartingBtn;
/**
* 快捷键
@ -173,12 +188,13 @@ public class SettingWindowController {
@FXML
public void initialize() {
preSelectedBtn = shapeBtn;
preSelectedBtn = basicBtn;
initConfiguration();
initConfigurationListener();
}
private void initConfiguration() {
initBasicConfiguration();
initBrushConfiguration();
initRectangleConfiguration();
initEllipseConfiguration();
@ -186,6 +202,12 @@ public class SettingWindowController {
}
private void initConfigurationListener() {
// 是否开机自启
selfStartingGroup.selectedToggleProperty()
.addListener((observable, oldValue, newValue) -> {
basicConfiguration.setSelfStarting(Boolean.parseBoolean((String) newValue.getUserData()));
AutoStartFile.toggleExists();
});
// 是否透明
transparentGroup.selectedToggleProperty()
.addListener((observable, oldValue, newValue) -> brushConfiguration.setTransparent(Boolean.parseBoolean((String) newValue.getUserData())));
@ -208,6 +230,15 @@ public class SettingWindowController {
.addListener((observable, oldValue, newValue) -> ellipseConfiguration.setBorderWidth(Math.round(newValue.doubleValue())));
}
private void initBasicConfiguration() {
// 是否开机自启
if (basicConfiguration.getSelfStarting()) {
selfStartingBtn.setSelected(true);
} else {
notSelfStartingBtn.setSelected(true);
}
}
private void initBrushConfiguration() {
// 是否透明
if (brushConfiguration.getTransparent()) {

View File

@ -0,0 +1,18 @@
package org.rococy.roomit.domain;
/**
* @author Rococy
* @date 2022/10/12
*/
public class BasicConfiguration {
private Boolean selfStarting;
public Boolean getSelfStarting() {
return selfStarting;
}
public void setSelfStarting(Boolean selfStarting) {
this.selfStarting = selfStarting;
}
}

View File

@ -6,10 +6,19 @@ package org.rococy.roomit.domain;
*/
public class ShapeConfiguration {
private BasicConfiguration basic;
private BrushConfiguration brush;
private RectangleConfiguration rectangle;
private EllipseConfiguration ellipse;
public BasicConfiguration getBasic() {
return basic;
}
public void setBasic(BasicConfiguration basic) {
this.basic = basic;
}
public BrushConfiguration getBrush() {
return brush;
}

View File

@ -25,22 +25,17 @@ public class TrayIconMouseListener implements MouseListener {
public void mousePressed(MouseEvent e) {
switch (e.getButton()) {
// 左键
case MOUSE_LEFT_BUTTON: {
Platform.runLater(MainWindow::toggleDisplay);
break;
}
case MOUSE_LEFT_BUTTON -> Platform.runLater(MainWindow::toggleDisplay);
// 右键
case MOUSE_RIGHT_BUTTON: {
case MOUSE_RIGHT_BUTTON -> {
Platform.runLater(() -> {
if (trayWindow == null) {
trayWindow = new TrayWindow();
}
trayWindow.show(e.getXOnScreen(), e.getYOnScreen());
});
break;
}
}
}

View File

@ -0,0 +1,92 @@
package org.rococy.roomit.util;
import org.rococy.roomit.config.ConfigurationManager;
import org.rococy.roomit.domain.BasicConfiguration;
import java.io.*;
import java.util.StringJoiner;
/**
* @author Rococy
* @date 2022/10/12
*/
public class AutoStartFile {
private static final String START_UP_FILE_PATH = "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\RoomIt.bat";
private static final String COPY_AUTO_START_FILE_PATH = ResourceUtils.getResourceByProject("config/copy_auto_start_file.bat");
private static final String CANCEL_AUTO_START_FILE_PATH = ResourceUtils.getResourceByProject("config/cancel_auto_start.bat");
private static final String BAT_FILE_PATH = ResourceUtils.getResourceByProject("config/RoomIt.bat");
private static final String AUTO_START_EXEC_FILE_PATH = ResourceUtils.getResourceByProject("");
private static final BasicConfiguration BASIC_CONFIGURATION = ConfigurationManager.getBasicConfiguration();
public static void toggleExists() {
if (BASIC_CONFIGURATION.getSelfStarting()) {
generate();
} else {
delete();
}
}
private static void generate() {
try {
if (new File(START_UP_FILE_PATH).exists()) {
return;
}
File file = new File(BAT_FILE_PATH);
if (!file.exists()) {
file.createNewFile();
}
FileWriter fos = new FileWriter(file);
StringJoiner fileContentJoiner = new StringJoiner("\n");
String driveLetter = AUTO_START_EXEC_FILE_PATH.substring(0, 2);
fileContentJoiner.add("@echo off");
fileContentJoiner.add(driveLetter);
fileContentJoiner.add("cd " + ResourceUtils.getResourceByProject(""));
fileContentJoiner.add("RoomIt");
fos.write(fileContentJoiner.toString());
fos.flush();
fos.close();
executeBatFile(COPY_AUTO_START_FILE_PATH);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void delete() {
executeBatFile(CANCEL_AUTO_START_FILE_PATH);
}
/**
* 执行bat文件
*
* @param file bat文件路径
* @return bat文件输出log
*/
private static String executeBatFile(String file) {
String cmdCommand = "cmd.exe /c " + file;
StringBuilder stringBuilder = new StringBuilder();
Process process;
try {
process = Runtime.getRuntime().exec(cmdCommand);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream(), "GBK"));
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
return stringBuilder.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

View File

@ -13,10 +13,11 @@ public class ResourceUtils {
private static final Class<MainWindow> MAIN_WINDOW_CLASS = MainWindow.class;
private static final String USER_DIR = System.getProperty("user.dir").replaceAll("\\\\", "/");
private ResourceUtils() {
}
public static URL getResource(String name) {
return MAIN_WINDOW_CLASS.getResource(name);
}
@ -25,4 +26,7 @@ public class ResourceUtils {
return MAIN_WINDOW_CLASS.getResourceAsStream(name);
}
public static String getResourceByProject(String name) {
return USER_DIR.concat("/").concat(name);
}
}

View File

@ -42,7 +42,7 @@
.content {
-fx-pref-width: 400px;
-fx-padding: 10px 10px 20px;
-fx-spacing: 35px;
-fx-spacing: 20px;
}
.content .h1 {

View File

@ -10,20 +10,43 @@
fx:id="container" id="container">
<VBox styleClass="side">
<!-- 形状 -->
<Button viewOrder="0" text="形 状" fx:id="shapeBtn" onMouseClicked="#switchSideButtons"
<!-- 基础 -->
<Button viewOrder="0" text="基 础" fx:id="basicBtn" onMouseClicked="#switchSideButtons"
styleClass="side-button, selected"/>
<!-- 形状 -->
<Button viewOrder="1" text="形 状" fx:id="shapeBtn" onMouseClicked="#switchSideButtons"
styleClass="side-button"/>
<!-- 快捷键 -->
<Button viewOrder="1" text="快 捷 键" fx:id="keyBoardBtn" onMouseClicked="#switchSideButtons"
<Button viewOrder="2" text="快 捷 键" fx:id="keyBoardBtn" onMouseClicked="#switchSideButtons"
styleClass="side-button"/>
<!-- 关于 -->
<Button viewOrder="2" text="关 于" fx:id="aboutBtn" onMouseClicked="#switchSideButtons" styleClass="side-button"/>
<Button viewOrder="3" text="关 于" fx:id="aboutBtn" onMouseClicked="#switchSideButtons" styleClass="side-button"/>
</VBox>
<ScrollPane fx:id="scrollPane" styleClass="scroll-pane">
<VBox styleClass="content" fx:id="contentVbox">
<!-- 形状 -->
<!-- 基础 -->
<VBox userData="0" style="-fx-spacing: 8px">
<Label styleClass="h1">基础</Label>
<VBox style="-fx-spacing: 15px">
<!-- 开机自启 -->
<VBox>
<HBox style="-fx-spacing: 10px">
<Label>开机自启:</Label>
<fx:define>
<ToggleGroup fx:id="selfStartingGroup"/>
</fx:define>
<RadioButton fx:id="selfStartingBtn" text="是" userData="true"
toggleGroup="$selfStartingGroup" selected="true"/>
<RadioButton fx:id="notSelfStartingBtn" text="否" userData="false"
toggleGroup="$selfStartingGroup"/>
</HBox>
</VBox>
</VBox>
</VBox>
<!-- 形状 -->
<VBox userData="0.13" style="-fx-spacing: 8px">
<Label styleClass="h1">形状</Label>
<VBox style="-fx-spacing: 15px">
@ -49,7 +72,7 @@
<HBox style="-fx-spacing: 10px">
<Label style="-fx-padding: 0px 0">线条粗细:</Label>
<RoomItSlider fx:id="brushLineWidthSlider" styleClass="jfx-slider" min="1" max="10"
blockIncrement="1"/>
blockIncrement="1"/>
</HBox>
</VBox>
@ -65,7 +88,7 @@
<HBox style="-fx-spacing: 10px">
<Label style="-fx-padding: 0px 0">边框粗细:</Label>
<RoomItSlider fx:id="rectangleBorderWidthSlider" styleClass="jfx-slider" min="1" max="10"
blockIncrement="1"/>
blockIncrement="1"/>
</HBox>
</VBox>
@ -81,14 +104,14 @@
<HBox style="-fx-spacing: 10px">
<Label style="-fx-padding: 0px 0">边框粗细:</Label>
<RoomItSlider fx:id="ellipseBorderWidthSlider" styleClass="jfx-slider" min="1" max="10"
blockIncrement="1"/>
blockIncrement="1"/>
</HBox>
</VBox>
</VBox>
</VBox>
<!-- 快捷键 -->
<VBox userData="0.70">
<VBox userData="0.81">
<Label styleClass="h1">快捷键</Label>
<GridPane styleClass="keyboard-wrapper">
<!-- 定义行和列 -->
@ -110,57 +133,57 @@
<Label styleClass="label" GridPane.rowIndex="1" GridPane.columnIndex="1">切换为画笔:</Label>
<TextField fx:id="brushKeyboard" userData="brush" editable="false"
onKeyPressed="#keyboardTextFieldKeyPress"
onMouseClicked="#keyboardTextFieldMouseClick"
styleClass="jfx-text-field" GridPane.rowIndex="1" GridPane.columnIndex="2"/>
onKeyPressed="#keyboardTextFieldKeyPress"
onMouseClicked="#keyboardTextFieldMouseClick"
styleClass="jfx-text-field" GridPane.rowIndex="1" GridPane.columnIndex="2"/>
<Label styleClass="label" GridPane.rowIndex="2" GridPane.columnIndex="1">切换为矩形:</Label>
<TextField fx:id="rectangleKeyboard" userData="rectangle" editable="false"
onKeyPressed="#keyboardTextFieldKeyPress"
onMouseClicked="#keyboardTextFieldMouseClick" GridPane.rowIndex="2"
GridPane.columnIndex="2"/>
onKeyPressed="#keyboardTextFieldKeyPress"
onMouseClicked="#keyboardTextFieldMouseClick" GridPane.rowIndex="2"
GridPane.columnIndex="2"/>
<Label styleClass="label" GridPane.rowIndex="3" GridPane.columnIndex="1">切换为椭圆:</Label>
<TextField fx:id="ellipseKeyboard" userData="ellipse" editable="false"
onKeyPressed="#keyboardTextFieldKeyPress"
onMouseClicked="#keyboardTextFieldMouseClick" GridPane.rowIndex="3"
GridPane.columnIndex="2"/>
onKeyPressed="#keyboardTextFieldKeyPress"
onMouseClicked="#keyboardTextFieldMouseClick" GridPane.rowIndex="3"
GridPane.columnIndex="2"/>
<Label styleClass="label" GridPane.rowIndex="4" GridPane.columnIndex="1">弹出工具箱:</Label>
<TextField fx:id="toolsKeyboard" userData="tools" editable="false"
onKeyPressed="#keyboardTextFieldKeyPress"
onMouseClicked="#keyboardTextFieldMouseClick" GridPane.rowIndex="4"
GridPane.columnIndex="2"/>
onKeyPressed="#keyboardTextFieldKeyPress"
onMouseClicked="#keyboardTextFieldMouseClick" GridPane.rowIndex="4"
GridPane.columnIndex="2"/>
<Label styleClass="label" GridPane.rowIndex="5" GridPane.columnIndex="1">弹出颜色框:</Label>
<TextField fx:id="colorPickerKeyboard" userData="colorPicker" editable="false"
onKeyPressed="#keyboardTextFieldKeyPress"
onMouseClicked="#keyboardTextFieldMouseClick" GridPane.rowIndex="5"
GridPane.columnIndex="2"/>
onKeyPressed="#keyboardTextFieldKeyPress"
onMouseClicked="#keyboardTextFieldMouseClick" GridPane.rowIndex="5"
GridPane.columnIndex="2"/>
<Label styleClass="label" GridPane.rowIndex="6" GridPane.columnIndex="1">放大鼠标:</Label>
<TextField fx:id="expendMouseKeyboard" userData="expendMouse" editable="false"
onKeyPressed="#keyboardTextFieldKeyPress"
onMouseClicked="#keyboardTextFieldMouseClick" GridPane.rowIndex="6"
GridPane.columnIndex="2"/>
onKeyPressed="#keyboardTextFieldKeyPress"
onMouseClicked="#keyboardTextFieldMouseClick" GridPane.rowIndex="6"
GridPane.columnIndex="2"/>
<Label styleClass="label" GridPane.rowIndex="7" GridPane.columnIndex="1">清空画板:</Label>
<TextField fx:id="clearKeyboard" userData="clear" editable="false"
onKeyPressed="#keyboardTextFieldKeyPress"
onMouseClicked="#keyboardTextFieldMouseClick" GridPane.rowIndex="7"
GridPane.columnIndex="2"/>
onKeyPressed="#keyboardTextFieldKeyPress"
onMouseClicked="#keyboardTextFieldMouseClick" GridPane.rowIndex="7"
GridPane.columnIndex="2"/>
<Label styleClass="label" GridPane.rowIndex="8" GridPane.columnIndex="1">截图并复制:</Label>
<TextField fx:id="screenshotAndCopyKeyboard" userData="screenshotAndCopy" editable="false"
onKeyPressed="#keyboardTextFieldKeyPress"
onMouseClicked="#keyboardTextFieldMouseClick"
GridPane.rowIndex="8" GridPane.columnIndex="2"/>
onKeyPressed="#keyboardTextFieldKeyPress"
onMouseClicked="#keyboardTextFieldMouseClick"
GridPane.rowIndex="8" GridPane.columnIndex="2"/>
<Label styleClass="label" GridPane.rowIndex="9" GridPane.columnIndex="1">启动或关闭:</Label>
<TextField fx:id="openOrCloseKeyboard" userData="openOrClose" editable="false"
onKeyPressed="#keyboardTextFieldKeyPress"
onMouseClicked="#keyboardTextFieldMouseClick" GridPane.rowIndex="9"
GridPane.columnIndex="2"/>
onKeyPressed="#keyboardTextFieldKeyPress"
onMouseClicked="#keyboardTextFieldMouseClick" GridPane.rowIndex="9"
GridPane.columnIndex="2"/>
</GridPane>
</VBox>