Skip to content

Commit

Permalink
Merge pull request #97 from litwak913/window-system
Browse files Browse the repository at this point in the history
 Added window system switching feature
  • Loading branch information
isHarryh authored Jan 19, 2025
2 parents 2e5ed11 + 6f967ff commit d3676d4
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 9 deletions.
3 changes: 2 additions & 1 deletion assets/ArkPetsConfigDefault.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@
"transition_duration":0.3,
"transition_type":"EASE_OUT_CUBIC",
"window_style_toolwindow":true,
"window_style_topmost":true
"window_style_topmost":true,
"window_system":"AUTO"
}
5 changes: 5 additions & 0 deletions assets/UI/SettingsModule.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@
<JFXCheckBox fx:id="configWindowToolwindow" mnemonicParsing="false" text="桌宠作为后台程序启动"/>
<JFXButton fx:id="configWindowToolwindowHelp"/>
</HBox>
<HBox>
<Label text="窗口系统"/>
<JFXComboBox fx:id="configWindowSystem" prefWidth="100.0"/>
<JFXButton fx:id="configWindowSystemHelp"/>
</HBox>
<Separator/>
<HBox>
<Label text="日志级别"/>
Expand Down
14 changes: 14 additions & 0 deletions core/src/cn/harryh/arkpets/ArkConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
package cn.harryh.arkpets;

import cn.harryh.arkpets.platform.WindowSystem;
import cn.harryh.arkpets.transitions.EasingFunction;
import cn.harryh.arkpets.utils.IOUtils.FileUtil;
import cn.harryh.arkpets.utils.Logger;
Expand Down Expand Up @@ -100,6 +101,8 @@ public class ArkConfig implements Serializable {
public boolean window_style_toolwindow;
/** @since ArkPets 3.2 */ @JSONField(defaultValue = "true")
public boolean window_style_topmost;
/** @since ArkPets 4.0 */ @JSONField(defaultValue = "AUTO")
public String window_system;

private ArkConfig() {
}
Expand Down Expand Up @@ -175,6 +178,17 @@ public static EasingFunction getEasingFunctionFrom(String string) {
}
}

/** @see WindowSystem
*/
public static WindowSystem getWindowSystemFrom(String string) {
try {
return WindowSystem.valueOf(string);
} catch (IllegalArgumentException e) {
Logger.warn("Config", "Invalid window system, using auto detect");
return WindowSystem.AUTO;
}
}

