andlabs / ui

Platform-native GUI library for Go.
Other
8.33k stars 652 forks source link

dynamically add entry #366

Closed hundredwz closed 4 years ago

hundredwz commented 4 years ago

I create a box widget and after one http resuest, I want to add one result entry to the box. But the window stuck and no operations could work.

This is the code

        var buf bytes.Buffer
        for i, v := range records {
            buf.Reset()
            buf.WriteString(strconv.Itoa(i+1) + "\t")
            buf.WriteString(v.ID + "\t")
            buf.WriteString(v.Title + "\t")
            buf.WriteString(v.Authors + "\n")
            buf.WriteString(v.Abstract + "\n\n")
            entry:=ui.NewEntry()
            entry.SetReadOnly(true)
            entry.SetText(buf.String())
            box.Append(entry,false)
        }

Could anyone help me?

andlabs commented 4 years ago

You'll need to provide a bit more context. Where is this run?

hundredwz commented 4 years ago

You'll need to provide a bit more context. Where is this run?

func (s *SingleBibLayout) search(engine lib.Lib, title string) {
    records, err := engine.Search(title)
    s.progressBar.Hide()
    if err != nil {
        entry:=ui.NewEntry()
        entry.SetText(err.Error())
        s.resultEntry.Append(entry,false)
    } else {
        var buf bytes.Buffer
        for i, v := range records {
            buf.Reset()
            buf.WriteString(strconv.Itoa(i+1) + "\t")
            buf.WriteString(v.ID + "\t")
            buf.WriteString(v.Title + "\t")
            buf.WriteString(v.Authors + "\n")
            buf.WriteString(v.Abstract + "\n\n")
            entry:=ui.NewEntry()
            entry.SetReadOnly(true)
            entry.SetText(buf.String())
            s.resultEntry.Append(entry,false)
        }
    }
}

func (s *SingleBibLayout) MakeSearchPage() ui.Control {
    hbox := ui.NewHorizontalBox()
    hbox.SetPadded(true)

    vbox := ui.NewVerticalBox()
    vbox.SetPadded(true)
    hbox.Append(vbox, false)

    // library
    libBox := ui.NewHorizontalBox()
    libBox.SetPadded(true)
    s.ieeeCK = ui.NewCheckbox("IEEE")
    s.ieeeCK.SetChecked(true) //default checked
    s.googleCK = ui.NewCheckbox("Google")
    libBox.Append(s.ieeeCK, false)
    libBox.Append(s.googleCK, false)
    vbox.Append(libBox, false)

    // paper title
    s.titleEntry = ui.NewEntry()
    vbox.Append(s.titleEntry, false)

    // search button
    s.schBtn = ui.NewButton("search")
    s.schBtn.OnClicked(func(button *ui.Button) {
        s.progressBar.Show()
        s.progressBar.SetValue(-1)
        title := s.titleEntry.Text()
        if s.ieeeCK.Checked() {
            ieeeEngine := s.engine["IEEE"]
            go s.search(ieeeEngine, title)
        }
        if s.googleCK.Checked() {
            googleEngine := s.engine["Google"]
            go s.search(googleEngine, title)
        }
    })
    vbox.Append(s.schBtn, false)
    s.progressBar = ui.NewProgressBar()
    s.progressBar.Hide()
    vbox.Append(s.progressBar, false)

    // right part
    hbox.Append(ui.NewVerticalSeparator(), false)

    s.resultEntry=ui.NewVerticalBox()
    for i := 1; i < 20; i++ {
        entry:=ui.NewEntry()
        entry.SetText("a")
        s.resultEntry.Append(entry,false)
    }
    group := ui.NewGroup("search result")
    group.SetChild(s.resultEntry)
    //s.resultEntry.SetReadOnly(true)
    hbox.Append(group, true)

    return hbox
}

the above code is how this run.

When this process is called, the ui has already been started.

I don't know if this could work and whether there are some other ways to accomplish it.

andlabs commented 4 years ago

You can't run libui functions in a separate goroutine. You'll need to use ui.QueueMain() to update the UI in search().

hundredwz commented 4 years ago

yea, now I succeed. Thank you.