Skip to content

Commit

Permalink
LibCore: Convert CTimer to ObjectPtr
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed Sep 20, 2019
1 parent c34fd10 commit 50a6560
Show file tree
Hide file tree
Showing 22 changed files with 91 additions and 70 deletions.
13 changes: 7 additions & 6 deletions Applications/PaintBrush/SprayTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@

SprayTool::SprayTool()
{
m_timer.on_timeout = [=]() {
m_timer = CTimer::create();
m_timer->on_timeout = [&]() {
paint_it();
};
m_timer.set_interval(200);
m_timer->set_interval(200);
}

SprayTool::~SprayTool()
Expand Down Expand Up @@ -54,22 +55,22 @@ void SprayTool::on_mousedown(GMouseEvent& event)

m_color = m_widget->color_for(event);
m_last_pos = event.position();
m_timer.start();
m_timer->start();
paint_it();
}

void SprayTool::on_mousemove(GMouseEvent& event)
{
m_last_pos = event.position();
if (m_timer.is_active()) {
if (m_timer->is_active()) {
paint_it();
m_timer.restart(m_timer.interval());
m_timer->restart(m_timer->interval());
}
}

void SprayTool::on_mouseup(GMouseEvent&)
{
m_timer.stop();
m_timer->stop();
}

void SprayTool::on_contextmenu(GContextMenuEvent& event)
Expand Down
2 changes: 1 addition & 1 deletion Applications/PaintBrush/SprayTool.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SprayTool final : public Tool {
private:
virtual const char* class_name() const override { return "SprayTool"; }
void paint_it();
CTimer m_timer;
ObjectPtr<CTimer> m_timer;
Point m_last_pos;
Color m_color;
OwnPtr<GMenu> m_context_menu;
Expand Down
2 changes: 1 addition & 1 deletion Applications/SoundPlayer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ int main(int argc, char** argv)

auto next_sample_buffer = loader.get_more_samples();

new CTimer(100, [&] {
auto timer = CTimer::create(100, [&] {
if (!next_sample_buffer) {
sample_widget->set_buffer(nullptr);
return;
Expand Down
2 changes: 1 addition & 1 deletion Applications/SystemMonitor/NetworkStatisticsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ NetworkStatisticsWidget::NetworkStatisticsWidget(GWidget* parent)
net_tcp_fields.empend("bytes_out", "Bytes Out", TextAlignment::CenterRight);
m_socket_table_view->set_model(GJsonArrayModel::create("/proc/net/tcp", move(net_tcp_fields)));

m_update_timer = new CTimer(
m_update_timer = CTimer::create(
1000, [this] {
update_models();
},
Expand Down
2 changes: 1 addition & 1 deletion Applications/SystemMonitor/NetworkStatisticsWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ class NetworkStatisticsWidget final : public GWidget {

GTableView* m_adapter_table_view { nullptr };
GTableView* m_socket_table_view { nullptr };
CTimer* m_update_timer { nullptr };
ObjectPtr<CTimer> m_update_timer;
};
2 changes: 1 addition & 1 deletion Applications/SystemMonitor/ProcessStacksWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ProcessStacksWidget::ProcessStacksWidget(GWidget* parent)
m_stacks_editor = new GTextEditor(GTextEditor::Type::MultiLine, this);
m_stacks_editor->set_readonly(true);

m_timer = new CTimer(1000, [this] { refresh(); }, this);
m_timer = CTimer::create(1000, [this] { refresh(); }, this);
}

ProcessStacksWidget::~ProcessStacksWidget()
Expand Down
3 changes: 2 additions & 1 deletion Applications/SystemMonitor/ProcessStacksWidget.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <LibCore/ObjectPtr.h>
#include <LibGUI/GTextEditor.h>
#include <LibGUI/GWidget.h>

Expand All @@ -17,5 +18,5 @@ class ProcessStacksWidget final : public GWidget {
private:
pid_t m_pid { -1 };
GTextEditor* m_stacks_editor { nullptr };
CTimer* m_timer { nullptr };
ObjectPtr<CTimer> m_timer;
};
12 changes: 6 additions & 6 deletions Applications/SystemMonitor/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ int main(int argc, char** argv)
auto* process_table_view = new ProcessTableView(*cpu_graph, process_table_container);
auto* memory_stats_widget = new MemoryStatsWidget(*memory_graph, graphs_container);

auto* refresh_timer = new CTimer(1000, [&] {
auto refresh_timer = CTimer::create(1000, [&] {
process_table_view->refresh();
memory_stats_widget->refresh();
});
Expand Down Expand Up @@ -167,19 +167,19 @@ int main(int argc, char** argv)
};

auto frequency_menu = make<GMenu>("Frequency");
frequency_menu->add_action(GAction::create("0.25 sec", [refresh_timer](auto&) {
frequency_menu->add_action(GAction::create("0.25 sec", [&](auto&) {
refresh_timer->restart(250);
}));
frequency_menu->add_action(GAction::create("0.5 sec", [refresh_timer](auto&) {
frequency_menu->add_action(GAction::create("0.5 sec", [&](auto&) {
refresh_timer->restart(500);
}));
frequency_menu->add_action(GAction::create("1 sec", [refresh_timer](auto&) {
frequency_menu->add_action(GAction::create("1 sec", [&](auto&) {
refresh_timer->restart(1000);
}));
frequency_menu->add_action(GAction::create("3 sec", [refresh_timer](auto&) {
frequency_menu->add_action(GAction::create("3 sec", [&](auto&) {
refresh_timer->restart(3000);
}));
frequency_menu->add_action(GAction::create("5 sec", [refresh_timer](auto&) {
frequency_menu->add_action(GAction::create("5 sec", [&](auto&) {
refresh_timer->restart(5000);
}));
menubar->add_menu(move(frequency_menu));
Expand Down
25 changes: 14 additions & 11 deletions Applications/Terminal/TerminalWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ TerminalWidget::TerminalWidget(int ptm_fd, RefPtr<CConfigFile> config)
, m_notifier(ptm_fd, CNotifier::Read)
, m_config(move(config))
{
m_cursor_blink_timer = CTimer::create();
m_visual_beep_timer = CTimer::create();

set_frame_shape(FrameShape::Container);
set_frame_shadow(FrameShadow::Sunken);
set_frame_thickness(2);
Expand All @@ -36,10 +39,10 @@ TerminalWidget::TerminalWidget(int ptm_fd, RefPtr<CConfigFile> config)
};

dbgprintf("Terminal: Load config file from %s\n", m_config->file_name().characters());
m_cursor_blink_timer.set_interval(m_config->read_num_entry("Text",
m_cursor_blink_timer->set_interval(m_config->read_num_entry("Text",
"CursorBlinkInterval",
500));
m_cursor_blink_timer.on_timeout = [this] {
m_cursor_blink_timer->on_timeout = [this] {
m_cursor_blink_state = !m_cursor_blink_state;
update_cursor();
};
Expand Down Expand Up @@ -103,10 +106,10 @@ void TerminalWidget::event(CEvent& event)
if (event.type() == GEvent::WindowBecameActive || event.type() == GEvent::WindowBecameInactive) {
m_in_active_window = event.type() == GEvent::WindowBecameActive;
if (!m_in_active_window) {
m_cursor_blink_timer.stop();
m_cursor_blink_timer->stop();
} else {
m_cursor_blink_state = true;
m_cursor_blink_timer.start();
m_cursor_blink_timer->start();
}
invalidate_cursor();
update();
Expand All @@ -117,9 +120,9 @@ void TerminalWidget::event(CEvent& event)
void TerminalWidget::keydown_event(GKeyEvent& event)
{
// Reset timer so cursor doesn't blink while typing.
m_cursor_blink_timer.stop();
m_cursor_blink_timer->stop();
m_cursor_blink_state = true;
m_cursor_blink_timer.start();
m_cursor_blink_timer->start();

switch (event.key()) {
case KeyCode::Key_Up:
Expand Down Expand Up @@ -196,7 +199,7 @@ void TerminalWidget::paint_event(GPaintEvent& event)

painter.add_clip_rect(event.rect());

if (m_visual_beep_timer.is_active())
if (m_visual_beep_timer->is_active())
painter.fill_rect(frame_inner_rect(), Color::Red);
else
painter.fill_rect(frame_inner_rect(), Color(Color::Black).with_alpha(m_opacity));
Expand All @@ -223,7 +226,7 @@ void TerminalWidget::paint_event(GPaintEvent& event)
continue;
auto& line = line_for_visual_row(row);
bool has_only_one_background_color = line.has_only_one_background_color();
if (m_visual_beep_timer.is_active())
if (m_visual_beep_timer->is_active())
painter.fill_rect(row_rect, Color::Red);
else if (has_only_one_background_color)
painter.fill_rect(row_rect, lookup_color(line.attributes[0].background_color).with_alpha(m_opacity));
Expand Down Expand Up @@ -563,9 +566,9 @@ void TerminalWidget::beep()
sysbeep();
return;
}
m_visual_beep_timer.restart(200);
m_visual_beep_timer.set_single_shot(true);
m_visual_beep_timer.on_timeout = [this] {
m_visual_beep_timer->restart(200);
m_visual_beep_timer->set_single_shot(true);
m_visual_beep_timer->on_timeout = [this] {
force_repaint();
};
force_repaint();
Expand Down
4 changes: 2 additions & 2 deletions Applications/Terminal/TerminalWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ class TerminalWidget final : public GFrame

int m_glyph_width { 0 };

CTimer m_cursor_blink_timer;
CTimer m_visual_beep_timer;
ObjectPtr<CTimer> m_cursor_blink_timer;
ObjectPtr<CTimer> m_visual_beep_timer;
RefPtr<CConfigFile> m_config;

GScrollBar* m_scrollbar { nullptr };
Expand Down
2 changes: 1 addition & 1 deletion Demos/WidgetGallery/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ int main(int argc, char** argv)
button2->set_enabled(false);

auto* progress1 = new GProgressBar(main_widget);
new CTimer(100, [progress1] {
auto timer = CTimer::create(100, [progress1] {
progress1->set_value(progress1->value() + 1);
if (progress1->value() == progress1->max())
progress1->set_value(progress1->min());
Expand Down
15 changes: 8 additions & 7 deletions Games/Minesweeper/Field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,12 @@ Field::Field(GLabel& flag_label, GLabel& time_label, GButton& face_button, GWidg
, m_on_size_changed(move(on_size_changed))
{
srand(time(nullptr));
m_timer.on_timeout = [this] {
m_timer = CTimer::create();
m_timer->on_timeout = [this] {
++m_time_elapsed;
m_time_label.set_text(String::format("%u.%u", m_time_elapsed / 10, m_time_elapsed % 10));
};
m_timer.set_interval(100);
m_timer->set_interval(100);
set_frame_thickness(2);
set_frame_shape(FrameShape::Container);
set_frame_shadow(FrameShadow::Sunken);
Expand Down Expand Up @@ -187,7 +188,7 @@ void Field::reset()
m_time_label.set_text("0");
m_flags_left = m_mine_count;
m_flag_label.set_text(String::number(m_flags_left));
m_timer.stop();
m_timer->stop();
set_greedy_for_hits(false);
set_face(Face::Default);

Expand Down Expand Up @@ -325,8 +326,8 @@ void Field::on_square_clicked_impl(Square& square, bool should_flood_fill)
return;
if (square.is_considering)
return;
if (!m_timer.is_active())
m_timer.start();
if (!m_timer->is_active())
m_timer->start();
update();
square.is_swept = true;
square.button->set_visible(false);
Expand Down Expand Up @@ -417,7 +418,7 @@ void Field::on_square_middle_clicked(Square& square)

void Field::win()
{
m_timer.stop();
m_timer->stop();
set_greedy_for_hits(true);
set_face(Face::Good);
for_each_square([&](auto& square) {
Expand All @@ -429,7 +430,7 @@ void Field::win()

void Field::game_over()
{
m_timer.stop();
m_timer->stop();
set_greedy_for_hits(true);
set_face(Face::Bad);
reveal_mines();
Expand Down
2 changes: 1 addition & 1 deletion Games/Minesweeper/Field.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Field final : public GFrame {
GButton& m_face_button;
GLabel& m_flag_label;
GLabel& m_time_label;
CTimer m_timer;
ObjectPtr<CTimer> m_timer;
int m_time_elapsed { 0 };
int m_flags_left { 0 };
Face m_face { Face::Default };
Expand Down
16 changes: 14 additions & 2 deletions Libraries/LibCore/CTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@

#include <AK/Function.h>
#include <LibCore/CObject.h>
#include <LibCore/ObjectPtr.h>

class CTimer final : public CObject {
C_OBJECT(CTimer)
public:
explicit CTimer(CObject* parent = nullptr);
CTimer(int interval, Function<void()>&& timeout_handler, CObject* parent = nullptr);
static ObjectPtr<CTimer> create(CObject* parent = nullptr)
{
return new CTimer(parent);
}

static ObjectPtr<CTimer> create(int interval, Function<void()>&& timeout_handler, CObject* parent = nullptr)
{
return new CTimer(interval, move(timeout_handler), parent);
}

virtual ~CTimer() override;

void start();
Expand All @@ -31,6 +40,9 @@ class CTimer final : public CObject {
Function<void()> on_timeout;

private:
explicit CTimer(CObject* parent = nullptr);
CTimer(int interval, Function<void()>&& timeout_handler, CObject* parent = nullptr);

virtual void timer_event(CTimerEvent&) override;

bool m_active { false };
Expand Down
14 changes: 7 additions & 7 deletions Libraries/LibGUI/GAbstractButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ GAbstractButton::GAbstractButton(GWidget* parent)
GAbstractButton::GAbstractButton(const StringView& text, GWidget* parent)
: GWidget(parent)
, m_text(text)
, m_auto_repeat_timer(this)
{
m_auto_repeat_timer.on_timeout = [this] {
m_auto_repeat_timer = CTimer::create(this);
m_auto_repeat_timer->on_timeout = [this] {
click();
};
}
Expand Down Expand Up @@ -71,9 +71,9 @@ void GAbstractButton::mousemove_event(GMouseEvent& event)
m_being_pressed = being_pressed;
if (m_auto_repeat_interval) {
if (!m_being_pressed)
m_auto_repeat_timer.stop();
m_auto_repeat_timer->stop();
else
m_auto_repeat_timer.start(m_auto_repeat_interval);
m_auto_repeat_timer->start(m_auto_repeat_interval);
}
update();
}
Expand All @@ -94,7 +94,7 @@ void GAbstractButton::mousedown_event(GMouseEvent& event)

if (m_auto_repeat_interval) {
click();
m_auto_repeat_timer.start(m_auto_repeat_interval);
m_auto_repeat_timer->start(m_auto_repeat_interval);
}
}
}
Expand All @@ -107,8 +107,8 @@ void GAbstractButton::mouseup_event(GMouseEvent& event)
dbgprintf("GAbstractButton::mouse_up_event: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
#endif
if (event.button() == GMouseButton::Left) {
bool was_auto_repeating = m_auto_repeat_timer.is_active();
m_auto_repeat_timer.stop();
bool was_auto_repeating = m_auto_repeat_timer->is_active();
m_auto_repeat_timer->stop();
if (is_enabled()) {
bool was_being_pressed = m_being_pressed;
m_being_pressed = false;
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibGUI/GAbstractButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class GAbstractButton : public GWidget {
bool m_exclusive { false };

int m_auto_repeat_interval { 0 };
CTimer m_auto_repeat_timer;
ObjectPtr<CTimer> m_auto_repeat_timer;
};

template<>
Expand Down
Loading

0 comments on commit 50a6560

Please sign in to comment.