mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:28:12 +00:00
GWidget: Tidy up the hit-testing code somewhat.
This commit is contained in:
parent
c812d63ea6
commit
952f334de7
3 changed files with 11 additions and 13 deletions
|
@ -296,7 +296,7 @@ Rect GWidget::screen_relative_rect() const
|
||||||
return window_relative_rect().translated(window()->position());
|
return window_relative_rect().translated(window()->position());
|
||||||
}
|
}
|
||||||
|
|
||||||
GWidget* GWidget::child_at(const Point& point)
|
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 (!children()[i]->is_widget())
|
||||||
|
@ -310,13 +310,13 @@ GWidget* GWidget::child_at(const Point& point)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
GWidget::HitTestResult GWidget::hit_test(int x, int y)
|
GWidget::HitTestResult GWidget::hit_test(const Point& position)
|
||||||
{
|
{
|
||||||
if (is_greedy_for_hits())
|
if (is_greedy_for_hits())
|
||||||
return { this, x, y };
|
return { this, position };
|
||||||
if (auto* child = child_at({ x, y }))
|
if (auto* child = child_at(position))
|
||||||
return child->hit_test(x - child->x(), y - child->y());
|
return child->hit_test(position - child->relative_position());
|
||||||
return { this, x, y };
|
return { this, position };
|
||||||
}
|
}
|
||||||
|
|
||||||
void GWidget::set_window(GWindow* window)
|
void GWidget::set_window(GWindow* window)
|
||||||
|
|
|
@ -92,12 +92,10 @@ public:
|
||||||
|
|
||||||
struct HitTestResult {
|
struct HitTestResult {
|
||||||
GWidget* widget { nullptr };
|
GWidget* widget { nullptr };
|
||||||
int localX { 0 };
|
Point local_position;
|
||||||
int localY { 0 };
|
|
||||||
};
|
};
|
||||||
HitTestResult hit_test(int x, int y);
|
HitTestResult hit_test(const Point&);
|
||||||
|
GWidget* child_at(const Point&) const;
|
||||||
GWidget* child_at(const Point&);
|
|
||||||
|
|
||||||
virtual const char* class_name() const override { return "GWidget"; }
|
virtual const char* class_name() const override { return "GWidget"; }
|
||||||
|
|
||||||
|
|
|
@ -191,8 +191,8 @@ void GWindow::event(CEvent& event)
|
||||||
if (!m_main_widget)
|
if (!m_main_widget)
|
||||||
return;
|
return;
|
||||||
if (m_main_widget) {
|
if (m_main_widget) {
|
||||||
auto result = m_main_widget->hit_test(mouse_event.x(), mouse_event.y());
|
auto result = m_main_widget->hit_test(mouse_event.position());
|
||||||
auto local_event = make<GMouseEvent>((GEvent::Type)event.type(), Point { result.localX, result.localY }, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers());
|
auto local_event = make<GMouseEvent>((GEvent::Type)event.type(), result.local_position, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers());
|
||||||
ASSERT(result.widget);
|
ASSERT(result.widget);
|
||||||
set_hovered_widget(result.widget);
|
set_hovered_widget(result.widget);
|
||||||
if (mouse_event.buttons() != 0 && !m_automatic_cursor_tracking_widget)
|
if (mouse_event.buttons() != 0 && !m_automatic_cursor_tracking_widget)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue