diff options
author | j-james | 2022-12-22 01:36:49 +0000 |
---|---|---|
committer | j-james | 2022-12-22 01:36:49 +0000 |
commit | 0e693fd4814572712a4192cb4f176e71f7a344f7 (patch) | |
tree | 9fb7daba882a025d368775aed9d518f565a40db2 | |
parent | 35741efd699e67382367411fcb991e2366a6774f (diff) |
Implement the basics of a recursive layout renderer
-rw-r--r-- | data/example.html | 54 | ||||
-rw-r--r-- | src/main/model/layout/BlockLayout.java | 8 | ||||
-rw-r--r-- | src/main/model/layout/DocumentLayout.java | 3 | ||||
-rw-r--r-- | src/main/model/layout/InlineLayout.java | 18 | ||||
-rw-r--r-- | src/main/model/layout/Layout.java | 3 | ||||
-rw-r--r-- | src/main/ui/BrowserCanvas.java | 32 | ||||
-rw-r--r-- | src/main/ui/BrowserWindow.java | 1 |
7 files changed, 85 insertions, 34 deletions
diff --git a/data/example.html b/data/example.html index b62fc42..8eff336 100644 --- a/data/example.html +++ b/data/example.html @@ -6,33 +6,6 @@ <meta charset="utf-8" /> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> - <style type="text/css"> - body { - background-color: #f0f0f2; - margin: 0; - padding: 0; - font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; - - } - div { - width: 600px; - margin: 5em auto; - padding: 2em; - background-color: #fdfdff; - border-radius: 0.5em; - box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02); - } - a:link, a:visited { - color: #38488f; - text-decoration: none; - } - @media (max-width: 700px) { - div { - margin: 0 auto; - width: auto; - } - } - </style> </head> <body> @@ -44,3 +17,30 @@ </div> </body> </html> +<style type="text/css"> +body { + background-color: #f0f0f2; + margin: 0; + padding: 0; + font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; + +} +div { + width: 600px; + margin: 5em auto; + padding: 2em; + background-color: #fdfdff; + border-radius: 0.5em; + box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02); +} +a:link, a:visited { + color: #38488f; + text-decoration: none; +} +@media (max-width: 700px) { + div { + margin: 0 auto; + width: auto; + } +} +</style> diff --git a/src/main/model/layout/BlockLayout.java b/src/main/model/layout/BlockLayout.java index 7f59dc6..f011847 100644 --- a/src/main/model/layout/BlockLayout.java +++ b/src/main/model/layout/BlockLayout.java @@ -19,13 +19,17 @@ public class BlockLayout extends Layout { this.setLocation(this.getParent().getLocation()); this.getPreviousSibling().ifPresent( sibling -> this.setY(sibling.getY() + sibling.getHeight())); + this.getPreviousSibling().ifPresent( + sibling -> System.out.println("bluh" + sibling.getAssociatedNode().getData())); - - this.setDimension(this.getParent().getDimension()); +// this.setDimension(this.getParent().getDimension()); for (Layout child : this.getChildren()) { child.layout(); this.setHeight(this.getHeight() + child.getHeight()); } + System.out.println(this.getAssociatedNode().getData() + this.getLocation()); + System.out.println(System.identityHashCode(this.getLocation())); +// System.out.println(this.getAssociatedNode().getData() + this.getDimension()); } } diff --git a/src/main/model/layout/DocumentLayout.java b/src/main/model/layout/DocumentLayout.java index 299be25..92c0b00 100644 --- a/src/main/model/layout/DocumentLayout.java +++ b/src/main/model/layout/DocumentLayout.java @@ -1,6 +1,7 @@ package model.layout; import java.awt.*; +import java.util.ArrayList; // root document layout style public class DocumentLayout extends Layout { @@ -17,7 +18,7 @@ public class DocumentLayout extends Layout { // MODIFIES: this // EFFECTS: recursively constructs the layout tree public void layout() { - this.setLocation(new Point(0, 0)); + this.setLocation(new Point(10, 20)); this.setDimension(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT)); for (Layout child : this.getChildren()) { diff --git a/src/main/model/layout/InlineLayout.java b/src/main/model/layout/InlineLayout.java index 2de69d6..8e375d5 100644 --- a/src/main/model/layout/InlineLayout.java +++ b/src/main/model/layout/InlineLayout.java @@ -1,6 +1,8 @@ package model.layout; +import model.html.ElementNode; import model.html.Node; +import model.html.TextNode; import java.awt.*; @@ -25,12 +27,26 @@ public class InlineLayout extends Layout { this.setWidth(this.getParent().getWidth()); this.setCursor(this.getX(), this.getY()); + Node node = this.getAssociatedNode(); + if (node instanceof TextNode) { + if (node.getData().length() > 5) { + this.setHeight(20); +// this.setWidth(this.getWidth() + node.getData().length()); + } + } else if (node instanceof ElementNode) { + if (((ElementNode) node).getTag().equals("a")) { + this.setX(this.getX() + this.getParent().getWidth()); + } + } + for (Layout child : this.getChildren()) { child.layout(); + this.setHeight(this.getHeight() + child.getHeight()); // fixme } // todo: recurse to calculate cursor - this.setHeight(cursor.getY() - this.getY()); +// this.setHeight(cursor.getY() - this.getY()); +// System.out.println(this.getAssociatedNode().getData() + this.getLocation()); } public void setCursor(Point cursor) { diff --git a/src/main/model/layout/Layout.java b/src/main/model/layout/Layout.java index 4dbedb5..4d9f48f 100644 --- a/src/main/model/layout/Layout.java +++ b/src/main/model/layout/Layout.java @@ -86,7 +86,8 @@ public abstract class Layout { // man, fuck design patterns, this is so much goddamn code public void setLocation(Point point) { - this.location = point; + this.location.x = point.x; + this.location.y = point.y; } public void setX(double x) { diff --git a/src/main/ui/BrowserCanvas.java b/src/main/ui/BrowserCanvas.java index 1502c3d..a58f6e1 100644 --- a/src/main/ui/BrowserCanvas.java +++ b/src/main/ui/BrowserCanvas.java @@ -3,6 +3,8 @@ package ui; import model.html.ElementNode; import model.html.TextNode; import model.html.Node; +import model.layout.DocumentLayout; +import model.layout.Layout; import javax.swing.*; import java.awt.*; @@ -10,12 +12,22 @@ import java.util.*; public class BrowserCanvas extends JPanel { private ArrayList<Node> html; + private DocumentLayout currentLayout; // MODIFIES: this // EFFECTS: constructs a BrowserCanvas object public BrowserCanvas(ArrayList<Node> html) { super(); this.html = html; + this.currentLayout = Layout.constructTree(html); + printTree(this.currentLayout.getChildren()); + } + + private void printTree(ArrayList<Layout> tree) { + for (Layout node : tree) { + System.out.println(System.identityHashCode(node.getLocation())); + printTree((node).getChildren()); + } } // EFFECTS: paints a component onto a canvas @@ -23,11 +35,11 @@ public class BrowserCanvas extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); Point location = new Point(10, 20); // we need a mutable reference - renderHtml(html, g, location); + renderHtml(this.currentLayout.getChildren(), g, location); } // EFFECTS: naively renders our html file by printing text nodes - private void renderHtml(ArrayList<Node> html, Graphics g, Point location) { + /*private void renderHtml(ArrayList<Node> html, Graphics g, Point location) { for (Node node : html) { if (node instanceof TextNode) { if (node.getData().length() > 5) { @@ -38,6 +50,22 @@ public class BrowserCanvas extends JPanel { renderHtml(((ElementNode) node).getChildren(), g, location); } } + }*/ + + private void renderHtml(ArrayList<Layout> tree, Graphics g, Point location) { + for (Layout layout : tree) { +// System.out.println(layout.getLocation()); + g.drawRect(layout.getLocation().x, layout.getLocation().y, layout.getDimension().width, layout.getDimension().height); + if (layout.getAssociatedNode() instanceof TextNode) { + if (layout.getAssociatedNode().getData().length() > 5) { +// System.out.println(location); + g.drawString(layout.getAssociatedNode().getData(), layout.getLocation().x, layout.getLocation().y); + g.drawString("X", 10, 20); + } + } else { + renderHtml(layout.getChildren(), g, location); + } + } } } diff --git a/src/main/ui/BrowserWindow.java b/src/main/ui/BrowserWindow.java index 640fb1a..2375fc1 100644 --- a/src/main/ui/BrowserWindow.java +++ b/src/main/ui/BrowserWindow.java @@ -43,6 +43,7 @@ public class BrowserWindow extends JFrame { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(WIDTH, HEIGHT); render("/home/apropos/Projects/website/j-james/index.html"); +// render("data/example.hctml"); // browserBar.addTab("/home/apropos/Projects/website/j-james/index.html"); setVisible(true); setClosingBehavior(); |