本文整理汇总了Golang中github.com/conformal/gotk3/glib.IdleAdd函数的典型用法代码示例。如果您正苦于以下问题:Golang IdleAdd函数的具体用法?Golang IdleAdd怎么用?Golang IdleAdd使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了IdleAdd函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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)
}
}
示例2: 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
}
}
示例3: 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)
}
}
示例4: txSenderAndReplyListener
// txSenderAndReplyListener triggers btcgui to send btcwallet a JSON
// request to create and send a transaction. If sending the transaction
// succeeds, the recipients in the send coins notebook tab are cleared.
// If the transaction fails because the wallet is not unlocked, the
// unlock dialog is shown, and after a successful unlock, creating and
// sending the tx is tried a second time.
//
// This is written to be run as a goroutine executing outside of the GTK
// main event loop.
func txSenderAndReplyListener(sendTo map[string]float64) {
triggers.sendTx <- sendTo
err := <-triggerReplies.sendTx
// -13 is the error code for needing an unlocked wallet.
if jsonErr, ok := err.(*btcjson.Error); ok {
switch jsonErr.Code {
case -13:
// Wallet must be unlocked first. Show unlock dialog.
glib.IdleAdd(func() {
unlockSuccessful := make(chan bool)
go func() {
for {
success, ok := <-unlockSuccessful
if !ok {
// A closed channel indicates
// the dialog was cancelled.
// Abort sending the transaction.
return
}
if success {
// Try send again.
go txSenderAndReplyListener(sendTo)
return
}
}
}()
d, err := createUnlockDialog(unlockForTxSend, unlockSuccessful)
if err != nil {
// TODO(jrick): log error to file
log.Printf("[ERR] could not create unlock dialog: %v\n", err)
return
}
d.Run()
d.Destroy()
})
default:
// Generic case to display an error.
glib.IdleAdd(func() {
d := errorDialog("Unable to send transaction",
fmt.Sprintf("%s\nError code: %d", jsonErr.Message, jsonErr.Code))
d.Run()
d.Destroy()
})
}
return
}
// Send was successful, so clear recipient widgets.
glib.IdleAdd(resetRecipients)
}
示例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: updateAddresses
// updateAddresses listens for new wallet addresses, updating the GUI when
// necessary.
func updateAddresses() {
for {
addrs := <-updateChans.addrs
glib.IdleAdd(func() {
RecvCoins.Store.Clear()
})
for i := range addrs {
addr := addrs[i]
glib.IdleAdd(func() {
iter := RecvCoins.Store.Append()
RecvCoins.Store.Set(iter, []int{1},
[]interface{}{addr})
})
}
}
}
示例7: cmdGetAddressesByAccount
// cmdGetAddressesByAccount requests all addresses for an account.
//
// TODO(jrick): support non-default accounts.
// TODO(jrick): stop throwing away errors.
func cmdGetAddressesByAccount(ws *websocket.Conn) {
n := <-NewJSONID
msg, err := btcjson.CreateMessageWithId("getaddressesbyaccount", n, "")
if err != nil {
updateChans.addrs <- []string{}
}
replyHandlers.Lock()
replyHandlers.m[n] = func(result interface{}, err *btcjson.Error) {
if r, ok := result.([]interface{}); ok {
addrs := []string{}
for _, v := range r {
addrs = append(addrs, v.(string))
}
updateChans.addrs <- addrs
} else {
if err.Code == btcjson.ErrWalletInvalidAccountName.Code {
glib.IdleAdd(func() {
if dialog, err := createNewWalletDialog(); err != nil {
dialog.Run()
}
})
}
updateChans.addrs <- []string{}
}
}
replyHandlers.Unlock()
if err = websocket.Message.Send(ws, msg); err != nil {
replyHandlers.Lock()
delete(replyHandlers.m, n)
replyHandlers.Unlock()
updateChans.addrs <- []string{}
}
}
示例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: 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()
}
示例10: 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()
}
示例11: 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()
}
示例12: downloadFile
func (self *PopupMenu) downloadFile(msgId, url, ext string) {
go func() {
glib.IdleAdd(func() {
NewFileChooserWindow(self.parent, msgId, url, ext).window.ShowAll()
})
}()
}
示例13: 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")
}
}
示例14: 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
}
示例15: updateConnectionState
// updateConnectionState listens for connection status changes to btcd
// and btcwallet, updating the GUI when necessary.
func updateConnectionState() {
// Statusbar messages for various connection states.
btcdd := "Disconnected from btcd"
btcwc := "Established connection to btcwallet"
btcwd := "Disconnected from btcwallet. Attempting reconnect..."
for {
select {
case conn := <-updateChans.btcwalletConnected:
if conn {
glib.IdleAdd(func() {
//MenuBar.Settings.New.SetSensitive(true)
//MenuBar.Settings.Encrypt.SetSensitive(true)
MenuBar.Settings.TxFee.SetSensitive(true)
// Lock/Unlock sensitivity is set by wallet notification.
RecvCoins.NewAddrBtn.SetSensitive(true)
StatusElems.Lab.SetText(btcwc)
StatusElems.Pb.Hide()
})
} else {
glib.IdleAdd(func() {
//MenuBar.Settings.New.SetSensitive(false)
//MenuBar.Settings.Encrypt.SetSensitive(false)
MenuBar.Settings.Lock.SetSensitive(false)
MenuBar.Settings.Unlock.SetSensitive(false)
MenuBar.Settings.TxFee.SetSensitive(false)
SendCoins.SendBtn.SetSensitive(false)
RecvCoins.NewAddrBtn.SetSensitive(false)
StatusElems.Lab.SetText(btcwd)
StatusElems.Pb.Hide()
})
}
case conn := <-updateChans.btcdConnected:
if conn {
glib.IdleAdd(func() {
SendCoins.SendBtn.SetSensitive(true)
})
} else {
glib.IdleAdd(func() {
SendCoins.SendBtn.SetSensitive(false)
StatusElems.Lab.SetText(btcdd)
StatusElems.Pb.Hide()
})
}
}
}
}