require method

bool require(
  1. bool supported,
  2. String feature, [
  3. String? fallback
])

Check if a feature is supported and handle if not.

Returns true if the feature is supported, false otherwise.

supported - The capability flag to check. feature - Human-readable feature name for error messages. fallback - Description of fallback behavior (optional).

Example:

if (!guard.require(capabilities.blur, 'Blur effect', 'Using solid color.')) {
  // Apply fallback
  return;
}
// Feature is supported, proceed

Implementation

bool require(bool supported, String feature, [String? fallback]) {
  if (supported) return true;

  switch (behavior) {
    case UnsupportedBehavior.throwError:
      final message = StringBuffer()
        ..write('$feature is not supported on ${capabilities.platform}');
      if (fallback != null) {
        message.write('. $fallback');
      }
      throw UnsupportedError(message.toString());

    case UnsupportedBehavior.warnOnce:
      if (!_warned.contains(feature)) {
        _warned.add(feature);
        final message = StringBuffer()
          ..write('[FloatingPalette] $feature not supported on ${capabilities.platform}');
        if (fallback != null) {
          message.write('. $fallback');
        }
        debugPrint(message.toString());
      }
      return false;

    case UnsupportedBehavior.ignore:
      return false;
  }
}