setButton 设置按钮
setButton
类型: MethodDeclaration
所属类: CommandFunctionList
定义位置: command.ts
描述
设置按钮
参数
| 参数名 | 类型 | 描述 | 默认值 | 
|---|---|---|---|
| \{element, properties\} | `{ | ||
| element: ElementGetter | |||
| properties: Array<{key: string, value: any}> | |||
| }` | - | - | 
返回值
类型: CommandFunction
源代码
位置: 第 4565 行
protected setButton({element, properties}: {
    element: ElementGetter
    properties: Array<{key: string, value: any}>
  }): CommandFunction {
    const method = this.setButton as any
    if (!method.initialized) {
      method.initialized = true
      method.arrayMap = {
        'normalClip': true,
        'hoverClip': true,
        'activeClip': true,
        'normalTint': true,
        'hoverTint': true,
        'activeTint': true,
      }
    }
    const getElement = Command.compileElement(element)
    const variables: Array<{key: string, value: any}> = []
    const constants: Array<any> = []
    for (const property of properties) {
      switch (property.key) {
        case 'content': {
          const getter = Command.compileTextContent(property.value)
          // @ts-ignore
          if (!getter.constant) {
            variables.push({
              key: property.key,
              value: getter,
            })
            continue
          }
          // 如果内容是常量,进入默认分支
        }
        default:
          constants.push(property)
          continue
      }
    }
    // 对单属性变量进行优化
    if (variables.length === 1 && constants.length === 0) {
      const {key, value} = variables[0]
      return () => {
        const element = getElement()
        if (element instanceof ButtonElement) {
          // @ts-ignore
          element[key] = value()
        }
        return true
      }
    }
    // 对单属性常量进行优化
    if (variables.length === 0 && constants.length === 1) {
      const {key, value} = constants[0]
      return () => {
        const element = getElement()
        if (element instanceof ButtonElement) {
          if (method.arrayMap[key]) {
            // @ts-ignore
            Array.fill(element[key], value)
          } else {
            // @ts-ignore
            element[key] = value
          }
        }
        return true
      }
    }
    return () => {
      const element = getElement()
      if (element instanceof ButtonElement) {
        for (const property of variables) {
          // @ts-ignore
          element[property.key] = property.value()
        }
        for (const property of constants) {
          if (element instanceof ButtonElement) {
            if (method.arrayMap[property.key]) {
              // @ts-ignore
              element[property.key].set(property.value)
            } else {
              // @ts-ignore
              element[property.key] = property.value
            }
          }
        }
      }
      return true
    }
  }
文档生成时间:2025/7/21 20:53:39