Serenity Operating System
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
8#include "TimelineHeader.h"
9#include "Process.h"
10#include "Profile.h"
11#include <AK/LexicalPath.h>
12#include <LibGUI/FileIconProvider.h>
13#include <LibGUI/Painter.h>
14#include <LibGfx/Palette.h>
15
16namespace Profiler {
17
18TimelineHeader::TimelineHeader(Profile& profile, Process const& process)
19 : m_profile(profile)
20 , m_process(process)
21{
22 set_frame_shape(Gfx::FrameShape::Panel);
23 set_frame_shadow(Gfx::FrameShadow::Raised);
24 set_fixed_size(200, 40);
25 update_selection();
26
27 m_icon = GUI::FileIconProvider::icon_for_executable(m_process.executable).bitmap_for_size(32);
28 m_text = DeprecatedString::formatted("{} ({})", LexicalPath::basename(m_process.executable), m_process.pid);
29}
30
31void TimelineHeader::paint_event(GUI::PaintEvent& event)
32{
33 GUI::Frame::paint_event(event);
34 GUI::Painter painter(*this);
35 painter.add_clip_rect(event.rect());
36
37 painter.fill_rect(frame_inner_rect(), m_selected ? palette().selection() : palette().button());
38
39 Gfx::IntRect icon_rect { frame_thickness() + 2, 0, 32, 32 };
40 icon_rect.center_vertically_within(frame_inner_rect());
41
42 if (m_icon)
43 painter.blit(icon_rect.location(), *m_icon, m_icon->rect());
44
45 Gfx::IntRect text_rect {
46 icon_rect.right() + 6,
47 icon_rect.y(),
48 width() - 32,
49 32
50 };
51 text_rect.center_vertically_within(frame_inner_rect());
52
53 auto const& font = m_selected ? painter.font().bold_variant() : painter.font();
54 auto color = m_selected ? palette().selection_text() : palette().button_text();
55 painter.draw_text(text_rect, m_text, font, Gfx::TextAlignment::CenterLeft, color);
56}
57
58void TimelineHeader::update_selection()
59{
60 m_selected = m_profile.has_process_filter() && m_profile.process_filter_contains(m_process.pid, m_process.start_valid);
61 update();
62}
63
64void TimelineHeader::mousedown_event(GUI::MouseEvent& event)
65{
66 if (event.button() != GUI::MouseButton::Primary)
67 return;
68 m_selected = !m_selected;
69 on_selection_change(m_selected);
70}
71
72}