Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full screen events #2041

Open
wants to merge 85 commits into
base: main
Choose a base branch
from

Conversation

ne0rrmatrix
Copy link
Contributor

@ne0rrmatrix ne0rrmatrix commented Jul 19, 2024

  • Feature/Proposal

Add Support for Full Screen Events for Windows, Android, iOS and Mac Catalyst.

Description of Change

Add event handlers to notify developer when full screen status changes.

API:

/// <summary>
/// Backing store for the <see cref="FullScreenState"/> property.
/// </summary>
public static readonly BindableProperty FullScreenProperty =
	BindableProperty.Create(nameof(FullScreenState), typeof(MediaElementScreenState), typeof(MediaElement), 
		MediaElementScreenState.Default, propertyChanged: OnFullScreenPropertyChanged);
/// <summary>
/// Gets the full screen state of the media element.
/// </summary>
public MediaElementScreenState FullScreenState
{
	get => (MediaElementScreenState)GetValue(FullScreenProperty);
	private set => SetValue(FullScreenProperty, value);
}

XAML:

<toolkit:MediaElement
    x:Name="MediaElement"
    ShouldAutoPlay="True"
    Source="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
    MetadataArtworkUrl="https://lh3.googleusercontent.com/pw/AP1GczNRrebWCJvfdIau1EbsyyYiwAfwHS0JXjbioXvHqEwYIIdCzuLodQCZmA57GADIo5iB3yMMx3t_vsefbfoHwSg0jfUjIXaI83xpiih6d-oT7qD_slR0VgNtfAwJhDBU09kS5V2T5ZML-WWZn8IrjD4J-g=w1792-h1024-s-no-gm"
    MetadataTitle="Big Buck Bunny"
    MetadataArtist="Blender Foundation"
    MediaEnded="OnMediaEnded"
    MediaFailed="OnMediaFailed"
    MediaOpened="OnMediaOpened"
    PositionChanged="OnPositionChanged"
    StateChanged="OnStateChanged"
    SeekCompleted="OnSeekCompleted"
    FullScreenStateChanged="MediaElement_FullScreenStateChanged"/>

Code Behind:

void MediaElement_FullScreenStateChanged(object? sender, FullScreenStateChangedEventArgs e) =>
	logger.LogInformation("FullScreen State Changed. Old State: {PreviousState}, New State: {NewState}", e.PreviousState, e.NewState);

Linked Issues

PR Checklist

Additional information

This is a PR to implement a solution for adding support for developers to have an the choice to see when user goes into full full screen and back again.

Note for Maintainers: If you see a need for a name change please go ahead and edit. Names are important. I have no opinion on naming of classes/functions/variables. If you see a need for them to change feel free to make those edits.

This commit introduces full screen state management for media elements. The `CommunityToolkit.Maui.Primitives` namespace was added to several files to support this feature. A new `MediaElementScreenState` enum and `FullScreenStateChangedEventArgs` class were added to represent the full screen state and event data respectively. The `IMediaElement` and `MediaElement` interfaces were updated to include a `FullScreenState` property, a `FullScreenStateChanged` event, and a `FullScreenChanged` method. The `MediaElementPage` class now subscribes to the `FullScreenStateChanged` event and logs information when triggered. The `MauiMediaElement` classes for Android and Windows, and the `MediaManagerDelegate` class for MacOS, were updated to include a `WindowsChanged` event that triggers when the full screen state changes. The `MediaManager` classes for Android, MacOS, and Windows now subscribe to the `WindowsChanged` event and update the full screen state when triggered. The `MediaManager` class for MacOS sets the `Delegate` property of the `AVPlayerViewController` to a new instance of `MediaManagerDelegate`.
- Updated MediaElementPage.xaml to include a new event handler `FullScreenStateChanged` for better responsiveness to fullscreen changes.
- Removed subscription to `FullScreenStateChanged` in MediaElementPage.xaml.cs, indicating a shift in how fullscreen changes are managed.
- Improved Android fullscreen support in MauiMediaElement.android.cs by invoking `OnWindowsChanged` with `FullScreenStateChangedEventArgs` to accurately track fullscreen state changes.
Updated the comment for the `OnWindowsChanged` static event in the `MediaManager` class to more accurately describe its purpose related to changes in the full screen state of the media element. The previous, less descriptive comment was replaced with a clearer explanation.
This commit overhauls the handling of full-screen state changes across Android, Windows, and macOS within a Maui application. A new record, `FullScreenEvents`, has been introduced in the `MediaManager` class to centralize event handling, replacing platform-specific `WindowsChanged` events with a unified approach. This refactor includes the removal of redundant `using` directives, adjustments in event invocation to utilize the new centralized event, and general code cleanup for better readability and maintainability. Platform-specific code has been updated to align with this new mechanism, streamlining the full-screen state change process across different operating systems.
- Removed `OnWindowsChanged` from `MediaManager` in platform-specific files (Android, macOS, Windows) due to redundancy and centralized this logic by adding a new protected method `OnWindowsChanged` to the `FullScreenEvents` record in `MediaManager.shared.cs`. This change aims to reduce code duplication and improve the management of full-screen state changes across different platforms.
Centralized the subscription to FullScreenEvents.WindowsChanged in the MediaManager class to improve maintainability and readability. Removed redundant event unsubscriptions in platform-specific MediaManager files, streamlining event management.
- Renamed `OnWindowsChanged` to `OnFullScreenStatusChanged` in `MediaManager` and `FullScreenEvents` for clarity.
- Added `MediaElement` property to `FullScreenEvents` for better media element access.
In `MauiMediaElement.android.cs`, the visibility of `CurrentPlatformContext` has been changed from `readonly` to `public readonly`, enhancing its accessibility for external use. Additionally, in `MediaManager.shared.cs`, a comment has been added to acknowledge similarities with PR CommunityToolkit#1918 on the CommunityToolkit/Maui repository.
- Changed `CurrentPlatformContext` in `MauiMediaElement.android.cs` from `public` to `internal`, limiting its accessibility to within its assembly.
- Modified `FullScreenEvents` in `MediaManager.shared.cs`:
  1. Access modifier updated from `public` to `internal`.
  2. Converted from a `record` to a `readonly record struct`, making it a value type and immutable.
