Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open window with data shared from parent app #24

Open
rorystephenson opened this issue Apr 26, 2024 · 0 comments
Open

Open window with data shared from parent app #24

rorystephenson opened this issue Apr 26, 2024 · 0 comments

Comments

@rorystephenson
Copy link
Contributor

rorystephenson commented Apr 26, 2024

Thanks for this plugin @jiusanzhou.

I have a use case where I want to open a window and immediately show some data passed from the parent app to the window. I don't want to start FloatwingPlugin until the user actually needs to see the window because some users never use windows so it is unnecessary to have it always running.

I am using the following code to start the app and show the window:

  Future<void> _initAsyncState() async {
    await FloatwingPlugin().initialize();

    // get permission first
    if (!await FloatwingPlugin().checkPermission()) {
      FloatwingPlugin().openPermissionSetting();
      return Future.value(false);
    }

    await FloatwingPlugin().isServiceRunning().then((running) async {
      if (!running)
        await FloatwingPlugin().startService().then((_) {
          print("start the background service success.");
        });
    });
    final existingWindow = FloatwingPlugin().windows[_window.id];
    if (existingWindow != null) {
      _window.share(
        'Re-opened at ${DateTime.now().second % 60}',
        name: 'myOverlayData',
      );
    } else {
      _window.on(EventType.WindowStarted, (window, data) {
        _window.share(
          'Opened at ${DateTime.now().second % 60}',
          name: 'myOverlayData',
        );
      });
      await _window.create(start: true);
    }
  }

My window is run via an entrypoint:

@pragma("vm:entry-point")
void overlayMain() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(NormalView().floatwing(app: true));
}

The NormalView widget listens to shared data in the initState like so:

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

    WidgetsBinding.instance.addPostFrameCallback((_) {
      _window = Window.of(context);
      _window?.onData((source, name, data) async {
        if (name != 'myOverlayData') return;

        debugPrint('*' * 80);
        debugPrint('Got data: $data, from: $source, name: $name');
        debugPrint('*' * 80);
        setState(() => _data = data);
      });
    });
  }

When I open the window the data is null, probably because the onData listener is registered after _window.share() gets called so it misses the data. If I run _initAsyncState() again it successfully passes the data with the 'Re-opened...' message, so the data sharing is working.

Is there a way to make sure the window is ready to receive the data instead so it doesn't get missed whilst the window is opening? If not, one potential solution for this problem would be to add a new EventType.WindowListen event which gets triggered when the window starts listening to data, then the shareData call can be placed in this callback.

WDYT?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant