Author: LakeFox
Email: [email protected]
Date: Mon, 10 Jun 2024 10:31:30 -0600
docs/gui/index.html
Selectors and Change Detection
Commits
ae78990ed5b06e02833bd8e578a3af1df49adc00 ae742fdbda42322f4fa1d30761f516bf1ad45a70 1ae1fe418977632fafe3e61c5f02e87f0cad6c87 099527773e9bc03b40b9ce5d8a492ebbcdebcf32 b6acff3a27a7c66e35ab7ea5ffe48f1a69efaf11 07144afe32b8dd752fc4d80efd81529e2df9e4e5 ca735fa2c136dd5f35a5ccf2ffe2133b906c7f94 e2ea9c897ee1e89f89cd5c043632326348191b3a 10f4ce166028ee8ff926170d4d9e17d54e63a4a4 4bd704181610864a1fc4ebe65f20220ea1f1c00b 1c468e139a0f87655e4222b8512c38bec429d067 d8d6cadd2741b05c7a121ccb6753c5a48b8ad1b4 4c0529a8475408ee41d4e4dbd9c969db14594d37 ed8180f077d294d1e2f347f6c30d2977e8670b42 9113424c26a247352fe7b111fff39d1562f6c342 f783d5cacccf047368f8f20fc687bf5ad8f6f66a 66084134570c2a3d87e1336a2ab2ca50341b93b2 031df3b2fd4851345dd6f4c443057141d5b9e65b baf5d8cc1ccf27d14e4e58edcc4c170bba6445df e9c8f7961cf8b188938535b318b50397a76268ba 29572e238d6ba3f401717032bd12a2f409bf1ea9 d6025b04ef9516745adae85a478cbf9f98433597 476d6415583067674f395660961e33b9b40c3e66 906dd99de66ad7df92011a52fb19e0319224741e 4c44132d28d06548d2c90c2f8ca72dd3047fa52d 79e4c754771ba514b29a36e96a7570f266de3c41 36aaa13c4ea4d1235c64febfe9a8fd797037bbb2 6d4b83918686647193953a59c166baf7455b4996 c6a1e4a5102e9a3b5045957c22a0a73ac1a7ca4c db934f090648cdcd96e2f5f12a9bad762c60bfcd 7af8fc3f425ca3814ce1088fbd8a7f82513a1bb2 1c650539c11b715b14ee9013ad7d7faa8f733a7a 717ed8747f4df855c63e13ff95dcab720b8713cd 1c786f0d0efe1a47c285564824c9503881bef393 af527f354d956456e2e71a7f81f1470e74888e69 94ded12a3e3953459ccfbec2f225cecb43b47c73 c0081dc2f877de951fd8b0cd37136568a7ebb586 cbc825d6b458d50debc5913c6b5ed442138d7462 0a3baa3c4708754c79d023ad0addb4cbfb2844c2 a07b34e07b359327e413ccb7ea2dcdf1a91504de 4bf00213a866402775b162fb6f4b7b0bc37a8af7 1b690fa946047f4b897b417d8bc3b02c54cfdb2d 83b05e82a5f28bbe288565ae8a65943ba681cb4a 37d58d4f9de6af12611dada7ba5ae42d51ef05c6 8bbfc0ded0504595111765963d396a4574773e05 81049efedf0b3b521fb2949d8eb03d5ba681b0a5 6dcb369a2fa2bb69f94598fb4224a1349a80db6c c3e4d0cb62570c56ef5bc530f57eeea613f083fc 82d0fbb470daae4091bf30fb57707d66bd193992 166ec9ab6f4a3af0fe522a7dbe166951d2b39b31 992895a8842900d9a0803e7099611d650aba257d 4904b0b2812f9f0f45a59f2efacae89dabe9fe7c 36beac951788f5fb7ab2eeb3381300b07993bfe8 541818a92a5be6442879cd9b3420e1ef27f6e663 43ac3a1bbe6e727869a26ab8697c035eb60577fa d2afd27150a869ff41b8e100e82f20af54146105 0374f24d4685954662fa9db05939a8cbcd492b1c ed04d15b29d4df0329e8a4725c1a371a5cb0a20e 39fc4123096b04a4584b48d6efbb1acd5acacbf9 18315df2eb529bab6c5c79607ecb94626b170ae6 c519a195b90ad689d60d85365252e433b7488eaf d0d2839012c26032488bfe657797b4b590f926c7 8e0cb82ccac15fbc3e08bb53b148563e62437a44 d4413acd4dbad43507018f24e0456a9bc432d156 0dc05886b3ae0c59667f03a245e0728ab094f130 c9a3652d779d9700d9a9d797fd0c58d349a58de1 76425d002c70c3d853c117389a3b407efc53c7c2 fb409ef93b05cd4268e83c7201da73b6b29d482a da1cce88d00f7f7c373df1ce66b18e26e3a2d6e9 fa74cd48bb8959855c9d6b874f874671611611f7 610079be424f3a72ad02aa62e1c89607481750e0 60fda643c807730c3774beaa10a0910a31a4278a 6d2bcdcad816fd2b31c612fe45d3fd5a2fb14f98 b2d960e7d263bee55badbca47dc5d4d4f2d61084 f2ba1e3a96dd571d142c5a37c462743a21148e5f 25cbd33758fdb18f79aa1cf6500d1c1c024ee516 cde2d71846d1f69e5f386d354006465d61a4d19c cf708c22c45491dbdbbdbbb4450b08969f2e33bc 7bda495fbc9ea2c3aa87ac73a5cf4881eecf1482 9499d6967b394b70a22d9be34f47ee1b7a3fc55e 3aa3e67cd8ab769bc2c12b46fc77b11be2b1a0dd abed4927dc46e3e952fd21472f7ae4150ee16044 73953118e02b4c2d8159279624810dddd75e9100 031e4b18d91d825b56ef1e9eda5f1638bcd26629 fa3e1c57efa140ea58f56c6abf1f5f12f9bf2c7d a9b1b82a40fa6c462a9919b2832a89eb6bb47784 a8bf5e2504bdbe257686d48c2bfcaf061abfc95d e2830e4a6650ca8a1c53116a6d86f2975951dbbf 4a48b4c6905999527a7fc8d32e53d8c347c9ac74 9cfdb8c7ddce3a1b34a6590d8872f820601c2c53 128fad8298e89eadafa5feea43ca94f5115b9090 95d4595418fa814d47707441f2dc98d07eac12d6 5e35f7131d999b406eaeafc689374e3426845e6d 3402992fd25a64838e7acecfba3fad645c6c2511 8fcabdf5a34e7d80de988c10426c27adb0622087 1884dd57148595841cfb324f5b812d8640964978 6008cc8c3444f86d729ae5a81ae26919da1bb516 03d40c0d7e8feda38add1d89e3805df74997d915 810685c4aac0a651beaecd0488443a3b293b315d a666abc115c6693b355d5a8ef9cdfc3d77a088ca bcd3619f96c4157ee5b6450bc2bd1f23f27c9d81 9c428f6ab674582e022ea469ef2ac1e3f7ccb933 6e99df4296470d349da01f2e29698dbb34692a0f ed739dfaeb1ca5da10b2c75236faee40bacaf851 006a9351f3320662bff0b96d5d27daa1ba4e52e4 d6b38763cf83ad275aed0450151a97098f48859a 4ea031466d93057705a52caf2dfa21929b215730 a1bead3d96a11f4badedbd5a6eb984473c1cd309 7aa36472009f43cad843b465f67d2927f3428fdf 5f875adcd63ebd9ac9dfaef3ea16e6a6d3735503 4c0cc347e5e52d8ed99e84d42b309c4541d9280c c3de867fbbc27aaefba7aa88f97b5b1406c7c58e 00754639948b6612016ed5c434e97f44e7c65ed6 ffe16b1e768209b1c2021301a142b7fcd9a35a4b 800e568c600187f7228b228874ad20fb57f35166 d36b349e0eea73e99253b64fae3afeb5c4c276ca a7d8eff776e4e28a85e7835ee7ff6d50775748e7 bd0bd280c89d5d742e0ef13673c854436657466f 42da0bb422cdca21c8491af40ca9631206d8e964 e81415f70bedf456983b18692b4ae04355f15283 09c0a9a40ca6d8a8b6eaa6f06ce3f8c2440f7ee1 26eaa5b9c8cc2a84f9de04e17e01f201c122989e 649f49d0618e3c8cb940c4d259a536dfcc66020f 27ec987a8e8a9bc645f6cd38d9f8d5d52e7dbedb eb566ec8284ba205d27699a9bf465e26158b5098 4bdf14d6decad2ef14a77c108227273dd700e9a3 5598cb31dc238e16f58da2f793a1e2f7f5768dba 0bfe812e3d1f25756efcf92162961f5d55c3a5d8 9f96c81ba868d6269f3698c64ca8a83e74b0510f d5f01925db8323eeabbc43ec1b72a685da00dd6b f09195ec50a24c35947cb52feca1978ac5599871 0a10380275edaffc88632ef444a15b15c47ec17e 3f08ca6c77812c1d39f2949d2bab7416c77cc4ca af784841593347b2fd440fb78872fe9ac0fe1c09 9aa492880457b6fc20968f5d036cb6fe912e8964 c622cc4a0d279b98570b0af11fc67278e3355556 4d5dead4c4d3b3e34198e59e6ee0f6b3ca7c64b1 2191ceb040dd58a0794250e9e6c795a509b9fad5 775c1d3bc761967ff2f0a83e345c951117a5e9aa ace03eee6a19e6a12ad12c25e310ac3d223d2d48 f10dbc643cb334dc70657a086a38aadb59b59e29 ea63ec4d6f32b8a652d495d9c66d777313271e4a 038178b09b92d678107449519b75a977a08f45e9 f9cdbbb9ebfc1db9261b430427aa719be56a53c1 0a200674c645cf59ae69f25a25440db0b89c0c30 2e7189bb5db8a803c0ade25613a50bfcc264746a e6ca3edcf724fb8f2457423d9261ab73faf1f5da 6ff2d4a7c2fbc14ea856eb891f019c7978905834 72fac5e32be9bbf531dc8e324f2b865eb265ec4f 351cd2077d369fc0a9b9ec8675ede29288d41f35 061dd2a2659df35d1edd4ba74d67e6e72b531bf7 3c32ab918568bf4f59b0a8b393020de888dc2c3e 08fa11670d73b4f71226541761e09a99fc8a17c9 9d94e556f3eb471fd312af194ca7c4b82b67da6c db70db80c56513ebad6b95b66273e623198c5f44 e666311a274481b4af90702c916004ccbf9b6d8a 9780e12fa7acfb3a5be7d4171068c8c100858c53 baee9c1074cbf844b7c4a3c837ebb17f7861d075 0aae4ce623c90bfd7ea6c4f20967bf50001dc22f c8b42e81407d55a6444986deb435fa8144822c49 9e9967bb87524a82ae9411ca974d3b081ec52c53 3eac611da268444f07e1f718ffd18c7f47bc2a4c fbb2b3a689be07c57cde471e651ba06d7ecf7316 28d61bf3fae304b22ce0380c93a6f997e8c77e32 992c464588845b09ff2b4a61b5de719b9cdaf356 716fff934be2016bc00b2fcf17e19bc0ecfdeeb0 69a2005824c09fa33828ec5ffa24b9df8cd5bcc0 4fa6810e9e944c7d0c4ff06bed6a534e33869034 8dbbcec096317e9016c2f3478be4d458dbb34ad8 2c7c01be15245ae8997185fee247c00106cc81db 5af9b8d40ec97f5741d9a69161ef4aa10c879724 c9851e1b750de6c324c749b4bff5725d79e2b610 0a9f5a297bb97486fc23bb3a0bb39cb8b5b8cc27 aa4bda96b90fab60bf383c255d60b9580b5b6a31 cd8a892d7f6ca71c5d591d4282d98083462d77fe 98cc1a202fd2dbf27e87378c1588edc5eb1c3d60 d3068f5500a98d2a2b71d4c01d5296477224b743 1b28153bff1449200899115d4a1d9f612fac716c d992717d5df7bf68a5907446f9b31c78b5a01f51 497033c40c559bceb5c7980c8d48bd1d935188e3 d967a66b91fd990264e5a4b134385d80e5980c57 a397e9dda8244b6977adb9daeb5ab5ddff6e6140 60b804dc01593469962dae00a1e4004f0a8d7c53 a7b240cf9199d727e494b97ad2d9b65440a9ef1c 2458e3538eef44deb6f3d6a4831aef19414fd0ed c19139b782f5124ea86fc1610fe13ea85f77a279 c67f095bcad89fc6e6b635792a96ed0ed295e1a8 9ecebc8512dbb2a0ed61df1901eafd3832893bf8 95a551f86f299d7f8b424c26da3ff77400368fed ae5c8d61d8c75ed4b01dac60edd37859bc8f9d8f e54f11cae13b053a44afe10cdb47b65f535caca9 9b4fcfd0bee437354defe7e0f431857ff21c0edb ee5c2a995e1b9affef5a8e12f23cff8b0813d2ab 034c3e1601aaa0cba81118030a849485eea2b7ea 0cce165ede3a66867a863d5cb86afff5db406587 375b4fd6eca2d9d63959f9c755de6f8c5d89d302 8bec92a21036bedb734571d443f7b1a14daaa6eb 14d0b11516cd8bbdea84a3fa6cf53cff9619d902 c1e77a9311a8679328d0dc1337dd940682296927 6ab614d3e218d4c5cb3f69001a71350d3d7c65ec 77f6db23a5b65257959cc55e03eff09ee8f25cfc 64b7a40bb5620ec0522d9609fc961cff9cafe68a 62dca0c033d444aa579970c4c871b802c2a82d60 21f5480d3441d7c918a817368172bc14d714a3e8 3fc038d8db20f298b843a7eeeb31f25abf5a2788 492236c84ca4cfc3d8aea9d32d75b6a65a2fe52a 759a12b81483024d52d7b2055f66d675c05ea372 50580213060d8392a2da78eec0b8f08adddeba62 6374e3bb760d470dacdadfc14ef7cb824d9e194c 964b76fe981ab840a2cbc8a807789abae92ae660 b30861536f72d0ffc97c8780b62759fb764ed40b 5edb09e6ea1ba70cdac64a2196481b1f038301c2 34eeb6773589a95ecf66484b999cf443e57632d0 4ee9c48fa4b6f983c03acf77e8d04720fe43e1a6 88ebb8d8af483c96640f7df804e87fca20284c4d ce9fadbe2bd8bac62b475a387a8577f8a806fd52 9e7eb5f80cde5398270e1c4c85258118e22fe8ef a19227e38c38ceb21695ac14ea7754002980cf55 4277bc56f4490533734b1ed78fb5a9c8575f7afb 424479529f31e00ff3263795d42218a026fa528a 91c0289a560e3f3860d7b39549010adf917b8d1b 62a80750f59f92786acc48b18c3365d021bc5590 c0de5849434d88dd115eac9e5bc8eafcd0a0fa76 f869b8a4d7f88ac88437c9f3cfb4416784990e98 ee80bb3fe9cdd54af1ce4aba7f5e2fd136a2499c 8563a689d5957e4fd3ff9fecfbf97def28de4bff de8179675f9250b28678671addf56e33e5829e79 43b815a08101d267bc5d937d0b003a419bb2abd9 9e4164f6b3f847f371351a324905def648ea1747 1f26e1cc07a0de2edb516769b74e99b965b6d75d c36c0e51aad0f40f3f61406e786ba198ab6d90f9 c727bae4cb644789b0a975ec665c8b02652d4651 a61b91abb5ba8128c1438cc59b2f054c71f28d2a 2e1cecf0bba482b9976f3d8f274611195b0cf2f8 707ef18dccda6b917e1b421291e47e658555b3d2 b456956bfd85041b5b70b50d5fb2eaf3c9de64f2 f39d14d001e3dd0e73c4f8200f4132b47ebb40d3 9f0bcb8f5d1922199afb7bc8f8bb57bb10d0fe12 e877036f029a0198d9da39775443c8ac2132309b 2391f2f3881dbdabf96ca33c931e9cd5f42cedc9 82189594b7e20243f5ece31c6f7b804d6b83b0d7 5abd3260994cc77992f5c6437bdcdf45c32d9867 2519053ee4c5b2ce1c2851fee0d47d612d27aa1f 05584371da7da40cdab309ea2809c51c02069384 b61c8342cee8a0942ce276c2d2de773347ab4dcd c7b80824ade46956377b955838b73325d6fbe4d9 9866874e4701f30f1a293a7fd2e5d242bbf82c4f 2308aecbde415948894a55e53201c5abb7184039 e8b44094228f41fa5996788b977d07072fcf87a6 2facd93337f682bd8aa1222805c46720a53edc99 2d9288cec441356241f7d59d120de06ee7b97fdd
Diff
diff --git a/docs/gui/index.html b/docs/gui/index.html
index b6b45d2..61e1cfe 100644
--- a/docs/gui/index.html
+++ b/docs/gui/index.html
@@ -42,190 +42,190 @@
-  5	"crypto/sha256"
-  6	_ "embed"
-  7	"encoding/json"
-  8	"fmt"
-  9	"gui/cstyle"
- 10	"gui/cstyle/plugins/flex"
- 11	"gui/cstyle/plugins/inline"
- 12	"gui/cstyle/plugins/textAlign"
- 13	"gui/cstyle/transformers/text"
- 14	"gui/window"
- 15	"time"
- 16
- 17	"gui/element"
- 18	"gui/events"
- 19	"gui/utils"
- 20	"net/url"
- 21	"os"
- 22	"path/filepath"
- 23	"regexp"
- 24	"strconv"
- 25	"strings"
- 26
- 27	rl "github.com/gen2brain/raylib-go/raylib"
- 28	"golang.org/x/net/html"
- 29)
- 30
- 31// _ "net/http/pprof"
- 32
- 33//go:embed master.css
- 34var mastercss string
- 35
- 36type Window struct {
- 37	CSS      cstyle.CSS
- 38	Document element.Node
- 39	Adapter  func()
- 40}
- 41
- 42func Open(path string) Window {
- 43	window := New()
- 44
- 45	styleSheets, styleTags, htmlNodes := parseHTMLFromFile(path)
- 46
- 47	for _, v := range styleSheets {
- 48		window.CSS.StyleSheet(v)
- 49	}
- 50
- 51	for _, v := range styleTags {
- 52		window.CSS.StyleTag(v)
- 53	}
- 54
- 55	CreateNode(htmlNodes, &window.Document)
- 56
- 57	return window
- 58}
- 59
- 60func New() Window {
- 61	css := cstyle.CSS{
- 62		Width:  800,
- 63		Height: 450,
- 64	}
- 65
- 66	css.StyleTag(mastercss)
- 67	// This is still apart of computestyle
- 68	// css.AddPlugin(position.Init())
- 69	css.AddPlugin(inline.Init())
- 70	// css.AddPlugin(block.Init())
- 71	css.AddPlugin(textAlign.Init())
- 72	// css.AddPlugin(inlineText.Init())
- 73	css.AddPlugin(flex.Init())
- 74
- 75	css.AddTransformer(text.Init())
- 76
- 77	el := element.Node{}
- 78	document := el.CreateElement("ROOT")
- 79	document.Style["width"] = "800px"
- 80	document.Style["height"] = "450px"
- 81	document.Properties.Id = "ROOT"
- 82	return Window{
- 83		CSS:      css,
- 84		Document: document,
- 85	}
- 86}
- 87
- 88func (w *Window) Render(doc element.Node, state *map[string]element.State) []element.State {
- 89	s := *state
- 90
- 91	flatDoc := flatten(doc)
- 92
- 93	store := []element.State{}
- 94
- 95	keys := []string{}
- 96
- 97	for _, v := range flatDoc {
- 98		store = append(store, s[v.Properties.Id])
- 99		keys = append(keys, v.Properties.Id)
-100	}
-101
-102	// Create a set of keys to keep
-103	keysSet := make(map[string]struct{}, len(keys))
-104	for _, key := range keys {
-105		keysSet[key] = struct{}{}
-106	}
-107
-108	// Iterate over the map and delete keys not in the set
-109	for k := range s {
-110		if _, found := keysSet[k]; !found {
-111			delete(s, k)
-112		}
-113	}
-114
-115	return store
-116}
-117
-118func flatten(n element.Node) []element.Node {
-119	var nodes []element.Node
-120	nodes = append(nodes, n)
-121
-122	children := n.Children
-123	if len(children) > 0 {
-124		for _, ch := range children {
-125			chNodes := flatten(ch)
-126			nodes = append(nodes, chNodes...)
-127		}
-128	}
-129	return nodes
-130}
-131
-132func View(data *Window, width, height int32) {
-133	debug := false
-134	data.Document.Style["width"] = strconv.Itoa(int(width)) + "px"
-135	data.Document.Style["height"] = strconv.Itoa(int(height)) + "px"
-136
-137	wm := window.NewWindowManager()
-138	wm.FPSCounterOn = true
-139
-140	wm.OpenWindow(width, height)
-141	defer wm.CloseWindow()
-142
-143	evts := map[string]element.EventList{}
-144
-145	eventStore := &evts
-146
-147	state := map[string]element.State{}
-148
-149	shouldStop := false
-150
-151	// var hash []byte
-152	var rd []element.State
-153
-154	lastChange := time.Now()
-155
-156	// Main game loop
-157	for !wm.WindowShouldClose() && !shouldStop {
-158		// fmt.Println("######################")
-159		rl.BeginDrawing()
-160		if !shouldStop && debug {
-161			shouldStop = true
-162		}
-163		// Check if the window size has changed
-164		newWidth := int32(rl.GetScreenWidth())
-165		newHeight := int32(rl.GetScreenHeight())
-166
-167		resize := false
-168
-169		if newWidth != width || newHeight != height {
-170			resize = true
-171			rl.ClearBackground(rl.RayWhite)
-172			// Window has been resized, handle the event
-173			width = newWidth
-174			height = newHeight
-175
-176			data.CSS.Width = float32(width)
-177			data.CSS.Height = float32(height)
-178
-179			data.Document.Style["width"] = strconv.Itoa(int(width)) + "px"
-180			data.Document.Style["height"] = strconv.Itoa(int(height)) + "px"
-181		}
-182
-183		// newHash, _ := hashStruct(&data.Document.Children[0])
-184		eventStore = events.GetEvents(&data.Document.Children[0], &state, eventStore)
-185		for _, v := range *eventStore {
-186			if v.Event.MouseOver {
-187
-188				if v.Event.Target.TagName == "body" || v.Event.Target.TagName == "html" {
-189					continue
-190				} else {
-191					w := state[v.Event.Target.Properties.Id].Width
-192					h := state[v.Event.Target.Properties.Id].Height
-193					x := state[v.Event.Target.Properties.Id].X
-194					y := state[v.Event.Target.Properties.Id].Y
+  5	"bytes"
+  6	"crypto/sha256"
+  7	_ "embed"
+  8	"encoding/json"
+  9	"fmt"
+ 10	"gui/cstyle"
+ 11	"gui/cstyle/plugins/flex"
+ 12	"gui/cstyle/plugins/inline"
+ 13	"gui/cstyle/plugins/textAlign"
+ 14	"gui/cstyle/transformers/text"
+ 15	"gui/window"
+ 16	"time"
+ 17
+ 18	"gui/element"
+ 19	"gui/events"
+ 20	"gui/utils"
+ 21	"net/url"
+ 22	"os"
+ 23	"path/filepath"
+ 24	"regexp"
+ 25	"strconv"
+ 26	"strings"
+ 27
+ 28	rl "github.com/gen2brain/raylib-go/raylib"
+ 29	"golang.org/x/net/html"
+ 30)
+ 31
+ 32// _ "net/http/pprof"
+ 33
+ 34//go:embed master.css
+ 35var mastercss string
+ 36
+ 37type Window struct {
+ 38	CSS      cstyle.CSS
+ 39	Document element.Node
+ 40	Adapter  func()
+ 41}
+ 42
+ 43func Open(path string) Window {
+ 44	window := New()
+ 45
+ 46	styleSheets, styleTags, htmlNodes := parseHTMLFromFile(path)
+ 47
+ 48	for _, v := range styleSheets {
+ 49		window.CSS.StyleSheet(v)
+ 50	}
+ 51
+ 52	for _, v := range styleTags {
+ 53		window.CSS.StyleTag(v)
+ 54	}
+ 55
+ 56	CreateNode(htmlNodes, &window.Document)
+ 57
+ 58	return window
+ 59}
+ 60
+ 61func New() Window {
+ 62	css := cstyle.CSS{
+ 63		Width:  800,
+ 64		Height: 450,
+ 65	}
+ 66
+ 67	css.StyleTag(mastercss)
+ 68	// This is still apart of computestyle
+ 69	// css.AddPlugin(position.Init())
+ 70	css.AddPlugin(inline.Init())
+ 71	// css.AddPlugin(block.Init())
+ 72	css.AddPlugin(textAlign.Init())
+ 73	// css.AddPlugin(inlineText.Init())
+ 74	css.AddPlugin(flex.Init())
+ 75
+ 76	css.AddTransformer(text.Init())
+ 77
+ 78	el := element.Node{}
+ 79	document := el.CreateElement("ROOT")
+ 80	document.Style["width"] = "800px"
+ 81	document.Style["height"] = "450px"
+ 82	document.Properties.Id = "ROOT"
+ 83	return Window{
+ 84		CSS:      css,
+ 85		Document: document,
+ 86	}
+ 87}
+ 88
+ 89func (w *Window) Render(doc element.Node, state *map[string]element.State) []element.State {
+ 90	s := *state
+ 91
+ 92	flatDoc := flatten(doc)
+ 93
+ 94	store := []element.State{}
+ 95
+ 96	keys := []string{}
+ 97
+ 98	for _, v := range flatDoc {
+ 99		store = append(store, s[v.Properties.Id])
+100		keys = append(keys, v.Properties.Id)
+101	}
+102
+103	// Create a set of keys to keep
+104	keysSet := make(map[string]struct{}, len(keys))
+105	for _, key := range keys {
+106		keysSet[key] = struct{}{}
+107	}
+108
+109	// Iterate over the map and delete keys not in the set
+110	for k := range s {
+111		if _, found := keysSet[k]; !found {
+112			delete(s, k)
+113		}
+114	}
+115
+116	return store
+117}
+118
+119func flatten(n element.Node) []element.Node {
+120	var nodes []element.Node
+121	nodes = append(nodes, n)
+122
+123	children := n.Children
+124	if len(children) > 0 {
+125		for _, ch := range children {
+126			chNodes := flatten(ch)
+127			nodes = append(nodes, chNodes...)
+128		}
+129	}
+130	return nodes
+131}
+132
+133func View(data *Window, width, height int32) {
+134	debug := false
+135	data.Document.Style["width"] = strconv.Itoa(int(width)) + "px"
+136	data.Document.Style["height"] = strconv.Itoa(int(height)) + "px"
+137
+138	wm := window.NewWindowManager()
+139	wm.FPSCounterOn = true
+140
+141	wm.OpenWindow(width, height)
+142	defer wm.CloseWindow()
+143
+144	evts := map[string]element.EventList{}
+145
+146	eventStore := &evts
+147
+148	state := map[string]element.State{}
+149
+150	shouldStop := false
+151
+152	var hash []byte
+153	var rd []element.State
+154
+155	lastChange := time.Now()
+156
+157	// Main game loop
+158	for !wm.WindowShouldClose() && !shouldStop {
+159		// fmt.Println("######################")
+160		rl.BeginDrawing()
+161		if !shouldStop && debug {
+162			shouldStop = true
+163		}
+164		// Check if the window size has changed
+165		newWidth := int32(rl.GetScreenWidth())
+166		newHeight := int32(rl.GetScreenHeight())
+167
+168		resize := false
+169
+170		if newWidth != width || newHeight != height {
+171			resize = true
+172			rl.ClearBackground(rl.RayWhite)
+173			// Window has been resized, handle the event
+174			width = newWidth
+175			height = newHeight
+176
+177			data.CSS.Width = float32(width)
+178			data.CSS.Height = float32(height)
+179
+180			data.Document.Style["width"] = strconv.Itoa(int(width)) + "px"
+181			data.Document.Style["height"] = strconv.Itoa(int(height)) + "px"
+182		}
+183
+184		newHash, _ := hashStruct(&data.Document.Children[0])
+185		eventStore = events.GetEvents(&data.Document.Children[0], &state, eventStore)
+186		if !bytes.Equal(hash, newHash) || resize {
+187			if wm.FPS != 30 {
+188				wm.SetFPS(30)
+189			}
+190			lastChange = time.Now()
+191			hash = newHash
+192			newDoc := CopyNode(data.CSS, data.Document.Children[0], &data.Document)
+193
+194			newDoc = data.CSS.Transform(newDoc)
@@ -233,55 +233,55 @@
-196					el := getElementByPropId(&data.Document.Children[0], v.Event.Target.Properties.Id)
-197
-198					if el != nil {
-199						data.CSS.ComputeNodeStyle(el, &state)
-200
-201						if w != state[v.Event.Target.Properties.Id].Width &&
-202							h != state[v.Event.Target.Properties.Id].Height &&
-203							x != state[v.Event.Target.Properties.Id].X &&
-204							y != state[v.Event.Target.Properties.Id].Y {
-205							fmt.Println("RELOAD")
-206						} else {
-207							fmt.Println("SWAP")
-208						}
-209
-210					}
-211
-212				}
-213
-214			}
-215
-216		}
-217		if resize {
-218			if wm.FPS != 30 {
-219				wm.SetFPS(30)
-220			}
-221			lastChange = time.Now()
-222			// hash = newHash
-223			newDoc := CopyNode(data.CSS, data.Document.Children[0], &data.Document)
-224
-225			newDoc = data.CSS.Transform(newDoc)
-226
-227			data.CSS.ComputeNodeStyle(&newDoc, &state)
-228			rd = data.Render(newDoc, &state)
-229			wm.LoadTextures(rd)
-230
-231			// AddHTML(&newDoc)
-232			// fmt.Println(newDoc.QuerySelector("body").InnerHTML)
-233
-234			AddHTML(&data.Document)
-235			fmt.Println(time.Since(lastChange))
-236		}
-237		wm.Draw(rd)
-238
-239		// could use a return value that indicates whether or not a event has ran to ramp/deramp fps based on activity
-240
-241		events.RunEvents(eventStore)
-242		// ran := events.RunEvents(eventStore)
-243
-244		if time.Since(lastChange) > 5*time.Second {
-245			if wm.FPS != 1 {
-246				wm.SetFPS(1)
-247			}
-248		}
-249
-250		rl.EndDrawing()
+196			data.CSS.ComputeNodeStyle(&newDoc, &state)
+197			rd = data.Render(newDoc, &state)
+198			wm.LoadTextures(rd)
+199
+200			// AddHTML(&newDoc)
+201			// fmt.Println(newDoc.QuerySelector("body").InnerHTML)
+202
+203			AddHTML(&data.Document)
+204			fmt.Println(time.Since(lastChange))
+205		}
+206		wm.Draw(rd)
+207
+208		// could use a return value that indicates whether or not a event has ran to ramp/deramp fps based on activity
+209
+210		events.RunEvents(eventStore)
+211		// ran := events.RunEvents(eventStore)
+212
+213		if time.Since(lastChange) > 5*time.Second {
+214			if wm.FPS != 1 {
+215				wm.SetFPS(1)
+216			}
+217		}
+218
+219		rl.EndDrawing()
+220	}
+221}
+222
+223func CopyNode(c cstyle.CSS, node element.Node, parent *element.Node) element.Node {
+224	n := element.Node{}
+225	n.TagName = node.TagName
+226	n.InnerText = node.InnerText
+227	n.Style = node.Style
+228	n.Id = node.Id
+229	n.ClassList = node.ClassList
+230	n.Href = node.Href
+231	n.Src = node.Src
+232	n.Title = node.Title
+233	n.Attribute = node.Attribute
+234	n.Value = node.Value
+235	n.ScrollY = node.ScrollY
+236	n.InnerHTML = node.InnerHTML
+237	n.OuterHTML = node.OuterHTML
+238	n.Properties.Id = node.Properties.Id
+239	n.Properties.Focusable = node.Properties.Focusable
+240	n.Properties.Focused = node.Properties.Focused
+241	n.Properties.Editable = node.Properties.Editable
+242	n.Properties.Hover = node.Properties.Hover
+243	n.Properties.Selected = node.Properties.Selected
+244
+245	n.Parent = parent
+246
+247	n.Style = c.GetStyles(n)
+248
+249	for _, v := range node.Children {
+250		n.Children = append(n.Children, CopyNode(c, v, &n))
@@ -289,84 +289,84 @@
-252}
-253
-254func CopyNode(c cstyle.CSS, node element.Node, parent *element.Node) element.Node {
-255	n := element.Node{}
-256	n.TagName = node.TagName
-257	n.InnerText = node.InnerText
-258	n.Style = node.Style
-259	n.Id = node.Id
-260	n.ClassList = node.ClassList
-261	n.Href = node.Href
-262	n.Src = node.Src
-263	n.Title = node.Title
-264	n.Attribute = node.Attribute
-265	n.Value = node.Value
-266	n.ScrollY = node.ScrollY
-267	n.InnerHTML = node.InnerHTML
-268	n.OuterHTML = node.OuterHTML
-269	n.Properties.Id = node.Properties.Id
-270	n.Properties.Focusable = node.Properties.Focusable
-271	n.Properties.Focused = node.Properties.Focused
-272	n.Properties.Editable = node.Properties.Editable
-273	n.Properties.Hover = node.Properties.Hover
-274	n.Properties.Selected = node.Properties.Selected
-275
-276	n.Parent = parent
-277
-278	n.Style = c.GetStyles(n)
-279
-280	for _, v := range node.Children {
-281		n.Children = append(n.Children, CopyNode(c, v, &n))
-282	}
-283	return n
-284}
-285
-286func CreateNode(node *html.Node, parent *element.Node) {
-287	if node.Type == html.ElementNode {
-288		newNode := parent.CreateElement(node.Data)
-289		for _, attr := range node.Attr {
-290			if attr.Key == "class" {
-291				classes := strings.Split(attr.Val, " ")
-292				for _, class := range classes {
-293					newNode.ClassList.Add(class)
-294				}
-295			} else if attr.Key == "id" {
-296				newNode.Id = attr.Val
-297			} else if attr.Key == "contenteditable" && (attr.Val == "" || attr.Val == "true") {
-298				newNode.Properties.Editable = true
-299			} else if attr.Key == "href" {
-300				newNode.Href = attr.Val
-301			} else if attr.Key == "src" {
-302				newNode.Src = attr.Val
-303			} else if attr.Key == "title" {
-304				newNode.Title = attr.Val
-305			} else {
-306				newNode.SetAttribute(attr.Key, attr.Val)
-307			}
-308		}
-309		newNode.InnerText = strings.TrimSpace(utils.GetInnerText(node))
-310		// Recursively traverse child nodes
-311		for child := node.FirstChild; child != nil; child = child.NextSibling {
-312			if child.Type == html.ElementNode {
-313				CreateNode(child, &newNode)
-314			}
-315		}
-316		parent.AppendChild(newNode)
-317
-318	} else {
-319		for child := node.FirstChild; child != nil; child = child.NextSibling {
-320			if child.Type == html.ElementNode {
-321				CreateNode(child, parent)
-322			}
-323		}
-324	}
-325}
-326
-327func AddHTML(n *element.Node) {
-328	// Head is not renderable
-329	n.InnerHTML = utils.InnerHTML(*n)
-330	tag, closing := utils.NodeToHTML(*n)
-331	n.OuterHTML = tag + n.InnerHTML + closing
-332	for i := range n.Children {
-333		AddHTML(&n.Children[i])
-334	}
-335}
+252	return n
+253}
+254
+255func CreateNode(node *html.Node, parent *element.Node) {
+256	if node.Type == html.ElementNode {
+257		newNode := parent.CreateElement(node.Data)
+258		for _, attr := range node.Attr {
+259			if attr.Key == "class" {
+260				classes := strings.Split(attr.Val, " ")
+261				for _, class := range classes {
+262					newNode.ClassList.Add(class)
+263				}
+264			} else if attr.Key == "id" {
+265				newNode.Id = attr.Val
+266			} else if attr.Key == "contenteditable" && (attr.Val == "" || attr.Val == "true") {
+267				newNode.Properties.Editable = true
+268			} else if attr.Key == "href" {
+269				newNode.Href = attr.Val
+270			} else if attr.Key == "src" {
+271				newNode.Src = attr.Val
+272			} else if attr.Key == "title" {
+273				newNode.Title = attr.Val
+274			} else {
+275				newNode.SetAttribute(attr.Key, attr.Val)
+276			}
+277		}
+278		newNode.InnerText = strings.TrimSpace(utils.GetInnerText(node))
+279		// Recursively traverse child nodes
+280		for child := node.FirstChild; child != nil; child = child.NextSibling {
+281			if child.Type == html.ElementNode {
+282				CreateNode(child, &newNode)
+283			}
+284		}
+285		parent.AppendChild(newNode)
+286
+287	} else {
+288		for child := node.FirstChild; child != nil; child = child.NextSibling {
+289			if child.Type == html.ElementNode {
+290				CreateNode(child, parent)
+291			}
+292		}
+293	}
+294}
+295
+296func AddHTML(n *element.Node) {
+297	// Head is not renderable
+298	n.InnerHTML = utils.InnerHTML(*n)
+299	tag, closing := utils.NodeToHTML(*n)
+300	n.OuterHTML = tag + n.InnerHTML + closing
+301	for i := range n.Children {
+302		AddHTML(&n.Children[i])
+303	}
+304}
+305
+306func parseHTMLFromFile(path string) ([]string, []string, *html.Node) {
+307	file, _ := os.Open(path)
+308	defer file.Close()
+309
+310	scanner := bufio.NewScanner(file)
+311	var htmlContent string
+312
+313	for scanner.Scan() {
+314		htmlContent += scanner.Text() + "\n"
+315	}
+316
+317	htmlContent = removeHTMLComments(htmlContent)
+318
+319	doc, _ := html.Parse(strings.NewReader(encapsulateText(removeWhitespaceBetweenTags(htmlContent))))
+320
+321	// Extract stylesheet link tags and style tags
+322	stylesheets := extractStylesheets(doc, filepath.Dir(path))
+323	styleTags := extractStyleTags(doc)
+324
+325	return stylesheets, styleTags, doc
+326}
+327
+328func extractStylesheets(n *html.Node, baseDir string) []string {
+329	var stylesheets []string
+330
+331	var dfs func(*html.Node)
+332	dfs = func(node *html.Node) {
+333		if node.Type == html.ElementNode && node.Data == "link" {
+334			var href string
+335			isStylesheet := false
@@ -374,18 +374,18 @@
-337func parseHTMLFromFile(path string) ([]string, []string, *html.Node) {
-338	file, _ := os.Open(path)
-339	defer file.Close()
-340
-341	scanner := bufio.NewScanner(file)
-342	var htmlContent string
-343
-344	for scanner.Scan() {
-345		htmlContent += scanner.Text() + "\n"
-346	}
-347
-348	htmlContent = removeHTMLComments(htmlContent)
-349
-350	doc, _ := html.Parse(strings.NewReader(encapsulateText(removeWhitespaceBetweenTags(htmlContent))))
-351
-352	// Extract stylesheet link tags and style tags
-353	stylesheets := extractStylesheets(doc, filepath.Dir(path))
-354	styleTags := extractStyleTags(doc)
+337			for _, attr := range node.Attr {
+338				if attr.Key == "rel" && attr.Val == "stylesheet" {
+339					isStylesheet = true
+340				} else if attr.Key == "href" {
+341					href = attr.Val
+342				}
+343			}
+344
+345			if isStylesheet {
+346				resolvedHref := localizePath(baseDir, href)
+347				stylesheets = append(stylesheets, resolvedHref)
+348			}
+349		}
+350
+351		for c := node.FirstChild; c != nil; c = c.NextSibling {
+352			dfs(c)
+353		}
+354	}
@@ -393,34 +393,34 @@
-356	return stylesheets, styleTags, doc
-357}
-358
-359func extractStylesheets(n *html.Node, baseDir string) []string {
-360	var stylesheets []string
-361
-362	var dfs func(*html.Node)
-363	dfs = func(node *html.Node) {
-364		if node.Type == html.ElementNode && node.Data == "link" {
-365			var href string
-366			isStylesheet := false
-367
-368			for _, attr := range node.Attr {
-369				if attr.Key == "rel" && attr.Val == "stylesheet" {
-370					isStylesheet = true
-371				} else if attr.Key == "href" {
-372					href = attr.Val
-373				}
-374			}
-375
-376			if isStylesheet {
-377				resolvedHref := localizePath(baseDir, href)
-378				stylesheets = append(stylesheets, resolvedHref)
-379			}
-380		}
-381
-382		for c := node.FirstChild; c != nil; c = c.NextSibling {
-383			dfs(c)
-384		}
-385	}
-386
-387	dfs(n)
-388	return stylesheets
-389}
+356	dfs(n)
+357	return stylesheets
+358}
+359
+360func extractStyleTags(n *html.Node) []string {
+361	var styleTags []string
+362
+363	var dfs func(*html.Node)
+364	dfs = func(node *html.Node) {
+365		if node.Type == html.ElementNode && node.Data == "style" {
+366			var styleContent strings.Builder
+367			for c := node.FirstChild; c != nil; c = c.NextSibling {
+368				if c.Type == html.TextNode {
+369					styleContent.WriteString(c.Data)
+370				}
+371			}
+372			styleTags = append(styleTags, styleContent.String())
+373		}
+374
+375		for c := node.FirstChild; c != nil; c = c.NextSibling {
+376			dfs(c)
+377		}
+378	}
+379
+380	dfs(n)
+381	return styleTags
+382}
+383
+384func localizePath(rootPath, filePath string) string {
+385	// Check if the file path has a scheme, indicating it's a URL
+386	u, err := url.Parse(filePath)
+387	if err == nil && u.Scheme != "" {
+388		return filePath
+389	}
@@ -428,2 +428,2 @@
-391func extractStyleTags(n *html.Node) []string {
-392	var styleTags []string
+391	// Join the root path and the file path to create an absolute path
+392	absPath := filepath.Join(rootPath, filePath)
@@ -431,30 +431,30 @@
-394	var dfs func(*html.Node)
-395	dfs = func(node *html.Node) {
-396		if node.Type == html.ElementNode && node.Data == "style" {
-397			var styleContent strings.Builder
-398			for c := node.FirstChild; c != nil; c = c.NextSibling {
-399				if c.Type == html.TextNode {
-400					styleContent.WriteString(c.Data)
-401				}
-402			}
-403			styleTags = append(styleTags, styleContent.String())
-404		}
-405
-406		for c := node.FirstChild; c != nil; c = c.NextSibling {
-407			dfs(c)
-408		}
-409	}
-410
-411	dfs(n)
-412	return styleTags
-413}
-414
-415func localizePath(rootPath, filePath string) string {
-416	// Check if the file path has a scheme, indicating it's a URL
-417	u, err := url.Parse(filePath)
-418	if err == nil && u.Scheme != "" {
-419		return filePath
-420	}
-421
-422	// Join the root path and the file path to create an absolute path
-423	absPath := filepath.Join(rootPath, filePath)
+394	// If the absolute path is the same as the original path, return it
+395	if absPath == filePath {
+396		return filePath
+397	}
+398
+399	return "./" + absPath
+400}
+401
+402func encapsulateText(htmlString string) string {
+403	openOpen := regexp.MustCompile(`(<\w+[^>]*>)([^<]+)(<\w+[^>]*>)`)
+404	closeOpen := regexp.MustCompile(`(</\w+[^>]*>)([^<]+)(<\w+[^>]*>)`)
+405	closeClose := regexp.MustCompile(`(<\/\w+[^>]*>)([^<]+)(<\/\w+[^>]*>)`)
+406	a := matchFactory(openOpen)
+407	t := openOpen.ReplaceAllStringFunc(htmlString, a)
+408	// fmt.Println(t)
+409	b := matchFactory(closeOpen)
+410	u := closeOpen.ReplaceAllStringFunc(t, b)
+411	// fmt.Println(u)
+412	c := matchFactory(closeClose)
+413	v := closeClose.ReplaceAllStringFunc(u, c)
+414	// fmt.Println(v)
+415	return v
+416}
+417
+418func matchFactory(re *regexp.Regexp) func(string) string {
+419	return func(match string) string {
+420		submatches := re.FindStringSubmatch(match)
+421		if len(submatches) != 4 {
+422			return match
+423		}
@@ -462,22 +462,22 @@
-425	// If the absolute path is the same as the original path, return it
-426	if absPath == filePath {
-427		return filePath
-428	}
-429
-430	return "./" + absPath
-431}
-432
-433func encapsulateText(htmlString string) string {
-434	openOpen := regexp.MustCompile(`(<\w+[^>]*>)([^<]+)(<\w+[^>]*>)`)
-435	closeOpen := regexp.MustCompile(`(</\w+[^>]*>)([^<]+)(<\w+[^>]*>)`)
-436	closeClose := regexp.MustCompile(`(<\/\w+[^>]*>)([^<]+)(<\/\w+[^>]*>)`)
-437	a := matchFactory(openOpen)
-438	t := openOpen.ReplaceAllStringFunc(htmlString, a)
-439	// fmt.Println(t)
-440	b := matchFactory(closeOpen)
-441	u := closeOpen.ReplaceAllStringFunc(t, b)
-442	// fmt.Println(u)
-443	c := matchFactory(closeClose)
-444	v := closeClose.ReplaceAllStringFunc(u, c)
-445	// fmt.Println(v)
-446	return v
+425		// Process submatches
+426		if len(removeWhitespace(submatches[2])) > 0 {
+427			return submatches[1] + "<notaspan>" + submatches[2] + "</notaspan>" + submatches[3]
+428		} else {
+429			return match
+430		}
+431	}
+432}
+433func removeWhitespace(htmlString string) string {
+434	// Remove extra white space
+435	reSpaces := regexp.MustCompile(`\s+`)
+436	htmlString = reSpaces.ReplaceAllString(htmlString, " ")
+437
+438	// Trim leading and trailing white space
+439	htmlString = strings.TrimSpace(htmlString)
+440
+441	return htmlString
+442}
+443
+444func removeHTMLComments(htmlString string) string {
+445	re := regexp.MustCompile(`<!--[\s\S]*?-->`)
+446	return re.ReplaceAllString(htmlString, "")
@@ -486,69 +486,23 @@
-449func matchFactory(re *regexp.Regexp) func(string) string {
-450	return func(match string) string {
-451		submatches := re.FindStringSubmatch(match)
-452		if len(submatches) != 4 {
-453			return match
-454		}
-455
-456		// Process submatches
-457		if len(removeWhitespace(submatches[2])) > 0 {
-458			return submatches[1] + "<notaspan>" + submatches[2] + "</notaspan>" + submatches[3]
-459		} else {
-460			return match
-461		}
-462	}
-463}
-464func removeWhitespace(htmlString string) string {
-465	// Remove extra white space
-466	reSpaces := regexp.MustCompile(`\s+`)
-467	htmlString = reSpaces.ReplaceAllString(htmlString, " ")
-468
-469	// Trim leading and trailing white space
-470	htmlString = strings.TrimSpace(htmlString)
-471
-472	return htmlString
-473}
-474
-475func removeHTMLComments(htmlString string) string {
-476	re := regexp.MustCompile(`<!--[\s\S]*?-->`)
-477	return re.ReplaceAllString(htmlString, "")
-478}
-479
-480// important to allow the notspans to be injected, the spaces after removing the comments cause the regexp to fail
-481func removeWhitespaceBetweenTags(html string) string {
-482	// Create a regular expression to match spaces between angle brackets
-483	re := regexp.MustCompile(`>\s+<`)
-484	// Replace all matches of spaces between angle brackets with "><"
-485	return re.ReplaceAllString(html, "><")
-486}
-487
-488// Function to hash a struct using SHA-256
-489func hashStruct(s interface{}) ([]byte, error) {
-490	// Convert struct to JSON
-491	jsonData, err := json.Marshal(s)
-492	if err != nil {
-493		return nil, err
-494	}
-495
-496	// Hash the JSON data using SHA-256
-497	hasher := sha256.New()
-498	hasher.Write(jsonData)
-499	hash := hasher.Sum(nil)
-500
-501	return hash, nil
-502}
-503
-504func getElementByPropId(n *element.Node, id string) *element.Node {
-505	if n.Properties.Id == id {
-506		return n
-507	} else if len(n.Children) > 0 {
-508		for i := 0; i < len(n.Children); i++ {
-509			n.Children[i].Parent = n
-510			item := getElementByPropId(&n.Children[i], id)
-511			if item != nil {
-512				return item
-513			}
-514		}
-515	}
-516	return nil
-517}
+449// important to allow the notspans to be injected, the spaces after removing the comments cause the regexp to fail
+450func removeWhitespaceBetweenTags(html string) string {
+451	// Create a regular expression to match spaces between angle brackets
+452	re := regexp.MustCompile(`>\s+<`)
+453	// Replace all matches of spaces between angle brackets with "><"
+454	return re.ReplaceAllString(html, "><")
+455}
+456
+457// Function to hash a struct using SHA-256
+458func hashStruct(s interface{}) ([]byte, error) {
+459	// Convert struct to JSON
+460	jsonData, err := json.Marshal(s)
+461	if err != nil {
+462		return nil, err
+463	}
+464
+465	// Hash the JSON data using SHA-256
+466	hasher := sha256.New()
+467	hasher.Write(jsonData)
+468	hash := hasher.Sum(nil)
+469
+470	return hash, nil
+471}


    
        
        
        
        
        GRIM UI 
        
        
        
        
    
    
        
        

GUI

GUI is the source package

# Open?(go)

# New?(go)

# View?(go)

# CreateNode?(go)

# extractStylesheets?(go)

# extractStyleTags?(go)

# localizePath?(go)

# encapsulateText?(go)

# matchFactory?(go)

# removeWhitespace?(go)

# removeHTMLComments?(go)

  1package gui
  2
  3import (
  4	"bufio"
  5	"crypto/sha256"
  6	_ "embed"
  7	"encoding/json"
  8	"fmt"
  9	"gui/cstyle"
 10	"gui/cstyle/plugins/flex"
 11	"gui/cstyle/plugins/inline"
 12	"gui/cstyle/plugins/textAlign"
 13	"gui/cstyle/transformers/text"
 14	"gui/window"
 15	"time"
 16
 17	"gui/element"
 18	"gui/events"
 19	"gui/utils"
 20	"net/url"
 21	"os"
 22	"path/filepath"
 23	"regexp"
 24	"strconv"
 25	"strings"
 26
 27	rl "github.com/gen2brain/raylib-go/raylib"
 28	"golang.org/x/net/html"
 29)
 30
 31// _ "net/http/pprof"
 32
 33//go:embed master.css
 34var mastercss string
 35
 36type Window struct {
 37	CSS      cstyle.CSS
 38	Document element.Node
 39	Adapter  func()
 40}
 41
 42func Open(path string) Window {
 43	window := New()
 44
 45	styleSheets, styleTags, htmlNodes := parseHTMLFromFile(path)
 46
 47	for _, v := range styleSheets {
 48		window.CSS.StyleSheet(v)
 49	}
 50
 51	for _, v := range styleTags {
 52		window.CSS.StyleTag(v)
 53	}
 54
 55	CreateNode(htmlNodes, &window.Document)
 56
 57	return window
 58}
 59
 60func New() Window {
 61	css := cstyle.CSS{
 62		Width:  800,
 63		Height: 450,
 64	}
 65
 66	css.StyleTag(mastercss)
 67	// This is still apart of computestyle
 68	// css.AddPlugin(position.Init())
 69	css.AddPlugin(inline.Init())
 70	// css.AddPlugin(block.Init())
 71	css.AddPlugin(textAlign.Init())
 72	// css.AddPlugin(inlineText.Init())
 73	css.AddPlugin(flex.Init())
 74
 75	css.AddTransformer(text.Init())
 76
 77	el := element.Node{}
 78	document := el.CreateElement("ROOT")
 79	document.Style["width"] = "800px"
 80	document.Style["height"] = "450px"
 81	document.Properties.Id = "ROOT"
 82	return Window{
 83		CSS:      css,
 84		Document: document,
 85	}
 86}
 87
 88func (w *Window) Render(doc element.Node, state *map[string]element.State) []element.State {
 89	s := *state
 90
 91	flatDoc := flatten(doc)
 92
 93	store := []element.State{}
 94
 95	keys := []string{}
 96
 97	for _, v := range flatDoc {
 98		store = append(store, s[v.Properties.Id])
 99		keys = append(keys, v.Properties.Id)
100	}
101
102	// Create a set of keys to keep
103	keysSet := make(map[string]struct{}, len(keys))
104	for _, key := range keys {
105		keysSet[key] = struct{}{}
106	}
107
108	// Iterate over the map and delete keys not in the set
109	for k := range s {
110		if _, found := keysSet[k]; !found {
111			delete(s, k)
112		}
113	}
114
115	return store
116}
117
118func flatten(n element.Node) []element.Node {
119	var nodes []element.Node
120	nodes = append(nodes, n)
121
122	children := n.Children
123	if len(children) > 0 {
124		for _, ch := range children {
125			chNodes := flatten(ch)
126			nodes = append(nodes, chNodes...)
127		}
128	}
129	return nodes
130}
131
132func View(data *Window, width, height int32) {
133	debug := false
134	data.Document.Style["width"] = strconv.Itoa(int(width)) + "px"
135	data.Document.Style["height"] = strconv.Itoa(int(height)) + "px"
136
137	wm := window.NewWindowManager()
138	wm.FPSCounterOn = true
139
140	wm.OpenWindow(width, height)
141	defer wm.CloseWindow()
142
143	evts := map[string]element.EventList{}
144
145	eventStore := &evts
146
147	state := map[string]element.State{}
148
149	shouldStop := false
150
151	// var hash []byte
152	var rd []element.State
153
154	lastChange := time.Now()
155
156	// Main game loop
157	for !wm.WindowShouldClose() && !shouldStop {
158		// fmt.Println("######################")
159		rl.BeginDrawing()
160		if !shouldStop && debug {
161			shouldStop = true
162		}
163		// Check if the window size has changed
164		newWidth := int32(rl.GetScreenWidth())
165		newHeight := int32(rl.GetScreenHeight())
166
167		resize := false
168
169		if newWidth != width || newHeight != height {
170			resize = true
171			rl.ClearBackground(rl.RayWhite)
172			// Window has been resized, handle the event
173			width = newWidth
174			height = newHeight
175
176			data.CSS.Width = float32(width)
177			data.CSS.Height = float32(height)
178
179			data.Document.Style["width"] = strconv.Itoa(int(width)) + "px"
180			data.Document.Style["height"] = strconv.Itoa(int(height)) + "px"
181		}
182
183		// newHash, _ := hashStruct(&data.Document.Children[0])
184		eventStore = events.GetEvents(&data.Document.Children[0], &state, eventStore)
185		for _, v := range *eventStore {
186			if v.Event.MouseOver {
187
188				if v.Event.Target.TagName == "body" || v.Event.Target.TagName == "html" {
189					continue
190				} else {
191					w := state[v.Event.Target.Properties.Id].Width
192					h := state[v.Event.Target.Properties.Id].Height
193					x := state[v.Event.Target.Properties.Id].X
194					y := state[v.Event.Target.Properties.Id].Y
195
196					el := getElementByPropId(&data.Document.Children[0], v.Event.Target.Properties.Id)
197
198					if el != nil {
199						data.CSS.ComputeNodeStyle(el, &state)
200
201						if w != state[v.Event.Target.Properties.Id].Width &&
202							h != state[v.Event.Target.Properties.Id].Height &&
203							x != state[v.Event.Target.Properties.Id].X &&
204							y != state[v.Event.Target.Properties.Id].Y {
205							fmt.Println("RELOAD")
206						} else {
207							fmt.Println("SWAP")
208						}
209
210					}
211
212				}
213
214			}
215
216		}
217		if resize {
218			if wm.FPS != 30 {
219				wm.SetFPS(30)
220			}
221			lastChange = time.Now()
222			// hash = newHash
223			newDoc := CopyNode(data.CSS, data.Document.Children[0], &data.Document)
224
225			newDoc = data.CSS.Transform(newDoc)
226
227			data.CSS.ComputeNodeStyle(&newDoc, &state)
228			rd = data.Render(newDoc, &state)
229			wm.LoadTextures(rd)
230
231			// AddHTML(&newDoc)
232			// fmt.Println(newDoc.QuerySelector("body").InnerHTML)
233
234			AddHTML(&data.Document)
235			fmt.Println(time.Since(lastChange))
236		}
237		wm.Draw(rd)
238
239		// could use a return value that indicates whether or not a event has ran to ramp/deramp fps based on activity
240
241		events.RunEvents(eventStore)
242		// ran := events.RunEvents(eventStore)
243
244		if time.Since(lastChange) > 5*time.Second {
245			if wm.FPS != 1 {
246				wm.SetFPS(1)
247			}
248		}
249
250		rl.EndDrawing()
251	}
252}
253
254func CopyNode(c cstyle.CSS, node element.Node, parent *element.Node) element.Node {
255	n := element.Node{}
256	n.TagName = node.TagName
257	n.InnerText = node.InnerText
258	n.Style = node.Style
259	n.Id = node.Id
260	n.ClassList = node.ClassList
261	n.Href = node.Href
262	n.Src = node.Src
263	n.Title = node.Title
264	n.Attribute = node.Attribute
265	n.Value = node.Value
266	n.ScrollY = node.ScrollY
267	n.InnerHTML = node.InnerHTML
268	n.OuterHTML = node.OuterHTML
269	n.Properties.Id = node.Properties.Id
270	n.Properties.Focusable = node.Properties.Focusable
271	n.Properties.Focused = node.Properties.Focused
272	n.Properties.Editable = node.Properties.Editable
273	n.Properties.Hover = node.Properties.Hover
274	n.Properties.Selected = node.Properties.Selected
275
276	n.Parent = parent
277
278	n.Style = c.GetStyles(n)
279
280	for _, v := range node.Children {
281		n.Children = append(n.Children, CopyNode(c, v, &n))
282	}
283	return n
284}
285
286func CreateNode(node *html.Node, parent *element.Node) {
287	if node.Type == html.ElementNode {
288		newNode := parent.CreateElement(node.Data)
289		for _, attr := range node.Attr {
290			if attr.Key == "class" {
291				classes := strings.Split(attr.Val, " ")
292				for _, class := range classes {
293					newNode.ClassList.Add(class)
294				}
295			} else if attr.Key == "id" {
296				newNode.Id = attr.Val
297			} else if attr.Key == "contenteditable" && (attr.Val == "" || attr.Val == "true") {
298				newNode.Properties.Editable = true
299			} else if attr.Key == "href" {
300				newNode.Href = attr.Val
301			} else if attr.Key == "src" {
302				newNode.Src = attr.Val
303			} else if attr.Key == "title" {
304				newNode.Title = attr.Val
305			} else {
306				newNode.SetAttribute(attr.Key, attr.Val)
307			}
308		}
309		newNode.InnerText = strings.TrimSpace(utils.GetInnerText(node))
310		// Recursively traverse child nodes
311		for child := node.FirstChild; child != nil; child = child.NextSibling {
312			if child.Type == html.ElementNode {
313				CreateNode(child, &newNode)
314			}
315		}
316		parent.AppendChild(newNode)
317
318	} else {
319		for child := node.FirstChild; child != nil; child = child.NextSibling {
320			if child.Type == html.ElementNode {
321				CreateNode(child, parent)
322			}
323		}
324	}
325}
326
327func AddHTML(n *element.Node) {
328	// Head is not renderable
329	n.InnerHTML = utils.InnerHTML(*n)
330	tag, closing := utils.NodeToHTML(*n)
331	n.OuterHTML = tag + n.InnerHTML + closing
332	for i := range n.Children {
333		AddHTML(&n.Children[i])
334	}
335}
336
337func parseHTMLFromFile(path string) ([]string, []string, *html.Node) {
338	file, _ := os.Open(path)
339	defer file.Close()
340
341	scanner := bufio.NewScanner(file)
342	var htmlContent string
343
344	for scanner.Scan() {
345		htmlContent += scanner.Text() + "\n"
346	}
347
348	htmlContent = removeHTMLComments(htmlContent)
349
350	doc, _ := html.Parse(strings.NewReader(encapsulateText(removeWhitespaceBetweenTags(htmlContent))))
351
352	// Extract stylesheet link tags and style tags
353	stylesheets := extractStylesheets(doc, filepath.Dir(path))
354	styleTags := extractStyleTags(doc)
355
356	return stylesheets, styleTags, doc
357}
358
359func extractStylesheets(n *html.Node, baseDir string) []string {
360	var stylesheets []string
361
362	var dfs func(*html.Node)
363	dfs = func(node *html.Node) {
364		if node.Type == html.ElementNode && node.Data == "link" {
365			var href string
366			isStylesheet := false
367
368			for _, attr := range node.Attr {
369				if attr.Key == "rel" && attr.Val == "stylesheet" {
370					isStylesheet = true
371				} else if attr.Key == "href" {
372					href = attr.Val
373				}
374			}
375
376			if isStylesheet {
377				resolvedHref := localizePath(baseDir, href)
378				stylesheets = append(stylesheets, resolvedHref)
379			}
380		}
381
382		for c := node.FirstChild; c != nil; c = c.NextSibling {
383			dfs(c)
384		}
385	}
386
387	dfs(n)
388	return stylesheets
389}
390
391func extractStyleTags(n *html.Node) []string {
392	var styleTags []string
393
394	var dfs func(*html.Node)
395	dfs = func(node *html.Node) {
396		if node.Type == html.ElementNode && node.Data == "style" {
397			var styleContent strings.Builder
398			for c := node.FirstChild; c != nil; c = c.NextSibling {
399				if c.Type == html.TextNode {
400					styleContent.WriteString(c.Data)
401				}
402			}
403			styleTags = append(styleTags, styleContent.String())
404		}
405
406		for c := node.FirstChild; c != nil; c = c.NextSibling {
407			dfs(c)
408		}
409	}
410
411	dfs(n)
412	return styleTags
413}
414
415func localizePath(rootPath, filePath string) string {
416	// Check if the file path has a scheme, indicating it's a URL
417	u, err := url.Parse(filePath)
418	if err == nil && u.Scheme != "" {
419		return filePath
420	}
421
422	// Join the root path and the file path to create an absolute path
423	absPath := filepath.Join(rootPath, filePath)
424
425	// If the absolute path is the same as the original path, return it
426	if absPath == filePath {
427		return filePath
428	}
429
430	return "./" + absPath
431}
432
433func encapsulateText(htmlString string) string {
434	openOpen := regexp.MustCompile(`(<\w+[^>]*>)([^<]+)(<\w+[^>]*>)`)
435	closeOpen := regexp.MustCompile(`(</\w+[^>]*>)([^<]+)(<\w+[^>]*>)`)
436	closeClose := regexp.MustCompile(`(<\/\w+[^>]*>)([^<]+)(<\/\w+[^>]*>)`)
437	a := matchFactory(openOpen)
438	t := openOpen.ReplaceAllStringFunc(htmlString, a)
439	// fmt.Println(t)
440	b := matchFactory(closeOpen)
441	u := closeOpen.ReplaceAllStringFunc(t, b)
442	// fmt.Println(u)
443	c := matchFactory(closeClose)
444	v := closeClose.ReplaceAllStringFunc(u, c)
445	// fmt.Println(v)
446	return v
447}
448
449func matchFactory(re *regexp.Regexp) func(string) string {
450	return func(match string) string {
451		submatches := re.FindStringSubmatch(match)
452		if len(submatches) != 4 {
453			return match
454		}
455
456		// Process submatches
457		if len(removeWhitespace(submatches[2])) > 0 {
458			return submatches[1] + "<notaspan>" + submatches[2] + "</notaspan>" + submatches[3]
459		} else {
460			return match
461		}
462	}
463}
464func removeWhitespace(htmlString string) string {
465	// Remove extra white space
466	reSpaces := regexp.MustCompile(`\s+`)
467	htmlString = reSpaces.ReplaceAllString(htmlString, " ")
468
469	// Trim leading and trailing white space
470	htmlString = strings.TrimSpace(htmlString)
471
472	return htmlString
473}
474
475func removeHTMLComments(htmlString string) string {
476	re := regexp.MustCompile(`<!--[\s\S]*?-->`)
477	return re.ReplaceAllString(htmlString, "")
478}
479
480// important to allow the notspans to be injected, the spaces after removing the comments cause the regexp to fail
481func removeWhitespaceBetweenTags(html string) string {
482	// Create a regular expression to match spaces between angle brackets
483	re := regexp.MustCompile(`>\s+<`)
484	// Replace all matches of spaces between angle brackets with "><"
485	return re.ReplaceAllString(html, "><")
486}
487
488// Function to hash a struct using SHA-256
489func hashStruct(s interface{}) ([]byte, error) {
490	// Convert struct to JSON
491	jsonData, err := json.Marshal(s)
492	if err != nil {
493		return nil, err
494	}
495
496	// Hash the JSON data using SHA-256
497	hasher := sha256.New()
498	hasher.Write(jsonData)
499	hash := hasher.Sum(nil)
500
501	return hash, nil
502}
503
504func getElementByPropId(n *element.Node, id string) *element.Node {
505	if n.Properties.Id == id {
506		return n
507	} else if len(n.Children) > 0 {
508		for i := 0; i < len(n.Children); i++ {
509			n.Children[i].Parent = n
510			item := getElementByPropId(&n.Children[i], id)
511			if item != nil {
512				return item
513			}
514		}
515	}
516	return nil
517}