Skip to content

Truly independent web browser

1.7 KB
Raw Blame
/*
 * Copyright (c) 2026-present, the Ladybird developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Function.h>
#include <AK/String.h>
#include <AK/kmalloc.h>
#include <LibGfx/Size.h>
#include <LibWebView/Forward.h>

namespace WebView {

// The floating window that shows a page's Picture-in-Picture video, and keeps that video's aspect ratio. Sizes are in
// CSS pixels.
class WEBVIEW_API PictureInPictureWindow {
public:
    AK_ALLOC_WITH_KMALLOC;

    static Gfx::IntSize aspect_ratio(Gfx::IntSize video_size);
    static Gfx::IntSize minimum_size(Gfx::IntSize video_size);
    static Gfx::IntSize maximum_size(Gfx::IntSize video_size, Gfx::IntSize screen_size);
    static Gfx::IntSize initial_size(Gfx::IntSize video_size, Gfx::IntSize screen_size);
    // The window keeps its width as its video changes shape, as far as its minimum and maximum sizes allow.
    static Gfx::IntSize size_for_video_size(Gfx::IntSize window_size, Gfx::IntSize video_size, Gfx::IntSize screen_size);

    virtual ~PictureInPictureWindow() = default;

    virtual Gfx::IntSize size() const = 0;
    virtual String handle() const = 0;
    virtual void hide() = 0;
    virtual void set_video_size(Gfx::IntSize) = 0;

    void close_page();
    void act_for_document(CanonicalDocument const&);
    void leave_owners_tab();

    // The user closes the window to stop watching its video, or to go back to the video's tab and watch it there.
    enum class VideoKeepsPlaying : bool {
        No,
        Yes,
    };

    Function<void(Gfx::IntSize)> on_resize;
    Function<void(VideoKeepsPlaying)> on_close;
    Function<void()> on_page_close;

protected:
    void report_page_close_of(ViewImplementation&);

private:
    ViewImplementation* m_page_view { nullptr };
};

}