Skip to content

Commit

Permalink
dialog: avoid dialog hanging off screen when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
tajtiattila committed Aug 8, 2016
1 parent 7cddab9 commit 765f165
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions dialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ func (dlg *Dialog) Show() {
ob := dlg.owner.Bounds()

if dlg.centerInOwnerWhenRun {
dlg.SetBounds(Rectangle{
dlg.SetBounds(fitRectToScreen(dlg.hWnd, Rectangle{
ob.X + (ob.Width-size.Width)/2,
ob.Y + (ob.Height-size.Height)/2,
size.Width,
size.Height,
})
}))
}
} else {
dlg.SetBounds(dlg.Bounds())
Expand All @@ -238,6 +238,41 @@ func (dlg *Dialog) Show() {
dlg.focusFirstCandidateDescendant()
}

func fitRectToScreen(hWnd win.HWND, r Rectangle) Rectangle {
var mi win.MONITORINFO
mi.CbSize = uint32(unsafe.Sizeof(mi))

if !win.GetMonitorInfo(win.MonitorFromWindow(
hWnd, win.MONITOR_DEFAULTTOPRIMARY), &mi) {

return r
}

mon := rectangleFromRECT(mi.RcWork)

mon.Height -= int(win.GetSystemMetrics(win.SM_CYCAPTION))

if r.Width <= mon.Width {
switch {
case r.X < mon.X:
r.X = mon.X
case r.X+r.Width > mon.X+mon.Width:
r.X = mon.X + mon.Width - r.Width
}
}

if r.Height <= mon.Height {
switch {
case r.Y < mon.Y:
r.Y = mon.Y
case r.Y+r.Height > mon.Y+mon.Height:
r.Y = mon.Y + mon.Height - r.Height
}
}

return r
}

func (dlg *Dialog) Run() int {
dlg.Show()

Expand Down

0 comments on commit 765f165

Please sign in to comment.