npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

capacitor-screen-orientation

v0.0.11

Published

Handle screen orientation of a mobile device.

Downloads

313

Readme

Ionic Capacitor Screen Orientation

For detailed tutorial on how to use this plugin visit: https://medium.com/@hinddeep.purohit007/handling-screen-orientation-changes-in-capacitor-apps-19fe339578a6

Demo Application: https://github.com/hinddeep/Demo-Ionic-Screen_Orientation

Platforms Supported: Android, iOS

The Capacitor plugin I’ve developed can be used to detect the current of orientation of the screen, lock the screen in a particular orientation (disable auto-rotate) or unlock screen rotation (enable auto-rotate) and to listen for orientation changes.

Installation

npm install capacitor-screen-orientation

Android Configuration:

Open MainActivity.java and add the following code inside this.init() add(ScreenOrientation.class); Adding the above mentioned line will add the following import statement: import com.bkon.capacitor.screenorientation.ScreenOrientation; If you encounter errors, please add both the lines manually to MainActivity.java

If you want to listen for the orientation change event on Android:

  1. Open “AndroidManifest.xml” for your app
  2. Find the Activity tag
  3. Go to android:configChanges=”...”
  4. Remove ‘orientation |’ from configChanges

Supported Orientations:

  1. LANDSCAPE: left of right is decided by the device’s sensor
  2. LANDSCAPE_PRIMARY: explicitly specified by developer
  3. LANDSCAPE_SECONDARY: explicitly specified by developer
  4. PORTRAIT: up or upside down is decided by the device’s sensor
  5. PORTRAIT_PRIMARY: explicitly specified by the user
  6. PORTRAIT_SECONDARY: explicitly specified by the user
  7. CURRENT: current orientation of the device

SPECIAL NOTE: Ionic has implicitly disabled PORTRAIT_SECONDARY.

iOS Configuration:

If you want to lock the screen to the specified orientation on iOS:

  1. Open AppDelegate.swift for your app
  2. Add the following code:

var orientationLock = UIInterfaceOrientationMask.all

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { return self.orientationLock }

@objc func setOrientationLock(_ notification: Notification) { if let data = notification.userInfo as? [String: Int] { for (_, mask) in data { switch mask { case 1: self.orientationLock = UIInterfaceOrientationMask.portrait break; case 2: self.orientationLock = UIInterfaceOrientationMask.portraitUpsideDown break; case 3: self.orientationLock = UIInterfaceOrientationMask.landscapeRight break; case 4: self.orientationLock = UIInterfaceOrientationMask.landscapeLeft break; case 5: self.orientationLock = UIInterfaceOrientationMask.landscape break; default: self.orientationLock = UIInterfaceOrientationMask.all } } } }

  1. Locate: "func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {"
  2. Add the following code inside the function: NotificationCenter.default.addObserver(self, selector: #selector(self.setOrientationLock), name: NSNotification.Name(rawValue: "CAPOrientationLocked"), object: nil)

Supported Orientations:

  1. all: all orientations
  2. allButUpsideDown: all orientations other than upside down
  3. landscape: left of right is decided by the device’s sensor
  4. landscapeLeft: explicitly specified by the user
  5. landscapeRight: explicitly specified by the user
  6. portrait: up or upside down is decided by the device’s sensor
  7. portraitUpsideDown: explicitly specified by the user

SPECIAL NOTE: Ionic has implicitly disabled portraitUpsideDown.

Import the Plugin in your Web app

Open app.component.ts file and import the plugin as follows: import { Plugins } from "@capacitor/core"; const { ScreenOrientation } = Plugins; import 'capacitor-screen-orientation'

SPECIAL NOTE: Remove import 'capacitor-screen-orientation' when compiling app for Android and iOS. THe native plugin will not be invoked if you forget to remove the import statement before building for Android and iOS Platform.

Handle screen orientation:

  1. Create a function to get the current screen orientation: async getOrientation() { let obj = await ScreenOrientation.getScreenOrientation(); this.screen_orientation = obj.orientation; }

  2. Create a function to lock the screen in a particular orientation: async lockOrientation() { await ScreenOrientation.lockScreenOrientation({ orientation: this.screen_orientation_lock, }); }

Supported values for screen_orientation_lock variable:

  1. LANDSCAPE_PRIMARY (Android and iOS)
  2. PORTRAIT_PRIMARY (Android and iOS)
  3. LANDSCAPE_SECONDARY (Android and iOS)
  4. PORTRAIT_SECONDARY (Android and iOS)
  5. LANDSCAPE (Android only)
  6. PORTRAIT (Android only)
  7. CURRENT (Android only)
  1. Create a function to unlock screen rotation: async UnlockOrientation() { await ScreenOrientation.unlockScreenOrientation({}); }

  2. Create a function that handles subscription to the orientation change event: subscribeToOrientationChanges() { ScreenOrientation.addListener("orientation_changed", (data) => { this.screen_orientation_event = data.orientation; }); }

Rotate to a specific orientation (without locking the orientation):

NOTE: NOT supported on Android async rotate() { await ScreenOrientation.rotateTo({ orientation: this.rotateTo, }); }