Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Commit

Permalink
Updated Browser example
Browse files Browse the repository at this point in the history
  • Loading branch information
maneac committed Aug 20, 2019
1 parent 2a4c54c commit 139b3d6
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 131 deletions.
10 changes: 5 additions & 5 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ type WindowFlag int

// Feature flags for the Window instance
const (
WindowBorderless WindowFlag = 1 << 0
WindowTitled WindowFlag = 1 << 1
WindowResizable WindowFlag = 1 << 2
WindowMaximizable WindowFlag = 1 << 3
)
WindowBorderless WindowFlag = 1 << 0
WindowTitled WindowFlag = 1 << 1
WindowResizable WindowFlag = 1 << 2
WindowMaximizable WindowFlag = 1 << 3
)
37 changes: 1 addition & 36 deletions examples/Browser/tab.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import (
"github.com/maneac/go-ultralight"
)

const inspectorHeight = 300

type tab struct {
ui *ui
overlay *ultralight.Overlay
inspectorOverlay *ultralight.Overlay
id uint
isReadyToClose bool
containerWidth, containerHeight int
Expand Down Expand Up @@ -61,45 +58,13 @@ func (t *tab) view() *ultralight.View {
func (t *tab) show() {
t.overlay.Show()
t.overlay.Focus()
if t.inspectorOverlay != nil {
t.inspectorOverlay.Show()
}
}

func (t *tab) hide() {
t.overlay.Hide()
t.overlay.Unfocus()
if t.inspectorOverlay != nil {
t.inspectorOverlay.Hide()
}
}

func (t *tab) toggleInspector() {
// if t.inspectorOverlay == nil {
// overlay := t.ui.window.CreateOverlay(t.overlay.GetView().Inspector(), 0, 0)
// } else {
// if t.inspectorOverlay.IsHidden() {
// t.inspectorOverlay.Show()
// } else {
// t.inspectorOverlay.Hide()
// }
// }
// t.resize(t.containerWidth, t.containerHeight)
}

func (t *tab) resize(width, height int) {
t.containerWidth = width
t.containerHeight = height
contentHeight := t.containerHeight
if t.inspectorOverlay != nil && !t.inspectorOverlay.IsHidden() {
t.inspectorOverlay.Resize(width, height)
contentHeight -= inspectorHeight
}
if contentHeight < 1 {
contentHeight = 1
}
t.overlay.Resize(t.containerWidth, t.containerHeight)
if t.inspectorOverlay != nil && !t.inspectorOverlay.IsHidden() {
t.inspectorOverlay.MoveTo(0, t.overlay.GetY()+int(t.overlay.GetHeight()))
}
t.overlay.Resize(width, height)
}
169 changes: 85 additions & 84 deletions examples/Browser/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func createUI(window *ultralight.Window) *ui {
u.activeTabID = 0
u.tabIDCounter = 0

//Inherited
window.SetResizeCallback(func(_, _ int) {
tabHeight := window.GetHeight() - uiHeight
if tabHeight < 1 {
Expand All @@ -45,97 +44,99 @@ func createUI(window *ultralight.Window) *ui {
}
})

view.SetDOMReadyCallback(func() {
view.BindJSCallback("OnBack", func(v *ultralight.View, params []string) {
t := u.activeTab()
if t != nil {
t.view().GoBack()
}
})
view.BindJSCallback("OnForward", func(v *ultralight.View, params []string) {
t := u.activeTab()
if t != nil {
t.view().GoForward()
}
})
view.BindJSCallback("OnRefresh", func(v *ultralight.View, params []string) {
t := u.activeTab()
if t != nil {
t.view().Reload()
}
})
view.BindJSCallback("OnStop", func(v *ultralight.View, params []string) {
t := u.activeTab()
if t != nil {
t.view().Stop()
view.SetDOMReadyCallback(bindCallbacks(&u, view))

view.LoadURL("file://assets/ui.html")

return &u
}

func bindCallbacks(u *ui, view *ultralight.View) {
view.BindJSCallback("OnBack", func(v *ultralight.View, params []string) {
t := u.activeTab()
if t != nil {
t.view().GoBack()
}
})
view.BindJSCallback("OnForward", func(v *ultralight.View, params []string) {
t := u.activeTab()
if t != nil {
t.view().GoForward()
}
})
view.BindJSCallback("OnRefresh", func(v *ultralight.View, params []string) {
t := u.activeTab()
if t != nil {
t.view().Reload()
}
})
view.BindJSCallback("OnStop", func(v *ultralight.View, params []string) {
t := u.activeTab()
if t != nil {
t.view().Stop()
}
})
view.BindJSCallback("OnToggleTools", func(v *ultralight.View, params []string) {
t := u.activeTab()
if t != nil {
t.toggleInspector()
}
})
view.BindJSCallback("OnRequestNewTab", func(v *ultralight.View, params []string) {
u.createNewTab()
})
view.BindJSCallback("OnRequestTabClose", func(v *ultralight.View, params []string) {
if len(params) == 1 {
intID, _ := strconv.Atoi(params[0])
id := uint(intID)
tab := u.tabs[id]
if tab == nil {
return
}
})
view.BindJSCallback("OnToggleTools", func(v *ultralight.View, params []string) {
t := u.activeTab()
if t != nil {
t.toggleInspector()
if len(u.tabs) == 1 {
globalBrowser.app.Quit()
}
})
view.BindJSCallback("OnRequestNewTab", func(v *ultralight.View, params []string) {
u.createNewTab()
})
view.BindJSCallback("OnRequestTabClose", func(v *ultralight.View, params []string) {
if len(params) == 1 {
intID, _ := strconv.Atoi(params[0])
id := uint(intID)
tab := u.tabs[id]
if tab == nil {
return
}
if len(u.tabs) == 1 {
globalBrowser.app.Quit()
}
u.view().EvaluateScript(fmt.Sprintf("closeTab(%d)", id))
if id != u.activeTabID {
delete(u.tabs, id)
} else {
tab.setReadyToClose(true)
}
u.view().EvaluateScript(fmt.Sprintf("closeTab(%d)", id))
if id != u.activeTabID {
delete(u.tabs, id)
} else {
tab.setReadyToClose(true)
}
})
view.BindJSCallback("OnActiveTabChange", func(v *ultralight.View, params []string) {
if len(params) == 1 {
intID, _ := strconv.Atoi(params[0])
id := uint(intID)
tab := u.tabs[id]
if tab == nil {
return
}
u.tabs[u.activeTabID].hide()

if u.tabs[u.activeTabID].readyToClose() {
delete(u.tabs, u.activeTabID)
}

u.activeTabID = id
tab.show()

tabView := tab.view()
u.setLoading(tabView.IsLoading())
u.setCanGoBack(tabView.CanGoBack())
u.setCanGoForward(tabView.CanGoForward())
u.setURL(tabView.GetURL())
}
})
view.BindJSCallback("OnActiveTabChange", func(v *ultralight.View, params []string) {
if len(params) == 1 {
intID, _ := strconv.Atoi(params[0])
id := uint(intID)
tab := u.tabs[id]
if tab == nil {
return
}
})
view.BindJSCallback("OnRequestChangeURL", func(v *ultralight.View, params []string) {
if len(params) == 1 {
if len(u.tabs) > 0 {
u.tabs[u.activeTabID].view().LoadURL(params[0])
}
u.tabs[u.activeTabID].hide()

if u.tabs[u.activeTabID].readyToClose() {
delete(u.tabs, u.activeTabID)
}
})

u.createNewTab()
})
u.activeTabID = id
tab.show()

view.LoadURL("file://assets/ui.html")
tabView := tab.view()
u.setLoading(tabView.IsLoading())
u.setCanGoBack(tabView.CanGoBack())
u.setCanGoForward(tabView.CanGoForward())
u.setURL(tabView.GetURL())
}
})
view.BindJSCallback("OnRequestChangeURL", func(v *ultralight.View, params []string) {
if len(params) == 1 {
if len(u.tabs) > 0 {
u.tabs[u.activeTabID].view().LoadURL(params[0])
}
}
})

return &u
u.createNewTab()
}

func (u *ui) createNewTab() {
Expand Down
10 changes: 5 additions & 5 deletions examples/Tutorial 4 - JavaScript/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "github.com/maneac/go-ultralight"
// MyApp handles the overlay logic for a window
type MyApp struct {
overlay *ultralight.Overlay
}
}

func createMyApp(window *ultralight.Window) {
myApp := &MyApp{}
Expand Down Expand Up @@ -40,19 +40,19 @@ func main() {
config := ultralight.CreateConfig()
app := config.CreateApp()

// Create a Window
// Create a Window
window := ultralight.CreateWindow(app.GetMainMonitor(), 300, 300, false, ultralight.WindowTitled)

// Set the title of the Window
window.SetTitle("Tutorial 4 - JavaScript")

// Bind the Window to the App instance
app.SetWindow(window)

// Creates a MyApp instance to handle the Overlays and JavaScript
// NOTE: this structure is unnecessary with these bindings
createMyApp(window)

// Runs the app
app.Run()
}
}
1 change: 0 additions & 1 deletion structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package ultralight
// #include <ultralight.h>
import "C"


// JSBindFunc defines the structure of JavaScript callback functions, where
// 'params' is an array of the parameters passed to the JavaScript function
type JSBindFunc func(view *View, params []string)
Expand Down

0 comments on commit 139b3d6

Please sign in to comment.