window-area.uvue 4.7 KB
Newer Older
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
<template>
  <view style="flex:1;">
    <view id="statusBar" class="statusBar" v-if="statusBarArea.width > 0 && statusBarArea.height > 0"
      :style="{'width': statusBarArea.width,'height': statusBarArea.height}">
    </view>
    <view id="cutoutArea" class="cutoutArea" v-if="cutoutArea.length > 0" v-for="(item, _) in cutoutArea"
      :style="{'top': item.top,'left': item.left,'width': item.width,'height': item.height}"></view>
    <view id="safeArea" class="safeArea"
      :style="{'top': safeArea.top,'left': safeArea.left,'width': safeArea.width,'height': safeArea.height}"></view>
    <view id="bottomNavigationIndicator" class="bottomNavigationIndicator"
      v-if="bottomNavigationIndicatorArea.width > 0 && bottomNavigationIndicatorArea.height > 0"
      :style="{'width': bottomNavigationIndicatorArea.width,'height': bottomNavigationIndicatorArea.height}"></view>
    <view style="flex: 1;justify-content: center;align-items: center;">
      <view style="margin: 5px 0;">
        <text style="color: red;">系统状态栏区域</text>
      </view>
      <view style="margin: 5px 0;">
        <text style="color: orange;">摄像头区域</text>
      </view>
      <view style="margin: 5px 0;">
        <text style="color: green;">安全区域</text>
      </view>
      <view style="margin: 5px 0;">
        <text style="color: blue;">系统导航栏区域</text>
      </view>
      <!-- <view style="flex-direction: row;align-items: center;margin: 5px 0">
        <text>显示系统状态栏</text>
        <switch :checked="isStatusBarShow" @change="statusBarChange"></switch>
      </view>
      <view style="flex-direction: row;align-items: center;margin: 5px 0">
        <text>显示系统导航栏</text>
        <switch :checked="isBottomNavigationIndicatorShow" @change="bottomNavigationIndicatorChange"></switch>
      </view> -->
    </view>
  </view>
</template>

<script setup>
  type StatusBarArea = {
    width : number,
    height : number
  }

  type CutoutArea = {
    top : number,
    left : number,
    width : number,
    height : number
  }

  type SafeArea = {
    top : number,
    left : number,
    width : number,
    height : number
  }

  type BottomNavigationIndicatorArea = {
    width : number,
    height : number
  }

  const statusBarArea = ref({ width: 0, height: 0 } as StatusBarArea);
  const cutoutArea = ref([] as CutoutArea[]);
  const safeArea = ref({ top: 0, left: 0, width: 0, height: 0 } as SafeArea);
  const bottomNavigationIndicatorArea = ref({ width: 0, height: 0 } as BottomNavigationIndicatorArea);
  const isStatusBarShow = ref(false);
  const isBottomNavigationIndicatorShow = ref(false);

  const getWindowInfo = () => {
    const info = uni.getWindowInfo();
    statusBarArea.value.width = info.windowWidth;
    statusBarArea.value.height = info.safeAreaInsets.top;
    cutoutArea.value.length = 0;
    (info.cutoutArea ?? []).forEach((item) => {
      cutoutArea.value.push({
        top: item.top,
        left: item.left,
        width: item.right - item.left,
        height: item.bottom - item.top
      } as CutoutArea);
    })
    safeArea.value.top = info.safeArea.top;
    safeArea.value.left = info.safeArea.left;
    safeArea.value.width = info.safeArea.width;
    safeArea.value.height = info.safeArea.height;
    bottomNavigationIndicatorArea.value.width = info.windowWidth;
    bottomNavigationIndicatorArea.value.height = info.safeAreaInsets.bottom;
  };

  const statusBarChange = (e : UniSwitchChangeEvent) => {
    const pages = getCurrentPages();
    pages[pages.length - 1].setPageStyle({
      'hideStatusBar': !e.detail.value,
    });
  };

  const bottomNavigationIndicatorChange = (e : UniSwitchChangeEvent) => {
    const pages = getCurrentPages();
    pages[pages.length - 1].setPageStyle({
      'hideBottomNavigationIndicator': !e.detail.value,
    });
  };

  onReady(() => {
    const pages = getCurrentPages();
    isStatusBarShow.value = !(pages[pages.length - 1].getPageStyle()['hideStatusBar'] as boolean);
    isBottomNavigationIndicatorShow.value = !(pages[pages.length - 1].getPageStyle()['hideBottomNavigationIndicator'] as boolean);
    getWindowInfo();
  });

  onResize((_ : OnResizeOptions) => {
    getWindowInfo();
  });
</script>

<style>
  .statusBar {
    position: absolute;
    border-style: solid;
    border-color: red;
    border-width: 4px;
  }

  .cutoutArea {
    position: absolute;
    border-style: solid;
    border-color: orange;
    border-width: 4px;
  }

  .safeArea {
    position: absolute;
    border-style: solid;
    border-color: green;
    border-width: 4px;
  }

  .bottomNavigationIndicator {
    position: absolute;
    bottom: 0;
    border-style: solid;
    border-color: blue;
    border-width: 4px;
  }
</style>