1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 09:58:14 +00:00

VideoPlayer: Port to GML

This commit is contained in:
implicitfield 2023-01-08 17:35:22 +02:00 committed by Sam Atkins
parent f0a33b2a8e
commit f63ecc2763
7 changed files with 95 additions and 42 deletions

View file

@ -4,11 +4,17 @@ serenity_component(
DEPENDS AudioServer
)
compile_gml(VideoPlayerWindow.gml VideoPlayerWindowGML.h videoplayer_window_gml)
set(SOURCES
main.cpp
VideoFrameWidget.cpp
VideoPlayerWidget.cpp
)
set(GENERATED_SOURCES
VideoPlayerWindowGML.h
)
serenity_app(VideoPlayer ICON app-video-player)
target_link_libraries(VideoPlayer PRIVATE LibVideo LibAudio LibCore LibGfx LibGUI LibMain)

View file

@ -9,10 +9,13 @@
#include "VideoFrameWidget.h"
REGISTER_WIDGET(VideoPlayer, VideoFrameWidget);
namespace VideoPlayer {
VideoFrameWidget::VideoFrameWidget()
{
REGISTER_BOOL_PROPERTY("auto_resize", auto_resize, set_auto_resize);
set_auto_resize(true);
}

View file

@ -20,7 +20,7 @@ enum class VideoSizingMode : u8 {
Sentinel
};
class VideoFrameWidget : public GUI::Frame {
class VideoFrameWidget final : public GUI::Frame {
C_OBJECT(VideoFrameWidget)
public:
virtual ~VideoFrameWidget() override = default;

View file

@ -15,29 +15,28 @@
#include <LibGUI/Toolbar.h>
#include <LibGUI/ToolbarContainer.h>
#include <LibGUI/Window.h>
#include <Userland/Applications/VideoPlayer/VideoPlayerWindowGML.h>
#include "VideoPlayerWidget.h"
namespace VideoPlayer {
VideoPlayerWidget::VideoPlayerWidget(GUI::Window& window)
: m_window(window)
ErrorOr<NonnullRefPtr<VideoPlayerWidget>> VideoPlayerWidget::try_create()
{
set_fill_with_background_color(true);
auto main_widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) VideoPlayerWidget()));
TRY(main_widget->load_from_gml(videoplayer_window_gml));
set_layout<GUI::VerticalBoxLayout>();
TRY(main_widget->setup_interface());
m_video_display = add<VideoFrameWidget>();
m_video_display->set_auto_resize(false);
return main_widget;
}
ErrorOr<void> VideoPlayerWidget::setup_interface()
{
m_video_display = find_descendant_of_type_named<VideoPlayer::VideoFrameWidget>("video_frame");
m_video_display->on_click = [&]() { toggle_pause(); };
auto& player_controls_widget = add<GUI::Widget>();
player_controls_widget.set_layout<GUI::VerticalBoxLayout>();
player_controls_widget.set_max_height(50);
m_seek_slider = player_controls_widget.add<GUI::HorizontalSlider>();
m_seek_slider->set_fixed_height(20);
m_seek_slider->set_enabled(false);
m_seek_slider = find_descendant_of_type_named<GUI::HorizontalSlider>("seek_slider");
m_seek_slider->on_change = [&](int value) {
if (!m_playback_manager)
return;
@ -48,13 +47,9 @@ VideoPlayerWidget::VideoPlayerWidget(GUI::Window& window)
set_current_timestamp(timestamp);
m_playback_manager->seek_to_timestamp(timestamp);
};
m_seek_slider->set_jump_to_cursor(true);
auto& toolbar_container = player_controls_widget.add<GUI::ToolbarContainer>();
m_toolbar = toolbar_container.add<GUI::Toolbar>();
m_play_icon = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/play.png"sv).release_value_but_fixme_should_propagate_errors();
m_pause_icon = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/pause.png"sv).release_value_but_fixme_should_propagate_errors();
m_play_icon = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/play.png"sv));
m_pause_icon = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/pause.png"sv));
m_play_pause_action = GUI::Action::create("Play", { Key_Space }, m_play_icon, [&](auto&) {
toggle_pause();
@ -64,20 +59,12 @@ VideoPlayerWidget::VideoPlayerWidget(GUI::Window& window)
cycle_sizing_modes();
});
m_toolbar->add_action(*m_play_pause_action);
m_toolbar->add<GUI::VerticalSeparator>();
m_timestamp_label = m_toolbar->add<GUI::Label>();
m_timestamp_label->set_autosize(true);
m_timestamp_label = find_descendant_of_type_named<GUI::Label>("timestamp");
m_volume_slider = find_descendant_of_type_named<GUI::HorizontalSlider>("volume_slider");
find_descendant_of_type_named<GUI::Button>("playback")->set_action(*m_play_pause_action);
find_descendant_of_type_named<GUI::Button>("sizing")->set_action(*m_cycle_sizing_modes_action);
m_toolbar->add<GUI::Widget>(); // Filler widget
m_toolbar->add_action(*m_cycle_sizing_modes_action);
m_toolbar->add<GUI::VerticalSeparator>();
m_volume_slider = m_toolbar->add<GUI::HorizontalSlider>();
m_volume_slider->set_min(0);
m_volume_slider->set_max(100);
m_volume_slider->set_fixed_width(100);
return {};
}
void VideoPlayerWidget::close_file()
@ -176,7 +163,7 @@ void VideoPlayerWidget::on_decoding_error(Video::DecoderError const& error)
break;
}
GUI::MessageBox::show(&m_window, DeprecatedString::formatted(text_format, error.string_literal()), "Video Player encountered an error"sv);
GUI::MessageBox::show(window(), DeprecatedString::formatted(text_format, error.string_literal()), "Video Player encountered an error"sv);
}
void VideoPlayerWidget::update_seek_slider_max()