/** @see com.badlogic.gdx.graphics.Color
*/
public static Color getGdxColorFrom(String string) {
Expand Down
3 changes: 3 additions & 0 deletions core/src/cn/harryh/arkpets/concurrent/ProcessPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;


Expand Down Expand Up @@ -67,6 +68,8 @@ public Future<ProcessResult> submit(Class<?> clazz, List<String> jvmArgs, List<S
command.addAll(args);
// Process execution
ProcessBuilder builder = new ProcessBuilder(command);
Map<String, String> env = builder.environment();
env.remove("GDK_BACKEND");
Process process = builder.inheritIO().start();
int exitValue = process.waitFor();
return new ProcessResult(exitValue, process.pid());
Expand Down
21 changes: 21 additions & 0 deletions core/src/cn/harryh/arkpets/platform/NullHWndCtrl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,27 @@
package cn.harryh.arkpets.platform;


import com.badlogic.gdx.Gdx;


public class NullHWndCtrl extends HWndCtrl {
private static boolean startupFind;
private int lastw, lasth;

public NullHWndCtrl() {
super("", new WindowRect());
}

public static NullHWndCtrl find(String className, String windowName) {
if (windowName.equals("ArkPets")) {
if (!NullHWndCtrl.startupFind) {
NullHWndCtrl.startupFind = true;
return null;
}
}
return new NullHWndCtrl();
}

@Override
public boolean isForeground() {
return false;
Expand All @@ -35,6 +51,11 @@ public void setForeground() {

@Override
public void setWindowPosition(HWndCtrl insertAfter, int x, int y, int w, int h) {
if (lasth != h || lastw != w) {
lasth = h;
lastw = w;
Gdx.graphics.setWindowedMode(w, h);
}
}

@Override
Expand Down
39 changes: 36 additions & 3 deletions core/src/cn/harryh/arkpets/platform/WindowSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,24 @@ public static WindowSystem detectWindowSystem() {
}

/** Initializes the platform window system.
* @param platform WindowSystem to initialize.
*/
public static void init() {
PLATFORM = detectWindowSystem();
public static void init(WindowSystem platform) {
PLATFORM = platform;
if (PLATFORM == WindowSystem.AUTO){
PLATFORM = detectWindowSystem();
}
Logger.info("System", "Using " + PLATFORM.toString() + " Window System");
switch (PLATFORM) {
// TODO
}
}

/** Get current WindowSystem.
* @return The current WindowSystem.
*/
public static WindowSystem getWindowSystem() {
return PLATFORM;
}

/** Finds a window.
Expand All @@ -58,7 +72,7 @@ public static HWndCtrl findWindow(String className, String windowText) {
return User32HWndCtrl.find(className, windowText);
}
default -> {
return new NullHWndCtrl();
return NullHWndCtrl.find(className, windowText);
}
}
}
Expand Down Expand Up @@ -97,4 +111,23 @@ public static HWndCtrl getTopmostWindow() {
public static void free() {
// TODO
}

/** Return current WindowSystem should enable resize.
*/
public static boolean needResize() {
switch (PLATFORM) {
case X11, MUTTER, KWIN -> {
return true;
}
default -> {
return false;
}
}
}

/** Return current WindowSystem should enable decoration.
*/
public static boolean needDecorated() {
return PLATFORM == NULL;
}
}
10 changes: 5 additions & 5 deletions desktop/src/cn/harryh/arkpets/EmbeddedLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static void main (String[] args) {
ArgPending.argCache = args;
// Logger
Logger.initialize(LogConfig.logCorePath, LogConfig.logCoreMaxKeep);
ArkConfig appConfig = Objects.requireNonNull(ArkConfig.getConfig());
ArkConfig appConfig = Objects.requireNonNull(ArkConfig.getConfig(), "ArkConfig returns a null instance, please check the config file.");
try {
Logger.setLevel(appConfig.logging_level);
} catch (Exception ignored) {
Expand Down Expand Up @@ -82,16 +82,16 @@ protected void process(String command, String addition) {
Logger.info("System", "Entering the app of EmbeddedLauncher");
Logger.info("System", "ArkPets version is " + appVersion);
Logger.debug("System", "Default charset is " + Charset.defaultCharset());

WindowSystem windowSystem = ArkConfig.getWindowSystemFrom(appConfig.window_system);
try {
WindowSystem.init();
WindowSystem.init(windowSystem);
Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
// Configure FPS
config.setForegroundFPS(fpsDefault);
config.setIdleFPS(fpsDefault);
// Configure window layout
config.setDecorated(false);
config.setResizable(false);
config.setDecorated(WindowSystem.needDecorated());
config.setResizable(WindowSystem.needResize());
config.setWindowedMode(coreWidthDefault, coreHeightDefault);
config.setWindowPosition(0, 0);
// Configure window title
Expand Down
63 changes: 63 additions & 0 deletions desktop/src/cn/harryh/arkpets/controllers/SettingsModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import cn.harryh.arkpets.guitasks.CheckAppUpdateTask;
import cn.harryh.arkpets.guitasks.GuiTask;
import cn.harryh.arkpets.platform.StartupConfig;
import cn.harryh.arkpets.platform.WindowSystem;
import cn.harryh.arkpets.utils.*;
import cn.harryh.arkpets.utils.GuiComponents.*;
import com.badlogic.gdx.graphics.Color;
import com.jfoenix.controls.*;
import com.sun.jna.Platform;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.fxml.FXML;
Expand All @@ -25,6 +27,7 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -92,6 +95,10 @@ public final class SettingsModule implements Controller<ArkHomeFX> {
@FXML
private JFXButton configWindowToolwindowHelp;

@FXML
private JFXComboBox<NamedItem<String>> configWindowSystem;
@FXML
private JFXButton configWindowSystemHelp;
@FXML
private Label aboutQueryUpdate;
@FXML
Expand Down Expand Up @@ -350,6 +357,62 @@ public String getContent() {
};
}
};

NamedItem<String>[] items = (NamedItem[]) getWindowSystemItems().toArray();
new ComboBoxSetup<>(configWindowSystem).setItems(items)
.selectValue(app.config.window_system, app.config.window_system)
.setOnNonNullValueUpdated((observable, oldValue, newValue) -> {
app.config.window_system = newValue.value();
app.config.save();
});
new HelpHandbookEntrance(app.body, configWindowSystemHelp) {
@Override
public Handbook getHandbook() {
return new ControlHelpHandbook((Labeled) configWindowSystem.getParent().getChildrenUnmodifiable().get(0)) {
@Override
public String getContent() {
return getWindowSystemInfo();
}
};
}
};
}

private static ArrayList<NamedItem<String>> getWindowSystemItems() {
ArrayList<NamedItem<String>> windowSystemItems = new ArrayList<>();
windowSystemItems.add(new NamedItem<>("自动", WindowSystem.AUTO.name()));
if (Platform.isWindows()) {
windowSystemItems.add(new NamedItem<>("User32", WindowSystem.USER32.name()));
}
if (Platform.isLinux()) {
windowSystemItems.add(new NamedItem<>("X11", WindowSystem.X11.name()));
windowSystemItems.add(new NamedItem<>("Mutter", WindowSystem.MUTTER.name()));
windowSystemItems.add(new NamedItem<>("KWin", WindowSystem.KWIN.name()));
}
if (Platform.isMac()) {
windowSystemItems.add(new NamedItem<>("Quartz", WindowSystem.QUARTZ.name()));
}
windowSystemItems.add(new NamedItem<>("NULL", WindowSystem.NULL.name()));
return windowSystemItems;
}

private static String getWindowSystemInfo() {
String content = "不同平台对于窗口查询、操作有不同的 API,除非你遇到了桌宠窗口的问题,否则通常不需要更改。以下是对 API 的简单介绍:\n";
if (Platform.isWindows()) {
content += "User32 —— Windows 窗口系统。\n";
}
if (Platform.isLinux()) {
content += """
Mutter —— GNOME 环境,需要安装集成扩展。
KWin —— KDE 环境,需要安装集成插件。
X11 —— 通用 X11 环境支持,适用于 Xfce,Mate,LXDE 等环境。
""";
}
if (Platform.isMac()) {
content += "Quartz —— MacOS Quartz 窗口系统。\n";
}
content += "NULL —— 空实现,桌宠不会有任何窗口交互。";
return content;
}

private void initAbout() {
Expand Down

0 comments on commit d3676d4

Please sign in to comment.