Skip to content
Permalink
Browse files

Hololens - Show/hide media controls according to playback state

  • Loading branch information
ferjm committed Nov 28, 2019
1 parent c810962 commit d633c8b9da39c1dbf7e92debb907a58cd13de0da
@@ -70,6 +70,20 @@ void BrowserPage::BindServoEvents() {
servoControl().OnCaptureGesturesEnded(
[=] { navigationBar().IsHitTestVisible(true); });
urlTextbox().GotFocus(std::bind(&BrowserPage::OnURLFocused, this, _1));
servoControl().OnMediaSessionMetadata(
[=](hstring title, hstring artist, hstring album) {});
servoControl().OnMediaSessionPlaybackStateChange([=](const auto &,
int state) {
if (state == 1 /* none */) {
mediaControls().Visibility(Visibility::Collapsed);
return;
}
mediaControls().Visibility(Visibility::Visible);
playButton().Visibility(state == 3 /* paused */ ? Visibility::Visible
: Visibility::Collapsed);
pauseButton().Visibility(state == 3 /* paused */ ? Visibility::Collapsed
: Visibility::Visible);
});
}

void BrowserPage::OnURLFocused(Windows::Foundation::IInspectable const &) {
@@ -137,9 +137,9 @@
<Button Margin="0,10,10,0" Click="OnXRPkgWarningDismissClick">Dismiss</Button>
</StackPanel>
</StackPanel>
<CommandBar Grid.Row="3" Visibility="Collapsed">
<AppBarButton Icon="Play" Label="Play" Click="OnMediaControlsPlayClicked"/>
<AppBarButton Icon="Pause" Label="Pause" Click="OnMediaControlsPauseClicked"/>
<CommandBar Grid.Row="3" x:Name="mediaControls" Visibility="Collapsed">
<AppBarButton Icon="Play" Label="Play" x:Name="playButton" Visibility="Collapsed" Click="OnMediaControlsPlayClicked"/>
<AppBarButton Icon="Pause" Label="Pause" x:Name="pauseButton" Click="OnMediaControlsPauseClicked"/>

<CommandBar.Content>
<TextBlock Text="Now playing..." Margin="12,14"/>
@@ -56,14 +56,25 @@ const char *get_clipboard_contents() {
return nullptr;
}

void on_media_session_metadata(const char *title, const char *album,
const char *artist) {
return sServo->Delegate().OnServoMediaSessionMetadata(
char2hstring(title), char2hstring(album), char2hstring(artist));
}

void on_media_session_playback_state_change(const int state) {
return sServo->Delegate().OnServoMediaSessionPlaybackStateChange(state);
}

Servo::Servo(hstring url, hstring args, GLsizei width, GLsizei height,
float dpi, ServoDelegate &aDelegate)
: mWindowHeight(height), mWindowWidth(width), mDelegate(aDelegate) {

capi::CInitOptions o;
hstring defaultPrefs = L" --pref dom.webxr.enabled";
o.args = *hstring2char(args + defaultPrefs);
o.url = *hstring2char(url);
o.url =
"https://ferjm.github.io/web-api-tests/video/mp4.html"; //*hstring2char(url);
o.width = mWindowWidth;
o.height = mWindowHeight;
o.density = dpi;
@@ -110,6 +121,9 @@ Servo::Servo(hstring url, hstring args, GLsizei width, GLsizei height,
c.on_ime_state_changed = &on_ime_state_changed;
c.get_clipboard_contents = &get_clipboard_contents;
c.set_clipboard_contents = &set_clipboard_contents;
c.on_media_session_metadata = &on_media_session_metadata;
c.on_media_session_playback_state_change =
&on_media_session_playback_state_change;

capi::register_panic_handler(&on_panic);

@@ -36,6 +36,8 @@ class ServoDelegate {
virtual void OnServoIMEStateChanged(bool) = 0;
virtual void Flush() = 0;
virtual void MakeCurrent() = 0;
virtual void OnServoMediaSessionMetadata(hstring, hstring, hstring) = 0;
virtual void OnServoMediaSessionPlaybackStateChange(int) = 0;

protected:
virtual ~ServoDelegate(){};
@@ -432,6 +432,15 @@ void ServoControl::OnServoIMEStateChanged(bool aShow) {
// https://docs.microsoft.com/en-us/windows/win32/winauto/uiauto-implementingtextandtextrange
}

void ServoControl::OnServoMediaSessionMetadata(hstring title, hstring artist,
hstring album) {
RunOnUIThread([=] { mOnMediaSessionMetadataEvent(title, artist, album); });
}

void ServoControl::OnServoMediaSessionPlaybackStateChange(int state) {
RunOnUIThread([=] { mOnMediaSessionPlaybackStateChangeEvent(*this, state); });
}

template <typename Callable> void ServoControl::RunOnUIThread(Callable cb) {
Dispatcher().RunAsync(CoreDispatcherPriority::High, cb);
}
@@ -70,6 +70,23 @@ struct ServoControl : ServoControlT<ServoControl>, public servo::ServoDelegate {
mOnCaptureGesturesEndedEvent.remove(token);
}

winrt::event_token
OnMediaSessionMetadata(MediaSessionMetadataDelegate const &handler) {
return mOnMediaSessionMetadataEvent.add(handler);
};
void OnMediaSessionMetadata(winrt::event_token const &token) noexcept {
mOnMediaSessionMetadataEvent.remove(token);
}

winrt::event_token OnMediaSessionPlaybackStateChange(
Windows::Foundation::EventHandler<int> const &handler) {
return mOnMediaSessionPlaybackStateChangeEvent.add(handler);
};
void
OnMediaSessionPlaybackStateChange(winrt::event_token const &token) noexcept {
mOnMediaSessionPlaybackStateChangeEvent.remove(token);
}

void SetTransientMode(bool transient) { mTransient = transient; }

void SetArgs(hstring args) { mArgs = args; }
@@ -87,6 +104,9 @@ struct ServoControl : ServoControlT<ServoControl>, public servo::ServoDelegate {
virtual bool OnServoAllowNavigation(winrt::hstring);
virtual void OnServoAnimatingChanged(bool);
virtual void OnServoIMEStateChanged(bool);
virtual void OnServoMediaSessionMetadata(winrt::hstring, winrt::hstring,
winrt::hstring);
virtual void OnServoMediaSessionPlaybackStateChange(int);

private:
winrt::event<Windows::Foundation::EventHandler<hstring>> mOnURLChangedEvent;
@@ -96,6 +116,9 @@ struct ServoControl : ServoControlT<ServoControl>, public servo::ServoDelegate {
winrt::event<EventDelegate> mOnLoadEndedEvent;
winrt::event<EventDelegate> mOnCaptureGesturesStartedEvent;
winrt::event<EventDelegate> mOnCaptureGesturesEndedEvent;
winrt::event<MediaSessionMetadataDelegate> mOnMediaSessionMetadataEvent;
winrt::event<Windows::Foundation::EventHandler<int>>
mOnMediaSessionPlaybackStateChangeEvent;

float mDPI = 1;
hstring mInitialURL = DEFAULT_URL;
@@ -2,6 +2,7 @@ namespace ServoApp {

delegate void EventDelegate();
delegate void HistoryChangedDelegate(Boolean back, Boolean forward);
delegate void MediaSessionMetadataDelegate(String title, String artist, String album);

runtimeclass ServoControl : Windows.UI.Xaml.Controls.Control {
ServoControl();
@@ -20,5 +21,7 @@ namespace ServoApp {
event HistoryChangedDelegate OnHistoryChanged;
event Windows.Foundation.EventHandler<String> OnTitleChanged;
event Windows.Foundation.EventHandler<String> OnURLChanged;
event MediaSessionMetadataDelegate OnMediaSessionMetadata;
event Windows.Foundation.EventHandler<int> OnMediaSessionPlaybackStateChange;
}
} // namespace ServoApp

0 comments on commit d633c8b

Please sign in to comment.
You can’t perform that action at this time.