@ne0rrmatrix ne0rrmatrix added the needs discussion Discuss it on the next Monthly standup label Jul 30, 2024
@ne0rrmatrix ne0rrmatrix requested review from vhugogarcia and removed request for pictos and jfversluis February 3, 2025 01:52
@ne0rrmatrix ne0rrmatrix removed the needs discussion Discuss it on the next Monthly standup label Feb 13, 2025
@ne0rrmatrix ne0rrmatrix requested review from a team and removed request for vhugogarcia February 15, 2025 14:11
@ne0rrmatrix ne0rrmatrix requested a review from Copilot March 15, 2025 23:55
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds full screen event support to the MediaElement control across multiple platforms including Windows, Android, iOS, and Mac Catalyst. Key changes include the introduction of a new full screen state enum and event args, integration of full screen state change events into the MediaManager and MediaElement implementations, and corresponding updates to the interface and sample usage.

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/CommunityToolkit.Maui.MediaElement/Primitives/MediaElementFullScreenState.cs Added enum for full screen state with two states (FullScreen, Default).
src/CommunityToolkit.Maui.MediaElement/Primitives/FullScreenStateChangedEventArgs.cs Introduced event arguments for full screen state change.
src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.shared.cs Integrated full screen events by subscribing and handling events via a new record struct.
src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.windows.cs Triggered full screen state changes on button clicks.
src/CommunityToolkit.Maui.MediaElement/Interfaces/IMediaElement.cs Added the FullScreenState property and the FullScreenStateChanged event to the interface.
src/CommunityToolkit.Maui.MediaElement/MediaElement.shared.cs Updated MediaElement implementation with full screen state support.
src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.macios.cs Implemented full screen event handling via a custom delegate.
src/CommunityToolkit.Maui.MediaElement/Views/MauiMediaElement.android.cs Updated full screen transitions to trigger the new events.
samples/CommunityToolkit.Maui.Sample/Pages/Views/MediaElement/MediaElementPage.xaml.cs Added sample logging for full screen state changes.
Comments suppressed due to low confidence (2)

src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.shared.cs:52

  • The event name 'WindowsChanged' could be misleading since it is used for full screen state changes on all platforms. Consider renaming it to 'FullScreenStateChanged' or a similarly descriptive name.
public static event EventHandler<FullScreenStateChangedEventArgs>? WindowsChanged;

src/CommunityToolkit.Maui.MediaElement/MediaElement.shared.cs:556

  • The local variable 'MediaElement' should be renamed using camelCase (e.g., 'mediaElement') to follow C# naming conventions.
var MediaElement = (MediaElement)bindable;

Updated full-screen state change handling to use a new static class `FullScreenEvents` instead of `MediaManager.FullScreenEvents`. This change centralizes event management across Android, Windows, and macOS, improving code organization and maintainability while preserving existing functionality.
Updated the FullScreenEvents class to replace static event handling with instance methods, allowing for multiple instances and improved event management. Adjusted related calls in MauiMediaElement.android.cs and MauiMediaElement.windows.cs to use the new instance methods. Added a static instance of FullScreenEvents in MediaManager for centralized event management.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📽️ MediaElement Issue/PR that has to do with MediaElement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Proposal] API that allows developers to hook a media element event and know the full screen status.
4 participants