Everywhere: Use ElapsedTimer::elapsed_time() for comparisons
Simplify a lot of uses of ElapsedTimer by converting the callers to elapsed_time from elapsed, as the AK::Time returned is better for unit conversions and comparisons against constants.
This commit is contained in:
parent
4afa6e264c
commit
ddf348daeb
Notes:
sideshowbarker
2024-07-17 02:00:06 +09:00
Author: https://github.com/ADKaster Commit: https://github.com/SerenityOS/serenity/commit/ddf348daeb Pull-request: https://github.com/SerenityOS/serenity/pull/16771 Reviewed-by: https://github.com/kleinesfilmroellchen Reviewed-by: https://github.com/linusg ✅
8 changed files with 22 additions and 12 deletions
|
@ -22,7 +22,7 @@ public:
|
|||
|
||||
static void test_signal_handler(int signal)
|
||||
{
|
||||
auto actual_duration = Time::from_milliseconds(SuccessContext::signal_timer.elapsed());
|
||||
auto actual_duration = SuccessContext::signal_timer.elapsed_time();
|
||||
auto expected_duration = SuccessContext::timer_value;
|
||||
|
||||
// Add a small buffer to allow for latency on the system.
|
||||
|
|
|
@ -128,7 +128,7 @@ void DownloadWidget::did_progress(Optional<u32> total_size, u32 downloaded_size)
|
|||
StringBuilder builder;
|
||||
builder.append("Downloaded "sv);
|
||||
builder.append(human_readable_size(downloaded_size));
|
||||
builder.appendff(" in {} sec", m_elapsed_timer.elapsed() / 1000);
|
||||
builder.appendff(" in {} sec", m_elapsed_timer.elapsed_time().to_seconds());
|
||||
m_progress_label->set_text(builder.to_deprecated_string());
|
||||
}
|
||||
|
||||
|
|
|
@ -141,13 +141,13 @@ void FileOperationProgressWidget::did_error(StringView message)
|
|||
|
||||
DeprecatedString FileOperationProgressWidget::estimate_time(off_t bytes_done, off_t total_byte_count)
|
||||
{
|
||||
int elapsed = m_elapsed_timer.elapsed() / 1000;
|
||||
i64 const elapsed_seconds = m_elapsed_timer.elapsed_time().to_seconds();
|
||||
|
||||
if (bytes_done == 0 || elapsed < 3)
|
||||
if (bytes_done == 0 || elapsed_seconds < 3)
|
||||
return "Estimating...";
|
||||
|
||||
off_t bytes_left = total_byte_count - bytes_done;
|
||||
int seconds_remaining = (bytes_left * elapsed) / bytes_done;
|
||||
int seconds_remaining = (bytes_left * elapsed_seconds) / bytes_done;
|
||||
|
||||
if (seconds_remaining < 30)
|
||||
return DeprecatedString::formatted("{} seconds", 5 + seconds_remaining - seconds_remaining % 5);
|
||||
|
|
|
@ -100,6 +100,8 @@ bool CatDog::is_inspector() const
|
|||
|
||||
void CatDog::timer_event(Core::TimerEvent&)
|
||||
{
|
||||
using namespace AK::TimeLiterals;
|
||||
|
||||
if (has_flag(m_state, State::Alert))
|
||||
return;
|
||||
|
||||
|
@ -127,7 +129,7 @@ void CatDog::timer_event(Core::TimerEvent&)
|
|||
if (has_any_flag(m_state, State::Directions)) {
|
||||
m_idle_sleep_timer.start();
|
||||
} else {
|
||||
if (m_idle_sleep_timer.elapsed() > 5'000)
|
||||
if (m_idle_sleep_timer.elapsed_time() > 5_sec)
|
||||
m_state |= State::Sleeping;
|
||||
else
|
||||
m_state |= State::Idle;
|
||||
|
|
|
@ -192,11 +192,13 @@ ConnectionToServerWrapper* ConnectionToServerInstances::get_instance_wrapper(Dep
|
|||
|
||||
void ConnectionToServerWrapper::on_crash()
|
||||
{
|
||||
using namespace AK::TimeLiterals;
|
||||
|
||||
show_crash_notification();
|
||||
m_connection.clear();
|
||||
|
||||
static constexpr int max_crash_frequency_seconds = 10;
|
||||
if (m_last_crash_timer.is_valid() && m_last_crash_timer.elapsed() / 1000 < max_crash_frequency_seconds) {
|
||||
static constexpr Time max_crash_frequency = 10_sec;
|
||||
if (m_last_crash_timer.is_valid() && m_last_crash_timer.elapsed_time() < max_crash_frequency) {
|
||||
dbgln("LanguageServer crash frequency is too high");
|
||||
m_respawn_allowed = false;
|
||||
|
||||
|
|
|
@ -252,6 +252,8 @@ void TextEditor::doubleclick_event(MouseEvent& event)
|
|||
|
||||
void TextEditor::mousedown_event(MouseEvent& event)
|
||||
{
|
||||
using namespace AK::TimeLiterals;
|
||||
|
||||
if (event.button() != MouseButton::Primary) {
|
||||
return;
|
||||
}
|
||||
|
@ -262,7 +264,7 @@ void TextEditor::mousedown_event(MouseEvent& event)
|
|||
if (is_displayonly())
|
||||
return;
|
||||
|
||||
if (m_triple_click_timer.is_valid() && m_triple_click_timer.elapsed() < 250) {
|
||||
if (m_triple_click_timer.is_valid() && m_triple_click_timer.elapsed_time() < 250_ms) {
|
||||
m_triple_click_timer = Core::ElapsedTimer();
|
||||
select_current_line();
|
||||
return;
|
||||
|
|
|
@ -795,6 +795,8 @@ void TerminalWidget::mouseup_event(GUI::MouseEvent& event)
|
|||
|
||||
void TerminalWidget::mousedown_event(GUI::MouseEvent& event)
|
||||
{
|
||||
using namespace AK::TimeLiterals;
|
||||
|
||||
if (event.button() == GUI::MouseButton::Primary) {
|
||||
m_left_mousedown_position = event.position();
|
||||
m_left_mousedown_position_buffer = buffer_position_at(m_left_mousedown_position);
|
||||
|
@ -809,7 +811,7 @@ void TerminalWidget::mousedown_event(GUI::MouseEvent& event)
|
|||
m_active_href = {};
|
||||
m_active_href_id = {};
|
||||
|
||||
if (m_triple_click_timer.is_valid() && m_triple_click_timer.elapsed() < 250) {
|
||||
if (m_triple_click_timer.is_valid() && m_triple_click_timer.elapsed_time() < 250_ms) {
|
||||
int start_column = 0;
|
||||
int end_column = m_terminal.columns() - 1;
|
||||
|
||||
|
|
|
@ -243,6 +243,8 @@ void Service::spawn(int socket_fd)
|
|||
|
||||
void Service::did_exit(int exit_code)
|
||||
{
|
||||
using namespace AK::TimeLiterals;
|
||||
|
||||
VERIFY(m_pid > 0);
|
||||
VERIFY(!m_multi_instance);
|
||||
|
||||
|
@ -254,10 +256,10 @@ void Service::did_exit(int exit_code)
|
|||
if (!m_keep_alive)
|
||||
return;
|
||||
|
||||
int run_time_in_msec = m_run_timer.elapsed();
|
||||
auto run_time = m_run_timer.elapsed_time();
|
||||
bool exited_successfully = exit_code == 0;
|
||||
|
||||
if (!exited_successfully && run_time_in_msec < 1000) {
|
||||
if (!exited_successfully && run_time < 1_sec) {
|
||||
switch (m_restart_attempts) {
|
||||
case 0:
|
||||
dbgln("Trying again");
|
||||
|
|
Loading…
Add table
Reference in a new issue