From 2ab73b1dc463e97f4c6ff4a72fd3f136c2d62b25 Mon Sep 17 00:00:00 2001 From: hardliner66 Date: Sun, 6 Oct 2024 04:39:50 +0200 Subject: [PATCH] fix drag&drop for web --- lib/main.dart | 21 ++++ lib/src/app.dart | 119 +++++++++--------- linux/flutter/generated_plugin_registrant.cc | 8 ++ linux/flutter/generated_plugins.cmake | 2 + macos/Flutter/GeneratedPluginRegistrant.swift | 4 + pubspec.lock | 16 +++ pubspec.yaml | 1 + .../flutter/generated_plugin_registrant.cc | 6 + windows/flutter/generated_plugins.cmake | 2 + 9 files changed, 122 insertions(+), 57 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index eb69b48..61efeb1 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,12 +1,33 @@ import 'dart:io'; import 'package:flutter/material.dart'; +import 'package:flutter/foundation.dart' show kIsWeb; + +import 'package:window_manager/window_manager.dart'; import 'src/app.dart'; import 'src/settings/settings_controller.dart'; import 'src/settings/settings_service.dart'; void main(List args) async { + // Identify if Desktop or Mobile app. + + if (!kIsWeb) { + WidgetsFlutterBinding.ensureInitialized(); + + await windowManager.ensureInitialized(); + + WindowOptions windowOptions = const WindowOptions( + title: 'Fast Log Viewer', + center: true, + ); + + windowManager.waitUntilReadyToShow(windowOptions, () async { + await windowManager.show(); + await windowManager.focus(); + }); + } + // Set up the SettingsController, which will glue user settings to multiple // Flutter Widgets. final settingsController = SettingsController(SettingsService()); diff --git a/lib/src/app.dart b/lib/src/app.dart index 0b9225c..efa5dda 100644 --- a/lib/src/app.dart +++ b/lib/src/app.dart @@ -72,11 +72,13 @@ class LogViewerScreen extends StatefulWidget { class LogViewerScreenState extends State { List logs = []; List visibleLogs = []; - String search = ''; LogLevel selectedLogLevel = LogLevel.info; bool _isDragging = false; + TextEditingController controller = TextEditingController(text: ''); + late SettingsController settingsController; + @override void initState() { super.initState(); @@ -93,9 +95,49 @@ class LogViewerScreenState extends State { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: const Text('Fast Log Viewer'), actions: [ + const SizedBox(width: 8), + Expanded( + child: TextFormField( + controller: controller, + decoration: const InputDecoration( + labelText: 'Search Logs', + border: OutlineInputBorder(), + ), + onChanged: (value) { + setState(() { + _filterLogs(); + }); + }, + ), + ), + DropdownButton( + underline: Container(height: 0), + alignment: Alignment.center, + value: selectedLogLevel, + onChanged: (LogLevel? newValue) { + if (newValue != null) { + setState(() { + selectedLogLevel = newValue; + _filterLogs(); + }); + } + }, + items: LogLevel.values + .map>((LogLevel level) { + return DropdownMenuItem( + value: level, + child: Text(level.name), + ); + }).toList(), + ), + IconButton( + icon: const Icon(Icons.folder_open), + onPressed: _openFile, + ), DropdownButton( + underline: Container(height: 0), + alignment: Alignment.center, value: settingsController.themeMode, onChanged: (ThemeMode? newValue) async { if (newValue != null) { @@ -120,10 +162,7 @@ class LogViewerScreenState extends State { ), ], ), - IconButton( - icon: const Icon(Icons.folder_open), - onPressed: _openFile, - ), + const SizedBox(width: 8), ], ), body: DropTarget( @@ -139,10 +178,10 @@ class LogViewerScreenState extends State { }, onDragDone: (details) async { if (details.files.isNotEmpty) { - File file = File(details.files.first.path); - String fileContent = await file.readAsString(); + var bytes = await details.files.first.readAsBytes(); setState(() { - logs = _parseLogFile(fileContent); + logs = _parseLogFile(String.fromCharCodes(bytes)); + controller.clear(); _filterLogs(); _isDragging = false; }); @@ -154,46 +193,6 @@ class LogViewerScreenState extends State { : Theme.of(context).scaffoldBackgroundColor, child: Column( children: [ - Padding( - padding: const EdgeInsets.all(8.0), - child: Row( - children: [ - Expanded( - child: TextField( - decoration: const InputDecoration( - labelText: 'Search Logs', - border: OutlineInputBorder(), - ), - onChanged: (value) { - setState(() { - search = value; - _filterLogs(); - }); - }, - ), - ), - const SizedBox(width: 8.0), - DropdownButton( - value: selectedLogLevel, - onChanged: (LogLevel? newValue) { - if (newValue != null) { - setState(() { - selectedLogLevel = newValue; - _filterLogs(); - }); - } - }, - items: LogLevel.values - .map>((LogLevel level) { - return DropdownMenuItem( - value: level, - child: Text(level.name), - ); - }).toList(), - ), - ], - ), - ), Expanded( child: SelectionArea( child: ListView.builder( @@ -215,14 +214,19 @@ class LogViewerScreenState extends State { } Future _openFile() async { - FilePickerResult? result = await FilePicker.platform.pickFiles(); + FilePickerResult? result = + await FilePicker.platform.pickFiles(withData: true); if (result != null) { - File file = File(result.files.single.path!); - String fileContent = await file.readAsString(); - setState(() { - logs = _parseLogFile(fileContent); - _filterLogs(); - }); + if (result.files.isNotEmpty) { + var bytes = result.files.first.bytes; + if (bytes != null) { + setState(() { + logs = _parseLogFile(String.fromCharCodes(bytes)); + controller.clear(); + _filterLogs(); + }); + } + } } } @@ -230,7 +234,8 @@ class LogViewerScreenState extends State { setState(() { visibleLogs = logs .where((log) => - log.level.index >= selectedLogLevel.index && log.contains(search)) + log.level.index >= selectedLogLevel.index && + log.contains(controller.text)) .toList(); }); } diff --git a/linux/flutter/generated_plugin_registrant.cc b/linux/flutter/generated_plugin_registrant.cc index 8fdc934..59f41f5 100644 --- a/linux/flutter/generated_plugin_registrant.cc +++ b/linux/flutter/generated_plugin_registrant.cc @@ -7,9 +7,17 @@ #include "generated_plugin_registrant.h" #include +#include +#include void fl_register_plugins(FlPluginRegistry* registry) { g_autoptr(FlPluginRegistrar) desktop_drop_registrar = fl_plugin_registry_get_registrar_for_plugin(registry, "DesktopDropPlugin"); desktop_drop_plugin_register_with_registrar(desktop_drop_registrar); + g_autoptr(FlPluginRegistrar) screen_retriever_registrar = + fl_plugin_registry_get_registrar_for_plugin(registry, "ScreenRetrieverPlugin"); + screen_retriever_plugin_register_with_registrar(screen_retriever_registrar); + g_autoptr(FlPluginRegistrar) window_manager_registrar = + fl_plugin_registry_get_registrar_for_plugin(registry, "WindowManagerPlugin"); + window_manager_plugin_register_with_registrar(window_manager_registrar); } diff --git a/linux/flutter/generated_plugins.cmake b/linux/flutter/generated_plugins.cmake index 721c8d7..fc9244d 100644 --- a/linux/flutter/generated_plugins.cmake +++ b/linux/flutter/generated_plugins.cmake @@ -4,6 +4,8 @@ list(APPEND FLUTTER_PLUGIN_LIST desktop_drop + screen_retriever + window_manager ) list(APPEND FLUTTER_FFI_PLUGIN_LIST diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index 444bea8..36864ba 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -6,9 +6,13 @@ import FlutterMacOS import Foundation import desktop_drop +import screen_retriever import shared_preferences_foundation +import window_manager func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { DesktopDropPlugin.register(with: registry.registrar(forPlugin: "DesktopDropPlugin")) + ScreenRetrieverPlugin.register(with: registry.registrar(forPlugin: "ScreenRetrieverPlugin")) SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin")) + WindowManagerPlugin.register(with: registry.registrar(forPlugin: "WindowManagerPlugin")) } diff --git a/pubspec.lock b/pubspec.lock index 66ef9e4..7907055 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -237,6 +237,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.8" + screen_retriever: + dependency: transitive + description: + name: screen_retriever + sha256: "6ee02c8a1158e6dae7ca430da79436e3b1c9563c8cf02f524af997c201ac2b90" + url: "https://pub.dev" + source: hosted + version: "0.1.9" shared_preferences: dependency: "direct main" description: @@ -378,6 +386,14 @@ packages: url: "https://pub.dev" source: hosted version: "5.5.5" + window_manager: + dependency: "direct main" + description: + name: window_manager + sha256: ab8b2a7f97543d3db2b506c9d875e637149d48ee0c6a5cb5f5fd6e0dac463792 + url: "https://pub.dev" + source: hosted + version: "0.4.2" xdg_directories: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index f362b21..f285293 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -17,6 +17,7 @@ dependencies: file_picker: desktop_drop: shared_preferences: + window_manager: dev_dependencies: flutter_test: diff --git a/windows/flutter/generated_plugin_registrant.cc b/windows/flutter/generated_plugin_registrant.cc index bf646d2..ae6d7c2 100644 --- a/windows/flutter/generated_plugin_registrant.cc +++ b/windows/flutter/generated_plugin_registrant.cc @@ -7,8 +7,14 @@ #include "generated_plugin_registrant.h" #include +#include +#include void RegisterPlugins(flutter::PluginRegistry* registry) { DesktopDropPluginRegisterWithRegistrar( registry->GetRegistrarForPlugin("DesktopDropPlugin")); + ScreenRetrieverPluginRegisterWithRegistrar( + registry->GetRegistrarForPlugin("ScreenRetrieverPlugin")); + WindowManagerPluginRegisterWithRegistrar( + registry->GetRegistrarForPlugin("WindowManagerPlugin")); } diff --git a/windows/flutter/generated_plugins.cmake b/windows/flutter/generated_plugins.cmake index 696f9e4..845cc6b 100644 --- a/windows/flutter/generated_plugins.cmake +++ b/windows/flutter/generated_plugins.cmake @@ -4,6 +4,8 @@ list(APPEND FLUTTER_PLUGIN_LIST desktop_drop + screen_retriever + window_manager ) list(APPEND FLUTTER_FFI_PLUGIN_LIST