diff --git a/constants.go b/constants.go index e5eb6ff..04b58de 100644 --- a/constants.go +++ b/constants.go @@ -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 -) \ No newline at end of file + WindowBorderless WindowFlag = 1 << 0 + WindowTitled WindowFlag = 1 << 1 + WindowResizable WindowFlag = 1 << 2 + WindowMaximizable WindowFlag = 1 << 3 +) diff --git a/examples/Browser/tab.go b/examples/Browser/tab.go index a8193e7..ecab35d 100644 --- a/examples/Browser/tab.go +++ b/examples/Browser/tab.go @@ -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 @@ -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) } diff --git a/examples/Browser/ui.go b/examples/Browser/ui.go index 35b4ad0..5bca3bd 100644 --- a/examples/Browser/ui.go +++ b/examples/Browser/ui.go @@ -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 { @@ -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() { diff --git a/examples/Tutorial 4 - JavaScript/main.go b/examples/Tutorial 4 - JavaScript/main.go index 0a8b7c4..60d0e27 100644 --- a/examples/Tutorial 4 - JavaScript/main.go +++ b/examples/Tutorial 4 - JavaScript/main.go @@ -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{} @@ -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() -} \ No newline at end of file +} diff --git a/structures.go b/structures.go index 84680a4..7385585 100644 --- a/structures.go +++ b/structures.go @@ -8,7 +8,6 @@ package ultralight // #include 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)