Skip to content

Commit

Permalink
Incredibly smart BWindow::MoveOnScreen() method added.
Browse files Browse the repository at this point in the history
* Makes sure that the window is as complete as possible on screen.
  • Loading branch information
axeld committed Sep 3, 2015
1 parent b7d0120 commit ebf4cbe
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
3 changes: 2 additions & 1 deletion headers/os/interface/Window.h
@@ -1,5 +1,5 @@
/*
* Copyright 2001-2011, Haiku, Inc. All rights reserved.
* Copyright 2001-2015, Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef _WINDOW_H
Expand Down Expand Up @@ -170,6 +170,7 @@ class BWindow : public BLooper {
void CenterIn(const BRect& rect);
void CenterOnScreen();
void CenterOnScreen(screen_id id);
void MoveOnScreen();

virtual void Show();
virtual void Hide();
Expand Down
43 changes: 42 additions & 1 deletion src/kits/interface/Window.cpp
@@ -1,5 +1,5 @@
/*
* Copyright 2001-2014 Haiku, Inc. All rights reserved
* Copyright 2001-2015 Haiku, Inc. All rights reserved
* Distributed under the terms of the MIT License.
*
* Authors:
Expand Down Expand Up @@ -2568,6 +2568,47 @@ BWindow::CenterOnScreen(screen_id id)
}


void
BWindow::MoveOnScreen()
{
// Set size limits now if needed
UpdateSizeLimits();

BRect screenFrame = BScreen(this).Frame();
BRect frame = Frame();

float borderWidth;
float tabHeight;
_GetDecoratorSize(&borderWidth, &tabHeight);

frame.InsetBy(-borderWidth, -borderWidth);
frame.top -= tabHeight;

if (!frame.Intersects(screenFrame)) {
// Off and away
CenterOnScreen();
return;
}

// Move such that the upper left corner, and most of the window
// will be visible.
float left = frame.left;
if (left < screenFrame.left)
left = screenFrame.left;
else if (frame.right > screenFrame.right)
left = std::max(0.f, screenFrame.right - frame.Width());

float top = frame.top;
if (top < screenFrame.top)
top = screenFrame.top;
else if (frame.bottom > screenFrame.bottom)
top = std::max(0.f, screenFrame.bottom - frame.Height());

if (top != frame.top || left != frame.left)
MoveTo(left + borderWidth, top + tabHeight + borderWidth);
}


void
BWindow::Show()
{
Expand Down

0 comments on commit ebf4cbe

Please sign in to comment.