Skip to content

Instantly share code, notes, and snippets.

@AdnoC
Created January 10, 2018 18:09
Show Gist options
  • Save AdnoC/163f65e8df284ca52626e4dfcb7d8766 to your computer and use it in GitHub Desktop.
Save AdnoC/163f65e8df284ca52626e4dfcb7d8766 to your computer and use it in GitHub Desktop.
Open Neovim-Qt windows centered (simpler)
function! s:set_size(...)
" https://github.com/equalsraf/neovim-qt/issues/251
py3 << EOF
import vim
from ctypes import *
u32 = WinDLL('user32.dll')
GetForegroundWindow = u32.GetForegroundWindow
def center_window(hwnd):
MonitorFromWindow = u32.MonitorFromWindow
GetMonitorInfoA = u32.GetMonitorInfoA
SetWindowPos = u32.SetWindowPos
SetForegroundWindow = u32.SetForegroundWindow
class _rect_t(Structure):
_fields_ = [
('left', c_long),
('top', c_long),
('right', c_long),
('bottom', c_long)
]
class _monitor_info_t(Structure):
_fields_ = [
('cbSize', c_ulong),
('rcMonitor', _rect_t),
('rcWork', _rect_t),
('dwFlags', c_ulong)
]
MONITOR_DEFAULTTONEAREST = 0 # Probably
mon = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST)
# Monitor info code gotten from:
# http://nullege.com/codes/show/src%40s%40u%40SublimeSpeech-HEAD%40lib%40dragonfly%40windows%40monitor.py/118/windll.user32.GetMonitorInfoA/python
info = _monitor_info_t()
info.cbSize = sizeof(_monitor_info_t)
info.rcMonitor = _rect_t()
info.rcWork = _rect_t()
GetMonitorInfoA(mon, byref(info))
mon_width = info.rcMonitor.right - info.rcMonitor.left
mon_height = info.rcMonitor.bottom - info.rcMonitor.top
mid_w = mon_width / 2
mid_h = mon_height / 2
WIDTH = int(mon_width * 45.0/100.0)
HEIGHT = int(mon_height * 9.0/10.0)
LEFT = info.rcMonitor.left + int(mid_w - (WIDTH / 2))
TOP = info.rcMonitor.top + int(mid_h - (HEIGHT / 2))
SetWindowPos(hwnd, 0, LEFT, TOP, WIDTH, HEIGHT, 0)
SetForegroundWindow(hwnd)
vim_win = GetForegroundWindow()
center_window(vim_win)
EOF
endfunction
if exists('g:GuiLoaded')
call s:set_size()
endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment