TimelineHeader.cpp 2.1 KB

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