本文整理汇总了Golang中github.com/gotk3/gotk3/glib.IdleAdd函数的典型用法代码示例。如果您正苦于以下问题:Golang IdleAdd函数的具体用法?Golang IdleAdd怎么用?Golang IdleAdd使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了IdleAdd函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestWebView_Title
func TestWebView_Title(t *testing.T) {
webView := NewWebView()
defer webView.Destroy()
wantTitle := "foo"
var gotTitle string
webView.Connect("notify::title", func() {
glib.IdleAdd(func() bool {
gotTitle = webView.Title()
if gotTitle != "" {
gtk.MainQuit()
}
return false
})
})
glib.IdleAdd(func() bool {
webView.LoadHTML("<html><head><title>"+wantTitle+"</title></head><body></body></html>", "")
return false
})
gtk.Main()
if wantTitle != gotTitle {
t.Errorf("want title %q, got %q", wantTitle, gotTitle)
}
}
示例2: TestWebView_URI
func TestWebView_URI(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {})
wantURI := server.URL + "/"
var gotURI string
webView.Connect("notify::uri", func() {
glib.IdleAdd(func() bool {
gotURI = webView.URI()
if gotURI != "" {
gtk.MainQuit()
}
return false
})
})
glib.IdleAdd(func() bool {
webView.LoadURI(server.URL)
return false
})
gtk.Main()
if wantURI != gotURI {
t.Errorf("want URI %q, got %q", wantURI, gotURI)
}
}
示例3: createGui
func (app *Applet) createGui(init, show bool) {
if app.gui != nil {
glib.IdleAdd(func() {
app.Window().SetVisibility(show)
})
return
}
glib.IdleAdd(func() {
app.gui, app.win = guigtk.NewGui(app.cp)
if app.gui == nil {
return
}
app.gui.Load()
app.win.SetIconFromFile(app.FileLocation("icon")) // TODO: debug path.Join(localDir, "data/icon.png")
app.win.Connect("delete-event", func() bool { app.gui, app.win = nil, nil; return false })
// app.win.Connect("delete-event", func() bool { window.Iconify(); return true })
if init {
app.cpInit()
}
if !show {
app.win.Iconify()
}
})
}
示例4: EvaluateJavaScript
// EvaluateJavaScript runs the JavaScript in script in the view's context and
// returns the script's result as a Go value.
func (v *View) EvaluateJavaScript(script string) (result interface{}, err error) {
resultChan := make(chan interface{}, 1)
errChan := make(chan error, 1)
glib.IdleAdd(func() bool {
v.WebView.RunJavaScript(script, func(result *gojs.Value, err error) {
glib.IdleAdd(func() bool {
if err == nil {
goval, err := result.GoValue()
if err != nil {
errChan <- err
return false
}
resultChan <- goval
} else {
errChan <- err
}
return false
})
})
return false
})
select {
case result = <-resultChan:
return result, nil
case err = <-errChan:
return nil, err
}
}
示例5: main
func main() {
gtk.Init(nil)
win, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
if err != nil {
log.Fatal("Unable to create window:", err)
}
win.Connect("destroy", func() {
gtk.MainQuit()
})
win.Add(windowWidget())
// Native GTK is not thread safe, and thus, gotk3's GTK bindings may not
// be used from other goroutines. Instead, glib.IdleAdd() must be used
// to add a function to run in the GTK main loop when it is in an idle
// state.
//
// Two examples of using glib.IdleAdd() are shown below. The first runs
// a user created function, LabelSetTextIdle, and passes it two
// arguments for a label and the text to set it with. The second calls
// (*gtk.Label).SetText directly, passing in only the text as an
// argument.
//
// If the function passed to glib.IdleAdd() returns one argument, and
// that argument is a bool, this return value will be used in the same
// manner as a native g_idle_add() call. If this return value is false,
// the function will be removed from executing in the GTK main loop's
// idle state. If the return value is true, the function will continue
// to execute when the GTK main loop is in this state.
go func() {
for {
time.Sleep(time.Second)
s := fmt.Sprintf("Set a label %d time(s)!", nSets)
_, err := glib.IdleAdd(LabelSetTextIdle, topLabel, s)
if err != nil {
log.Fatal("IdleAdd() failed:", err)
}
nSets++
s = fmt.Sprintf("Set a label %d time(s)!", nSets)
_, err = glib.IdleAdd(bottomLabel.SetText, s)
if err != nil {
log.Fatal("IdleAdd() failed:", err)
}
nSets++
}
}()
win.ShowAll()
gtk.Main()
}
示例6: TestWebView_RunJavaScript
func TestWebView_RunJavaScript(t *testing.T) {
webView := NewWebView()
defer webView.Destroy()
wantResultString := "abc"
webView.Connect("load-changed", func(_ *glib.Object, loadEvent LoadEvent) {
switch loadEvent {
case LoadFinished:
webView.RunJavaScript(`document.getElementById("foo").innerHTML`, func(result *gojs.Value, err error) {
if err != nil {
t.Errorf("RunJavaScript error: %s", err)
}
resultString := webView.JavaScriptGlobalContext().ToStringOrDie(result)
if wantResultString != resultString {
t.Errorf("want result string %q, got %q", wantResultString, resultString)
}
gtk.MainQuit()
})
}
})
glib.IdleAdd(func() bool {
webView.LoadHTML(`<p id=foo>abc</p>`, "")
return false
})
gtk.Main()
}
示例7: TestWebView_LoadHTML
func TestWebView_LoadHTML(t *testing.T) {
webView := NewWebView()
defer webView.Destroy()
loadOk := false
webView.Connect("load-failed", func() {
t.Errorf("load failed")
})
webView.Connect("load-changed", func(_ *glib.Object, loadEvent LoadEvent) {
switch loadEvent {
case LoadFinished:
loadOk = true
gtk.MainQuit()
}
})
glib.IdleAdd(func() bool {
webView.LoadHTML("<p>hello</p>", "")
return false
})
gtk.Main()
if !loadOk {
t.Error("!loadOk")
}
}
示例8: TestWebView_LoadURI_load_failed
func TestWebView_LoadURI_load_failed(t *testing.T) {
webView := NewWebView()
defer webView.Destroy()
loadFailed := false
loadFinished := false
webView.Connect("load-failed", func() {
loadFailed = true
})
webView.Connect("load-changed", func(_ *glib.Object, loadEvent LoadEvent) {
switch loadEvent {
case LoadFinished:
loadFinished = true
gtk.MainQuit()
}
})
glib.IdleAdd(func() bool {
// Load a bad URL to trigger load failure.
webView.LoadURI("http://127.0.0.1:99999")
return false
})
gtk.Main()
if !loadFailed {
t.Error("!loadFailed")
}
if !loadFinished {
t.Error("!loadFinished")
}
}
示例9: NewView
// NewView creates a new View in the context.
func (c *Context) NewView() *View {
view := make(chan *View, 1)
glib.IdleAdd(func() bool {
webView := webkit2.NewWebView()
settings := webView.Settings()
settings.SetEnableWriteConsoleMessagesToStdout(true)
settings.SetUserAgentWithApplicationDetails("WebLoop", "v1")
v := &View{WebView: webView}
loadChangedHandler, _ := webView.Connect("load-changed", func(_ *glib.Object, loadEvent webkit2.LoadEvent) {
switch loadEvent {
case webkit2.LoadFinished:
// If we're here, then the load must not have failed, because
// otherwise we would've disconnected this handler in the
// load-failed signal handler.
v.load <- struct{}{}
}
})
webView.Connect("load-failed", func() {
v.lastLoadErr = ErrLoadFailed
webView.HandlerDisconnect(loadChangedHandler)
})
view <- v
return false
})
return <-view
}
示例10: TestWebView_GetSnapshot
func TestWebView_GetSnapshot(t *testing.T) {
webView := NewWebView()
defer webView.Destroy()
webView.Connect("load-changed", func(_ *glib.Object, loadEvent LoadEvent) {
switch loadEvent {
case LoadFinished:
webView.GetSnapshot(func(img *image.RGBA, err error) {
if err != nil {
t.Errorf("GetSnapshot error: %q", err)
}
if img.Pix == nil {
t.Error("!img.Pix")
}
if img.Stride == 0 || img.Rect.Max.X == 0 || img.Rect.Max.Y == 0 {
t.Error("!img.Stride or !img.Rect.Max.X or !img.Rect.Max.Y")
}
gtk.MainQuit()
})
}
})
glib.IdleAdd(func() bool {
webView.LoadHTML(`<p id=foo>abc</p>`, "")
return false
})
gtk.Main()
}
示例11: TestWebView_RunJavaScript_exception
func TestWebView_RunJavaScript_exception(t *testing.T) {
webView := NewWebView()
defer webView.Destroy()
wantErr := errors.New("An exception was raised in JavaScript")
webView.Connect("load-changed", func(_ *glib.Object, loadEvent LoadEvent) {
switch loadEvent {
case LoadFinished:
webView.RunJavaScript(`throw new Error("foo")`, func(result *gojs.Value, err error) {
if result != nil {
ctx := webView.JavaScriptGlobalContext()
t.Errorf("want result == nil, got %q", ctx.ToStringOrDie(result))
}
if !reflect.DeepEqual(wantErr, err) {
t.Errorf("want error %q, got %q", wantErr, err)
}
gtk.MainQuit()
})
}
})
glib.IdleAdd(func() bool {
webView.LoadHTML(`<p></p>`, "")
return false
})
gtk.Main()
}
示例12: Title
// Title returns the title of the current resource in the view.
func (v *View) Title() string {
title := make(chan string, 1)
glib.IdleAdd(func() bool {
title <- v.WebView.Title()
return false
})
return <-title
}
示例13: URI
// URI returns the URI of the current resource in the view.
func (v *View) URI() string {
uri := make(chan string, 1)
glib.IdleAdd(func() bool {
uri <- v.WebView.URI()
return false
})
return <-uri
}
示例14: addIdle
// addIdle adds a function to call on the next gtk idle cycle, to safely use
// the dock with our goroutines.
// It will also start the callIdle flush if it's not running.
//
func addIdle(call func()) {
idleMu.Lock()
idleDraw = append(idleDraw, call)
if !idleRun {
idleRun = true
glib.IdleAdd(callIdle)
}
idleMu.Unlock()
}
示例15: init
func init() {
Write = func(text string) error {
clip, e := gtk.ClipboardGet(gdk.SELECTION_CLIPBOARD)
if e != nil {
return e
}
glib.IdleAdd(func() {
clip.SetText(text)
})
return nil
}
Read = func() (string, error) {
clip, e := gtk.ClipboardGet(gdk.SELECTION_CLIPBOARD)
if e != nil {
return "", e
}
cs := make(chan (string))
ce := make(chan (error))
defer func() {
close(cs)
close(ce)
}()
done := false
glib.IdleAdd(func() { // Synced in the GTK loop to prevent thread crashs.
str, e := clip.WaitForText()
if !done {
done = true
cs <- str
ce <- e
}
})
go func() {
<-time.After(time.Second * 3)
if !done {
done = true
cs <- ""
ce <- errors.New("clipboard read timeout")
}
}()
return <-cs, <-ce
}
}