Files
alan 3ca1bf4a00 Remove Cupertino UI branches
Keep the Material-only status, text, and scale paths while preserving public signatures and the Cupertino colors still required by the status API.
2026-11-11 01:00:35 +00:00

152 lines
4.4 KiB
Dart

import 'package:flutter/widgets.dart';
import 'package:shared_preferences/shared_preferences.dart';
const uiScaleKey = 'pipelines.ui-scale.v1';
const minUiScale = 0.5;
const maxUiScale = 2.0;
abstract interface class UiScaleStore {
Future<double?> load();
Future<bool> save(double scale);
}
class SharedUiScaleStore implements UiScaleStore {
const SharedUiScaleStore();
@override
Future<double?> load() async {
try {
return (await SharedPreferences.getInstance()).getDouble(uiScaleKey);
} catch (_) {
return null;
}
}
@override
Future<bool> save(double scale) async {
try {
return await (await SharedPreferences.getInstance()).setDouble(
uiScaleKey,
scale,
);
} catch (_) {
return false;
}
}
}
/// The device-local interface size: every layout dimension, text, icon and
/// dialog is drawn this many times larger. Saved per device, like the
/// animation speed.
class UiScaleController extends ChangeNotifier {
UiScaleController({UiScaleStore? store})
: _store = store ?? const SharedUiScaleStore();
static final instance = UiScaleController();
final UiScaleStore _store;
double _scale = 1;
int _revision = 0;
bool _disposed = false;
bool saveFailed = false;
double get scale => _scale;
Future<void> restore() async {
final revision = _revision;
final value = await _store.load();
if (_disposed || revision != _revision || value == null) return;
setScale(value);
}
void setScale(double value) {
if (_disposed || !value.isFinite) return;
final next = value.clamp(minUiScale, maxUiScale).toDouble();
_revision++;
saveFailed = false;
if (next == _scale) return;
_scale = next;
notifyListeners();
}
Future<void> save() async {
final revision = _revision;
final saved = await _store.save(_scale);
if (_disposed || revision != _revision) return;
saveFailed = !saved;
notifyListeners();
}
Future<void> reset() async {
setScale(1);
await save();
}
@override
void dispose() {
_disposed = true;
super.dispose();
}
}
/// Lays [child] out in a viewport [UiScaleController.scale] times smaller and
/// paints it scaled up to fill the window, so everything grows or shrinks
/// together. Use it as a `MaterialApp.builder` child:
/// the app's own MediaQuery is read from the window, so the scaled query has
/// to be provided below it. Hit testing follows the transform.
class UiScaleHost extends StatelessWidget {
const UiScaleHost({super.key, required this.child, this.controller});
final Widget child;
final UiScaleController? controller;
/// The scale applied above [context], 1 when there is none.
static double of(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType<_UiScaleScope>()?.scale ?? 1;
@override
Widget build(BuildContext context) {
final controller = this.controller ?? UiScaleController.instance;
return ListenableBuilder(
listenable: controller,
builder: (context, child) {
final scale = controller.scale;
if (scale == 1) return _UiScaleScope(scale: 1, child: child!);
final media = MediaQuery.of(context);
return LayoutBuilder(
builder: (context, box) {
final width = box.maxWidth / scale;
final height = box.maxHeight / scale;
return _UiScaleScope(
scale: scale,
child: MediaQuery(
data: media.copyWith(
size: media.size / scale,
devicePixelRatio: media.devicePixelRatio * scale,
padding: media.padding / scale,
viewPadding: media.viewPadding / scale,
viewInsets: media.viewInsets / scale,
systemGestureInsets: media.systemGestureInsets / scale,
),
child: ClipRect(
child: FittedBox(
fit: BoxFit.fill,
alignment: Alignment.topLeft,
child: SizedBox(width: width, height: height, child: child),
),
),
),
);
},
);
},
child: child,
);
}
}
class _UiScaleScope extends InheritedWidget {
const _UiScaleScope({required this.scale, required super.child});
final double scale;
@override
bool updateShouldNotify(_UiScaleScope oldWidget) => oldWidget.scale != scale;
}