ScreenOrientation
Read the document's current orientation type and rotation angle, lock the device to a specific orientation, and get notified the moment the user rotates their phone.
@inject Bit.Butil.ScreenOrientation screenOrientationMDN reference
The current orientation as a strongly-typed ScreenOrientationType (portrait/landscape, primary/secondary), plus the rotation angle in degrees.
var type = await screenOrientation.GetOrientationType();
var angle = await screenOrientation.GetAngle();
// e.g. LandscapePrimary at 0 degrees on a desktop monitorLocks the containing document to one of the OrientationLockType values, and releases the lock again. Ideal for games or video players that only make sense in landscape.
await screenOrientation.Lock(OrientationLockType.Landscape);
// release the lock and return to the default behavior:
await screenOrientation.Unlock();Fires whenever the screen orientation changes - for example when the user rotates their phone. The handler receives an OrientationState carrying the new type and angle.
var subscription = await screenOrientation.SubscribeChange(state =>
{
// state.Type → ScreenOrientationType.PortraitPrimary, ...
// state.Angle → 0, 90, 180, 270
});
// later, when no longer needed:
await subscription.DisposeAsync();If you prefer explicit ids over disposable subscriptions, AddChange returns a Guid that can later be passed to RemoveChange, and RemoveAllChanges drops every listener registered on this instance.
// register and keep the id
var id = await screenOrientation.AddChange(OnOrientationChanged);
// remove a single listener by id
await screenOrientation.RemoveChange(id);
// or drop every registered listener at once
await screenOrientation.RemoveAllChanges();API reference
Task<ScreenOrientationType> GetOrientationType()Task<ushort> GetAngle()Task Lock(OrientationLockType lockType)Task Unlock()ValueTask<Guid> AddChange(Action<OrientationState> handler)ValueTask<ButilSubscription> SubscribeChange(Action<OrientationState> handler)ValueTask<Guid[]> RemoveChange(Action<OrientationState> handler)ValueTask RemoveChange(Guid id)ValueTask RemoveAllChanges()void InvokeScreenOrientationChange(Guid id, ushort angle, string type)ValueTask DisposeAsync()