2023-01-08 23:03:19 -07:00
|
|
|
package x
|
|
|
|
|
|
|
|
import "image"
|
|
|
|
import "github.com/jezek/xgb/xproto"
|
|
|
|
import "github.com/jezek/xgbutil/ewmh"
|
|
|
|
import "github.com/jezek/xgbutil/icccm"
|
|
|
|
import "github.com/jezek/xgbutil/xevent"
|
|
|
|
import "github.com/jezek/xgbutil/xwindow"
|
|
|
|
import "github.com/jezek/xgbutil/xgraphics"
|
2023-02-01 23:47:55 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/input"
|
2023-02-02 23:35:59 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/theme"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/config"
|
2023-02-01 23:47:55 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/canvas"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements"
|
2023-01-08 23:03:19 -07:00
|
|
|
|
|
|
|
type Window struct {
|
2023-01-09 09:31:59 -07:00
|
|
|
backend *Backend
|
|
|
|
xWindow *xwindow.Window
|
|
|
|
xCanvas *xgraphics.Image
|
2023-02-01 23:47:55 -07:00
|
|
|
canvas canvas.BasicCanvas
|
|
|
|
child elements.Element
|
2023-01-09 09:31:59 -07:00
|
|
|
onClose func ()
|
2023-01-08 23:03:19 -07:00
|
|
|
skipChildDrawCallback bool
|
|
|
|
|
2023-02-02 23:35:59 -07:00
|
|
|
theme theme.Theme
|
|
|
|
config config.Config
|
|
|
|
|
2023-01-08 23:03:19 -07:00
|
|
|
metrics struct {
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (backend *Backend) NewWindow (
|
|
|
|
width, height int,
|
|
|
|
) (
|
2023-02-01 23:47:55 -07:00
|
|
|
output elements.Window,
|
2023-01-08 23:03:19 -07:00
|
|
|
err error,
|
|
|
|
) {
|
|
|
|
if backend == nil { panic("nil backend") }
|
|
|
|
|
|
|
|
window := &Window { backend: backend }
|
|
|
|
|
|
|
|
window.xWindow, err = xwindow.Generate(backend.connection)
|
|
|
|
if err != nil { return }
|
|
|
|
window.xWindow.Create (
|
|
|
|
backend.connection.RootWin(),
|
|
|
|
0, 0, width, height, 0)
|
|
|
|
err = window.xWindow.Listen (
|
2023-01-26 00:08:07 -07:00
|
|
|
xproto.EventMaskExposure,
|
2023-01-08 23:03:19 -07:00
|
|
|
xproto.EventMaskStructureNotify,
|
|
|
|
xproto.EventMaskPointerMotion,
|
|
|
|
xproto.EventMaskKeyPress,
|
|
|
|
xproto.EventMaskKeyRelease,
|
|
|
|
xproto.EventMaskButtonPress,
|
|
|
|
xproto.EventMaskButtonRelease)
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
window.xWindow.WMGracefulClose (func (xWindow *xwindow.Window) {
|
|
|
|
window.Close()
|
|
|
|
})
|
2023-01-26 00:08:07 -07:00
|
|
|
|
|
|
|
xevent.ExposeFun(window.handleExpose).
|
|
|
|
Connect(backend.connection, window.xWindow.Id)
|
2023-01-08 23:03:19 -07:00
|
|
|
xevent.ConfigureNotifyFun(window.handleConfigureNotify).
|
|
|
|
Connect(backend.connection, window.xWindow.Id)
|
|
|
|
xevent.KeyPressFun(window.handleKeyPress).
|
|
|
|
Connect(backend.connection, window.xWindow.Id)
|
|
|
|
xevent.KeyReleaseFun(window.handleKeyRelease).
|
|
|
|
Connect(backend.connection, window.xWindow.Id)
|
|
|
|
xevent.ButtonPressFun(window.handleButtonPress).
|
|
|
|
Connect(backend.connection, window.xWindow.Id)
|
|
|
|
xevent.ButtonReleaseFun(window.handleButtonRelease).
|
|
|
|
Connect(backend.connection, window.xWindow.Id)
|
|
|
|
xevent.MotionNotifyFun(window.handleMotionNotify).
|
|
|
|
Connect(backend.connection, window.xWindow.Id)
|
2023-02-02 23:35:59 -07:00
|
|
|
|
|
|
|
window.SetTheme(backend.theme)
|
|
|
|
window.SetConfig(backend.config)
|
2023-01-08 23:03:19 -07:00
|
|
|
|
|
|
|
window.metrics.width = width
|
|
|
|
window.metrics.height = height
|
|
|
|
window.childMinimumSizeChangeCallback(8, 8)
|
|
|
|
|
|
|
|
window.reallocateCanvas()
|
|
|
|
|
|
|
|
backend.windows[window.xWindow.Id] = window
|
|
|
|
output = window
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-01 23:47:55 -07:00
|
|
|
func (window *Window) Adopt (child elements.Element) {
|
2023-01-19 13:02:56 -07:00
|
|
|
// disown previous child
|
2023-01-08 23:03:19 -07:00
|
|
|
if window.child != nil {
|
2023-01-19 13:02:56 -07:00
|
|
|
window.child.OnDamage(nil)
|
|
|
|
window.child.OnMinimumSizeChange(nil)
|
|
|
|
}
|
2023-02-01 23:47:55 -07:00
|
|
|
if previousChild, ok := window.child.(elements.Flexible); ok {
|
2023-01-19 13:02:56 -07:00
|
|
|
previousChild.OnFlexibleHeightChange(nil)
|
|
|
|
}
|
2023-02-01 23:47:55 -07:00
|
|
|
if previousChild, ok := window.child.(elements.Focusable); ok {
|
2023-01-30 15:01:47 -07:00
|
|
|
previousChild.OnFocusRequest(nil)
|
|
|
|
previousChild.OnFocusMotionRequest(nil)
|
|
|
|
if previousChild.Focused() {
|
|
|
|
previousChild.HandleUnfocus()
|
2023-01-15 10:23:13 -07:00
|
|
|
}
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
2023-01-19 13:02:56 -07:00
|
|
|
|
|
|
|
// adopt new child
|
2023-01-08 23:03:19 -07:00
|
|
|
window.child = child
|
2023-02-02 23:35:59 -07:00
|
|
|
if newChild, ok := child.(elements.Themeable); ok {
|
|
|
|
newChild.SetTheme(window.theme)
|
|
|
|
}
|
|
|
|
if newChild, ok := child.(elements.Configurable); ok {
|
|
|
|
newChild.SetConfig(window.config)
|
|
|
|
}
|
2023-02-01 23:47:55 -07:00
|
|
|
if newChild, ok := child.(elements.Flexible); ok {
|
2023-01-19 13:02:56 -07:00
|
|
|
newChild.OnFlexibleHeightChange(window.resizeChildToFit)
|
|
|
|
}
|
2023-02-01 23:47:55 -07:00
|
|
|
if newChild, ok := child.(elements.Focusable); ok {
|
2023-01-30 15:01:47 -07:00
|
|
|
newChild.OnFocusRequest(window.childSelectionRequestCallback)
|
2023-01-19 13:02:56 -07:00
|
|
|
}
|
2023-01-08 23:03:19 -07:00
|
|
|
if child != nil {
|
2023-01-19 13:02:56 -07:00
|
|
|
child.OnDamage(window.childDrawCallback)
|
|
|
|
child.OnMinimumSizeChange (func () {
|
|
|
|
window.childMinimumSizeChangeCallback (
|
|
|
|
child.MinimumSize())
|
2023-01-09 09:21:23 -07:00
|
|
|
})
|
2023-02-08 23:30:14 -07:00
|
|
|
if !window.childMinimumSizeChangeCallback(child.MinimumSize()) {
|
|
|
|
window.resizeChildToFit()
|
|
|
|
window.redrawChildEntirely()
|
|
|
|
}
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 23:47:55 -07:00
|
|
|
func (window *Window) Child () (child elements.Element) {
|
2023-01-08 23:03:19 -07:00
|
|
|
child = window.child
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (window *Window) SetTitle (title string) {
|
|
|
|
ewmh.WmNameSet (
|
|
|
|
window.backend.connection,
|
|
|
|
window.xWindow.Id,
|
|
|
|
title)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (window *Window) SetIcon (sizes []image.Image) {
|
|
|
|
wmIcons := []ewmh.WmIcon { }
|
|
|
|
|
|
|
|
for _, icon := range sizes {
|
|
|
|
width := icon.Bounds().Max.X
|
|
|
|
height := icon.Bounds().Max.Y
|
|
|
|
wmIcon := ewmh.WmIcon {
|
|
|
|
Width: uint(width),
|
|
|
|
Height: uint(height),
|
|
|
|
Data: make ([]uint, width * height),
|
|
|
|
}
|
|
|
|
|
|
|
|
// manually convert image data beacuse of course we have to do
|
|
|
|
// this
|
|
|
|
index := 0
|
|
|
|
for y := 0; y < height; y ++ {
|
|
|
|
for x := 0; x < width; x ++ {
|
|
|
|
r, g, b, a := icon.At(x, y).RGBA()
|
|
|
|
r >>= 8
|
|
|
|
g >>= 8
|
|
|
|
b >>= 8
|
|
|
|
a >>= 8
|
|
|
|
wmIcon.Data[index] =
|
|
|
|
(uint(a) << 24) |
|
|
|
|
(uint(r) << 16) |
|
|
|
|
(uint(g) << 8) |
|
|
|
|
(uint(b) << 0)
|
|
|
|
index ++
|
|
|
|
}}
|
|
|
|
|
|
|
|
wmIcons = append(wmIcons, wmIcon)
|
|
|
|
}
|
|
|
|
|
|
|
|
ewmh.WmIconSet (
|
|
|
|
window.backend.connection,
|
|
|
|
window.xWindow.Id,
|
|
|
|
wmIcons)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (window *Window) Show () {
|
|
|
|
if window.child == nil {
|
|
|
|
window.xCanvas.For (func (x, y int) xgraphics.BGRA {
|
|
|
|
return xgraphics.BGRA { }
|
|
|
|
})
|
|
|
|
|
|
|
|
window.pushRegion(window.xCanvas.Bounds())
|
|
|
|
}
|
|
|
|
|
|
|
|
window.xWindow.Map()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (window *Window) Hide () {
|
|
|
|
window.xWindow.Unmap()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (window *Window) Close () {
|
|
|
|
delete(window.backend.windows, window.xWindow.Id)
|
|
|
|
if window.onClose != nil { window.onClose() }
|
|
|
|
xevent.Detach(window.xWindow.X, window.xWindow.Id)
|
|
|
|
window.xWindow.Destroy()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (window *Window) OnClose (callback func ()) {
|
|
|
|
window.onClose = callback
|
|
|
|
}
|
|
|
|
|
2023-02-02 23:35:59 -07:00
|
|
|
func (window *Window) SetTheme (theme theme.Theme) {
|
|
|
|
window.theme = theme
|
|
|
|
if child, ok := window.child.(elements.Themeable); ok {
|
|
|
|
child.SetTheme(theme)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (window *Window) SetConfig (config config.Config) {
|
|
|
|
window.config = config
|
|
|
|
if child, ok := window.child.(elements.Configurable); ok {
|
|
|
|
child.SetConfig(config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-08 23:03:19 -07:00
|
|
|
func (window *Window) reallocateCanvas () {
|
2023-01-31 23:47:08 -07:00
|
|
|
window.canvas.Reallocate(window.metrics.width, window.metrics.height)
|
|
|
|
|
|
|
|
previousWidth, previousHeight := 0, 0
|
2023-01-08 23:03:19 -07:00
|
|
|
if window.xCanvas != nil {
|
2023-01-31 23:47:08 -07:00
|
|
|
previousWidth = window.xCanvas.Bounds().Dx()
|
|
|
|
previousHeight = window.xCanvas.Bounds().Dy()
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
2023-01-31 23:47:08 -07:00
|
|
|
|
|
|
|
newWidth := window.metrics.width
|
|
|
|
newHeight := window.metrics.height
|
|
|
|
larger := newWidth > previousWidth || newHeight > previousHeight
|
|
|
|
smaller := newWidth < previousWidth / 2 || newHeight < previousHeight / 2
|
|
|
|
if larger || smaller {
|
|
|
|
if window.xCanvas != nil {
|
|
|
|
window.xCanvas.Destroy()
|
|
|
|
}
|
|
|
|
window.xCanvas = xgraphics.New (
|
|
|
|
window.backend.connection,
|
|
|
|
image.Rect (
|
|
|
|
0, 0,
|
|
|
|
(newWidth / 64) * 64 + 64,
|
|
|
|
(newHeight / 64) * 64 + 64))
|
|
|
|
window.xCanvas.CreatePixmap()
|
|
|
|
}
|
|
|
|
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (window *Window) redrawChildEntirely () {
|
2023-02-12 23:49:33 -07:00
|
|
|
window.pushRegion(window.paste(window.canvas))
|
2023-01-31 14:13:20 -07:00
|
|
|
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (window *Window) resizeChildToFit () {
|
|
|
|
window.skipChildDrawCallback = true
|
2023-02-01 23:47:55 -07:00
|
|
|
if child, ok := window.child.(elements.Flexible); ok {
|
2023-01-19 13:02:56 -07:00
|
|
|
minimumHeight := child.FlexibleHeightFor(window.metrics.width)
|
2023-01-17 12:17:26 -07:00
|
|
|
minimumWidth, _ := child.MinimumSize()
|
2023-01-16 16:04:41 -07:00
|
|
|
|
|
|
|
icccm.WmNormalHintsSet (
|
|
|
|
window.backend.connection,
|
|
|
|
window.xWindow.Id,
|
|
|
|
&icccm.NormalHints {
|
|
|
|
Flags: icccm.SizeHintPMinSize,
|
|
|
|
MinWidth: uint(minimumWidth),
|
|
|
|
MinHeight: uint(minimumHeight),
|
|
|
|
})
|
|
|
|
|
|
|
|
if window.metrics.height >= minimumHeight &&
|
|
|
|
window.metrics.width >= minimumWidth {
|
2023-01-31 14:13:20 -07:00
|
|
|
window.child.DrawTo(window.canvas)
|
2023-01-16 16:04:41 -07:00
|
|
|
}
|
2023-01-15 10:23:13 -07:00
|
|
|
} else {
|
2023-01-31 14:13:20 -07:00
|
|
|
window.child.DrawTo(window.canvas)
|
2023-01-15 10:23:13 -07:00
|
|
|
}
|
2023-01-08 23:03:19 -07:00
|
|
|
window.skipChildDrawCallback = false
|
|
|
|
}
|
|
|
|
|
2023-02-01 23:47:55 -07:00
|
|
|
func (window *Window) childDrawCallback (region canvas.Canvas) {
|
2023-01-08 23:03:19 -07:00
|
|
|
if window.skipChildDrawCallback { return }
|
2023-01-26 16:01:39 -07:00
|
|
|
window.pushRegion(window.paste(region))
|
|
|
|
}
|
2023-01-08 23:03:19 -07:00
|
|
|
|
2023-02-01 23:47:55 -07:00
|
|
|
func (window *Window) paste (canvas canvas.Canvas) (updatedRegion image.Rectangle) {
|
2023-01-26 16:01:39 -07:00
|
|
|
data, stride := canvas.Buffer()
|
|
|
|
bounds := canvas.Bounds().Intersect(window.xCanvas.Bounds())
|
2023-01-08 23:03:19 -07:00
|
|
|
for x := bounds.Min.X; x < bounds.Max.X; x ++ {
|
|
|
|
for y := bounds.Min.Y; y < bounds.Max.Y; y ++ {
|
2023-01-13 23:54:57 -07:00
|
|
|
rgba := data[x + y * stride]
|
2023-01-26 16:01:39 -07:00
|
|
|
index := x * 4 + y * window.xCanvas.Stride
|
|
|
|
window.xCanvas.Pix[index + 0] = rgba.B
|
|
|
|
window.xCanvas.Pix[index + 1] = rgba.G
|
|
|
|
window.xCanvas.Pix[index + 2] = rgba.R
|
|
|
|
window.xCanvas.Pix[index + 3] = rgba.A
|
2023-01-08 23:03:19 -07:00
|
|
|
}}
|
|
|
|
|
2023-01-26 16:01:39 -07:00
|
|
|
return bounds
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
|
2023-02-08 23:30:14 -07:00
|
|
|
func (window *Window) childMinimumSizeChangeCallback (width, height int) (resized bool) {
|
2023-01-08 23:03:19 -07:00
|
|
|
icccm.WmNormalHintsSet (
|
|
|
|
window.backend.connection,
|
|
|
|
window.xWindow.Id,
|
|
|
|
&icccm.NormalHints {
|
|
|
|
Flags: icccm.SizeHintPMinSize,
|
|
|
|
MinWidth: uint(width),
|
|
|
|
MinHeight: uint(height),
|
|
|
|
})
|
|
|
|
newWidth := window.metrics.width
|
|
|
|
newHeight := window.metrics.height
|
|
|
|
if newWidth < width { newWidth = width }
|
|
|
|
if newHeight < height { newHeight = height }
|
|
|
|
if newWidth != window.metrics.width ||
|
|
|
|
newHeight != window.metrics.height {
|
|
|
|
window.xWindow.Resize(newWidth, newHeight)
|
2023-02-08 23:30:14 -07:00
|
|
|
return true
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
2023-02-08 23:30:14 -07:00
|
|
|
|
|
|
|
return false
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
|
2023-01-11 20:30:14 -07:00
|
|
|
func (window *Window) childSelectionRequestCallback () (granted bool) {
|
2023-02-08 23:30:14 -07:00
|
|
|
if _, ok := window.child.(elements.Focusable); ok {
|
|
|
|
return true
|
2023-01-15 10:23:13 -07:00
|
|
|
}
|
2023-02-08 23:30:14 -07:00
|
|
|
return false
|
2023-01-09 13:14:36 -07:00
|
|
|
}
|
|
|
|
|
2023-01-16 11:49:32 -07:00
|
|
|
func (window *Window) childSelectionMotionRequestCallback (
|
2023-02-01 23:47:55 -07:00
|
|
|
direction input.KeynavDirection,
|
2023-01-16 11:49:32 -07:00
|
|
|
) (
|
|
|
|
granted bool,
|
|
|
|
) {
|
2023-02-01 23:47:55 -07:00
|
|
|
if child, ok := window.child.(elements.Focusable); ok {
|
2023-01-30 15:01:47 -07:00
|
|
|
if !child.HandleFocus(direction) {
|
|
|
|
child.HandleUnfocus()
|
2023-01-16 11:49:32 -07:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-01-08 23:03:19 -07:00
|
|
|
func (window *Window) pushRegion (region image.Rectangle) {
|
|
|
|
if window.xCanvas == nil { panic("whoopsie!!!!!!!!!!!!!!") }
|
|
|
|
image, ok := window.xCanvas.SubImage(region).(*xgraphics.Image)
|
|
|
|
if ok {
|
|
|
|
image.XDraw()
|
2023-01-26 00:08:07 -07:00
|
|
|
image.XExpPaint (
|
|
|
|
window.xWindow.Id,
|
|
|
|
image.Bounds().Min.X,
|
|
|
|
image.Bounds().Min.Y)
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
}
|