1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-gtk.git synced 2024-11-01 03:54:16 +00:00
cchat-gtk/internal/ui/primitives/autoscroll/autoscroll.go
2020-08-20 16:53:13 -07:00

39 lines
1,021 B
Go

package autoscroll
import "github.com/gotk3/gotk3/gtk"
type ScrolledWindow struct {
gtk.ScrolledWindow
vadj gtk.Adjustment
Bottomed bool // :floshed:
}
func NewScrolledWindow() *ScrolledWindow {
gtksw, _ := gtk.ScrolledWindowNew(nil, nil)
gtksw.SetProperty("propagate-natural-height", true)
gtksw.SetProperty("window-placement", gtk.CORNER_BOTTOM_LEFT)
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.
if sw.Bottomed {
sw.ScrollToBottom()
}
})
sw.vadj.Connect("value-changed", func(adj *gtk.Adjustment) {
// Manually check if we're anchored on scroll.
sw.Bottomed = (adj.GetUpper() - adj.GetPageSize()) <= adj.GetValue()
})
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())
}