From de6879679d18536d90430beaa0a5280443c7fb79 Mon Sep 17 00:00:00 2001 From: Viktor Lidholt Date: Sun, 15 Nov 2026 16:20:45 +0100 Subject: [PATCH] feat: The phone preview frame looks like a generic iPhone. The bezel is a metal outline, a metal rim, a dark grey line and a black frame, with side buttons that are beveled and turn with the device. The shadow is larger and darker, closer to a macOS window's. Co-Authored-By: Claude Haiku 5.5 --- lib/preview/phone_chassis_painter.dart | 181 +++++++++++++++++++++++++ lib/preview/preview_device.dart | 28 ++-- lib/ui/preview_pane.dart | 82 +++++------ 3 files changed, 233 insertions(+), 58 deletions(-) create mode 100644 lib/preview/phone_chassis_painter.dart diff --git a/lib/preview/phone_chassis_painter.dart b/lib/preview/phone_chassis_painter.dart new file mode 100644 index 0000000..1087321 --- /dev/null +++ b/lib/preview/phone_chassis_painter.dart @@ -0,0 +1,181 @@ +import 'dart:math' as math; +import 'dart:ui' as ui; + +import 'package:flutter/material.dart'; + +import 'preview_device.dart'; + +/// Paints the body of the generic phone around the preview: a drop shadow, a +/// metal outer rim, a dark grey band, the black inner frame, and the side +/// buttons with a beveled edge. +/// +/// The body sits inside the frame box by [previewPhoneHardwareOverhang] on +/// every side. Its geometry does not depend on orientation; only the buttons +/// turn with the device, so in landscape they move to the top and bottom edges +/// the way they would on a phone turned on its side. +class PhoneChassisPainter extends CustomPainter { + const PhoneChassisPainter({required this.landscape, required this.dark}); + + /// Whether the device is turned on its side. + final bool landscape; + + /// Whether the app shell is dark. Only the shadow changes with it: the + /// device itself is the same graphite in both. + final bool dark; + + // Outside in: thin black outline, metal rim, dark grey line, black frame. + static const _outline = Color(0xFF000000); + static const _metalLight = Color(0xFFB4B4BA); + static const _metalMid = Color(0xFF6E6E74); + static const _metalDark = Color(0xFF8A8A90); + static const _grey = Color(0xFF2E2E32); + static const _bezel = Color(0xFF060607); + + static const _bevelLight = Color(0x66FFFFFF); + static const _bevelShade = Color(0x73000000); + + static const _outlineWidth = 1.2; + static const _metalWidth = 1.2; + // The black frame is the rest of the bezel; the grey line is a third of it. + static const _greyWidth = + (previewPhoneBezel - _outlineWidth - _metalWidth) / 4; + + @override + void paint(Canvas canvas, Size size) { + final body = Rect.fromLTWH( + previewPhoneHardwareOverhang, + previewPhoneHardwareOverhang, + size.width - previewPhoneHardwareOverhang * 2, + size.height - previewPhoneHardwareOverhang * 2, + ); + final bodyRadius = Radius.circular(previewPhoneOuterCornerRadius); + final bodyRRect = RRect.fromRectAndRadius(body, bodyRadius); + + _paintShadow(canvas, bodyRRect); + + // The buttons go under the body, so they appear to come out from behind + // its edge and never paint across the metal rim. + _paintButtons(canvas, size); + + // Each layer is the one outside it inset by its width, so the rings stack + // from the outline inward: black outline, metal, grey line, black frame. + canvas.drawRRect(bodyRRect, Paint()..color = _outline); + final metalRRect = bodyRRect.deflate(_outlineWidth); + canvas.drawRRect( + metalRRect, + Paint() + ..shader = ui.Gradient.linear( + body.topLeft, + body.bottomRight, + const [_metalLight, _metalMid, _metalDark], + const [0.0, 0.55, 1.0], + ), + ); + final greyRRect = metalRRect.deflate(_metalWidth); + canvas.drawRRect(greyRRect, Paint()..color = _grey); + canvas.drawRRect(greyRRect.deflate(_greyWidth), Paint()..color = _bezel); + } + + void _paintShadow(Canvas canvas, RRect body) { + final alpha = dark ? 0.75 : 0.45; + // The wide shadow sits low, like light from above; the tight one hugs the + // edge so the device still reads against a light backdrop. + canvas.drawRRect( + body.shift(const Offset(0, 10)), + Paint() + ..color = Colors.black.withValues(alpha: alpha) + ..maskFilter = const MaskFilter.blur(BlurStyle.normal, 14), + ); + canvas.drawRRect( + body.shift(const Offset(0, 2)), + Paint() + ..color = Colors.black.withValues(alpha: 0.22) + ..maskFilter = const MaskFilter.blur(BlurStyle.normal, 3), + ); + } + + /// Buttons are laid out on the portrait device and then turned into place. + void _paintButtons(Canvas canvas, Size size) { + final portrait = landscape ? Size(size.height, size.width) : size; + final buttons = _buttonRects(portrait); + + canvas.save(); + if (landscape) { + // Maps portrait (u, v) to landscape (width - v, u): the left edge becomes + // the top edge, as on a phone turned clockwise. + canvas.translate(size.width, 0); + canvas.rotate(math.pi / 2); + } + for (final rect in buttons) { + final button = RRect.fromRectAndRadius(rect, const Radius.circular(1.5)); + // Body of the button: lit from above, darker toward the bottom. + canvas.drawRRect( + button, + Paint() + ..shader = ui.Gradient.linear( + rect.topCenter, + rect.bottomCenter, + const [_metalLight, _metalMid, _metalDark], + const [0.0, 0.55, 1.0], + ), + ); + // Bevel: light along the top, shade along the bottom, inside the outline. + canvas.drawRRect( + button.deflate(0.9), + Paint() + ..style = PaintingStyle.stroke + ..strokeWidth = 0.5 + ..shader = ui.Gradient.linear( + rect.topCenter, + rect.bottomCenter, + const [_bevelLight, Color(0x00FFFFFF), _bevelShade], + const [0.0, 0.5, 1.0], + ), + ); + // Thin black outline, as the frame has. + canvas.drawRRect( + button.deflate(0.3), + Paint() + ..style = PaintingStyle.stroke + ..strokeWidth = 0.6 + ..color = _outline, + ); + } + canvas.restore(); + } + + /// Portrait positions: volume and action on the left, power on the right. + /// Each rect runs from the outer frame edge to a point just inside the body, + /// so it looks attached rather than floating. + List _buttonRects(Size portrait) { + const o = previewPhoneHardwareOverhang; + final bodyTop = o; + final bodyHeight = portrait.height - o * 2; + // Reaches two points past the body edge, under the metal, so no seam shows. + final attach = o + 2; + + Rect left(double top, double height) => Rect.fromLTRB( + 0, + bodyTop + bodyHeight * top, + attach, + bodyTop + bodyHeight * (top + height), + ); + Rect right(double top, double height) => Rect.fromLTRB( + portrait.width - attach, + bodyTop + bodyHeight * top, + portrait.width, + bodyTop + bodyHeight * (top + height), + ); + + return [ + left(0.14, 0.05), // action (silent) switch + left(0.21, 0.09), // volume up + left(0.31, 0.09), // volume down + right(0.24, 0.14), // side button + ]; + } + + @override + bool shouldRepaint(PhoneChassisPainter oldDelegate) => + oldDelegate.landscape != landscape || oldDelegate.dark != dark; +} diff --git a/lib/preview/preview_device.dart b/lib/preview/preview_device.dart index 5ef8deb..f56cfab 100644 --- a/lib/preview/preview_device.dart +++ b/lib/preview/preview_device.dart @@ -13,14 +13,14 @@ enum PreviewViewMode { phonePortrait, phoneLandscape, desktop } extension PreviewViewModeX on PreviewViewMode { PreviewDisplayMode get displayMode => switch (this) { PreviewViewMode.desktop => PreviewDisplayMode.desktop, - PreviewViewMode.phonePortrait || PreviewViewMode.phoneLandscape => - PreviewDisplayMode.phone, + PreviewViewMode.phonePortrait || + PreviewViewMode.phoneLandscape => PreviewDisplayMode.phone, }; PreviewOrientation get orientation => switch (this) { PreviewViewMode.phoneLandscape => PreviewOrientation.landscape, - PreviewViewMode.desktop || PreviewViewMode.phonePortrait => - PreviewOrientation.portrait, + PreviewViewMode.desktop || + PreviewViewMode.phonePortrait => PreviewOrientation.portrait, }; } @@ -37,17 +37,27 @@ Size previewPhoneViewport(PreviewOrientation orientation) { }; } -/// Outer frame size including bezel padding around the viewport. +/// Outer frame size: the viewport plus the bezel and the hardware overhang on +/// every side. The overhang is symmetric so the frame's box is the same in +/// both orientations; only the buttons on it turn with the device. Size previewPhoneFrameSize(PreviewOrientation orientation) { - const bezel = previewPhoneBezel * 2; + const edge = (previewPhoneBezel + previewPhoneHardwareOverhang) * 2; final viewport = previewPhoneViewport(orientation); - return Size(viewport.width + bezel, viewport.height + bezel); + return Size(viewport.width + edge, viewport.height + edge); } +/// Black glass between the screen and the metal edge. const previewPhoneBezel = 10.0; + +/// How far the side buttons stand proud of the body. The frame box includes +/// it, so the buttons are drawn inside the window's own bounds. +const previewPhoneHardwareOverhang = 3.0; + /// Screen corner radius (inner clip). -const previewPhoneCornerRadius = 28.0; -/// Outer frame radius — [previewPhoneCornerRadius] + [previewPhoneBezel] for even bezel at corners. +const previewPhoneCornerRadius = 36.0; + +/// Outer body radius — [previewPhoneCornerRadius] + [previewPhoneBezel], so +/// the bezel is an even width around the corners. const previewPhoneOuterCornerRadius = previewPhoneCornerRadius + previewPhoneBezel; const previewPhonePreviewPadding = 16.0; diff --git a/lib/ui/preview_pane.dart b/lib/ui/preview_pane.dart index 8b10ece..c0de457 100644 --- a/lib/ui/preview_pane.dart +++ b/lib/ui/preview_pane.dart @@ -6,6 +6,7 @@ import 'package:shad/shad.dart'; import 'package:webview_cef/webview_cef.dart'; import '../platform/compact_window.dart'; +import '../preview/phone_chassis_painter.dart'; import '../preview/preview_bootstrap.dart'; import '../preview/preview_cache_purge.dart'; import '../preview/preview_device.dart'; @@ -226,9 +227,7 @@ class PreviewPaneState extends State { ScriptInjectTime.LOAD_START, ), ) - ..add( - UserScript(previewBootstrapScript, ScriptInjectTime.LOAD_START), - ); + ..add(UserScript(previewBootstrapScript, ScriptInjectTime.LOAD_START)); final controller = WebviewManager().createWebView( loading: const SizedBox.shrink(), @@ -242,7 +241,8 @@ class PreviewPaneState extends State { _appliedBrightness = widget.mode.previewBrightness; _lastReloadToken = widget.process.previewReloadToken; final projectPath = widget.process.session.projectPath; - final staleProfile = projectPath != null && + final staleProfile = + projectPath != null && _cefProfileProjectPath != null && _cefProfileProjectPath != projectPath; if (staleProfile) { @@ -290,9 +290,11 @@ class PreviewPaneState extends State { _lastReloadToken = 0; _appliedBrightness = null; _loadedUrl = null; - unawaited(_disposeController().then((_) { - if (mounted) setState(() {}); - })); + unawaited( + _disposeController().then((_) { + if (mounted) setState(() {}); + }), + ); } void _onWebViewReadyChanged() { @@ -496,10 +498,7 @@ class PreviewPaneState extends State { return framed; } - return _PhonePreviewViewport( - viewport: viewport, - child: framed, - ); + return _PhonePreviewViewport(viewport: viewport, child: framed); } Widget _buildSurfaceStack({required Size viewport}) { @@ -507,9 +506,7 @@ class PreviewPaneState extends State { final phase = widget.process.phase; final processError = widget.process.error; final overlayError = _error ?? processError; - final cefReady = controller != null && - !_initializing && - controller.value; + final cefReady = controller != null && !_initializing && controller.value; final cefLoading = _shouldLoad && !cefReady && overlayError == null; Widget surface; @@ -521,10 +518,9 @@ class PreviewPaneState extends State { return const ColoredBox(color: Color(0xFF12122A)); } final viewMode = widget.mode.viewMode; - final phoneViewport = - viewMode.displayMode == PreviewDisplayMode.phone - ? viewport - : null; + final phoneViewport = viewMode.displayMode == PreviewDisplayMode.phone + ? viewport + : null; return PreviewSurfaceSync( controller: controller, explicitSize: phoneViewport, @@ -713,10 +709,7 @@ class _PhonePreviewViewport extends StatelessWidget { ), ); if (overlay != null) { - content = Stack( - fit: StackFit.expand, - children: [content, overlay!], - ); + content = Stack(fit: StackFit.expand, children: [content, overlay!]); } return Padding( @@ -732,10 +725,7 @@ class _PhonePreviewViewport extends StatelessWidget { } class _PhoneDeviceFrame extends StatelessWidget { - const _PhoneDeviceFrame({ - required this.viewport, - required this.child, - }); + const _PhoneDeviceFrame({required this.viewport, required this.child}); final Size viewport; final Widget child; @@ -743,30 +733,24 @@ class _PhoneDeviceFrame extends StatelessWidget { @override Widget build(BuildContext context) { final isDarkShell = ShadTheme.of(context).brightness == Brightness.dark; - final bezelColor = - isDarkShell ? const Color(0xFF2A2A2E) : const Color(0xFF1C1C1E); - - return Container( - padding: const EdgeInsets.all(previewPhoneBezel), - decoration: BoxDecoration( - color: bezelColor, - borderRadius: BorderRadius.circular(previewPhoneOuterCornerRadius), - boxShadow: [ - BoxShadow( - color: Colors.black.withValues(alpha: isDarkShell ? 0.55 : 0.28), - blurRadius: 28, - offset: const Offset(0, 10), - ), - BoxShadow( - color: Colors.black.withValues(alpha: 0.12), - blurRadius: 6, - offset: const Offset(0, 2), + final landscape = viewport.width > viewport.height; + const inset = previewPhoneBezel + previewPhoneHardwareOverhang; + final screenRadius = BorderRadius.circular(previewPhoneCornerRadius); + + return CustomPaint( + painter: PhoneChassisPainter(landscape: landscape, dark: isDarkShell), + child: Padding( + padding: const EdgeInsets.all(inset), + child: DecoratedBox( + // The glass edge: a hairline over the screen where it meets the + // bezel, which is what makes the screen read as set into the body. + position: DecorationPosition.foreground, + decoration: BoxDecoration( + borderRadius: screenRadius, + border: Border.all(color: Colors.black.withValues(alpha: 0.7)), ), - ], - ), - child: ClipRRect( - borderRadius: BorderRadius.circular(previewPhoneCornerRadius), - child: child, + child: ClipRRect(borderRadius: screenRadius, child: child), + ), ), ); }