lxn / walk

A Windows GUI toolkit for the Go Programming Language
Other
6.79k stars 885 forks source link

How does a scrollview get the scrollbar to automatically scroll to the bottom #689

Closed seth-shi closed 2 years ago

seth-shi commented 4 years ago

hi, I'm working on a gadget with walk, and I'm having a little trouble with it 20200527180452

My code is as follows


MainWindow{
        AssignTo: &App.MainWindow,
        Title:    App.I18n.Tr("app_name"),
        Icon:     "assets/icons/app.png",
        Size:     Size{Width: APP_WIDTH, Height: 300},
        Layout:   VBox{},
        MenuItems: []MenuItem{
            Menu{
                Text: App.I18n.Tr("file"),
                Items: []MenuItem{
                    Action{
                        Text:     App.I18n.Tr("clear logs"),
                        Shortcut: Shortcut{walk.ModControl, walk.KeyC},
                        OnTriggered: func() {

                            App.MainWindow.Synchronize(func() {

                                err := App.LogView.Children().Clear()
                                fmt.Println(err)
                            })
                        },
                    },
                    Separator{},
                    Action{
                        Text:        App.I18n.Tr("exit"),
                        OnTriggered: func() {

                            App.MainWindow.Close()
                        },
                    },
                },
            },
            Menu{
                Text:  App.I18n.Tr("language"),
                Items: buildLangMenu(),
            },
            Action{
                Text: App.I18n.Tr("help"),
                OnTriggered: func() {

                    err := exec.Command(`cmd`, `/c`, `start`, `https://github.com/seth-shi`).Start()

                    if err != nil {

                        // TODO
                        dialog, err := walk.NewDialog(App.MainWindow)
                        if err != nil {
                            panic(err)
                        }
                        dialog.SetTitle(`https://github.com/seth-shi`)
                        dialog.Show()
                    }
                },
            },
        },
        Children: []Widget{
            Label{
                AssignTo: &pathLabel,
                Text:     App.Config.Data.Path,
            },
            PushButton{
                Text: App.I18n.Tr("select sync path"),
                OnClicked: func() {

                    dlg := new(walk.FileDialog)
                    dlg.Title = App.I18n.Tr("select sync path")

                    if ok, err := dlg.ShowBrowseFolder(App.MainWindow); err != nil {
                        fmt.Println(err)
                        return
                    } else if !ok {
                        fmt.Println("no ok")
                        return
                    }

                    // 存储到环境目录
                    App.Config.Data.Path = dlg.FilePath
                    if nil == App.Config.Save() {
                        _ = pathLabel.SetText(App.Config.Data.Path)
                    }
                },
            },
            ScrollView{
                AssignTo: &App.LogView,
                HorizontalFixed: true,
                Alignment: AlignHNearVNear,
                Layout:          VBox{MarginsZero: true},
                Children: []Widget{},
            },
        },
    }.Run()

logTicker := time.NewTicker(time.Millisecond * 500)
    i := 0
    go func() {

        for _ = range logTicker.C {

            if App.MainWindow == nil {
                continue
            }

            App.MainWindow.Synchronize(func() {

                info := models.InfoLog("write info log")

                i ++
                if i%3==0 {
                    label := Label{
        MinSize:Size{enums.APP_SIZE - (9*6), 0},
        Alignment: AlignHNearVNear,
        TextColor: color,
        Background: labelBgColor,
        Text: strings.Join(logs, "  "),
    }

    label.Create(NewBuilder(App.LogView))

                } else {
                    label := Label{
        MinSize:Size{enums.APP_SIZE - (9*6), 0},
        Alignment: AlignHNearVNear,
        TextColor: color,
        Background: labelBgColor,
        Text: strings.Join(logs, "  "),
    }

    label.Create(NewBuilder(App.LogView))
                }
            })
        }
    }()

the Scrollview The contents of the list are written through a timer.

questions:

  1. Is my dynamic addition of label the correct operation?
  2. How to automatically scroll down when a scrollview appears, or add a label to the header.