Repository: SerenityOS/serenity
File: Userland/Demos/CatDog/main.cpp (C++)

Code before:

    window->show();
    window->set_always_on_top();
    catdog_widget->start_timer(250, Core::TimerShouldFireWhenNotVisible::Yes);

    auto advice_window = GUI::Window::construct();
    advice_window->set_title("CatDog Advice");
    advice_window->resize(225, 50);
    advice_window->set_frameless(true);
    advice_window->set_resizable(false);
    advice_window->set_has_alpha_channel(true);
    advice_window->set_alpha_hit_threshold(1.0f);

    auto advice_widget = advice_window->set_main_widget<SpeechBubble>(catdog_widget);
    advice_widget->set_layout<GUI::VerticalBoxLayout>(GUI::Margins {}, 0);

    auto advice_timer = Core::Timer::create_single_shot(15'000, [&] {
        window->move_to_front();
        advice_window->move_to_front();
        catdog_widget->set_roaming(false);
        int advice_window_x = window->x() - advice_window->width() / 2;
        int advice_window_y = window->y() - advice_window->height();
        if (advice_window_x < 0)
            advice_window_x = 0;
        if (advice_window_x + advice_window->width() > screen.width())
            advice_window_x = screen.width() - advice_window->width();
        if (advice_window_y < 0)
            advice_window_y = window->height() + window->y();
        advice_window->move_to(advice_window_x, advice_window_y);
        advice_window->show();
        advice_window->set_always_on_top();
    });
    advice_timer->start();

    advice_widget->on_dismiss = [&] {
        catdog_widget->set_roaming(true);
        advice_window->hide();
        advice_timer->start();
    };

    catdog_widget->on_state_change = [&] {
        sleep_action->set_text(catdog_widget->is_sleeping() ? "Wake CatDog..." : "Put CatDog to sleep...");
        sleep_action->set_icon(catdog_widget->is_sleeping() ? catdog_icon_wake : catdog_icon_sleep);

        // Reset advice timer to prevent waking too quickly.
        if (catdog_widget->is_sleeping())
            advice_timer->start();
    };

    // Let users toggle the advice functionality by clicking on catdog.
    catdog_widget->on_click = [&] {

Review comment (suggestion):
This is a little bit shorter:
```cpp
advice_window_x = clamp(advice_window_x, 0, screen.width() - advice_window->width());
```

Code after:

    window->show();
    window->set_always_on_top();
    catdog_widget->start_timer(250, Core::TimerShouldFireWhenNotVisible::Yes);

    auto advice_window = GUI::Window::construct();
    advice_window->set_title("CatDog Advice");
    advice_window->resize(225, 50);
    advice_window->set_frameless(true);
    advice_window->set_resizable(false);
    advice_window->set_has_alpha_channel(true);
    advice_window->set_alpha_hit_threshold(1.0f);

    auto advice_widget = advice_window->set_main_widget<SpeechBubble>(catdog_widget);
    advice_widget->set_layout<GUI::VerticalBoxLayout>(GUI::Margins {}, 0);

    auto advice_timer = Core::Timer::create_single_shot(15'000, [&] {
        window->move_to_front();
        advice_window->move_to_front();
        catdog_widget->set_roaming(false);

        static constexpr auto minimum_required_margin = 5;
        int advice_window_x = window->x() - advice_window->width() / 2 - minimum_required_margin;
        int advice_window_y = window->y() - advice_window->height();
        advice_window_x = clamp(advice_window_x, minimum_required_margin, screen.width() - advice_window->width() - minimum_required_margin);
        if (advice_window_y < minimum_required_margin)
            advice_window_y = window->height() + window->y();
        advice_window->move_to(advice_window_x, advice_window_y);
        advice_window->show();
        advice_window->set_always_on_top();
    });
    advice_timer->start();

    advice_widget->on_dismiss = [&] {
        catdog_widget->set_roaming(true);
        advice_window->hide();
        advice_timer->start();
    };

    catdog_widget->on_state_change = [&] {
        sleep_action->set_text(catdog_widget->is_sleeping() ? "Wake CatDog..." : "Put CatDog to sleep...");
        sleep_action->set_icon(catdog_widget->is_sleeping() ? catdog_icon_wake : catdog_icon_sleep);

        // Reset advice timer to prevent waking too quickly.
        if (catdog_widget->is_sleeping())
            advice_timer->start();
    };

    // Let users toggle the advice functionality by clicking on catdog.
    catdog_widget->on_click = [&] {
        if (advice_timer->is_active())