Skip to content

Commit

Permalink
#4764: Improve popup handling, fix mouse position calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Nov 11, 2022
1 parent 4e935bd commit 4462154
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 29 deletions.
5 changes: 4 additions & 1 deletion libs/wxutil/TransientPopupWindow.h
Expand Up @@ -49,7 +49,10 @@ class TransientPopupWindow :
ev.GetEventType() == wxEVT_AUX2_DOWN)
{
auto& mouseEvent = static_cast<wxMouseEvent&>(ev);
auto position = this->ScreenToClient(mouseEvent.GetPosition());
auto window = static_cast<wxWindow*>(ev.GetEventObject());

// Convert to coordinates of this window
auto position = this->ScreenToClient(window->ClientToScreen(mouseEvent.GetPosition()));

// Close if the click is outside of the window, but prevent double deletions
if (this->HitTest(position.x, position.y) == wxHT_WINDOW_OUTSIDE &&
Expand Down
75 changes: 50 additions & 25 deletions radiant/ui/mediabrowser/MediaBrowser.cpp
Expand Up @@ -47,10 +47,22 @@ MediaBrowser::~MediaBrowser()
disconnectListeners();
}

_browserPopup.Release();
closePopup();
_treeView = nullptr;
}

void MediaBrowser::closePopup()
{
if (!_browserPopup.get()) return;

if (!wxTheApp->IsScheduledForDestruction(_browserPopup.get()))
{
_browserPopup->Dismiss();
}

_browserPopup.Release();
}

void MediaBrowser::onPanelActivated()
{
// Request an idle callback now to process pending updates
Expand All @@ -60,6 +72,7 @@ void MediaBrowser::onPanelActivated()

void MediaBrowser::onPanelDeactivated()
{
closePopup();
}

void MediaBrowser::connectListeners()
Expand Down Expand Up @@ -120,38 +133,50 @@ void MediaBrowser::onIdle()

if (_treeView->IsDirectorySelected())
{
/// Dismiss any previous popups
if (_browserPopup.get())
{
if (!wxTheApp->IsScheduledForDestruction(_browserPopup.get()))
{
_browserPopup->Dismiss();
}

_browserPopup.Release();
}

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

// Remember this popup, if it's still around the next time we get here we need to destroy it
_browserPopup = popup;
auto popup = findOrCreateBrowserPopup();

auto browser = new TextureDirectoryBrowser(popup, _treeView->GetSelectedTextureFolderName());
popup->GetSizer()->Add(browser, 1, wxEXPAND | wxALL, 3);
auto browser = static_cast<TextureDirectoryBrowser*>(popup->FindWindow("TextureDirectoryBrowser"));
assert(browser);

// 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->PositionNextTo(this, verticalOffset, size);
popup->Layout();
// Set the (new) texture path and show the popup
browser->setTexturePath(_treeView->GetSelectedTextureFolderName());

popup->Popup();
}
}
}

wxutil::TransientPopupWindow* MediaBrowser::findOrCreateBrowserPopup()
{
// Check if we have any previous popup that is not scheduled to be destroyed
if (_browserPopup.get() && !wxTheApp->IsScheduledForDestruction(_browserPopup.get()))
{
// We can re-use this one
return _browserPopup.get();
}

// Create a new one
auto popup = new wxutil::TransientPopupWindow(this);

// Remember this popup
_browserPopup = popup;

auto browser = new TextureDirectoryBrowser(popup, _treeView->GetSelectedTextureFolderName());
browser->SetName("TextureDirectoryBrowser");
popup->GetSizer()->Add(browser, 1, wxEXPAND | wxALL, 3);

// 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->PositionNextTo(this, verticalOffset, size);
popup->Layout();

return popup;
}

void MediaBrowser::queueTreeReload()
{
_reloadTreeOnIdle = true;
Expand Down
2 changes: 2 additions & 0 deletions radiant/ui/mediabrowser/MediaBrowser.h
Expand Up @@ -94,6 +94,8 @@ class MediaBrowser :

void onShaderClipboardSourceChanged();
void sendSelectionToShaderClipboard();
wxutil::TransientPopupWindow* findOrCreateBrowserPopup();
void closePopup();
};

}
13 changes: 10 additions & 3 deletions radiant/ui/texturebrowser/TextureDirectoryBrowser.h
Expand Up @@ -18,9 +18,16 @@ class TextureDirectoryBrowser :

public:
TextureDirectoryBrowser(wxWindow* parent, const std::string& texturePath) :
TextureThumbnailBrowser(parent, false), // hide the toolbar
_texturePath(os::standardPathWithSlash(texturePath))
{}
TextureThumbnailBrowser(parent, false) // hide the toolbar
{
setTexturePath(texturePath);
}

void setTexturePath(const std::string& texturePath)
{
_texturePath = os::standardPathWithSlash(texturePath);
queueUpdate();
}

protected:
void populateTiles() override
Expand Down

0 comments on commit 4462154

Please sign in to comment.