From 325d9445fdf3f0239abca2fecac7e7ee6a973762 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20C=C3=A9sar=20Neves=20Enumo?= Date: Sat, 8 May 2021 03:27:13 -0300 Subject: [PATCH] Profiler: Migrate mouse events to TimelineView This change allows for continuous mouse events when hovering the layout spaces between tracks. --- Userland/DevTools/Profiler/TimelineTrack.cpp | 56 +++++--------------- Userland/DevTools/Profiler/TimelineTrack.h | 12 ++--- Userland/DevTools/Profiler/TimelineView.cpp | 45 +++++++++++++++- Userland/DevTools/Profiler/TimelineView.h | 18 +++++-- Userland/DevTools/Profiler/main.cpp | 2 +- 5 files changed, 76 insertions(+), 57 deletions(-) diff --git a/Userland/DevTools/Profiler/TimelineTrack.cpp b/Userland/DevTools/Profiler/TimelineTrack.cpp index 74f4d09e6f..7a09d54db0 100644 --- a/Userland/DevTools/Profiler/TimelineTrack.cpp +++ b/Userland/DevTools/Profiler/TimelineTrack.cpp @@ -13,7 +13,7 @@ namespace Profiler { -TimelineTrack::TimelineTrack(TimelineView& view, Profile& profile, Process const& process) +TimelineTrack::TimelineTrack(TimelineView const& view, Profile const& profile, Process const& process) : m_view(view) , m_profile(profile) , m_process(process) @@ -29,6 +29,19 @@ TimelineTrack::~TimelineTrack() { } +void TimelineTrack::event(Core::Event& event) +{ + switch (event.type()) { + case GUI::Event::MouseUp: + case GUI::Event::MouseDown: + case GUI::Event::MouseMove: + event.ignore(); + default: + break; + } + GUI::Frame::event(event); +} + void TimelineTrack::paint_event(GUI::PaintEvent& event) { GUI::Frame::paint_event(event); @@ -76,45 +89,4 @@ void TimelineTrack::paint_event(GUI::PaintEvent& event) painter.fill_rect({ select_hover_x, frame_thickness(), 1, height() - frame_thickness() * 2 }, Color::NamedColor::Black); } -u64 TimelineTrack::timestamp_at_x(int x) const -{ - float column_width = (float)frame_inner_rect().width() / (float)m_profile.length_in_ms(); - float ms_into_profile = (float)x / column_width; - return m_profile.first_timestamp() + (u64)ms_into_profile; -} - -void TimelineTrack::mousedown_event(GUI::MouseEvent& event) -{ - if (event.button() != GUI::MouseButton::Left) - return; - - m_view.set_selecting({}, true); - m_view.set_select_start_time({}, timestamp_at_x(event.x())); - m_view.set_select_end_time({}, m_view.select_start_time()); - m_profile.set_timestamp_filter_range(m_view.select_start_time(), m_view.select_end_time()); - update(); -} - -void TimelineTrack::mousemove_event(GUI::MouseEvent& event) -{ - m_view.set_hover_time({}, timestamp_at_x(event.x())); - - if (m_view.is_selecting()) { - m_view.set_select_end_time({}, m_view.hover_time()); - m_profile.set_timestamp_filter_range(m_view.select_start_time(), m_view.select_end_time()); - } - - update(); -} - -void TimelineTrack::mouseup_event(GUI::MouseEvent& event) -{ - if (event.button() != GUI::MouseButton::Left) - return; - - m_view.set_selecting({}, false); - if (m_view.select_start_time() == m_view.select_end_time()) - m_profile.clear_timestamp_filter_range(); -} - } diff --git a/Userland/DevTools/Profiler/TimelineTrack.h b/Userland/DevTools/Profiler/TimelineTrack.h index 525f48d780..0fca8fea5e 100644 --- a/Userland/DevTools/Profiler/TimelineTrack.h +++ b/Userland/DevTools/Profiler/TimelineTrack.h @@ -21,17 +21,13 @@ public: virtual ~TimelineTrack() override; private: + virtual void event(Core::Event&) override; virtual void paint_event(GUI::PaintEvent&) override; - virtual void mousedown_event(GUI::MouseEvent&) override; - virtual void mousemove_event(GUI::MouseEvent&) override; - virtual void mouseup_event(GUI::MouseEvent&) override; - explicit TimelineTrack(TimelineView&, Profile&, Process const&); + explicit TimelineTrack(TimelineView const&, Profile const&, Process const&); - u64 timestamp_at_x(int x) const; - - TimelineView& m_view; - Profile& m_profile; + TimelineView const& m_view; + Profile const& m_profile; Process const& m_process; }; diff --git a/Userland/DevTools/Profiler/TimelineView.cpp b/Userland/DevTools/Profiler/TimelineView.cpp index ae71668cb9..7a36abd663 100644 --- a/Userland/DevTools/Profiler/TimelineView.cpp +++ b/Userland/DevTools/Profiler/TimelineView.cpp @@ -5,11 +5,13 @@ */ #include "TimelineView.h" +#include "Profile.h" #include namespace Profiler { -TimelineView::TimelineView() +TimelineView::TimelineView(Profile& profile) + : m_profile(profile) { set_layout(); set_shrink_to_fit(true); @@ -19,4 +21,45 @@ TimelineView::~TimelineView() { } +u64 TimelineView::timestamp_at_x(int x) const +{ + float column_width = (float)width() / (float)m_profile.length_in_ms(); + float ms_into_profile = (float)x / column_width; + return m_profile.first_timestamp() + (u64)ms_into_profile; +} + +void TimelineView::mousedown_event(GUI::MouseEvent& event) +{ + if (event.button() != GUI::MouseButton::Left) + return; + + set_selecting(true); + set_select_start_time(timestamp_at_x(event.x())); + set_select_end_time(select_start_time()); + m_profile.set_timestamp_filter_range(select_start_time(), select_end_time()); + update(); +} + +void TimelineView::mousemove_event(GUI::MouseEvent& event) +{ + set_hover_time(timestamp_at_x(event.x())); + + if (is_selecting()) { + set_select_end_time(hover_time()); + m_profile.set_timestamp_filter_range(select_start_time(), select_end_time()); + } + + update(); +} + +void TimelineView::mouseup_event(GUI::MouseEvent& event) +{ + if (event.button() != GUI::MouseButton::Left) + return; + + set_selecting(false); + if (select_start_time() == select_end_time()) + m_profile.clear_timestamp_filter_range(); +} + } diff --git a/Userland/DevTools/Profiler/TimelineView.h b/Userland/DevTools/Profiler/TimelineView.h index b276ab950c..9eed2ff7d1 100644 --- a/Userland/DevTools/Profiler/TimelineView.h +++ b/Userland/DevTools/Profiler/TimelineView.h @@ -10,6 +10,7 @@ namespace Profiler { +class Profile; class TimelineTrack; class TimelineView final : public GUI::Widget { @@ -25,14 +26,14 @@ public: u64 select_end_time() const { return m_select_end_time; } u64 hover_time() const { return m_hover_time; } - void set_selecting(Badge, bool value) + void set_selecting(bool value) { if (m_selecting == value) return; m_selecting = value; } - void set_select_start_time(Badge, u64 value) + void set_select_start_time(u64 value) { if (m_select_start_time == value) return; @@ -41,7 +42,7 @@ public: if (on_selection_change) on_selection_change(); } - void set_select_end_time(Badge, u64 value) + void set_select_end_time(u64 value) { if (m_select_end_time == value) return; @@ -50,7 +51,7 @@ public: if (on_selection_change) on_selection_change(); } - void set_hover_time(Badge, u64 value) + void set_hover_time(u64 value) { if (m_hover_time == value) return; @@ -61,8 +62,15 @@ public: } private: - TimelineView(); + virtual void mousedown_event(GUI::MouseEvent&) override; + virtual void mousemove_event(GUI::MouseEvent&) override; + virtual void mouseup_event(GUI::MouseEvent&) override; + explicit TimelineView(Profile&); + + u64 timestamp_at_x(int x) const; + + Profile& m_profile; bool m_selecting { false }; u64 m_select_start_time { 0 }; u64 m_select_end_time { 0 }; diff --git a/Userland/DevTools/Profiler/main.cpp b/Userland/DevTools/Profiler/main.cpp index b707fa6cb0..3b322f008a 100644 --- a/Userland/DevTools/Profiler/main.cpp +++ b/Userland/DevTools/Profiler/main.cpp @@ -96,7 +96,7 @@ int main(int argc, char** argv) timeline_header_container->set_fill_with_background_color(true); timeline_header_container->set_shrink_to_fit(true); - auto timeline_view = TimelineView::construct(); + auto timeline_view = TimelineView::construct(*profile); for (auto& process : profile->processes()) { bool matching_event_found = false; for (auto& event : profile->events()) {