From e0fddf6b59778d7dcd53e9c71d19f198a2d0496f Mon Sep 17 00:00:00 2001 From: ShuheiKubota Date: Sun, 10 Nov 2019 12:56:05 +0900 Subject: [PATCH] refactor: divide the code into files --- cmd_alpha.go | 24 +++++++++ cmd_minmax.go | 25 ++++++++++ cmd_resize.go | 72 +++++++++++++++++++++++++++ cmd_topmost.go | 21 ++++++++ vvin.go | 129 ------------------------------------------------- 5 files changed, 142 insertions(+), 129 deletions(-) create mode 100644 cmd_alpha.go create mode 100644 cmd_minmax.go create mode 100644 cmd_resize.go create mode 100644 cmd_topmost.go diff --git a/cmd_alpha.go b/cmd_alpha.go new file mode 100644 index 0000000..f22c363 --- /dev/null +++ b/cmd_alpha.go @@ -0,0 +1,24 @@ +package main + +import "errors" + +type alphaCmd struct { +} + +func (c alphaCmd) Run(args []string, g globalCmd) error { + if len(args) != 1 { + return errors.New("an argument is required") + } + + alpha := toInt(args[0], 255) + + style, _, _ := getWindowLong.Call(uintptr(g.targetHandle), GWL_EXSTYLE) + setWindowLong.Call(uintptr(g.targetHandle), GWL_EXSTYLE, style|WS_EX_LAYERED) + + setLayeredWindowAttributes.Call(uintptr(g.targetHandle), 0, uintptr(alpha), LWA_ALPHA) + if alpha == 255 { + setWindowLong.Call(uintptr(g.targetHandle), GWL_EXSTYLE, style&^WS_EX_LAYERED) + } + + return nil +} diff --git a/cmd_minmax.go b/cmd_minmax.go new file mode 100644 index 0000000..bd7a916 --- /dev/null +++ b/cmd_minmax.go @@ -0,0 +1,25 @@ +package main + +type minCmd struct { + Restore bool `cli:"restore,r"` +} + +func (c minCmd) Run(g globalCmd) { + if c.Restore { + showWindow.Call(uintptr(g.targetHandle), SW_RESTORE) + } else { + showWindow.Call(uintptr(g.targetHandle), SW_MINIMIZE) + } +} + +type maxCmd struct { + Restore bool `cli:"restore,r"` +} + +func (c maxCmd) Run(g globalCmd) { + if c.Restore { + showWindow.Call(uintptr(g.targetHandle), SW_RESTORE) + } else { + showWindow.Call(uintptr(g.targetHandle), SW_MAXIMIZE) + } +} diff --git a/cmd_resize.go b/cmd_resize.go new file mode 100644 index 0000000..38086e0 --- /dev/null +++ b/cmd_resize.go @@ -0,0 +1,72 @@ +package main + +import ( + "errors" + "unsafe" + + "github.com/shu-go/rog" +) + +type resizeCmd struct { + Left string `cli:"left,x"` + Top string `cli:"top,y"` + Width string `cli:"width,w"` + Height string `cli:"height,h"` + + NoRestorable bool `cli:"norestorable"` + + rect RECT +} + +func (c *resizeCmd) Before(g globalCmd) error { + if c.Left == "" && c.Top == "" && c.Width == "" && c.Height == "" { + return errors.New("no options") + } + + getWindowRect.Call(uintptr(g.targetHandle), uintptr(unsafe.Pointer(&c.rect))) + + oldrect := c.rect + + if g.Debug { + rog.Print(oldrect) + } + 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 g.Debug { + rog.Print(c.rect) + } + + return nil +} + +func (c resizeCmd) Run(g globalCmd) { + if !c.NoRestorable { + showWindow.Call(uintptr(g.targetHandle), SW_HIDE) + showWindow.Call(uintptr(g.targetHandle), SW_MAXIMIZE) + } + 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), + SWP_NOACTIVATE|SWP_NOZORDER) + if !c.NoRestorable { + showWindow.Call(uintptr(g.targetHandle), SW_SHOWNA) + } +} diff --git a/cmd_topmost.go b/cmd_topmost.go new file mode 100644 index 0000000..0d715f3 --- /dev/null +++ b/cmd_topmost.go @@ -0,0 +1,21 @@ +package main + +type topmostCmd struct { + Restore bool `cli:"restore,r"` +} + +func (c topmostCmd) Run(g globalCmd) { + hwndInsertAfter := HWND_TOPMOST + if c.Restore { + hwndInsertAfter = HWND_NOTOPMOST + } + + setWindowPos.Call( + uintptr(g.targetHandle), + hwndInsertAfter, + 0, + 0, + 0, + 0, + SWP_NOSIZE|SWP_NOMOVE) +} diff --git a/vvin.go b/vvin.go index 94f5801..993de5b 100644 --- a/vvin.go +++ b/vvin.go @@ -87,135 +87,6 @@ func (c *globalCmd) Before() error { return nil } -type minCmd struct { - Restore bool `cli:"restore,r"` -} - -func (c minCmd) Run(g globalCmd) { - if c.Restore { - showWindow.Call(uintptr(g.targetHandle), SW_RESTORE) - } else { - showWindow.Call(uintptr(g.targetHandle), SW_MINIMIZE) - } -} - -type maxCmd struct { - Restore bool `cli:"restore,r"` -} - -func (c maxCmd) Run(g globalCmd) { - if c.Restore { - showWindow.Call(uintptr(g.targetHandle), SW_RESTORE) - } else { - showWindow.Call(uintptr(g.targetHandle), SW_MAXIMIZE) - } -} - -type resizeCmd struct { - Left string `cli:"left,x"` - Top string `cli:"top,y"` - Width string `cli:"width,w"` - Height string `cli:"height,h"` - - NoRestorable bool `cli:"norestorable"` - - rect RECT -} - -func (c *resizeCmd) Before(g globalCmd) error { - if c.Left == "" && c.Top == "" && c.Width == "" && c.Height == "" { - return errors.New("no options") - } - - getWindowRect.Call(uintptr(g.targetHandle), uintptr(unsafe.Pointer(&c.rect))) - - oldrect := c.rect - - if g.Debug { - rog.Print(oldrect) - } - 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 g.Debug { - rog.Print(c.rect) - } - - return nil -} - -func (c resizeCmd) Run(g globalCmd) { - if !c.NoRestorable { - showWindow.Call(uintptr(g.targetHandle), SW_HIDE) - showWindow.Call(uintptr(g.targetHandle), SW_MAXIMIZE) - } - 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), - SWP_NOACTIVATE|SWP_NOZORDER) - if !c.NoRestorable { - showWindow.Call(uintptr(g.targetHandle), SW_SHOWNA) - } -} - -type alphaCmd struct { -} - -func (c alphaCmd) Run(args []string, g globalCmd) error { - if len(args) != 1 { - return errors.New("an argument is required") - } - - alpha := toInt(args[0], 255) - - style, _, _ := getWindowLong.Call(uintptr(g.targetHandle), GWL_EXSTYLE) - setWindowLong.Call(uintptr(g.targetHandle), GWL_EXSTYLE, style|WS_EX_LAYERED) - - setLayeredWindowAttributes.Call(uintptr(g.targetHandle), 0, uintptr(alpha), LWA_ALPHA) - if alpha == 255 { - setWindowLong.Call(uintptr(g.targetHandle), GWL_EXSTYLE, style&^WS_EX_LAYERED) - } - - return nil -} - -type topmostCmd struct { - Restore bool `cli:"restore,r"` -} - -func (c topmostCmd) Run(g globalCmd) { - hwndInsertAfter := HWND_TOPMOST - if c.Restore { - hwndInsertAfter = HWND_NOTOPMOST - } - - setWindowPos.Call( - uintptr(g.targetHandle), - hwndInsertAfter, - 0, - 0, - 0, - 0, - SWP_NOSIZE|SWP_NOMOVE) -} - func main() { app := gli.NewWith(&globalCmd{}) app.Name = "vvin"