From ea9f2a4ac1fce6f5d7b3a8405219d861ff2a2c6e Mon Sep 17 00:00:00 2001 From: Gabriel Machado <97042217+GabrielBRDeveloper@users.noreply.github.com> Date: Wed, 17 Jul 2024 22:18:36 -0400 Subject: [PATCH] Apply generic structure to display manager --- src/br/nullexcept/mux/app/Context.java | 5 +- .../mux/app/applets/DisplayApplet.java | 11 ++++ .../mux/core/texel/GlfwApplets.java | 55 +++++++++++++++++++ .../nullexcept/mux/core/texel/TexelAPI.java | 3 +- .../mux/res/DisplayInformation.java | 11 ++++ src/br/nullexcept/mux/res/Resources.java | 23 ++++---- 6 files changed, 90 insertions(+), 18 deletions(-) create mode 100644 src/br/nullexcept/mux/app/applets/DisplayApplet.java create mode 100644 src/br/nullexcept/mux/res/DisplayInformation.java diff --git a/src/br/nullexcept/mux/app/Context.java b/src/br/nullexcept/mux/app/Context.java index 8e83daf..5358736 100644 --- a/src/br/nullexcept/mux/app/Context.java +++ b/src/br/nullexcept/mux/app/Context.java @@ -10,14 +10,13 @@ public class Context { public static final String CLIPBOARD_APPLET = "applet.os.clipboard"; + public static final String DISPLAY_APPLET = "applet.os.display"; private final Resources mResource; - private final HashMap applets = new HashMap<>(); Launch _args; public Context(){ mResource = new Resources(this); - applets.putAll(Application.getProject().getCoreBootstrap().getSystemApplets()); } public File getFilesDir() { @@ -39,7 +38,7 @@ public String getString(String id) { } public T getApplet(String name) { - return (T) applets.get(name); + return (T) Application.getProject().getCoreBootstrap().getSystemApplets().get(name); } public LayoutInflater getLayoutInflater(){ diff --git a/src/br/nullexcept/mux/app/applets/DisplayApplet.java b/src/br/nullexcept/mux/app/applets/DisplayApplet.java new file mode 100644 index 0000000..b00b3e6 --- /dev/null +++ b/src/br/nullexcept/mux/app/applets/DisplayApplet.java @@ -0,0 +1,11 @@ +package br.nullexcept.mux.app.applets; + +import br.nullexcept.mux.app.Applet; +import br.nullexcept.mux.res.DisplayInformation; + +import java.util.List; + +public abstract class DisplayApplet extends Applet { + public abstract DisplayInformation getPrimaryDisplay(); + public abstract List getDisplays(); +} \ No newline at end of file diff --git a/src/br/nullexcept/mux/core/texel/GlfwApplets.java b/src/br/nullexcept/mux/core/texel/GlfwApplets.java index 23010f1..2696f69 100644 --- a/src/br/nullexcept/mux/core/texel/GlfwApplets.java +++ b/src/br/nullexcept/mux/core/texel/GlfwApplets.java @@ -4,13 +4,29 @@ import br.nullexcept.mux.app.Applet; import br.nullexcept.mux.app.Context; import br.nullexcept.mux.app.applets.ClipboardApplet; +import br.nullexcept.mux.app.applets.DisplayApplet; +import br.nullexcept.mux.graphics.Size; +import br.nullexcept.mux.res.DisplayInformation; import org.lwjgl.glfw.GLFW; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; class GlfwApplets { public static void initialize() { HashMap list = TexelAPI.applets; + list.put(Context.DISPLAY_APPLET, new DisplayApplet() { + @Override + public DisplayInformation getPrimaryDisplay() { + return new GlfwDisplay(GLFW.glfwGetPrimaryMonitor()); + } + + @Override + public List getDisplays() { + throw new RuntimeException("Not implemented support for multi-screen"); + } + }); list.put(Context.CLIPBOARD_APPLET, new ClipboardApplet() { @Override public String getContent() { @@ -30,3 +46,42 @@ public void setContent(String content) { }); } } + +class GlfwDisplay implements DisplayInformation { + private final long id; + + GlfwDisplay(long id) { + this.id = id; + } + + @Override + public Size getPhysicalSize() { + int[][] size = new int[2][1]; + GLFW.glfwGetMonitorPhysicalSize(id, size[0],size[1]); + return new Size(size[0][0], size[1][0]); + } + + @Override + public Size getResolution() { + int[][] size = new int[3][1]; + GLFW.glfwGetMonitorWorkarea(id, size[2],size[2],size[0], size[1]); + return new Size(size[0][0], size[1][0]); + } + + @Override + public String getMonitorId() { + return String.valueOf(id); + } + + @Override + public String getName() { + return GLFW.glfwGetMonitorName(id); + } + + @Override + public float getMonitorScale() { + float[] scale = new float[1]; + GLFW.glfwGetMonitorContentScale(id, scale,scale); + return scale[0]; + } +} diff --git a/src/br/nullexcept/mux/core/texel/TexelAPI.java b/src/br/nullexcept/mux/core/texel/TexelAPI.java index e2d3286..0343a36 100644 --- a/src/br/nullexcept/mux/core/texel/TexelAPI.java +++ b/src/br/nullexcept/mux/core/texel/TexelAPI.java @@ -24,12 +24,11 @@ class TexelAPI implements CoreBoostrap { public static void initialize() { VgTexel.initialize(); + GlfwApplets.initialize(); pointers.put(PointerIcon.Model.ARROW, GLFW.glfwCreateStandardCursor(GLFW.GLFW_ARROW_CURSOR)); pointers.put(PointerIcon.Model.HAND, GLFW.glfwCreateStandardCursor(GLFW.GLFW_HAND_CURSOR)); pointers.put(PointerIcon.Model.RESIZE, GLFW.glfwCreateStandardCursor(GLFW.GLFW_HRESIZE_CURSOR)); pointers.put(PointerIcon.Model.TEXT_SELECTION, GLFW.glfwCreateStandardCursor(GLFW.GLFW_IBEAM_CURSOR)); - - GlfwApplets.initialize(); } public static void destroy() { diff --git a/src/br/nullexcept/mux/res/DisplayInformation.java b/src/br/nullexcept/mux/res/DisplayInformation.java new file mode 100644 index 0000000..e5009fb --- /dev/null +++ b/src/br/nullexcept/mux/res/DisplayInformation.java @@ -0,0 +1,11 @@ +package br.nullexcept.mux.res; + +import br.nullexcept.mux.graphics.Size; + +public interface DisplayInformation { + Size getPhysicalSize(); + Size getResolution(); + String getMonitorId(); + String getName(); + float getMonitorScale(); +} diff --git a/src/br/nullexcept/mux/res/Resources.java b/src/br/nullexcept/mux/res/Resources.java index 2a803d5..d8a8315 100644 --- a/src/br/nullexcept/mux/res/Resources.java +++ b/src/br/nullexcept/mux/res/Resources.java @@ -1,7 +1,9 @@ package br.nullexcept.mux.res; import br.nullexcept.mux.app.Context; +import br.nullexcept.mux.app.applets.DisplayApplet; import br.nullexcept.mux.graphics.Drawable; +import br.nullexcept.mux.graphics.Size; import br.nullexcept.mux.graphics.fonts.Typeface; import br.nullexcept.mux.graphics.fonts.TypefaceFactory; import br.nullexcept.mux.lang.xml.XmlElement; @@ -41,7 +43,7 @@ public final class Resources { public Resources(Context ctx){ this.context = ctx; - metrics = new DisplayMetrics(); + metrics = new DisplayMetrics(ctx); inflater = new LayoutInflater(ctx); menuInflater = new MenuInflater(this); assetsManager = new AssetsManager("/assets/", ctx.getClass()); @@ -190,22 +192,17 @@ public static class DisplayMetrics { public final double sp; public final double px = 1.0; - public DisplayMetrics() { - long monitor = GLFW.glfwGetPrimaryMonitor(); - GLFWVidMode mode =GLFW.glfwGetVideoMode(monitor); - int dw = mode.width(); + public DisplayMetrics(Context ctx) { + DisplayApplet manager = ctx.getApplet(Context.DISPLAY_APPLET); + DisplayInformation display = manager.getPrimaryDisplay(); + int dw = display.getResolution().width; { - int[][] sizes = new int[2][1]; - GLFW.glfwGetMonitorPhysicalSize(monitor, sizes[0], sizes[1]); - pm = (double) dw / sizes[0][0]; + Size realSize = display.getPhysicalSize(); + pm = (double) dw / realSize.width; pt = (pm * 0.35277777777778); //72pt = 1inch cm = pm * 10; } - { - float[] scale = new float[1]; - GLFW.glfwGetMonitorContentScale(monitor,scale,scale); - sp = scale[0]; - } + sp = display.getMonitorScale(); } }