dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / html / media_controls_2.lucb
#==============================================================================================
#
# media_controls_2 - HTML::MediaControls: the controls' actions and the state they show
#
# DESCRIPTION:
# Port of LibWeb/HTML/MediaControls.cpp lines 430-639: toggling playback, setting the
# current time and the volume, muting, fullscreen, and updating the play / pause icon,
# the timeline, the timestamp, the volume and mute indicator, the fullscreen icon and the
# placeholder, and showing and hiding the controls on a hover timer.
#
#==============================================================================================
# mark: Actions =================================================================================
## Ported from MediaControls.cpp:434 Web::HTML::MediaControls::toggle_playback.
pub func html_media_controls_toggle_playback(this: HtmlMediaControls*):
if this.m_scrubbing_timeline != HtmlMediaControlsScrubbing.no:
return
html_media_element_toggle_playback(html_media_controls_media_element(this))
html_media_controls_show_controls(this)
## Ported from MediaControls.cpp:442 Web::HTML::MediaControls::set_current_time.
pub func html_media_controls_set_current_time(this: HtmlMediaControls*, time: f64):
html_media_element_set_current_time(html_media_controls_media_element(this), time)
html_media_controls_update_timeline(this)
html_media_controls_update_timestamp(this)
html_media_controls_show_controls(this)
## Ported from MediaControls.cpp:450 Web::HTML::MediaControls::set_volume.
pub func html_media_controls_set_volume(this: HtmlMediaControls*, volume: f64):
let clamped_volume = ak.clamp_f64(volume, 0.0, 1.0)
let media_element = html_media_controls_media_element(this)
html_media_element_set_volume(media_element, clamped_volume) else trap("MUST: set_volume")
html_media_element_set_muted(media_element, false)
html_media_controls_show_controls(this)
## Ported from MediaControls.cpp:458 Web::HTML::MediaControls::toggle_mute.
pub func html_media_controls_toggle_mute(this: HtmlMediaControls*):
let media_element = html_media_controls_media_element(this)
html_media_element_set_muted(media_element, not html_media_element_muted(media_element))
html_media_controls_show_controls(this)
## Ported from MediaControls.cpp:464 Web::HTML::MediaControls::toggle_fullscreen.
pub func html_media_controls_toggle_fullscreen(this: HtmlMediaControls*):
html_media_element_toggle_fullscreen(html_media_controls_media_element(this))
# mark: The state the controls show =============================================================
## Ported from MediaControls.cpp:470 Web::HTML::MediaControls::update_play_pause_icon.
pub func html_media_controls_update_play_pause_icon(this: HtmlMediaControls*):
let media_element = html_media_controls_media_element(this)
let play_pause_icon = html_media_controls_element(html_media_controls_dom(this).play_pause_icon)
var paused = false
if this.m_scrubbing_timeline != HtmlMediaControlsScrubbing.no:
paused = this.m_scrubbing_timeline == HtmlMediaControlsScrubbing.while_paused
else:
paused = html_media_element_paused(media_element)
_ = dom_token_list_toggle(dom_element_class_list(play_pause_icon), ak.string_from_str("playing"), not paused) else trap("MUST: toggle the playing class")
## Ported from MediaControls.cpp:485 Web::HTML::MediaControls::update_timeline.
pub func html_media_controls_update_timeline(this: HtmlMediaControls*):
let media_element = html_media_controls_media_element(this)
let timeline_fill = html_media_controls_element(html_media_controls_dom(this).timeline_fill)
let duration = html_media_element_duration(media_element)
var percentage = 0.0
if not math.is_nan(duration) and duration > 0.0:
percentage = (html_media_element_current_time(media_element) / duration) * 100.0
if this.m_last_timeline_percentage == percentage:
return
# MUST(String::formatted("{}%", percentage))
var builder = ak.string_builder_new()
ak.format_f64(&builder, percentage)
ak.string_builder_append(&builder, "%")
let width = ak.string_builder_to_string(&builder) else trap("MUST: String::formatted")
css_style_properties_set_property_property_id(dom_element_style_for_bindings(timeline_fill), css_data.PropertyId.width, ak.string_bytes_as_string_view(&width), ak.sv("")) else trap("MUST: set_property")
this.m_last_timeline_percentage = percentage
## Ported from MediaControls.cpp:502 Web::HTML::MediaControls::update_timestamp.
pub func html_media_controls_update_timestamp(this: HtmlMediaControls*):
let media_element = html_media_controls_media_element(this)
let timestamp_element = html_media_controls_element(html_media_controls_dom(this).timestamp_element)
let current = ak.human_readable_digital_time(ak.rounding_round_to_f64_i64(html_media_element_current_time(media_element)))
let duration = html_media_element_duration(media_element)
let total = ak.human_readable_digital_time(0 if math.is_nan(duration) else ak.rounding_round_to_f64_i64(duration))
# Utf16String::formatted("{} / {}", current, total)
var builder = ak.string_builder_new()
ak.string_builder_append_string_view(&builder, ak.string_bytes_as_string_view(¤t))
ak.string_builder_append(&builder, " / ")
ak.string_builder_append_string_view(&builder, ak.string_bytes_as_string_view(&total))
let text = ak.string_builder_to_string(&builder) else trap("MUST: Utf16String::formatted")
dom_node_set_text_content(timestamp_element.dom_node(), ak.utf16_string_from_utf8_string(&text)) else trap("MUST: set_text_content")
## update_volume_and_mute_indicator's `static constexpr auto icon_class = [](MuteIconState state) { ... }`.
func html_media_controls_icon_class(state: HtmlMediaControlsMuteIconState) -> ak.Vector[ak.String]:
match state:
.empty:
return ak.Vector[ak.String]()
.low:
return html_media_controls_class_list_of("low")
.high:
return html_media_controls_class_list_of("high")
_:
trap("VERIFY_NOT_REACHED: MuteIconState")
## Ported from MediaControls.cpp:514 Web::HTML::MediaControls::update_volume_and_mute_indicator.
pub func html_media_controls_update_volume_and_mute_indicator(this: HtmlMediaControls*):
let media_element = html_media_controls_media_element(this)
let dom = html_media_controls_dom(this)
let volume_fill = html_media_controls_element(dom.volume_fill)
let mute_button = html_media_controls_element(dom.mute_button)
let volume = html_media_element_volume(media_element)
let has_audio = html_audio_track_list_length(html_media_element_audio_tracks(media_element)) > 0
let muted = not has_audio or html_media_element_muted(media_element)
if muted:
css_style_properties_set_property_property_id(dom_element_style_for_bindings(volume_fill), css_data.PropertyId.width, ak.sv("0"), ak.sv("")) else trap("MUST: set_property")
else:
let percentage = volume * 100.0
# MUST(String::formatted("{}%", percentage))
var builder = ak.string_builder_new()
ak.format_f64(&builder, percentage)
ak.string_builder_append(&builder, "%")
let width = ak.string_builder_to_string(&builder) else trap("MUST: String::formatted")
css_style_properties_set_property_property_id(dom_element_style_for_bindings(volume_fill), css_data.PropertyId.width, ak.string_bytes_as_string_view(&width), ak.sv("")) else trap("MUST: set_property")
var new_volume_icon_state = HtmlMediaControlsMuteIconState.empty
if volume > 0.5:
new_volume_icon_state = HtmlMediaControlsMuteIconState.high
elif volume > 0.0:
new_volume_icon_state = HtmlMediaControlsMuteIconState.low
if new_volume_icon_state != this.m_mute_icon_state:
dom_token_list_remove(dom_element_class_list(mute_button), html_media_controls_icon_class(this.m_mute_icon_state)) else trap("MUST: remove the volume class")
dom_token_list_add(dom_element_class_list(mute_button), html_media_controls_icon_class(new_volume_icon_state)) else trap("MUST: add the volume class")
this.m_mute_icon_state = new_volume_icon_state
if muted != this.m_was_muted:
_ = dom_token_list_toggle(dom_element_class_list(mute_button), ak.string_from_str("muted"), muted) else trap("MUST: toggle the muted class")
this.m_was_muted = muted
if has_audio != this.m_had_audio:
_ = dom_token_list_toggle(dom_element_class_list(html_media_controls_element(dom.volume_area)), ak.string_from_str("hidden"), not has_audio) else trap("MUST: toggle the hidden class")
this.m_had_audio = has_audio
## Ported from MediaControls.cpp:574 Web::HTML::MediaControls::update_fullscreen_icon.
pub func html_media_controls_update_fullscreen_icon(this: HtmlMediaControls*):
let fullscreen_icon = html_media_controls_optional_element(html_media_controls_dom(this).fullscreen_icon) else return
let media_element = html_media_controls_media_element(this)
let is_fullscreen_element = dom_document_fullscreen_element(dom_node_document(media_element.dom_node())) == (DomElement*?)media_element.dom_element()
_ = dom_token_list_toggle(dom_element_class_list(fullscreen_icon), ak.string_from_str("fullscreen"), is_fullscreen_element) else trap("MUST: toggle the fullscreen class")
## Ported from MediaControls.cpp:587 Web::HTML::MediaControls::update_placeholder_visibility.
pub func html_media_controls_update_placeholder_visibility(this: HtmlMediaControls*):
_ = html_media_controls_media_element(this)
let placeholder_circle = html_media_controls_optional_element(html_media_controls_dom(this).placeholder_circle) else return
let display = ak.sv("flex") if html_media_controls_should_show_placeholder(this) else ak.sv("none")
css_style_properties_set_property_property_id(dom_element_style_for_bindings(placeholder_circle), css_data.PropertyId.display, display, ak.sv("")) else trap("MUST: set_property")
## Ported from MediaControls.cpp:598 Web::HTML::MediaControls::should_show_placeholder.
pub func html_media_controls_should_show_placeholder(this: const HtmlMediaControls*) -> bool:
let media_element = html_media_controls_media_element(this)
assert(html_media_controls_optional_element(html_media_controls_dom(this).placeholder_circle) != none)
let video_element = as_html_video_element(media_element)
return html_video_element_current_representation(video_element) != HtmlVideoElementRepresentation.video_frame
# mark: Showing and hiding ======================================================================
## show_controls' hover timer handler: `[&] { hide_controls(); }`.
func html_media_controls_hover_timeout(context: void*?):
html_media_controls_hide_controls((HtmlMediaControls*)(context else trap("hover timer: no controls")))
## Ported from MediaControls.cpp:609 Web::HTML::MediaControls::show_controls.
pub func html_media_controls_show_controls(this: HtmlMediaControls*):
let control_bar = html_media_controls_element(html_media_controls_dom(this).control_bar)
dom_token_list_add(dom_element_class_list(control_bar), html_s_visible_class()) else trap("MUST: add the visible class")
if this.m_hover_timer == none:
let hover_timeout_ms: i32 = 1000
let hover_timer = core_timer_create_single_shot(hover_timeout_ms, ak.Function0[unit](call = html_media_controls_hover_timeout, context = this))
this.m_hover_timer = hover_timer
core_timer_start(hover_timer)
else:
core_timer_restart(this.m_hover_timer else trap("unreachable"))
## Ported from MediaControls.cpp:626 Web::HTML::MediaControls::hide_controls.
pub func html_media_controls_hide_controls(this: HtmlMediaControls*):
let control_bar = html_media_controls_element(html_media_controls_dom(this).control_bar)
if this.m_scrubbing_timeline != HtmlMediaControlsScrubbing.no or this.m_scrubbing_volume or this.m_hovering_controls:
return
if html_media_controls_optional_element(html_media_controls_dom(this).placeholder_circle) != none and html_media_controls_should_show_placeholder(this):
return
dom_token_list_remove(dom_element_class_list(control_bar), html_s_visible_class()) else trap("MUST: remove the visible class")
# m_hover_timer.clear(): dropping the last reference to the timer stops it.
if let hover_timer = this.m_hover_timer:
core_timer_stop(hover_timer)
this.m_hover_timer = none