From e181b1cb820a69d92b77f74aeaee62961107f8a2 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 11 Jan 2023 20:00:46 +0000 Subject: [PATCH] Userland: Use Core::Timer::create_foo() factory functions where possible --- Userland/Applications/HexEditor/HexEditor.cpp | 8 ++---- Userland/Applications/HexEditor/HexEditor.h | 2 +- Userland/Applications/Piano/main.cpp | 4 +-- .../PixelPaint/Tools/SprayTool.cpp | 6 ++-- .../PixelPaint/Tools/TextTool.cpp | 6 ++-- .../SoundPlayer/PlaybackManager.cpp | 4 +-- Userland/Demos/CatDog/main.cpp | 7 ++--- .../Demos/WidgetGallery/DemoWizardDialog.cpp | 28 ++++++++++--------- Userland/DevTools/Profiler/main.cpp | 4 +-- .../Libraries/LibVideo/PlaybackManager.cpp | 11 ++------ Userland/Libraries/LibVideo/PlaybackManager.h | 4 +-- .../Services/WindowServer/WindowFrame.cpp | 4 +-- 12 files changed, 37 insertions(+), 51 deletions(-) diff --git a/Userland/Applications/HexEditor/HexEditor.cpp b/Userland/Applications/HexEditor/HexEditor.cpp index 03d762f603..206f2f1043 100644 --- a/Userland/Applications/HexEditor/HexEditor.cpp +++ b/Userland/Applications/HexEditor/HexEditor.cpp @@ -31,8 +31,7 @@ #include HexEditor::HexEditor() - : m_blink_timer(Core::Timer::construct()) - , m_document(make(ByteBuffer::create_zeroed(0).release_value_but_fixme_should_propagate_errors())) + : m_document(make(ByteBuffer::create_zeroed(0).release_value_but_fixme_should_propagate_errors())) { set_should_hide_unnecessary_scrollbars(true); set_focus_policy(GUI::FocusPolicy::StrongFocus); @@ -42,11 +41,10 @@ HexEditor::HexEditor() set_foreground_role(ColorRole::BaseText); vertical_scrollbar().set_step(line_height()); - m_blink_timer->set_interval(500); - m_blink_timer->on_timeout = [this]() { + m_blink_timer = Core::Timer::create_repeating(500, [this]() { m_cursor_blink_active = !m_cursor_blink_active; update(); - }; + }).release_value_but_fixme_should_propagate_errors(); m_blink_timer->start(); } diff --git a/Userland/Applications/HexEditor/HexEditor.h b/Userland/Applications/HexEditor/HexEditor.h index 8bb6ada27c..d7e085b49c 100644 --- a/Userland/Applications/HexEditor/HexEditor.h +++ b/Userland/Applications/HexEditor/HexEditor.h @@ -85,7 +85,7 @@ private: size_t m_position { 0 }; bool m_cursor_at_low_nibble { false }; EditMode m_edit_mode { Hex }; - NonnullRefPtr m_blink_timer; + RefPtr m_blink_timer; bool m_cursor_blink_active { false }; NonnullOwnPtr m_document; GUI::UndoStack m_undo_stack; diff --git a/Userland/Applications/Piano/main.cpp b/Userland/Applications/Piano/main.cpp index b8ad18c16c..1b72d7c78e 100644 --- a/Userland/Applications/Piano/main.cpp +++ b/Userland/Applications/Piano/main.cpp @@ -47,10 +47,10 @@ ErrorOr serenity_main(Main::Arguments arguments) window->resize(840, 600); window->set_icon(app_icon.bitmap_for_size(16)); - auto main_widget_updater = Core::Timer::construct(static_cast((1 / 30.0) * 1000), [&] { + auto main_widget_updater = TRY(Core::Timer::create_repeating(static_cast((1 / 30.0) * 1000), [&] { if (window->is_active()) Core::EventLoop::current().post_event(main_widget, make(0)); - }); + })); main_widget_updater->start(); auto& file_menu = window->add_menu("&File"); diff --git a/Userland/Applications/PixelPaint/Tools/SprayTool.cpp b/Userland/Applications/PixelPaint/Tools/SprayTool.cpp index a28bc03137..c1cf9f661b 100644 --- a/Userland/Applications/PixelPaint/Tools/SprayTool.cpp +++ b/Userland/Applications/PixelPaint/Tools/SprayTool.cpp @@ -22,11 +22,9 @@ namespace PixelPaint { SprayTool::SprayTool() { - m_timer = Core::Timer::construct(); - m_timer->on_timeout = [&]() { + m_timer = Core::Timer::create_repeating(200, [&]() { paint_it(); - }; - m_timer->set_interval(200); + }).release_value_but_fixme_should_propagate_errors(); } static double nrand() diff --git a/Userland/Applications/PixelPaint/Tools/TextTool.cpp b/Userland/Applications/PixelPaint/Tools/TextTool.cpp index b2d579bddd..f825ce224a 100644 --- a/Userland/Applications/PixelPaint/Tools/TextTool.cpp +++ b/Userland/Applications/PixelPaint/Tools/TextTool.cpp @@ -36,11 +36,9 @@ TextTool::TextTool() m_text_editor->set_wrapping_mode(GUI::TextEditor::WrappingMode::NoWrap); m_selected_font = Gfx::FontDatabase::default_font(); m_text_editor->set_font(m_selected_font); - m_cursor_blink_timer = Core::Timer::construct(); - m_cursor_blink_timer->on_timeout = [&]() { + m_cursor_blink_timer = Core::Timer::create_repeating(500, [&]() { m_cursor_blink_state = !m_cursor_blink_state; - }; - m_cursor_blink_timer->set_interval(500); + }).release_value_but_fixme_should_propagate_errors(); } void TextTool::on_tool_deactivation() diff --git a/Userland/Applications/SoundPlayer/PlaybackManager.cpp b/Userland/Applications/SoundPlayer/PlaybackManager.cpp index e929cf144d..bf7a00c1a4 100644 --- a/Userland/Applications/SoundPlayer/PlaybackManager.cpp +++ b/Userland/Applications/SoundPlayer/PlaybackManager.cpp @@ -11,11 +11,11 @@ PlaybackManager::PlaybackManager(NonnullRefPtr connec : m_connection(connection) { // FIXME: The buffer enqueuing should happen on a wholly independent second thread. - m_timer = Core::Timer::construct(PlaybackManager::update_rate_ms, [&]() { + m_timer = Core::Timer::create_repeating(PlaybackManager::update_rate_ms, [&]() { if (!m_loader) return; next_buffer(); - }); + }).release_value_but_fixme_should_propagate_errors(); m_device_sample_rate = connection->get_sample_rate(); } diff --git a/Userland/Demos/CatDog/main.cpp b/Userland/Demos/CatDog/main.cpp index b57dcbd5a6..6075cac907 100644 --- a/Userland/Demos/CatDog/main.cpp +++ b/Userland/Demos/CatDog/main.cpp @@ -66,17 +66,14 @@ ErrorOr serenity_main(Main::Arguments arguments) (void)TRY(advice_widget->try_set_layout()); advice_widget->layout()->set_spacing(0); - auto advice_timer = TRY(Core::Timer::try_create()); - advice_timer->set_interval(15'000); - advice_timer->set_single_shot(true); - advice_timer->on_timeout = [&] { + auto advice_timer = TRY(Core::Timer::create_single_shot(15'000, [&] { window->move_to_front(); advice_window->move_to_front(); catdog_widget->set_roaming(false); advice_window->move_to(window->x() - advice_window->width() / 2, window->y() - advice_window->height()); advice_window->show(); advice_window->set_always_on_top(); - }; + })); advice_timer->start(); advice_widget->on_dismiss = [&] { diff --git a/Userland/Demos/WidgetGallery/DemoWizardDialog.cpp b/Userland/Demos/WidgetGallery/DemoWizardDialog.cpp index a9156e8dbe..0cff4d365e 100644 --- a/Userland/Demos/WidgetGallery/DemoWizardDialog.cpp +++ b/Userland/Demos/WidgetGallery/DemoWizardDialog.cpp @@ -41,26 +41,28 @@ DemoWizardDialog::DemoWizardDialog(GUI::Window* parent_window) .release_value_but_fixme_should_propagate_errors(); m_page_2->body_widget().load_from_gml(demo_wizard_page_2_gml).release_value_but_fixme_should_propagate_errors(); m_page_2_progressbar = m_page_2->body_widget().find_descendant_of_type_named("page_2_progressbar"); - m_page_2_timer = Core::Timer::try_create(this).release_value_but_fixme_should_propagate_errors(); + m_page_2_timer = Core::Timer::create_repeating( + 100, [&]() { + if (m_page_2_progress_value < 100) + m_page_2_progress_value++; + m_page_2_progressbar->set_value(m_page_2_progress_value); + + // Go to final page on progress completion + if (m_page_2_progress_value == 100) { + m_page_2_progress_value = 0; + replace_page(*m_back_page); + } + }, + this) + .release_value_but_fixme_should_propagate_errors(); m_page_2->on_page_enter = [&]() { m_page_2_progress_value = 0; - m_page_2_timer->restart(100); + m_page_2_timer->restart(); }; m_page_2->on_page_leave = [&]() { m_page_2_progress_value = 0; m_page_2_timer->stop(); }; - m_page_2_timer->on_timeout = [&]() { - if (m_page_2_progress_value < 100) - m_page_2_progress_value++; - m_page_2_progressbar->set_value(m_page_2_progress_value); - - // Go to final page on progress completion - if (m_page_2_progress_value == 100) { - m_page_2_progress_value = 0; - replace_page(*m_back_page); - } - }; // Don't set a on_next_page handler for page 2 as we automatically navigate to the final page on progress completion // Create the back cover diff --git a/Userland/DevTools/Profiler/main.cpp b/Userland/DevTools/Profiler/main.cpp index 5660ed4a0d..3021605013 100644 --- a/Userland/DevTools/Profiler/main.cpp +++ b/Userland/DevTools/Profiler/main.cpp @@ -326,9 +326,9 @@ static bool prompt_to_stop_profiling(pid_t pid, DeprecatedString const& process_ auto& timer_label = widget->add("..."); Core::ElapsedTimer clock; clock.start(); - auto update_timer = Core::Timer::construct(100, [&] { + auto update_timer = Core::Timer::create_repeating(100, [&] { timer_label.set_text(DeprecatedString::formatted("{:.1} seconds", static_cast(clock.elapsed()) / 1000.0f)); - }); + }).release_value_but_fixme_should_propagate_errors(); update_timer->start(); auto& stop_button = widget->add("Stop"); diff --git a/Userland/Libraries/LibVideo/PlaybackManager.cpp b/Userland/Libraries/LibVideo/PlaybackManager.cpp index 725516dee4..77d881a8fb 100644 --- a/Userland/Libraries/LibVideo/PlaybackManager.cpp +++ b/Userland/Libraries/LibVideo/PlaybackManager.cpp @@ -33,16 +33,9 @@ PlaybackManager::PlaybackManager(Core::Object& event_handler, NonnullOwnPtr()) - , m_present_timer(Core::Timer::construct()) - , m_decode_timer(Core::Timer::construct()) { - m_present_timer->set_single_shot(true); - m_present_timer->set_interval(0); - m_present_timer->on_timeout = [&] { update_presented_frame(); }; - - m_decode_timer->set_single_shot(true); - m_decode_timer->set_interval(0); - m_decode_timer->on_timeout = [&] { on_decode_timer(); }; + m_present_timer = Core::Timer::create_single_shot(0, [&] { update_presented_frame(); }).release_value_but_fixme_should_propagate_errors(); + m_decode_timer = Core::Timer::create_single_shot(0, [&] { on_decode_timer(); }).release_value_but_fixme_should_propagate_errors(); } void PlaybackManager::set_playback_status(PlaybackStatus status) diff --git a/Userland/Libraries/LibVideo/PlaybackManager.h b/Userland/Libraries/LibVideo/PlaybackManager.h index 9fd77674b4..950c5be667 100644 --- a/Userland/Libraries/LibVideo/PlaybackManager.h +++ b/Userland/Libraries/LibVideo/PlaybackManager.h @@ -155,10 +155,10 @@ private: NonnullOwnPtr m_frame_queue; Optional m_next_frame; - NonnullRefPtr m_present_timer; + RefPtr m_present_timer; unsigned m_decoding_buffer_time_ms = 16; - NonnullRefPtr m_decode_timer; + RefPtr m_decode_timer; u64 m_skipped_frames; }; diff --git a/Userland/Services/WindowServer/WindowFrame.cpp b/Userland/Services/WindowServer/WindowFrame.cpp index 4bea34055a..74777c1d58 100644 --- a/Userland/Services/WindowServer/WindowFrame.cpp +++ b/Userland/Services/WindowServer/WindowFrame.cpp @@ -948,12 +948,12 @@ void WindowFrame::handle_menu_mouse_event(Menu& menu, MouseEvent const& event) void WindowFrame::start_flash_animation() { if (!m_flash_timer) { - m_flash_timer = Core::Timer::construct(100, [this] { + m_flash_timer = Core::Timer::create_repeating(100, [this] { VERIFY(m_flash_counter); invalidate_titlebar(); if (!--m_flash_counter) m_flash_timer->stop(); - }); + }).release_value_but_fixme_should_propagate_errors(); } m_flash_counter = 8; m_flash_timer->start();