Skip to content

Commit

Permalink
add Popup::CloseEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
HJfod committed Sep 20, 2024
1 parent 879ca57 commit 6270e1c
Showing 1 changed file with 60 additions and 1 deletion.
61 changes: 60 additions & 1 deletion loader/include/Geode/ui/Popup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,59 @@
#include <Geode/utils/cocos.hpp>

namespace geode {
template <typename... InitArgs>
template <class... InitArgs>
class Popup : public FLAlertLayer {
public:
/**
* Event posted when this popup is being closed
*/
class CloseEvent final : public ::geode::Event {
private:
class Impl final {
private:
Popup* popup;
friend class CloseEvent;
};
std::shared_ptr<Impl> m_impl;

friend class Popup;

CloseEvent(Popup* popup) : m_impl(std::make_shared<Impl>()) {
m_impl->popup = popup;
}

public:
Popup* getPopup() const {
return m_impl->popup;
}
};
class CloseEventFilter final : public ::geode::EventFilter<CloseEvent> {
public:
using Callback = void(CloseEvent*);

private:
class Impl final {
private:
Popup* popup;
friend class CloseEventFilter;
};
std::shared_ptr<Impl> m_impl;

friend class Popup;

CloseEventFilter(Popup* popup) : m_impl(std::make_shared<Impl>()) {
m_impl->popup = popup;
}

public:
ListenerResult handle(utils::MiniFunction<Callback> fn, CloseEvent* event) {
if (event->getPopup() == m_impl->popup) {
fn(event);
}
return ListenerResult::Propagate;
}
};

protected:
cocos2d::CCSize m_size;
cocos2d::extension::CCScale9Sprite* m_bgSprite;
Expand Down Expand Up @@ -115,6 +166,7 @@ namespace geode {
}

virtual void onClose(cocos2d::CCObject*) {
CloseEvent(this).post();
this->setKeypadEnabled(false);
this->setTouchEnabled(false);
this->removeFromParentAndCleanup(true);
Expand Down Expand Up @@ -158,6 +210,13 @@ namespace geode {
spr->setAnchorPoint(orig->getAnchorPoint());
m_closeBtn->setContentSize(origSize);
}

/**
* Returns an event filter that listens for when this popup is closed
*/
CloseEventFilter listenForClose() {
return CloseEventFilter(this);
}
};

GEODE_DLL FLAlertLayer* createQuickPopup(
Expand Down

0 comments on commit 6270e1c

Please sign in to comment.