diff --git a/app/os.go b/app/os.go index a6133e1c..012153c0 100644 --- a/app/os.go +++ b/app/os.go @@ -41,8 +41,6 @@ type Config struct { // CustomRenderer is true when the window content is rendered by the // client. CustomRenderer bool - // center is a flag used to center the window. Set by option. - center bool // Decorated reports whether window decorations are provided automatically. Decorated bool } diff --git a/app/os_macos.go b/app/os_macos.go index e957c789..5e6d7db6 100644 --- a/app/os_macos.go +++ b/app/os_macos.go @@ -344,13 +344,6 @@ func (w *window) Configure(options []Option) { cnf.MaxSize = cnf.MaxSize.Div(int(screenScale)) C.setMaxSize(w.window, C.CGFloat(cnf.MaxSize.X), C.CGFloat(cnf.MaxSize.Y)) } - if cnf.center { - r := C.getScreenFrame(w.window) // the screen size of the window - sz := w.config.Size - x := (int(r.size.width) - sz.X) / 2 - y := (int(r.size.height) - sz.Y) / 2 - C.setScreenFrame(w.window, C.CGFloat(x), C.CGFloat(y), C.CGFloat(sz.X), C.CGFloat(sz.Y)) - } } if cnf.Decorated != prev.Decorated { w.config.Decorated = cnf.Decorated @@ -372,6 +365,12 @@ func (w *window) setTitle(prev, cnf Config) { func (w *window) Perform(acts system.Action) { walkActions(acts, func(a system.Action) { switch a { + case system.ActionCenter: + r := C.getScreenFrame(w.window) // the screen size of the window + sz := w.config.Size + x := (int(r.size.width) - sz.X) / 2 + y := (int(r.size.height) - sz.Y) / 2 + C.setScreenFrame(w.window, C.CGFloat(x), C.CGFloat(y), C.CGFloat(sz.X), C.CGFloat(sz.Y)) case system.ActionRaise: C.raiseWindow(w.window) } diff --git a/app/os_windows.go b/app/os_windows.go index de22b201..704aa6b8 100644 --- a/app/os_windows.go +++ b/app/os_windows.go @@ -631,14 +631,6 @@ func (w *window) Configure(options []Option) { y := wr.Top dx := r.Right - r.Left dy := r.Bottom - r.Top - if w.config.center { - // Calculate center position on current monitor. - mi := windows.GetMonitorInfo(w.hwnd).Monitor - x = (mi.Right - mi.Left - dx) / 2 - y = (mi.Bottom - mi.Top - dy) / 2 - // Centering is done only once. - w.config.center = false - } windows.SetWindowPos(w.hwnd, 0, x, y, dx, dy, windows.SWP_NOOWNERZORDER|windows.SWP_FRAMECHANGED) windows.ShowWindow(w.hwnd, windows.SW_SHOWNORMAL) @@ -768,6 +760,18 @@ func (w *window) Close() { func (w *window) Perform(acts system.Action) { walkActions(acts, func(a system.Action) { switch a { + case system.ActionCenter: + if w.config.Mode != Windowed { + break + } + r := windows.GetWindowRect(w.hwnd) + dx := r.Right - r.Left + dy := r.Bottom - r.Top + // Calculate center position on current monitor. + mi := windows.GetMonitorInfo(w.hwnd).Monitor + x := (mi.Right - mi.Left - dx) / 2 + y := (mi.Bottom - mi.Top - dy) / 2 + windows.SetWindowPos(w.hwnd, 0, x, y, dx, dy, windows.SWP_NOOWNERZORDER|windows.SWP_FRAMECHANGED) case system.ActionRaise: w.raise() } diff --git a/app/os_x11.go b/app/os_x11.go index 5405b03b..cc753084 100644 --- a/app/os_x11.go +++ b/app/os_x11.go @@ -230,22 +230,6 @@ func (w *x11Window) Configure(options []Option) { if shints.flags != 0 { C.XSetWMNormalHints(w.x, w.xw, &shints) } - if cnf.center { - screen := C.XDefaultScreen(w.x) - width := C.XDisplayWidth(w.x, screen) - height := C.XDisplayHeight(w.x, screen) - - var attrs C.XWindowAttributes - C.XGetWindowAttributes(w.x, w.xw, &attrs) - width -= attrs.border_width - height -= attrs.border_width - - sz := w.config.Size - x := (int(width) - sz.X) / 2 - y := (int(height) - sz.Y) / 2 - - C.XMoveResizeWindow(w.x, w.xw, C.int(x), C.int(y), C.uint(sz.X), C.uint(sz.Y)) - } } if cnf.Decorated != prev.Decorated { w.config.Decorated = cnf.Decorated @@ -276,12 +260,31 @@ func (w *x11Window) setTitle(prev, cnf Config) { func (w *x11Window) Perform(acts system.Action) { walkActions(acts, func(a system.Action) { switch a { + case system.ActionCenter: + w.center() case system.ActionRaise: w.raise() } }) } +func (w *x11Window) center() { + screen := C.XDefaultScreen(w.x) + width := C.XDisplayWidth(w.x, screen) + height := C.XDisplayHeight(w.x, screen) + + var attrs C.XWindowAttributes + C.XGetWindowAttributes(w.x, w.xw, &attrs) + width -= attrs.border_width + height -= attrs.border_width + + sz := w.config.Size + x := (int(width) - sz.X) / 2 + y := (int(height) - sz.Y) / 2 + + C.XMoveResizeWindow(w.x, w.xw, C.int(x), C.int(y), C.uint(sz.X), C.uint(sz.Y)) +} + func (w *x11Window) raise() { var xev C.XEvent ev := (*C.XClientMessageEvent)(unsafe.Pointer(&xev)) diff --git a/app/window.go b/app/window.go index 641f3a7a..94274f46 100644 --- a/app/window.go +++ b/app/window.go @@ -989,12 +989,6 @@ func (w *Window) Perform(actions system.Action) { options = append(options, Maximized.Option()) case system.ActionUnmaximize: options = append(options, Windowed.Option()) - case system.ActionCenter: - options = append(options, - func(m unit.Metric, cnf *Config) { - // Set the flag so the driver can later do the actual centering. - cnf.center = true - }) case system.ActionClose: w.closing = true default: