diff --git a/app/lib/application/app.dart b/app/lib/application/app.dart index bf2819ec..2f343a33 100644 --- a/app/lib/application/app.dart +++ b/app/lib/application/app.dart @@ -1,6 +1,7 @@ import '../ui/adaptive/platform.dart'; import '../ui/theme/app_font_controller.dart'; import '../ui/theme/app_theme.dart'; +import '../ui/scale/ui_scale.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; @@ -58,12 +59,14 @@ class MyApp extends StatelessWidget { if (router != null) { return CupertinoApp.router( title: 'Pipelines', + builder: _scaled, theme: theme, routerConfig: router, ); } return CupertinoApp( title: 'Pipelines', + builder: _scaled, theme: theme, home: AdaptiveShell( destinationBodies: destinationBodies, @@ -76,6 +79,7 @@ class MyApp extends StatelessWidget { if (router != null) { return MaterialApp.router( title: 'Pipelines', + builder: _scaled, theme: appLightTheme(fontFamily: fontFamily), darkTheme: appDarkTheme(fontFamily: fontFamily), themeMode: ThemeMode.system, @@ -84,6 +88,7 @@ class MyApp extends StatelessWidget { } return MaterialApp( title: 'Pipelines', + builder: _scaled, theme: appLightTheme(fontFamily: fontFamily), darkTheme: appDarkTheme(fontFamily: fontFamily), themeMode: ThemeMode.system, @@ -96,4 +101,7 @@ class MyApp extends StatelessWidget { } bool get _platformIsCupertino => prefersCupertino(); + + static Widget _scaled(BuildContext context, Widget? child) => + UiScaleHost(child: child ?? const SizedBox.shrink()); } diff --git a/app/lib/application/bootstrap.dart b/app/lib/application/bootstrap.dart index 07022de2..222605cd 100644 --- a/app/lib/application/bootstrap.dart +++ b/app/lib/application/bootstrap.dart @@ -1,4 +1,5 @@ import '../core/session/server_session.dart'; +import '../ui/scale/ui_scale.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/widgets.dart'; @@ -58,6 +59,7 @@ Future runPipelinesApp({ // The device's chosen font loads beside the session; a slow download does // not hold the first frame, and applies when it lands. final animationSpeed = AnimationSpeedController.instance.restore(); + final uiScale = UiScaleController.instance.restore(); final quickSwitchAnimation = QuickSwitchAnimationController.instance .restore(); final font = AppFontController.instance.restore(wait: appFontStartupWait); @@ -65,6 +67,7 @@ Future runPipelinesApp({ await ServerSession.load(); await font; await animationSpeed; + await uiScale; await quickSwitchAnimation; final server = await resolveLaunchServer( base: base, diff --git a/app/lib/application/pipelines_root.dart b/app/lib/application/pipelines_root.dart index 65974169..bce7a570 100644 --- a/app/lib/application/pipelines_root.dart +++ b/app/lib/application/pipelines_root.dart @@ -16,6 +16,7 @@ import '../features/onboarding/onboarding_flow.dart'; import '../features/settings/server_connections.dart'; import '../features/settings/saved_servers_section.dart'; import '../features/settings/settings_store.dart'; +import '../ui/scale/ui_scale.dart'; import '../ui/theme/app_font_controller.dart'; import '../ui/theme/app_theme.dart'; import 'pipelines_app.dart'; @@ -369,12 +370,16 @@ class _PipelinesRootState extends State { builder: (context, _) => _cupertino ? CupertinoApp( title: 'Pipelines', + builder: (context, child) => + UiScaleHost(child: child ?? const SizedBox.shrink()), theme: appCupertinoThemeWith(fontFamily: fonts.family), debugShowCheckedModeBanner: false, home: flow, ) : MaterialApp( title: 'Pipelines', + builder: (context, child) => + UiScaleHost(child: child ?? const SizedBox.shrink()), theme: appLightTheme(fontFamily: fonts.family), darkTheme: appDarkTheme(fontFamily: fonts.family), debugShowCheckedModeBanner: false, diff --git a/app/lib/features/chat/conversation/chat_composer_bar.dart b/app/lib/features/chat/conversation/chat_composer_bar.dart index e4661aa7..45524c5a 100644 --- a/app/lib/features/chat/conversation/chat_composer_bar.dart +++ b/app/lib/features/chat/conversation/chat_composer_bar.dart @@ -4,6 +4,8 @@ import 'dart:math' as math; import 'package:flutter/cupertino.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; + +import '../../../ui/scale/ui_scale.dart'; import 'chat_stop_icon.dart'; import 'package:flutter/services.dart'; @@ -1176,9 +1178,9 @@ class _ChatComposerBarState extends State { int _maxLines(BuildContext context) { final media = MediaQuery.of(context); // The scaffold hides the keyboard from its body; the window still has it. - final keyboard = MediaQueryData.fromView( - View.of(context), - ).viewInsets.bottom; + final keyboard = + MediaQueryData.fromView(View.of(context)).viewInsets.bottom / + UiScaleHost.of(context); final visible = media.size.height - keyboard; final lineHeight = MediaQuery.textScalerOf(context).scale(22); final fit = (visible * 0.4 / lineHeight).floor(); diff --git a/app/lib/features/settings/settings_scale_section.dart b/app/lib/features/settings/settings_scale_section.dart new file mode 100644 index 00000000..435e9485 --- /dev/null +++ b/app/lib/features/settings/settings_scale_section.dart @@ -0,0 +1,120 @@ +import 'dart:async'; + +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +import '../../ui/scale/ui_scale.dart'; +import 'settings_group.dart'; +import 'settings_theme.dart'; + +/// "Interface size": a 0.5×–2.0× slider for the whole app on this device. +/// The value previews beside the slider while dragging and applies when the +/// drag ends, so the slider does not resize under the pointer. +class SettingsScaleSection extends StatefulWidget { + const SettingsScaleSection({super.key, this.controller}); + final UiScaleController? controller; + + @override + State createState() => _SettingsScaleSectionState(); +} + +class _SettingsScaleSectionState extends State { + double? _draft; + + UiScaleController get _controller => + widget.controller ?? UiScaleController.instance; + + void _apply(double value) { + setState(() => _draft = null); + _controller.setScale(value); + unawaited(_controller.save()); + } + + @override + Widget build(BuildContext context) => ListenableBuilder( + listenable: _controller, + builder: (context, _) { + final cupertino = usesCupertinoControls(context); + final value = _draft ?? _controller.scale; + final label = '${value.toStringAsFixed(1)}×'; + final reset = _controller.scale == 1 && _draft == null + ? null + : () { + setState(() => _draft = null); + unawaited(_controller.reset()); + }; + void drag(double next) => setState(() => _draft = next); + return SettingsGroup( + title: 'Interface size', + footer: Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + const Text( + 'Scales text, icons and spacing everywhere on this device. ' + 'Applies when you let go. Default 1.0×.', + ), + if (_controller.saveFailed) + Text( + 'Could not save; applies until reload.', + style: TextStyle(color: settingsErrorColor(context)), + ), + ], + ), + children: [ + SettingsGroupItem( + child: Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + Wrap( + alignment: WrapAlignment.spaceBetween, + crossAxisAlignment: WrapCrossAlignment.center, + children: [ + Text(label, style: settingsTextStyle(context, title: true)), + cupertino + ? CupertinoButton( + onPressed: reset, + child: const Text('Reset'), + ) + : TextButton( + onPressed: reset, + child: const Text('Reset'), + ), + ], + ), + Semantics( + label: 'Interface size', + child: cupertino + ? CupertinoSlider( + key: const Key('settings-ui-scale'), + value: value, + min: minUiScale, + max: maxUiScale, + divisions: 15, + onChanged: drag, + onChangeEnd: _apply, + ) + : Slider( + key: const Key('settings-ui-scale'), + value: value, + min: minUiScale, + max: maxUiScale, + divisions: 15, + label: label, + semanticFormatterCallback: (v) => + '${v.toStringAsFixed(1)} times', + onChanged: drag, + onChangeEnd: _apply, + ), + ), + const Wrap( + alignment: WrapAlignment.spaceBetween, + children: [Text('0.5× · Smaller'), Text('2.0× · Larger')], + ), + ], + ), + ), + ], + ); + }, + ); +} diff --git a/app/lib/features/settings/settings_screen.dart b/app/lib/features/settings/settings_screen.dart index dc123cf0..9587eafe 100644 --- a/app/lib/features/settings/settings_screen.dart +++ b/app/lib/features/settings/settings_screen.dart @@ -12,6 +12,7 @@ import 'settings_controller.dart'; import 'settings_controls.dart'; import 'settings_font_section.dart'; import 'settings_animation_section.dart'; +import 'settings_scale_section.dart'; import '../../ui/motion/animation_speed.dart'; import '../../ui/theme/app_font_controller.dart'; import 'settings_group.dart'; diff --git a/app/lib/features/settings/settings_topic_pages.dart b/app/lib/features/settings/settings_topic_pages.dart index 058d0d9a..51229081 100644 --- a/app/lib/features/settings/settings_topic_pages.dart +++ b/app/lib/features/settings/settings_topic_pages.dart @@ -6,6 +6,7 @@ extension _SettingsTopicPages on _SettingsScreenState { return { SettingsTopic.appearance: (_) => SettingsPageBody( children: [ + const SettingsScaleSection(), SettingsAnimationSection( controller: widget.animationSpeed ?? AnimationSpeedController.instance, diff --git a/app/lib/ui/scale/ui_scale.dart b/app/lib/ui/scale/ui_scale.dart new file mode 100644 index 00000000..6f7b1cfc --- /dev/null +++ b/app/lib/ui/scale/ui_scale.dart @@ -0,0 +1,151 @@ +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 load(); + Future save(double scale); +} + +class SharedUiScaleStore implements UiScaleStore { + const SharedUiScaleStore(); + + @override + Future load() async { + try { + return (await SharedPreferences.getInstance()).getDouble(uiScaleKey); + } catch (_) { + return null; + } + } + + @override + Future 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 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 save() async { + final revision = _revision; + final saved = await _store.save(_scale); + if (_disposed || revision != _revision) return; + saveFailed = !saved; + notifyListeners(); + } + + Future 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`/`CupertinoApp.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; +} diff --git a/app/test/ui/scale/ui_scale_test.dart b/app/test/ui/scale/ui_scale_test.dart new file mode 100644 index 00000000..55c94db4 --- /dev/null +++ b/app/test/ui/scale/ui_scale_test.dart @@ -0,0 +1,108 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/settings/settings_scale_section.dart'; +import 'package:pipelines_app/ui/scale/ui_scale.dart'; + +class _Store implements UiScaleStore { + double? saved; + double? stored; + @override + Future load() async => stored; + @override + Future save(double scale) async { + saved = scale; + return true; + } +} + +void main() { + test('restores, clamps and saves the scale', () async { + final store = _Store()..stored = 3; + final controller = UiScaleController(store: store); + await controller.restore(); + expect(controller.scale, maxUiScale); + controller.setScale(0.1); + expect(controller.scale, minUiScale); + await controller.save(); + expect(store.saved, minUiScale); + await controller.reset(); + expect(controller.scale, 1); + expect(store.saved, 1); + }); + + testWidgets('the host lays out smaller and paints scaled to fill', ( + tester, + ) async { + tester.view.physicalSize = const Size(800, 600); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + final controller = UiScaleController(store: _Store()); + Size? seen; + var taps = 0; + await tester.pumpWidget( + MaterialApp( + builder: (context, child) => + UiScaleHost(controller: controller, child: child!), + home: Builder( + builder: (context) { + seen = MediaQuery.sizeOf(context); + return Align( + alignment: Alignment.topLeft, + child: GestureDetector( + onTap: () => taps++, + behavior: HitTestBehavior.opaque, + child: const SizedBox(key: Key('box'), width: 100, height: 100), + ), + ); + }, + ), + ), + ); + expect(seen, const Size(800, 600)); + controller.setScale(2); + await tester.pump(); + expect(seen, const Size(400, 300)); + // The 100×100 box now covers 200×200 window pixels and is tappable there. + final rect = tester.getRect(find.byKey(const Key('box'))); + expect(rect.size, const Size(200, 200)); + await tester.tapAt(const Offset(180, 180)); + expect(taps, 1); + controller.setScale(0.5); + await tester.pump(); + expect(seen, const Size(1600, 1200)); + expect( + tester.getRect(find.byKey(const Key('box'))).size, + const Size(50, 50), + ); + }); + + testWidgets('the settings slider applies when the drag ends', (tester) async { + final store = _Store(); + final controller = UiScaleController(store: store); + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: SingleChildScrollView( + child: SettingsScaleSection(controller: controller), + ), + ), + ), + ); + expect(find.text('1.0×'), findsOneWidget); + final slider = tester.widget( + find.byKey(const Key('settings-ui-scale')), + ); + slider.onChanged!(1.5); + await tester.pump(); + expect(find.text('1.5×'), findsOneWidget); + expect(controller.scale, 1, reason: 'dragging only previews'); + slider.onChangeEnd!(1.5); + await tester.pump(); + expect(controller.scale, 1.5); + await tester.pump(); + expect(store.saved, 1.5); + await tester.tap(find.text('Reset')); + await tester.pump(); + expect(controller.scale, 1); + }); +}