From d167c0599849d654617de1f4769d5f4bdc803d2b Mon Sep 17 00:00:00 2001 From: "Park, Jonghyeon" Date: Sat, 19 Oct 2024 21:22:26 +0900 Subject: [PATCH] feat: Add serial device selector badge --- .../lib/components/device_selector.dart | 66 +++++++++++++++++++ .../lib/components/key_config_list.dart | 1 - .../lib/models/serial_descriptor.dart | 5 ++ .../lib/screens/main_config_page.dart | 3 + .../flutter/generated_plugin_registrant.cc | 4 ++ .../linux/flutter/generated_plugins.cmake | 1 + .../Flutter/GeneratedPluginRegistrant.swift | 2 + .../macos/Runner/DebugProfile.entitlements | 2 + .../macos/Runner/Release.entitlements | 2 + configurator/pubspec.lock | 40 +++++++++++ configurator/pubspec.yaml | 1 + .../flutter/generated_plugin_registrant.cc | 3 + .../windows/flutter/generated_plugins.cmake | 1 + 13 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 configurator/lib/components/device_selector.dart create mode 100644 configurator/lib/models/serial_descriptor.dart diff --git a/configurator/lib/components/device_selector.dart b/configurator/lib/components/device_selector.dart new file mode 100644 index 0000000..d6f3ca4 --- /dev/null +++ b/configurator/lib/components/device_selector.dart @@ -0,0 +1,66 @@ +import 'package:flutter/material.dart'; +import 'package:configurator/models/serial_descriptor.dart'; + +class DeviceSelector extends StatefulWidget { + const DeviceSelector({super.key}); + + @override + State createState() => _DeviceSelectorState(); +} + +class _DeviceSelectorState extends State { + List _serialDescriptors = [ + SerialDescriptor('COM', 'asdf'), + SerialDescriptor('EACH', 'None') + ]; + String? _selected; + + @override + Widget build(BuildContext context) { + return Container( + child: FilledButton( + style: FilledButton.styleFrom( + padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 12.0), + minimumSize: Size(0, 0), // Remove minimum size constraints + tapTargetSize: MaterialTapTargetSize.shrinkWrap, // Shrink to fit + ), + onPressed: () async { + final RenderBox button = context.findRenderObject() as RenderBox; + final RenderBox overlay = + Overlay.of(context).context.findRenderObject() as RenderBox; + + // Calculate the position where the menu should appear + final Offset buttonPosition = + button.localToGlobal(Offset.zero, ancestor: overlay); + final RelativeRect position = RelativeRect.fromRect( + Rect.fromLTWH( + buttonPosition.dx, + buttonPosition.dy, + button.size.width, + button.size.height, + ), + Offset.zero & overlay.size, + ); + + // Show the menu + final result = await showMenu( + context: context, + position: position, + items: _serialDescriptors.map>((each) { + return PopupMenuItem( + value: each.port, + child: Text('(${each.port}) ${each.description}')); + }).toList(), + ); + + // Handle the selection + if (result != null) { + setState(() { + _selected = result; + }); + } + }, + child: Text('State'), + )); + } +} diff --git a/configurator/lib/components/key_config_list.dart b/configurator/lib/components/key_config_list.dart index 334849b..b785258 100644 --- a/configurator/lib/components/key_config_list.dart +++ b/configurator/lib/components/key_config_list.dart @@ -2,7 +2,6 @@ import 'package:configurator/widgets/key_detect_field.dart'; import 'package:flutter/material.dart'; import 'package:configurator/models/keycode.dart'; import 'package:configurator/globals.dart'; -import 'dart:developer'; class KeyConfigListItemContainer { KeyConfigListItemContainer( diff --git a/configurator/lib/models/serial_descriptor.dart b/configurator/lib/models/serial_descriptor.dart new file mode 100644 index 0000000..d0203aa --- /dev/null +++ b/configurator/lib/models/serial_descriptor.dart @@ -0,0 +1,5 @@ +class SerialDescriptor { + late final String port; + late final String description; + SerialDescriptor(this.port, this.description); +} diff --git a/configurator/lib/screens/main_config_page.dart b/configurator/lib/screens/main_config_page.dart index 01bf465..80065e2 100644 --- a/configurator/lib/screens/main_config_page.dart +++ b/configurator/lib/screens/main_config_page.dart @@ -1,3 +1,4 @@ +import 'package:configurator/components/device_selector.dart'; import 'package:flutter/material.dart'; import 'package:configurator/components/key_config_list.dart'; @@ -19,6 +20,8 @@ class _MainConfigPageState extends State { child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ + Align(alignment: Alignment.center, child: + DeviceSelector(),), Text( 'Key Configuration', style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold), diff --git a/configurator/linux/flutter/generated_plugin_registrant.cc b/configurator/linux/flutter/generated_plugin_registrant.cc index e71a16d..103991d 100644 --- a/configurator/linux/flutter/generated_plugin_registrant.cc +++ b/configurator/linux/flutter/generated_plugin_registrant.cc @@ -6,6 +6,10 @@ #include "generated_plugin_registrant.h" +#include void fl_register_plugins(FlPluginRegistry* registry) { + g_autoptr(FlPluginRegistrar) flutter_libserialport_registrar = + fl_plugin_registry_get_registrar_for_plugin(registry, "FlutterLibserialportPlugin"); + flutter_libserialport_plugin_register_with_registrar(flutter_libserialport_registrar); } diff --git a/configurator/linux/flutter/generated_plugins.cmake b/configurator/linux/flutter/generated_plugins.cmake index 2e1de87..ed2dd96 100644 --- a/configurator/linux/flutter/generated_plugins.cmake +++ b/configurator/linux/flutter/generated_plugins.cmake @@ -3,6 +3,7 @@ # list(APPEND FLUTTER_PLUGIN_LIST + flutter_libserialport ) list(APPEND FLUTTER_FFI_PLUGIN_LIST diff --git a/configurator/macos/Flutter/GeneratedPluginRegistrant.swift b/configurator/macos/Flutter/GeneratedPluginRegistrant.swift index cccf817..2a004d2 100644 --- a/configurator/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/configurator/macos/Flutter/GeneratedPluginRegistrant.swift @@ -5,6 +5,8 @@ import FlutterMacOS import Foundation +import flutter_libserialport func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { + FlutterLibserialportPlugin.register(with: registry.registrar(forPlugin: "FlutterLibserialportPlugin")) } diff --git a/configurator/macos/Runner/DebugProfile.entitlements b/configurator/macos/Runner/DebugProfile.entitlements index dddb8a3..fa27e02 100644 --- a/configurator/macos/Runner/DebugProfile.entitlements +++ b/configurator/macos/Runner/DebugProfile.entitlements @@ -8,5 +8,7 @@ com.apple.security.network.server + com.apple.security.device.serial + diff --git a/configurator/macos/Runner/Release.entitlements b/configurator/macos/Runner/Release.entitlements index 852fa1a..649b017 100644 --- a/configurator/macos/Runner/Release.entitlements +++ b/configurator/macos/Runner/Release.entitlements @@ -4,5 +4,7 @@ com.apple.security.app-sandbox + com.apple.security.device.serial + diff --git a/configurator/pubspec.lock b/configurator/pubspec.lock index b9fd799..6dfb19a 100644 --- a/configurator/pubspec.lock +++ b/configurator/pubspec.lock @@ -49,6 +49,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.0.8" + dylib: + dependency: transitive + description: + name: dylib + sha256: bf609b3eb6492a3309b3d1dbe8f83a4031de5535dd7686be33487051cc760bb0 + url: "https://pub.dev" + source: hosted + version: "0.3.3" fake_async: dependency: transitive description: @@ -57,11 +65,27 @@ packages: url: "https://pub.dev" source: hosted version: "1.3.1" + ffi: + dependency: transitive + description: + name: ffi + sha256: "16ed7b077ef01ad6170a3d0c57caa4a112a38d7a2ed5602e0aca9ca6f3d98da6" + url: "https://pub.dev" + source: hosted + version: "2.1.3" flutter: dependency: "direct main" description: flutter source: sdk version: "0.0.0" + flutter_libserialport: + dependency: "direct main" + description: + name: flutter_libserialport + sha256: "20c320dcde8592a16f9badc0cacad61b1fb283dbec647b6ebfc1020f8274c67b" + url: "https://pub.dev" + source: hosted + version: "0.4.0" flutter_lints: dependency: "direct dev" description: @@ -99,6 +123,14 @@ packages: url: "https://pub.dev" source: hosted version: "3.0.1" + libserialport: + dependency: transitive + description: + name: libserialport + sha256: "392e1592def65282429832ec66fa25e9e163d3b37716b97691482e2406720727" + url: "https://pub.dev" + source: hosted + version: "0.3.0+1" lints: dependency: transitive description: @@ -139,6 +171,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.9.0" + platform: + dependency: transitive + description: + name: platform + sha256: "5d6b1b0036a5f331ebc77c850ebc8506cbc1e9416c27e59b439f917a902a4984" + url: "https://pub.dev" + source: hosted + version: "3.1.6" sky_engine: dependency: transitive description: flutter diff --git a/configurator/pubspec.yaml b/configurator/pubspec.yaml index fc0c118..617ead6 100644 --- a/configurator/pubspec.yaml +++ b/configurator/pubspec.yaml @@ -35,6 +35,7 @@ dependencies: # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.8 + flutter_libserialport: ^0.4.0 dev_dependencies: flutter_test: diff --git a/configurator/windows/flutter/generated_plugin_registrant.cc b/configurator/windows/flutter/generated_plugin_registrant.cc index 8b6d468..0480a6e 100644 --- a/configurator/windows/flutter/generated_plugin_registrant.cc +++ b/configurator/windows/flutter/generated_plugin_registrant.cc @@ -6,6 +6,9 @@ #include "generated_plugin_registrant.h" +#include void RegisterPlugins(flutter::PluginRegistry* registry) { + FlutterLibserialportPluginRegisterWithRegistrar( + registry->GetRegistrarForPlugin("FlutterLibserialportPlugin")); } diff --git a/configurator/windows/flutter/generated_plugins.cmake b/configurator/windows/flutter/generated_plugins.cmake index b93c4c3..b8919ce 100644 --- a/configurator/windows/flutter/generated_plugins.cmake +++ b/configurator/windows/flutter/generated_plugins.cmake @@ -3,6 +3,7 @@ # list(APPEND FLUTTER_PLUGIN_LIST + flutter_libserialport ) list(APPEND FLUTTER_FFI_PLUGIN_LIST