Загрузка данных


import { createWidget, widget } from '@zeppos/device-types'

// Создаем глобальный объект состояния, так как в WatchFace нет встроенного this.state
const state = {
  num1: 0,
  num2: 0,
  action: '+',
  result: 0
}

function calculate() {
  const n1 = state.num1
  const n2 = state.num2
  let res = 0

  switch (state.action) {
    case '+': res = n1 + n2; break
    case '-': res = n1 - n2; break
    case '*': res = n1 * n2; break
    case '/': 
      if (n2 === 0) return 'Error'
      res = n1 / n2
      break
    case 'x²': res = n1 * n1; break
    case '√': 
      if (n1 < 0) return 'Error'
      res = Math.sqrt(n1)
      break
  }

  if (res % 1 !== 0 && typeof res === 'number') {
    return Number(res.toFixed(2))
  }
  return res
}

WatchFace({
  onInit() {
    console.log('Калькулятор инициализирован!')
  },

  build() {
    // 1. Черный фон под экран Mi Band 7
    createWidget(widget.BG, { x: 0, y: 0, w: 192, h: 490, color: 0x000000 })

    // 2. Главное цифровое табло
    const display = createWidget(widget.TEXT, {
      x: 10, y: 40, w: 172, h: 60,
      color: 0x00FF00, text_size: 22,
      align_h: widget.ALIGN.CENTER_H,
      text: '0 + 0 = 0'
    })

    const updateDisplay = () => {
      state.result = calculate()
      display.setProperty(widget.prop.MORE, {
        text: `${state.num1} ${state.action} ${state.num2} = ${state.result}`
      })
    }

    // 3. Блок управления числами
    createWidget(widget.BUTTON, { x: 16, y: 120, w: 75, h: 45, radius: 8, normal_color: 0x333333, text: 'N1 +1' }).addEventListener(widget.EDGE_EVENT.TAP, () => { state.num1++; updateDisplay() })
    createWidget(widget.BUTTON, { x: 101, y: 120, w: 75, h: 45, radius: 8, normal_color: 0x333333, text: 'N2 +1' }).addEventListener(widget.EDGE_EVENT.TAP, () => { state.num2++; updateDisplay() })
    createWidget(widget.BUTTON, { x: 16, y: 175, w: 75, h: 45, radius: 8, normal_color: 0x333333, text: 'N1 -1' }).addEventListener(widget.EDGE_EVENT.TAP, () => { state.num1--; updateDisplay() })
    createWidget(widget.BUTTON, { x: 101, y: 175, w: 75, h: 45, radius: 8, normal_color: 0x333333, text: 'N2 -1' }).addEventListener(widget.EDGE_EVENT.TAP, () => { state.num2--; updateDisplay() })

    // 4. Блок выбора операций (Инженерный)
    const actions = ['+', '-', '*', '/', 'x²', '√']
    actions.forEach((act, index) => {
      const posX = 16 + (index % 2) * 85
      const posY = 235 + Math.floor(index / 2) * 55

      createWidget(widget.BUTTON, {
        x: posX, y: posY, w: 75, h: 45, radius: 22,
        normal_color: 0xFF9500, text: act, text_size: 18
      }).addEventListener(widget.EDGE_EVENT.TAP, () => {
        state.action = act
        updateDisplay()
      })
    })

    // 5. Кнопка полного сброса
    createWidget(widget.BUTTON, {
      x: 16, y: 405, w: 160, h: 45, radius: 8,
      normal_color: 0xAA0000, text: 'СБРОС (С)'
    }).addEventListener(widget.EDGE_EVENT.TAP, () => {
      state.num1 = 0
      state.num2 = 0
      state.action = '+'
      state.result = 0
      display.setProperty(widget.prop.MORE, { text: '0 + 0 = 0' })
    })
  },

  onDestroy() {
    console.log('Калькулятор закрыт')
  }
})