alexmercerind / flutter_acrylic

Flutter library for window acrylic, mica & transparency effects.
MIT License
605 stars 54 forks source link

macOS - Running in release mode causes program to hang #78

Closed kacikgoez closed 1 year ago

kacikgoez commented 1 year ago

Hey, when I create a fresh Flutter application in VSC, like this:

import 'package:flutter/material.dart';
import 'package:flutter_acrylic/window.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Window.initialize();
  runApp(const MainApp());
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
        body: Center(
          child: Text('Hello World!'),
        ),
      ),
    );
  }
}

I can run it in debug mode, but when I run flutter run --release, the application starts hanging and I get a permanent spinning pinwheel. Like I said, this is a fresh Flutter project where I only changed the Runner.xcodeproj to macOS version 10.15 and the Podfile to 10.15, because it's required by macos_window_utils, and the dependencies in pubspec.yaml:

name: flutter_acrylic_broken
description: A new Flutter project.
publish_to: 'none'
version: 0.1.0

environment:
  sdk: '>=2.19.6 <3.0.0'

dependencies:
  flutter:
    sdk: flutter
  flutter_acrylic: ^1.1.2

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^2.0.0

flutter:
  uses-material-design: true

Here's my flutter doctor -v:

[✓] Flutter (Channel stable, 3.7.12, on macOS 13.2 22D49 darwin-arm64, locale en-DE)
    • Flutter version 3.7.12 on channel stable at /Users/kacikgoz/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 4d9e56e694 (3 weeks ago), 2023-04-17 21:47:46 -0400
    • Engine revision 1a65d409c7
    • Dart version 2.19.6
    • DevTools version 2.20.1

[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14E222b
    • CocoaPods version 1.12.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).

[✓] IntelliJ IDEA Community Edition (version 2022.3.3)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.78.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.62.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-arm64   • macOS 13.2 22D49 darwin-arm64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 112.0.5615.137

[✓] HTTP Host Availability
    • All required HTTP hosts are available
Adrian-Samoticha commented 1 year ago

I have received an email showing that you have posted the following comment:

Interesting, I just updated my macOS, rebooted and now everything works. You can delete this issue, not sure what caused it.

It doesn’t show up here for some reason, but I’ll just close this issue for now.

kacikgoez commented 1 year ago

@Adrian-Samoticha no, it still happens, I might have ran it in debug mode when I rebooted or there's some kind of probabilistic nature to it. For now I switched to window_manager

Adrian-Samoticha commented 1 year ago

Do you get the same issue when running the example project?

kacikgoez commented 1 year ago

Interesting, the example works. I played around a bit, and found one example that works and that doesn't work. If I remove the Stack from SidebarFrame, the app freezes in release mode. This is the working example in release mode:

import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';

import 'package:flutter_acrylic/flutter_acrylic.dart';
import 'package:bitsdojo_window/bitsdojo_window.dart';
import 'package:flutter_acrylic_example/widgets/sidebar_frame/sidebar_frame.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Window.initialize();
  if (Platform.isWindows) {
    await Window.hideWindowControls();
  }
  runApp(MyApp());
  if (Platform.isWindows) {
    doWhenWindowReady(() {
      appWindow
        ..minSize = Size(640, 360)
        ..size = Size(720, 540)
        ..alignment = Alignment.center
        ..show();
    });
  }
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        splashFactory: InkRipple.splashFactory,
      ),
      darkTheme: ThemeData.dark().copyWith(
        splashFactory: InkRipple.splashFactory,
      ),
      themeMode: ThemeMode.dark,
      home: MyAppBody(),
    );
  }
}

class MyAppBody extends StatefulWidget {
  MyAppBody({Key? key}) : super(key: key);

  @override
  MyAppBodyState createState() => MyAppBodyState();
}

