Skip to content

Commit

Permalink
testing multiwindow support
Browse files Browse the repository at this point in the history
  • Loading branch information
IoannisMaras committed Jul 9, 2022
1 parent a86d6f4 commit 712d0dd
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 90 deletions.
147 changes: 147 additions & 0 deletions lib/event_widget.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import 'package:desktop_multi_window/desktop_multi_window.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:ui';

class EventWidget extends StatefulWidget {
const EventWidget({Key? key, required this.controller}) : super(key: key);

final WindowController controller;

@override
State<EventWidget> createState() => _EventWidgetState();
}

class MessageItem {
const MessageItem({this.content, required this.from, required this.method});

final int from;
final dynamic content;
final String method;

@override
String toString() {
return '$method($from): $content';
}

@override
int get hashCode => Object.hash(from, content, method);

@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
if (other.runtimeType != runtimeType) {
return false;
}
final MessageItem typedOther = other as MessageItem;
return typedOther.from == from && typedOther.content == content;
}
}

class _EventWidgetState extends State<EventWidget> {
final messages = <MessageItem>[];

final textInputController = TextEditingController();

final windowInputController = TextEditingController();

@override
void initState() {
super.initState();
DesktopMultiWindow.setMethodHandler(_handleMethodCallback);
}

@override
dispose() {
DesktopMultiWindow.setMethodHandler(null);
super.dispose();
}

Future<dynamic> _handleMethodCallback(
MethodCall call, int fromWindowId) async {
if (call.arguments.toString() == "ping") {
return "pong";
}
setState(() {
messages.insert(
0,
MessageItem(
from: fromWindowId,
method: call.method,
content: call.arguments,
),
);
});
}

@override
Widget build(BuildContext context) {
void submit() async {
final text = textInputController.text;
if (text.isEmpty) {
return;
}
final windowId = int.tryParse(windowInputController.text);
textInputController.clear();
final result =
await DesktopMultiWindow.invokeMethod(windowId!, "onSend", text);
debugPrint("onSend result: $result");
}

return Column(
children: [
Expanded(
child: ListView.builder(
itemCount: messages.length,
reverse: true,
itemBuilder: (context, index) =>
_MessageItemWidget(item: messages[index]),
),
),
Row(
children: [
SizedBox(
width: 100,
child: TextField(
controller: windowInputController,
decoration: const InputDecoration(
labelText: 'Window ID',
),
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
),
),
Expanded(
child: TextField(
controller: textInputController,
decoration: const InputDecoration(
hintText: 'Enter message',
),
onSubmitted: (text) => submit(),
),
),
IconButton(
icon: const Icon(Icons.send),
onPressed: submit,
),
],
),
],
);
}
}

class _MessageItemWidget extends StatelessWidget {
const _MessageItemWidget({Key? key, required this.item}) : super(key: key);

final MessageItem item;

@override
Widget build(BuildContext context) {
return ListTile(
title: Text("${item.method}(${item.from})"),
subtitle: Text(item.content.toString()),
);
}
}
194 changes: 104 additions & 90 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,115 +1,129 @@
import 'dart:convert';

import 'package:collection/collection.dart';
import 'package:desktop_lifecycle/desktop_lifecycle.dart';
import 'package:desktop_multi_window/desktop_multi_window.dart';
import 'package:flutter/material.dart';
import 'package:atlantas_windows/event_widget.dart';
import 'dart:ui';

void main() {
runApp(const MyApp());
void main(List<String> args) {
if (args.firstOrNull == 'multi_window') {
final windowId = int.parse(args[1]);
final argument = args[2].isEmpty
? const {}
: jsonDecode(args[2]) as Map<String, dynamic>;
runApp(_ExampleSubWindow(
windowController: WindowController.fromWindowId(windowId),
args: argument,
));
} else {
runApp(const _ExampleMainWindow());
}
}

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

@override
State<_ExampleMainWindow> createState() => _ExampleMainWindowState();
}

// This widget is the root of your application.
class _ExampleMainWindowState extends State<_ExampleMainWindow> {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Column(
children: [
TextButton(
onPressed: () async {
final window =
await DesktopMultiWindow.createWindow(jsonEncode({
'args1': 'Sub window',
'args2': 100,
'args3': true,
'bussiness': 'bussiness_test',
}));
window
..setFrame(const Offset(0, 0) & const Size(1280, 720))
..center()
..setTitle('Another window')
..show();
},
child: const Text('Create a new World!'),
),
TextButton(
child: const Text('Send event to all sub windows'),
onPressed: () async {
final subWindowIds =
await DesktopMultiWindow.getAllSubWindowIds();
for (final windowId in subWindowIds) {
DesktopMultiWindow.invokeMethod(
windowId,
'broadcast',
'Broadcast from main window',
);
}
},
),
Expanded(
child: EventWidget(controller: WindowController.fromWindowId(0)),
)
],
),
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);

// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
class _ExampleSubWindow extends StatelessWidget {
const _ExampleSubWindow({
Key? key,
required this.windowController,
required this.args,
}) : super(key: key);

// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".

final String title;

@override
State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;

void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
final WindowController windowController;
final Map? args;

@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Column(
children: [
if (args != null)
Text(
'Arguments: ${args.toString()}',
style: const TextStyle(fontSize: 20),
),
ValueListenableBuilder<bool>(
valueListenable: DesktopLifecycle.instance.isActive,
builder: (context, active, child) {
if (active) {
return const Text('Window Active');
} else {
return const Text('Window Inactive');
}
},
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
TextButton(
onPressed: () async {
windowController.close();
},
child: const Text('Close this window'),
),
Expanded(child: EventWidget(controller: windowController)),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
8 changes: 8 additions & 0 deletions linux/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@

#include "generated_plugin_registrant.h"

#include <desktop_lifecycle/desktop_lifecycle_plugin.h>
#include <desktop_multi_window/desktop_multi_window_plugin.h>

void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) desktop_lifecycle_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "DesktopLifecyclePlugin");
desktop_lifecycle_plugin_register_with_registrar(desktop_lifecycle_registrar);
g_autoptr(FlPluginRegistrar) desktop_multi_window_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "DesktopMultiWindowPlugin");
desktop_multi_window_plugin_register_with_registrar(desktop_multi_window_registrar);
}
2 changes: 2 additions & 0 deletions linux/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#

list(APPEND FLUTTER_PLUGIN_LIST
desktop_lifecycle
desktop_multi_window
)

list(APPEND FLUTTER_FFI_PLUGIN_LIST
Expand Down
Loading

0 comments on commit 712d0dd

Please sign in to comment.