From 7d0f70bfa0df485f26311238ea1054a99bcbf791 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 19 Jul 2023 11:05:37 +0100 Subject: [PATCH] Userland: Use AK::human_readable_digital_time() instead of custom code Use this handy AK function instead of reimplementing the formatting code several times. The one minor difference is that now, hours are only shown if the duration is at least an hour long, but that seems like an improvement to me. :^) --- .../Applications/SoundPlayer/PlaylistWidget.cpp | 8 ++------ Userland/Applications/SoundPlayer/PlaylistWidget.h | 1 - .../SoundPlayer/SoundPlayerWidgetAdvancedView.cpp | 3 ++- Userland/Games/Solitaire/main.cpp | 12 ++++-------- Userland/Games/Spider/main.cpp | 14 +++----------- 5 files changed, 11 insertions(+), 27 deletions(-) diff --git a/Userland/Applications/SoundPlayer/PlaylistWidget.cpp b/Userland/Applications/SoundPlayer/PlaylistWidget.cpp index cb6a5c00b0..3a43ece1ce 100644 --- a/Userland/Applications/SoundPlayer/PlaylistWidget.cpp +++ b/Userland/Applications/SoundPlayer/PlaylistWidget.cpp @@ -7,6 +7,7 @@ #include "PlaylistWidget.h" #include "Player.h" #include +#include #include #include #include @@ -37,7 +38,7 @@ GUI::Variant PlaylistModel::data(const GUI::ModelIndex& index, GUI::ModelRole ro case 0: return m_playlist_items[index.row()].extended_info->track_display_title.value_or(LexicalPath::title(m_playlist_items[index.row()].path)); case 1: - return format_duration(m_playlist_items[index.row()].extended_info->track_length_in_seconds.value_or(0)); + return human_readable_digital_time(m_playlist_items[index.row()].extended_info->track_length_in_seconds.value_or(0)); case 2: return m_playlist_items[index.row()].extended_info->group_name.value_or(""); case 3: @@ -68,11 +69,6 @@ DeprecatedString PlaylistModel::format_filesize(u64 size_in_bytes) return DeprecatedString::formatted("{} B", size_in_bytes); } -DeprecatedString PlaylistModel::format_duration(u32 duration_in_seconds) -{ - return DeprecatedString::formatted("{:02}:{:02}:{:02}", duration_in_seconds / 3600, (duration_in_seconds / 60) % 60, duration_in_seconds % 60); -} - ErrorOr PlaylistModel::column_name(int column) const { switch (column) { diff --git a/Userland/Applications/SoundPlayer/PlaylistWidget.h b/Userland/Applications/SoundPlayer/PlaylistWidget.h index 89c4ea0f0a..2bdf422ebe 100644 --- a/Userland/Applications/SoundPlayer/PlaylistWidget.h +++ b/Userland/Applications/SoundPlayer/PlaylistWidget.h @@ -31,7 +31,6 @@ private: Vector m_playlist_items; static DeprecatedString format_filesize(u64 size_in_bytes); - static DeprecatedString format_duration(u32 duration_in_seconds); }; class PlaylistTableView : public GUI::TableView { diff --git a/Userland/Applications/SoundPlayer/SoundPlayerWidgetAdvancedView.cpp b/Userland/Applications/SoundPlayer/SoundPlayerWidgetAdvancedView.cpp index 57345df9e7..3de28edc23 100644 --- a/Userland/Applications/SoundPlayer/SoundPlayerWidgetAdvancedView.cpp +++ b/Userland/Applications/SoundPlayer/SoundPlayerWidgetAdvancedView.cpp @@ -13,6 +13,7 @@ #include "SampleWidget.h" #include #include +#include #include #include #include @@ -243,7 +244,7 @@ void SoundPlayerWidgetAdvancedView::shuffle_mode_changed(Player::ShuffleMode) void SoundPlayerWidgetAdvancedView::time_elapsed(int seconds) { - m_timestamp_label->set_text(String::formatted("Elapsed: {:02}:{:02}:{:02}", seconds / 3600, seconds / 60, seconds % 60).release_value_but_fixme_should_propagate_errors()); + m_timestamp_label->set_text(String::formatted("Elapsed: {}", human_readable_digital_time(seconds)).release_value_but_fixme_should_propagate_errors()); } void SoundPlayerWidgetAdvancedView::file_name_changed(StringView name) diff --git a/Userland/Games/Solitaire/main.cpp b/Userland/Games/Solitaire/main.cpp index 59d36e5e7e..77c993e304 100644 --- a/Userland/Games/Solitaire/main.cpp +++ b/Userland/Games/Solitaire/main.cpp @@ -7,6 +7,7 @@ */ #include "Game.h" +#include #include #include #include @@ -92,7 +93,7 @@ ErrorOr serenity_main(Main::Arguments arguments) auto& statusbar = *widget->find_descendant_of_type_named("statusbar"); statusbar.set_text(0, TRY("Score: 0"_string)); statusbar.set_text(1, TRY(String::formatted("High Score: {}", high_score()))); - statusbar.set_text(2, TRY("Time: 00:00:00"_string)); + statusbar.set_text(2, TRY("Time: 00:00"_string)); app->on_action_enter = [&](GUI::Action& action) { statusbar.set_override_text(action.status_tip()); @@ -110,18 +111,13 @@ ErrorOr serenity_main(Main::Arguments arguments) auto timer = TRY(Core::Timer::create_repeating(1000, [&]() { ++seconds_elapsed; - - uint64_t hours = seconds_elapsed / 3600; - uint64_t minutes = (seconds_elapsed / 60) % 60; - uint64_t seconds = seconds_elapsed % 60; - - statusbar.set_text(2, String::formatted("Time: {:02}:{:02}:{:02}", hours, minutes, seconds).release_value_but_fixme_should_propagate_errors()); + statusbar.set_text(2, String::formatted("Time: {}", human_readable_digital_time(seconds_elapsed)).release_value_but_fixme_should_propagate_errors()); })); game.on_game_start = [&]() { seconds_elapsed = 0; timer->start(); - statusbar.set_text(2, "Time: 00:00:00"_string.release_value_but_fixme_should_propagate_errors()); + statusbar.set_text(2, "Time: 00:00"_string.release_value_but_fixme_should_propagate_errors()); }; game.on_game_end = [&](Solitaire::GameOverReason reason, uint32_t score) { if (timer->is_active()) diff --git a/Userland/Games/Spider/main.cpp b/Userland/Games/Spider/main.cpp index 7b1d7ca7b7..a7af3676a1 100644 --- a/Userland/Games/Spider/main.cpp +++ b/Userland/Games/Spider/main.cpp @@ -7,6 +7,7 @@ */ #include "Game.h" +#include #include #include #include @@ -29,15 +30,6 @@ enum class StatisticDisplay : u8 { __Count }; -static DeprecatedString format_seconds(uint64_t seconds_elapsed) -{ - uint64_t hours = seconds_elapsed / 3600; - uint64_t minutes = (seconds_elapsed / 60) % 60; - uint64_t seconds = seconds_elapsed % 60; - - return DeprecatedString::formatted("{:02}:{:02}:{:02}", hours, minutes, seconds); -} - ErrorOr serenity_main(Main::Arguments arguments) { TRY(Core::System::pledge("stdio recvfd sendfd rpath unix proc exec")); @@ -130,7 +122,7 @@ ErrorOr serenity_main(Main::Arguments arguments) statusbar.set_text(1, String::formatted("High Score: {}", high_score()).release_value_but_fixme_should_propagate_errors()); break; case StatisticDisplay::BestTime: - statusbar.set_text(1, String::formatted("Best Time: {}", format_seconds(best_time())).release_value_but_fixme_should_propagate_errors()); + statusbar.set_text(1, String::formatted("Best Time: {}", human_readable_digital_time(best_time())).release_value_but_fixme_should_propagate_errors()); break; default: VERIFY_NOT_REACHED(); @@ -158,7 +150,7 @@ ErrorOr serenity_main(Main::Arguments arguments) auto timer = TRY(Core::Timer::create_repeating(1000, [&]() { ++seconds_elapsed; - statusbar.set_text(2, String::formatted("Time: {}", format_seconds(seconds_elapsed)).release_value_but_fixme_should_propagate_errors()); + statusbar.set_text(2, String::formatted("Time: {}", human_readable_digital_time(seconds_elapsed)).release_value_but_fixme_should_propagate_errors()); })); game.on_game_start = [&]() {