Skip to content

Commit

Permalink
Apply generic structure to display manager
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielBRDeveloper committed Jul 18, 2024
1 parent d5b1475 commit ea9f2a4
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 18 deletions.
5 changes: 2 additions & 3 deletions src/br/nullexcept/mux/app/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Applet> applets = new HashMap<>();
Launch _args;

public Context(){
mResource = new Resources(this);
applets.putAll(Application.getProject().getCoreBootstrap().getSystemApplets());
}

public File getFilesDir() {
Expand All @@ -39,7 +38,7 @@ public String getString(String id) {
}

public <T extends Applet> T getApplet(String name) {
return (T) applets.get(name);
return (T) Application.getProject().getCoreBootstrap().getSystemApplets().get(name);
}

public LayoutInflater getLayoutInflater(){
Expand Down
11 changes: 11 additions & 0 deletions src/br/nullexcept/mux/app/applets/DisplayApplet.java
Original file line number Diff line number Diff line change
@@ -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<DisplayInformation> getDisplays();
}
55 changes: 55 additions & 0 deletions src/br/nullexcept/mux/core/texel/GlfwApplets.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Applet> list = TexelAPI.applets;
list.put(Context.DISPLAY_APPLET, new DisplayApplet() {
@Override
public DisplayInformation getPrimaryDisplay() {
return new GlfwDisplay(GLFW.glfwGetPrimaryMonitor());
}

@Override
public List<DisplayInformation> getDisplays() {
throw new RuntimeException("Not implemented support for multi-screen");
}
});
list.put(Context.CLIPBOARD_APPLET, new ClipboardApplet() {
@Override
public String getContent() {
Expand All @@ -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];
}
}
3 changes: 1 addition & 2 deletions src/br/nullexcept/mux/core/texel/TexelAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
11 changes: 11 additions & 0 deletions src/br/nullexcept/mux/res/DisplayInformation.java
Original file line number Diff line number Diff line change
@@ -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();
}
23 changes: 10 additions & 13 deletions src/br/nullexcept/mux/res/Resources.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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();
}
}

Expand Down

0 comments on commit ea9f2a4

Please sign in to comment.