Orientation: Mobile Orientation Control

更新时间:
2024-07-22
下载文档

Orientation: Mobile Orientation Control

Obtain and control the current display orientation of EdgerOS.

Developers can determine whether this function works in the EdgerOS mobile App environment through the following code:

edger.env().then(data => {
  if (data.env === 'edgerapp') {
    // We work in EdgerOS mobile App environment.
  }
}).catch(error => {
  console.error(error);
});

Functions

edger.orientation.state()

  • Returns: {Promise} Promise object.

Get current display orientation of EdgerOS.

Example

edger.orientation.state().then((data) => {
  const { orientation } = data;
}).catch(error => {
  console.error(error);
});

async / await

async function orientation() {
  try {
    return await edger.orientation.state();
  } catch (error) {
    console.error(error);
  }
}

The obtained data object contains the following member:

  • orientation {String} Current orientation.

The orientation possible values include: 'landscape', 'portrait'. Developers can choose the appropriate App display style based on this setting.

edger.orientation.lock(orientation)

  • orientation {String} New orientation to lock.
  • Returns: {Promise} Promise object.

Try to lock orientation.

Example

edger.orientation.lock('landscape').then((data) => {
  const { success } = data;
  if (success) {
    console.log('Lock landscape success');
  }
}).catch(error => {
  console.error(error);
});

async / await

async function orientationLock(orientation) {
  const ret = await edger.orientation.lock(orientation);
  return ret.success;
}

The data.success indicates whether the orientation lock is successful.

edger.orientation.unlock()

  • Returns: {Promise} Promise object.

Unlock orientation.

Example

edger.orientation.unlock().then((data) => {
  const { success } = data;
  if (success) {
    console.log('Unlock success');
  }
}).catch(error => {
  console.error(error);
});

async / await

async function orientationUnlock() {
  const ret = await edger.orientation.unlock();
  return ret.success;
}

Events

The unified event listener provided by Web-SDK:

const listener = (payload) => {
  // Event handling...
}

// add listener
edger.addEventListener('some-event', listener);

// or 
// onAction() is an alias of addEventListener().
edger.onAction('some-event', listener);

// remove listener
edger.removeEventListener('some-event', listener);

// remove all listeners
edger.removeAllListeners();

orientation

This event will be generated when EdgerOS display orientation was changed.

Example

const listener = (payload) => {
  const { orientation } = payload;
  console.log('Current orientation:', orientation);
}

edger.addEventListener('orientation', listener);
文档内容是否对您有所帮助?
有帮助
没帮助