Skip to content

Commit

Permalink
Fixed history bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
danfragoso committed May 24, 2020
1 parent 6e8b642 commit 7edcc9a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 41 deletions.
38 changes: 14 additions & 24 deletions browser.go
Expand Up @@ -9,7 +9,7 @@ import (
structs "thdwb/structs"
)

func loadDocument(browser *structs.WebBrowser, link string, callback func()) {
func loadDocument(browser *structs.WebBrowser, link string) {
URL := sauce.ParseURL(link)

if URL.Scheme == "" && URL.Host == "" {
Expand All @@ -22,38 +22,28 @@ func loadDocument(browser *structs.WebBrowser, link string, callback func()) {
parsedDocument.URL = resource.URL

browser.Document = parsedDocument
callback()
}

func loadDocumentFromAsset(document []byte) *structs.HTMLDocument {
parsedDocument := ketchup.ParseDocument(string(document))
parsedDocument.URL = sauce.ParseURL("thdwb://homepage/")

return parsedDocument
if browser.History.PageCount() == 0 || browser.History.Last().String() != resource.URL.String() {
browser.History.Push(resource.URL)
}
}

func loadDocumentFromUrl(browser *structs.WebBrowser, statusLabel *mustard.LabelWidget, urlInput *mustard.InputWidget, viewPort *mustard.CanvasWidget) {
statusLabel.SetContent("Loading: " + urlInput.GetValue())

go loadDocument(browser, urlInput.GetValue(), func() {
browser.History.Push(browser.Document.URL.String())

ctx := viewPort.GetContext()
ctx.SetRGB(1, 1, 1)
ctx.Clear()
loadDocument(browser, urlInput.GetValue())

perf.Start("render")
bun.RenderDocument(ctx, browser.Document)
perf.Stop("render")
perf.Start("render")
bun.RenderDocument(viewPort.GetContext(), browser.Document)
perf.Stop("render")

statusLabel.SetContent(createStatusLabel(perf))
viewPort.SetOffset(0)
viewPort.SetDrawingRepaint(true)
viewPort.RequestRepaint()
statusLabel.RequestRepaint()
statusLabel.SetContent(createStatusLabel(perf))
viewPort.SetOffset(0)
viewPort.SetDrawingRepaint(true)
viewPort.RequestRepaint()
statusLabel.RequestRepaint()

urlInput.SetValue(browser.Document.URL.String())
})
urlInput.SetValue(browser.Document.URL.String())
}

func createStatusLabel(perf *profiler.Profiler) string {
Expand Down
21 changes: 11 additions & 10 deletions main.go
Expand Up @@ -3,10 +3,8 @@ package main
import (
"runtime"

assets "thdwb/assets"
bun "thdwb/bun"
gg "thdwb/gg"
ketchup "thdwb/ketchup"
mustard "thdwb/mustard"
profiler "thdwb/profiler"
structs "thdwb/structs"
Expand All @@ -26,8 +24,7 @@ func main() {

perf = profiler.CreateProfiler()

browser := &structs.WebBrowser{Document: loadDocumentFromAsset(assets.HomePage()), History: &structs.History{}}
browser.History.Push("thdwb://homepage/")
browser := &structs.WebBrowser{Document: &structs.HTMLDocument{}, History: &structs.History{}}

app := mustard.CreateNewApp("THDWB")
window := mustard.CreateNewWindow("THDWB", 600, 600)
Expand All @@ -42,7 +39,9 @@ func main() {

debugFrame := createDebugFrame(window, browser)
rootFrame.AttachWidget(appBar)
browser.Document = ketchup.ParseDocument(browser.Document.RawDocument)

loadDocument(browser, "thdwb://homepage")
urlInput.SetValue(browser.Document.URL.String())

viewPort := mustard.CreateCanvasWidget(func(ctx *gg.Context) {
bun.RenderDocument(ctx, browser.Document)
Expand All @@ -58,13 +57,15 @@ func main() {
})

window.RegisterButton(goButton, func() {
loadDocumentFromUrl(browser, statusLabel, urlInput, viewPort)
go loadDocumentFromUrl(browser, statusLabel, urlInput, viewPort)
})

window.RegisterButton(backButton, func() {
browser.History.Pop()
urlInput.SetValue(browser.History.Last())
loadDocumentFromUrl(browser, statusLabel, urlInput, viewPort)
if browser.History.PageCount() > 1 {
browser.History.Pop()
urlInput.SetValue(browser.History.Last().String())
go loadDocumentFromUrl(browser, statusLabel, urlInput, viewPort)
}
})

window.AttachPointerPositionEventListener(func(pointerX, pointerY float64) {
Expand Down Expand Up @@ -95,7 +96,7 @@ func main() {
if browser.Document.SelectedElement.Element == "a" {
href := browser.Document.SelectedElement.Attr("href")
urlInput.SetValue(href)
loadDocumentFromUrl(browser, statusLabel, urlInput, viewPort)
go loadDocumentFromUrl(browser, statusLabel, urlInput, viewPort)
}
}
})
Expand Down
1 change: 1 addition & 0 deletions sauce/sauce.go
Expand Up @@ -16,6 +16,7 @@ func GetResource(URL *url.URL) *structs.Resource {
if URL.Host == "homepage" {
return &structs.Resource{
Body: string(assets.HomePage()),
URL: URL,
}
}

Expand Down
12 changes: 8 additions & 4 deletions structs/structs.go
Expand Up @@ -29,14 +29,18 @@ type HTMLDocument struct {
}

type History struct {
pages []string
pages []*url.URL
}

func (history *History) Push(url string) {
history.pages = append(history.pages, url)
func (history *History) PageCount() int {
return len(history.pages)
}

func (history *History) Last() string {
func (history *History) Push(URL *url.URL) {
history.pages = append(history.pages, URL)
}

func (history *History) Last() *url.URL {
return history.pages[len(history.pages)-1]
}

Expand Down
5 changes: 2 additions & 3 deletions ui.go
Expand Up @@ -17,14 +17,14 @@ func createDebugFrame(window *mustard.Window, browser *structs.WebBrowser) *must
debugBar.SetBackgroundColor("#eee")
debugFrame.SetHeight(400)

source := mustard.CreateTextWidget(browser.Document.RawDocument)
source := mustard.CreateTextWidget("")
source.SetFontSize(12)

dv := mustard.CreateFrame(mustard.HorizontalFrame)
dv.SetBackgroundColor("#999")
dv.SetWidth(1)

jsonByte, _ := json.MarshalIndent(browser.Document.RootElement, "", " ")
jsonByte, _ := json.MarshalIndent("", "", " ")
json := mustard.CreateTextWidget(string(jsonByte))
json.SetWidth(200)
json.SetFontSize(12)
Expand All @@ -50,7 +50,6 @@ func createMainBar(window *mustard.Window, browser *structs.WebBrowser) (*mustar

inputFrame := mustard.CreateFrame(mustard.VerticalFrame)
urlInput := mustard.CreateInputWidget()
urlInput.SetValue(browser.Document.URL.String())
icon := mustard.CreateFrame(mustard.VerticalFrame)
img := mustard.CreateImageWidget(assets.Logo())

Expand Down

0 comments on commit 7edcc9a

Please sign in to comment.