vvin/cmd_resize.go

89 lines
1.8 KiB
Go
Raw Permalink Normal View History

2023-10-17 00:00:50 -04:00
//go:build windows
// +build windows
2023-10-16 09:51:11 -04:00
package vvin
2019-11-09 22:56:05 -05:00
import (
"errors"
"os"
2019-11-09 22:56:05 -05:00
"unsafe"
"github.com/shu-go/nmfmt"
2019-11-09 22:56:05 -05:00
)
type resizeCmd struct {
Left string `cli:"left,x"`
Top string `cli:"top,y"`
Width string `cli:"width,w"`
Height string `cli:"height,h"`
2019-11-17 23:45:45 -05:00
Restore bool `cli:"restore,r"`
2019-11-09 22:56:05 -05:00
NoRestorable bool `cli:"norestorable"`
2019-11-18 04:42:38 -05:00
rect rect
2019-11-09 22:56:05 -05:00
}
func (c *resizeCmd) Before(g globalCmd) error {
2019-11-17 23:45:45 -05:00
if c.Left == "" && c.Top == "" && c.Width == "" && c.Height == "" && !c.Restore {
2019-11-09 22:56:05 -05:00
return errors.New("no options")
}
getWindowRect.Call(uintptr(g.targetHandle), uintptr(unsafe.Pointer(&c.rect)))
oldrect := c.rect
g.debug(os.Stderr, "$=original\n",
nmfmt.M{
"original": oldrect,
})
2019-11-09 22:56:05 -05:00
if c.Left != "" {
c.rect.Left = toInt(c.Left, g.scrWidth)
}
if c.Top != "" {
c.rect.Top = toInt(c.Top, g.scrHeight)
}
if c.Width != "" {
c.rect.Right = c.rect.Left + toInt(c.Width, g.scrWidth)
} else {
c.rect.Right = c.rect.Left + (oldrect.Right - oldrect.Left)
}
if c.Height != "" {
c.rect.Bottom = c.rect.Top + toInt(c.Height, g.scrHeight)
} else {
c.rect.Bottom = c.rect.Top + (oldrect.Bottom - oldrect.Top)
}
if c.Restore {
g.debug(os.Stderr, "restore\n")
} else {
g.debug(os.Stderr, "resized $rect\n", nmfmt.M{"rect": c.rect})
2019-11-09 22:56:05 -05:00
}
return nil
}
func (c resizeCmd) Run(g globalCmd) {
2019-11-17 23:45:45 -05:00
if c.Restore {
2019-11-18 04:42:38 -05:00
showWindow.Call(uintptr(g.targetHandle), swRestore)
2019-11-17 23:45:45 -05:00
return
}
2019-11-09 22:56:05 -05:00
if !c.NoRestorable {
2019-11-18 04:42:38 -05:00
showWindow.Call(uintptr(g.targetHandle), swHide)
showWindow.Call(uintptr(g.targetHandle), swMaximize)
2019-11-09 22:56:05 -05:00
}
setWindowPos.Call(
uintptr(g.targetHandle),
0,
uintptr(c.rect.Left),
uintptr(c.rect.Top),
uintptr(c.rect.Right-c.rect.Left),
uintptr(c.rect.Bottom-c.rect.Top),
2019-11-18 04:42:38 -05:00
swpNoActivate|swpNoZOrder)
2019-11-09 22:56:05 -05:00
if !c.NoRestorable {
2019-11-18 04:42:38 -05:00
showWindow.Call(uintptr(g.targetHandle), swShowNA)
2019-11-09 22:56:05 -05:00
}
}