class MyAppBodyState extends State<MyAppBody> {
  WindowEffect effect = WindowEffect.transparent;
  Color color = Platform.isWindows ? Color(0xCC222222) : Colors.transparent;
  MacOSBlurViewState macOSBlurViewState =
      MacOSBlurViewState.followsWindowActiveState;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // The [TitlebarSafeArea] widget is required when running on macOS and
    // enabling the full-size content view using
    // [Window.setFullSizeContentView]. It ensures that its child is not covered
    // by the macOS title bar.
    return TitlebarSafeArea(
      child: SidebarFrame(
        macOSBlurViewState: macOSBlurViewState,
        sidebar: SizedBox.expand(
          child: Scaffold(),
        ),
        child: Stack(
          children: [Text("yo")],
        ),
      ),
    );
  }
}

This is the broken example in release mode, I only changed the child of the SidebarFrame. I assume this is unintended behavior?

import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';

import 'package:flutter_acrylic/flutter_acrylic.dart';
import 'package:bitsdojo_window/bitsdojo_window.dart';
import 'package:flutter_acrylic_example/widgets/sidebar_frame/sidebar_frame.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Window.initialize();
  if (Platform.isWindows) {
    await Window.hideWindowControls();
  }
  runApp(MyApp());
  if (Platform.isWindows) {
    doWhenWindowReady(() {
      appWindow
        ..minSize = Size(640, 360)
        ..size = Size(720, 540)
        ..alignment = Alignment.center
        ..show();
    });
  }
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        splashFactory: InkRipple.splashFactory,
      ),
      darkTheme: ThemeData.dark().copyWith(
        splashFactory: InkRipple.splashFactory,
      ),
      themeMode: ThemeMode.dark,
      home: MyAppBody(),
    );
  }
}

class MyAppBody extends StatefulWidget {
  MyAppBody({Key? key}) : super(key: key);

  @override
  MyAppBodyState createState() => MyAppBodyState();
}

class MyAppBodyState extends State<MyAppBody> {
  WindowEffect effect = WindowEffect.transparent;
  Color color = Platform.isWindows ? Color(0xCC222222) : Colors.transparent;
  MacOSBlurViewState macOSBlurViewState =
      MacOSBlurViewState.followsWindowActiveState;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // The [TitlebarSafeArea] widget is required when running on macOS and
    // enabling the full-size content view using
    // [Window.setFullSizeContentView]. It ensures that its child is not covered
    // by the macOS title bar.
    return TitlebarSafeArea(
      child: SidebarFrame(
        macOSBlurViewState: macOSBlurViewState,
        sidebar: SizedBox.expand(
          child: Scaffold(),
        ),
        child: Text("yo"),
      ),
    );
  }
}
Adrian-Samoticha commented 1 year ago

Both of your examples as well as the example project freeze in release mode on my machine. Interestingly, the same thing happens in Flutter 3.7.0, which rules out a breaking change in Flutter. I genuinely have no idea what is causing the issue and will need to investigate further.

Adrian-Samoticha commented 1 year ago

When running it in release via Xcode like this:

image image

and then running it through Xcode, it magically starts working correctly, even when running it via flutter run --release afterward.

I genuinely have no idea why this is the case.

EDIT: I did see some errors appear on my console, though, so maybe I should check if those are related to the problem.

kacikgoez commented 1 year ago

I'm trying to figure it out as well, I don't know anything about Swift, but I can replicate in Xcode by running the broken Flutter build executable in Xcode's debugger, so first flutter build macos and then Xcode > Debug > Debug Executable. The only error being shown to me is caused by bitsdojo_window_macos, if I remove bitsdojo and rebuild it won't print anything. The code freezes when adding the flutterViewController to MacOSWindowUtilsViewController.

Adrian-Samoticha commented 1 year ago

flutter_acrylic 1.1.2 no longer needs you to edit any Swift files. You should be able to just set your build configuration to “Release” in Xcode and run it there (at least that worked for me).

kacikgoez commented 1 year ago

Yes, when I run it in Xcode it works for me as well. Debugging the broken Flutter build at least gives some insight on what's happening