page.ts 861 B

12345678910111213141516171819202122232425262728
  1. import { withResolvers } from "../promise";
  2. export function setPageOrientation(orientation: 'portrait' | 'landscape') {
  3. const { promise, resolve, reject } = withResolvers();
  4. (<any>wx).setPageOrientation({ orientation, success: resolve, fail: reject });
  5. return promise;
  6. }
  7. export function getPageOrientation() {
  8. const { windowWidth, windowHeight, safeArea } = wx.getWindowInfo();
  9. const { bottom } = wx.getMenuButtonBoundingClientRect();
  10. if (windowWidth > windowHeight) {
  11. return {
  12. orientation: 'landscape',
  13. left: safeArea?.left ?? 0,
  14. right: 0,
  15. width: windowWidth - (safeArea.left ?? 0),
  16. height: (safeArea?.height ?? windowHeight) - bottom
  17. } as const;
  18. } else {
  19. return {
  20. orientation: 'portrait',
  21. width: windowWidth,
  22. height: (safeArea?.bottom ?? windowHeight) - bottom
  23. } as const;
  24. }
  25. }