func NewView() *View { widget := C.webkit_web_view_new() view := &View{ Widget: widget, View: (*C.WebKitWebView)(unsafe.Pointer(widget)), } // ready to show connect(view.View, "ready-to-show", func() { C.gtk_widget_show_all(view.Widget) }) // page load state changed connect(view.View, "load-changed", func(_, ev interface{}) { p("load changed %d\n", ev.(int)) }) // context context := C.webkit_web_view_get_context(view.View) C.webkit_web_context_set_spell_checking_enabled(context, C.gtk_false()) C.webkit_web_context_set_tls_errors_policy(context, C.WEBKIT_TLS_ERRORS_POLICY_IGNORE) C.webkit_web_context_set_disk_cache_directory(context, toGStr(os.TempDir())) // settings settings := C.webkit_web_view_get_settings(view.View) C.webkit_settings_set_enable_java(settings, C.gtk_false()) C.webkit_settings_set_enable_tabs_to_links(settings, C.gtk_false()) C.webkit_settings_set_enable_dns_prefetching(settings, C.gtk_true()) C.webkit_settings_set_javascript_can_access_clipboard(settings, C.gtk_true()) C.webkit_settings_set_enable_site_specific_quirks(settings, C.gtk_true()) C.webkit_settings_set_enable_smooth_scrolling(settings, C.gtk_true()) // handle cookie cookieManager := C.webkit_web_context_get_cookie_manager(context) C.webkit_cookie_manager_set_persistent_storage(cookieManager, toGStr(cookieFilePath), C.WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT) return view }
func view_is_focus(viewp unsafe.Pointer) bool { return C.gtk_widget_is_focus((*C.GtkWidget)(viewp)) == C.gtk_true() }
func main() { // init gtk var argc C.int C.gtk_init(&argc, nil) // root window win := C.gtk_window_new(C.GTK_WINDOW_TOPLEVEL) connect(win, "destroy", func() { // quit gtk when window is closed C.gtk_main_quit() }) // root grid grid := C.gtk_grid_new() C.gtk_container_add(asContainer(win), grid) // webview container pages := C.gtk_notebook_new() C.gtk_grid_attach(asGrid(grid), pages, 0, 0, 1, 1) C.gtk_widget_set_vexpand(pages, C.gtk_true()) C.gtk_widget_set_hexpand(pages, C.gtk_true()) C.gtk_notebook_set_tab_pos(asNotebook(pages), C.GTK_POS_LEFT) // new view constructor var newView func() *View newView = func() *View { view := NewView() label := C.gtk_label_new(nil) C.gtk_label_set_use_markup(asLabel(label), C.gtk_true()) C.gtk_misc_set_alignment(asMisc(label), 0, 0.5) C.gtk_notebook_append_page(asNotebook(pages), view.Widget, label) // new view is requested connect(view.View, "create", func() *C.GtkWidget { return newView().Widget }) // set tab label to page title connect(view.View, "notify::title", func() { var title string // trim long title for _, r := range fromGStr(C.webkit_web_view_get_title(view.View)) { if len(title) > 32 { break } title += string(r) } C.gtk_label_set_markup(asLabel(label), toGStr(fmt.Sprintf(`<span font="10">%s</span>`, title))) }) return view } // first view view := newView() C.webkit_web_view_load_uri(view.View, toGStr("http://www.bilibili.tv")) // show window and run C.gtk_widget_show_all(win) C.gtk_main() }