mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-05-02 17:28:48 +00:00
LibCore+LibGUI: Add is<T>(CObject&) and to<T>(CObject&) helpers.
This commit is contained in:
parent
723ba91f74
commit
0c85d3dba9
Notes:
sideshowbarker
2024-07-19 13:55:24 +09:00
Author: https://github.com/awesomekling
Commit: 0c85d3dba9
5 changed files with 53 additions and 30 deletions
|
@ -58,3 +58,20 @@ private:
|
||||||
bool m_widget { false };
|
bool m_widget { false };
|
||||||
Vector<CObject*> m_children;
|
Vector<CObject*> m_children;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename T> inline bool is(const CObject&) { return false; }
|
||||||
|
template<> inline bool is<CObject>(const CObject&) { return true; }
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline T& to(CObject& object)
|
||||||
|
{
|
||||||
|
ASSERT(is<T>(object));
|
||||||
|
return static_cast<T&>(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline const T& to(const CObject& object)
|
||||||
|
{
|
||||||
|
ASSERT(is<T>(object));
|
||||||
|
return static_cast<const T&>(object);
|
||||||
|
}
|
||||||
|
|
|
@ -33,9 +33,9 @@ void GStackWidget::resize_event(GResizeEvent& event)
|
||||||
|
|
||||||
void GStackWidget::child_event(CChildEvent& event)
|
void GStackWidget::child_event(CChildEvent& event)
|
||||||
{
|
{
|
||||||
if (!event.child() || !event.child()->is_widget())
|
if (!event.child() || !is<GWidget>(*event.child()))
|
||||||
return GWidget::child_event(event);
|
return GWidget::child_event(event);
|
||||||
auto& child = static_cast<GWidget&>(*event.child());
|
auto& child = to<GWidget>(*event.child());
|
||||||
if (event.type() == GEvent::ChildAdded) {
|
if (event.type() == GEvent::ChildAdded) {
|
||||||
if (!m_active_widget)
|
if (!m_active_widget)
|
||||||
set_active_widget(&child);
|
set_active_widget(&child);
|
||||||
|
@ -44,12 +44,10 @@ void GStackWidget::child_event(CChildEvent& event)
|
||||||
} else if (event.type() == GEvent::ChildRemoved) {
|
} else if (event.type() == GEvent::ChildRemoved) {
|
||||||
if (m_active_widget == &child) {
|
if (m_active_widget == &child) {
|
||||||
GWidget* new_active_widget = nullptr;
|
GWidget* new_active_widget = nullptr;
|
||||||
for (auto* new_child : children()) {
|
for_each_child_widget([&] (auto& new_child) {
|
||||||
if (new_child->is_widget()) {
|
new_active_widget = &new_child;
|
||||||
new_active_widget = static_cast<GWidget*>(new_child);
|
return IterationDecision::Abort;
|
||||||
break;
|
});
|
||||||
}
|
|
||||||
}
|
|
||||||
set_active_widget(new_active_widget);
|
set_active_widget(new_active_widget);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,9 +50,9 @@ Rect GTabWidget::child_rect_for_size(const Size& size) const
|
||||||
|
|
||||||
void GTabWidget::child_event(CChildEvent& event)
|
void GTabWidget::child_event(CChildEvent& event)
|
||||||
{
|
{
|
||||||
if (!event.child() || !event.child()->is_widget())
|
if (!event.child() || !is<GWidget>(*event.child()))
|
||||||
return GWidget::child_event(event);
|
return GWidget::child_event(event);
|
||||||
auto& child = static_cast<GWidget&>(*event.child());
|
auto& child = to<GWidget>(*event.child());
|
||||||
if (event.type() == GEvent::ChildAdded) {
|
if (event.type() == GEvent::ChildAdded) {
|
||||||
if (!m_active_widget)
|
if (!m_active_widget)
|
||||||
set_active_widget(&child);
|
set_active_widget(&child);
|
||||||
|
|
|
@ -26,13 +26,13 @@ GWidget::~GWidget()
|
||||||
void GWidget::child_event(CChildEvent& event)
|
void GWidget::child_event(CChildEvent& event)
|
||||||
{
|
{
|
||||||
if (event.type() == GEvent::ChildAdded) {
|
if (event.type() == GEvent::ChildAdded) {
|
||||||
if (event.child() && event.child()->is_widget() && layout())
|
if (event.child() && is<GWidget>(*event.child()) && layout())
|
||||||
layout()->add_widget(static_cast<GWidget&>(*event.child()));
|
layout()->add_widget(to<GWidget>(*event.child()));
|
||||||
}
|
}
|
||||||
if (event.type() == GEvent::ChildRemoved) {
|
if (event.type() == GEvent::ChildRemoved) {
|
||||||
if (layout()) {
|
if (layout()) {
|
||||||
if (event.child() && event.child()->is_widget())
|
if (event.child() && is<GWidget>(*event.child()))
|
||||||
layout()->remove_widget(static_cast<GWidget&>(*event.child()));
|
layout()->remove_widget(to<GWidget>(*event.child()));
|
||||||
else
|
else
|
||||||
invalidate_layout();
|
invalidate_layout();
|
||||||
}
|
}
|
||||||
|
@ -321,9 +321,9 @@ Rect GWidget::screen_relative_rect() const
|
||||||
GWidget* GWidget::child_at(const Point& point) const
|
GWidget* GWidget::child_at(const Point& point) const
|
||||||
{
|
{
|
||||||
for (int i = children().size() - 1; i >= 0; --i) {
|
for (int i = children().size() - 1; i >= 0; --i) {
|
||||||
if (!children()[i]->is_widget())
|
if (!is<GWidget>(*children()[i]))
|
||||||
continue;
|
continue;
|
||||||
auto& child = *(GWidget*)children()[i];
|
auto& child = to<GWidget>(*children()[i]);
|
||||||
if (!child.is_visible())
|
if (!child.is_visible())
|
||||||
continue;
|
continue;
|
||||||
if (child.relative_rect().contains(point))
|
if (child.relative_rect().contains(point))
|
||||||
|
|
|
@ -141,18 +141,8 @@ public:
|
||||||
|
|
||||||
void set_window(GWindow*);
|
void set_window(GWindow*);
|
||||||
|
|
||||||
GWidget* parent_widget()
|
GWidget* parent_widget();
|
||||||
{
|
const GWidget* parent_widget() const;
|
||||||
if (parent() && parent()->is_widget())
|
|
||||||
return static_cast<GWidget*>(parent());
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
const GWidget* parent_widget() const
|
|
||||||
{
|
|
||||||
if (parent() && parent()->is_widget())
|
|
||||||
return static_cast<const GWidget*>(parent());
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void set_fill_with_background_color(bool b) { m_fill_with_background_color = b; }
|
void set_fill_with_background_color(bool b) { m_fill_with_background_color = b; }
|
||||||
bool fill_with_background_color() const { return m_fill_with_background_color; }
|
bool fill_with_background_color() const { return m_fill_with_background_color; }
|
||||||
|
@ -189,8 +179,8 @@ public:
|
||||||
void for_each_child_widget(Callback callback)
|
void for_each_child_widget(Callback callback)
|
||||||
{
|
{
|
||||||
for_each_child([&] (auto& child) {
|
for_each_child([&] (auto& child) {
|
||||||
if (child.is_widget())
|
if (is<GWidget>(child))
|
||||||
return callback(static_cast<GWidget&>(child));
|
return callback(to<GWidget>(child));
|
||||||
return IterationDecision::Continue;
|
return IterationDecision::Continue;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -233,3 +223,21 @@ private:
|
||||||
|
|
||||||
HashMap<GShortcut, GAction*> m_local_shortcut_actions;
|
HashMap<GShortcut, GAction*> m_local_shortcut_actions;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<> inline bool is<GWidget>(const CObject& object)
|
||||||
|
{
|
||||||
|
return object.is_widget();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline GWidget* GWidget::parent_widget()
|
||||||
|
{
|
||||||
|
if (parent() && is<GWidget>(*parent()))
|
||||||
|
return &to<GWidget>(*parent());
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
inline const GWidget* GWidget::parent_widget() const
|
||||||
|
{
|
||||||
|
if (parent() && is<GWidget>(*parent()))
|
||||||
|
return &to<const GWidget>(*parent());
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue