// form_factor.dart — 三端形态判定（mobile / tablet / desktop）
//
// 决定外壳与页面布局走哪一套。用 defaultTargetPlatform（web 安全，不依赖 dart:io）
// + 窗口宽度联合判定：桌面平台优先 desktop，窗口拉极窄时降级 mobile 避免溢出。
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';

enum FormFactor { mobile, tablet, desktop }

bool get _isDesktopPlatform {
  if (kIsWeb) return false;
  switch (defaultTargetPlatform) {
    case TargetPlatform.macOS:
    case TargetPlatform.windows:
    case TargetPlatform.linux:
      return true;
    default:
      return false;
  }
}

extension FormFactorContext on BuildContext {
  /// 当前形态。桌面平台(macOS/Win/Linux)且宽≥680 → desktop；触屏/Web 宽≥600 → tablet；否则 mobile。
  FormFactor get formFactor {
    final w = MediaQuery.sizeOf(this).width;
    if (_isDesktopPlatform) return w >= 680 ? FormFactor.desktop : FormFactor.mobile;
    if (w >= 600) return FormFactor.tablet;
    return FormFactor.mobile;
  }

  bool get isDesktop => formFactor == FormFactor.desktop;
  bool get isTablet => formFactor == FormFactor.tablet;
  bool get isMobile => formFactor == FormFactor.mobile;
}
