Skip to content

Commit

Permalink
#4764: Better popup sizing and placement
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Nov 6, 2022
1 parent 7555819 commit 70e32ff
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 22 deletions.
53 changes: 53 additions & 0 deletions libs/wxutil/TransientPopupWindow.h
@@ -0,0 +1,53 @@
#pragma once

#include <wx/popupwin.h>
#include <wx/sizer.h>
#include "MultiMonitor.h"

namespace wxutil
{

class TransientPopupWindow :
public wxPopupTransientWindow
{
public:
TransientPopupWindow(wxWindow* parent) :
wxPopupTransientWindow(parent, wxBORDER_DEFAULT)
{
SetSizer(new wxBoxSizer(wxVERTICAL));
}

// Attempts to position the popup to the right or left of the given window
// The vertical offset is added to the screen position of the given window
void PositionNextTo(wxWindow* win, int verticalOffset, const wxSize& size)
{
auto rectScreen = MultiMonitor::getMonitorForWindow(win);

SetSize(size);

// Try right first
auto rightTop = win->GetScreenRect().GetRightTop();
rightTop.y += verticalOffset;

if (rightTop.x + size.GetWidth() < rectScreen.GetX() + rectScreen.GetWidth())
{
SetPosition(rightTop);
return;
}

// Failed, place to the left
auto leftTop = win->GetScreenRect().GetLeftTop();
leftTop.y += verticalOffset;

if (leftTop.x - size.GetWidth() > rectScreen.GetX())
{
SetPosition(wxPoint(leftTop.x - size.GetWidth(), leftTop.y));
return;
}

// Fall back to the wxwidgets default algorithm
Position(win->GetScreenPosition(), size);
}
};

}
31 changes: 9 additions & 22 deletions radiant/ui/mediabrowser/MediaBrowser.cpp
Expand Up @@ -19,6 +19,8 @@
#include "FocusMaterialRequest.h"
#include "ui/UserInterfaceModule.h"
#include "ui/texturebrowser/TextureDirectoryBrowser.h"
#include "wxutil/MultiMonitor.h"
#include "wxutil/TransientPopupWindow.h"

namespace ui
{
Expand Down Expand Up @@ -196,32 +198,17 @@ void MediaBrowser::_onTreeViewSelectionChanged(wxDataViewEvent& ev)
_preview->ClearPreview();

// When a directory is selected, open the popup
auto popup = new wxPopupTransientWindow(this);

popup->SetSizer(new wxBoxSizer(wxVERTICAL));

auto popup = new wxutil::TransientPopupWindow(this);
auto browser = new TextureDirectoryBrowser(popup, _treeView->GetSelectedFullname());
//browser->SetMinSize(size);

wxPoint posScreen;
wxSize sizeScreen;

const int displayNum = wxDisplay::GetFromPoint(GetScreenPosition());
if ( displayNum != wxNOT_FOUND )
{
wxRect rectScreen = wxDisplay(displayNum).GetGeometry();
posScreen = rectScreen.GetPosition();
sizeScreen = rectScreen.GetSize();
}
popup->GetSizer()->Add(browser, 1, wxEXPAND | wxALL, 3);

// From the upper edge of the mediabrowser to the bottom of the screen (minus a few pixels)
wxSize size(600, (posScreen.y + sizeScreen.y) - GetScreenPosition().y - 40);
// Size reaching from the upper edge of the mediabrowser to the bottom of the screen (minus a few pixels)
auto rectScreen = wxutil::MultiMonitor::getMonitorForWindow(this);
int verticalOffset = -(GetScreenPosition().y - rectScreen.GetY()) / 2;
wxSize size(630, rectScreen.GetY() + rectScreen.GetHeight() - GetScreenPosition().y - verticalOffset - 40);

popup->GetSizer()->Add(browser, 1, wxEXPAND);
popup->SetPosition(this->GetScreenRect().GetRightTop());
popup->SetSize(size);
popup->PositionNextTo(this, verticalOffset, size);
popup->Layout();
//popup->Position(this->GetScreenRect().GetRightTop(), size);
popup->Popup();
}

Expand Down
1 change: 1 addition & 0 deletions tools/msvc/wxutillib.vcxproj
Expand Up @@ -203,6 +203,7 @@
<ClInclude Include="..\..\libs\wxutil\sourceview\DefinitionView.h" />
<ClInclude Include="..\..\libs\wxutil\sourceview\SourceView.h" />
<ClInclude Include="..\..\libs\wxutil\Splitter.h" />
<ClInclude Include="..\..\libs\wxutil\TransientPopupWindow.h" />
<ClInclude Include="..\..\libs\wxutil\WindowPosition.h" />
<ClInclude Include="..\..\libs\wxutil\WindowState.h" />
<ClInclude Include="..\..\libs\wxutil\XmlResourceBasedWidget.h" />
Expand Down
1 change: 1 addition & 0 deletions tools/msvc/wxutillib.vcxproj.filters
Expand Up @@ -186,6 +186,7 @@
<Filter>dataview</Filter>
</ClInclude>
<ClInclude Include="..\..\libs\wxutil\DockablePanel.h" />
<ClInclude Include="..\..\libs\wxutil\TransientPopupWindow.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\libs\wxutil\dialog\MessageBox.cpp">
Expand Down

0 comments on commit 70e32ff

Please sign in to comment.