View file

@ -20,9 +20,11 @@
namespace VideoPlayer {
class VideoPlayerWidget final : public GUI::Widget {
C_OBJECT(VideoPlayerWidget)
C_OBJECT_ABSTRACT(VideoPlayerWidget)
public:
static ErrorOr<NonnullRefPtr<VideoPlayerWidget>> try_create();
virtual ~VideoPlayerWidget() override = default;
void close_file();
void open_file(StringView filename);
void resume_playback();
@ -37,8 +39,8 @@ public:
ErrorOr<void> initialize_menubar(GUI::Window&);
private:
VideoPlayerWidget(GUI::Window&);
VideoPlayerWidget() = default;
ErrorOr<void> setup_interface();
void update_play_pause_icon();
void update_seek_slider_max();
void set_current_timestamp(Time);
@ -51,15 +53,11 @@ private:
void event(Core::Event&) override;
GUI::Window& m_window;
DeprecatedString m_path;
RefPtr<VideoFrameWidget> m_video_display;
RefPtr<GUI::HorizontalSlider> m_seek_slider;
RefPtr<GUI::Toolbar> m_toolbar;
RefPtr<Gfx::Bitmap> m_play_icon;
RefPtr<Gfx::Bitmap> m_pause_icon;

View file

@ -0,0 +1,59 @@
@GUI::Widget {
fill_with_background_color: true
layout: @GUI::VerticalBoxLayout {}
@VideoPlayer::VideoFrameWidget {
name: "video_frame"
auto_resize: false
}
@GUI::Widget {
max_height: 50
layout: @GUI::VerticalBoxLayout {}
@GUI::HorizontalSlider {
name: "seek_slider"
fixed_height: 20
enabled: false
jump_to_cursor: true
}
@GUI::ToolbarContainer {
@GUI::Toolbar {
name: "toolbar"
@GUI::Button {
name: "playback"
icon: "/res/icons/16x16/play.png"
fixed_width: 24
button_style: "Coolbar"
}
@GUI::VerticalSeparator {}
@GUI::Label {
name: "timestamp"
autosize: true
}
@GUI::Layout::Spacer {}
@GUI::Button {
name: "sizing"
text: "..."
fixed_width: 24
button_style: "Coolbar"
}
@GUI::VerticalSeparator {}
@GUI::HorizontalSlider {
name: "volume_slider"
min: 0
max: 100
fixed_width: 100
}
}
}
}
}

View file

@ -24,7 +24,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
window->resize(640, 480);
window->set_resizable(true);
auto main_widget = TRY(window->set_main_widget<VideoPlayer::VideoPlayerWidget>(window));
auto main_widget = TRY(window->set_main_widget<VideoPlayer::VideoPlayerWidget>());
main_widget->update_title();
TRY(main_widget->initialize_menubar(window));