2020-06-04 23:00:41 +00:00
|
|
|
package autoscroll
|
|
|
|
|
|
|
|
import "github.com/gotk3/gotk3/gtk"
|
|
|
|
|
|
|
|
type ScrolledWindow struct {
|
|
|
|
gtk.ScrolledWindow
|
|
|
|
vadj gtk.Adjustment
|
2020-06-06 07:44:36 +00:00
|
|
|
Bottomed bool // :floshed:
|
2020-06-04 23:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewScrolledWindow() *ScrolledWindow {
|
|
|
|
gtksw, _ := gtk.ScrolledWindowNew(nil, nil)
|
|
|
|
gtksw.SetProperty("propagate-natural-height", true)
|
|
|
|
|
|
|
|
sw := &ScrolledWindow{*gtksw, *gtksw.GetVAdjustment(), true} // bottomed by default
|
|
|
|
sw.Connect("size-allocate", func(_ *gtk.ScrolledWindow) {
|
|
|
|
// We can't really trust Gtk to be competent.
|
2020-06-06 07:44:36 +00:00
|
|
|
if sw.Bottomed {
|
2020-06-04 23:00:41 +00:00
|
|
|
sw.ScrollToBottom()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
sw.vadj.Connect("value-changed", func(adj *gtk.Adjustment) {
|
|
|
|
// Manually check if we're anchored on scroll.
|
2020-06-06 07:44:36 +00:00
|
|
|
sw.Bottomed = (adj.GetUpper() - adj.GetPageSize()) <= adj.GetValue()
|
2020-06-04 23:00:41 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return sw
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetVAdjustment overrides gtk.ScrolledWindow's.
|
|
|
|
func (s *ScrolledWindow) GetVAdjustment() *gtk.Adjustment {
|
|
|
|
return &s.vadj
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ScrolledWindow) ScrollToBottom() {
|
|
|
|
s.vadj.SetValue(s.vadj.GetUpper())
|
|
|
|
}
|