TimelineHeader.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "TimelineHeader.h"
  7. #include "Process.h"
  8. #include "Profile.h"
  9. #include <AK/LexicalPath.h>
  10. #include <LibGUI/FileIconProvider.h>
  11. #include <LibGUI/Painter.h>
  12. #include <LibGfx/Palette.h>
  13. namespace Profiler {
  14. TimelineHeader::TimelineHeader(Profile& profile, Process const& process)
  15. : m_profile(profile)
  16. , m_process(process)
  17. {
  18. set_frame_shape(Gfx::FrameShape::Panel);
  19. set_frame_shadow(Gfx::FrameShadow::Raised);
  20. set_fixed_size(200, 40);
  21. update_selection();
  22. m_icon = GUI::FileIconProvider::icon_for_executable(m_process.executable).bitmap_for_size(32);
  23. m_text = String::formatted("{} ({})", LexicalPath::basename(m_process.executable), m_process.pid);
  24. }
  25. TimelineHeader::~TimelineHeader()
  26. {
  27. }
  28. void TimelineHeader::paint_event(GUI::PaintEvent& event)
  29. {
  30. GUI::Frame::paint_event(event);
  31. GUI::Painter painter(*this);
  32. painter.add_clip_rect(event.rect());
  33. painter.fill_rect(frame_inner_rect(), m_selected ? palette().selection() : palette().button());
  34. Gfx::IntRect icon_rect { frame_thickness() + 2, 0, 32, 32 };
  35. icon_rect.center_vertically_within(frame_inner_rect());
  36. if (m_icon)
  37. painter.blit(icon_rect.location(), *m_icon, m_icon->rect());
  38. Gfx::IntRect text_rect {
  39. icon_rect.right() + 6,
  40. icon_rect.y(),
  41. width() - 32,
  42. 32
  43. };
  44. text_rect.center_vertically_within(frame_inner_rect());
  45. auto& font = m_selected ? painter.font().bold_variant() : painter.font();
  46. auto color = m_selected ? palette().selection_text() : palette().button_text();
  47. painter.draw_text(text_rect, m_text, font, Gfx::TextAlignment::CenterLeft, color);
  48. }
  49. void TimelineHeader::update_selection()
  50. {
  51. m_selected = m_profile.has_process_filter() && m_profile.process_filter_contains(m_process.pid, m_process.start_valid);
  52. update();
  53. }
  54. void TimelineHeader::mousedown_event(GUI::MouseEvent& event)
  55. {
  56. if (event.button() != GUI::MouseButton::Left)
  57. return;
  58. m_selected = !m_selected;
  59. on_selection_change(m_selected);
  60. }
  61. }