Skip to content

Commit

Permalink
wip: Update on 20240623-2
Browse files Browse the repository at this point in the history
  • Loading branch information
lijy91 committed Jun 23, 2024
1 parent 011fca1 commit 201b1c4
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 111 deletions.
88 changes: 1 addition & 87 deletions apps/biyi_app/lib/app/home/desktop_popup.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// TODO(lijy91): ModifierKey is deprecated, fix it later.
// ignore_for_file: deprecated_member_use

import 'dart:async';
import 'dart:convert';
import 'dart:io';
Expand All @@ -24,7 +21,6 @@ import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:influxui/influxui.dart';
import 'package:keypress_simulator/keypress_simulator.dart';
import 'package:protocol_handler/protocol_handler.dart';
import 'package:provider/provider.dart';
import 'package:screen_capturer/screen_capturer.dart';
Expand Down Expand Up @@ -963,89 +959,7 @@ class _DesktopPopupPageState extends State<DesktopPopupPage>
}

@override
Future<void> onShortcutKeyDownTranslateInputContent() async {
await keyPressSimulator.simulateKeyPress(
key: LogicalKeyboardKey.keyA,
modifiers: [
UniPlatform.isMacOS
? ModifierKey.metaModifier
: ModifierKey.controlModifier,
],
);
await keyPressSimulator.simulateKeyPress(
key: LogicalKeyboardKey.keyA,
modifiers: [
UniPlatform.isMacOS
? ModifierKey.metaModifier
: ModifierKey.controlModifier,
],
keyDown: false,
);

try {
ExtractedData? extractedData = await screenTextExtractor.extract(
mode: ExtractMode.screenSelection,
);

if ((extractedData?.text ?? '').isEmpty) {
throw Exception('Extracted text is empty');
}

TranslateResponse translateResponse = await translateClient
.use(Settings.instance.defaultTranslationEngineId!)
.translate(
TranslateRequest(
text: extractedData?.text ?? '',
sourceLanguage: kLanguageZH,
targetLanguage: kLanguageEN,
),
);

TextTranslation? textTranslation =
(translateResponse.translations).firstOrNull;

if (textTranslation != null) {
Clipboard.setData(ClipboardData(text: textTranslation.text));
}
} catch (error) {
return;
}

await keyPressSimulator.simulateKeyPress(
key: LogicalKeyboardKey.keyA,
modifiers: [
UniPlatform.isMacOS
? ModifierKey.metaModifier
: ModifierKey.controlModifier,
],
);
await keyPressSimulator.simulateKeyPress(
key: LogicalKeyboardKey.keyA,
modifiers: [
UniPlatform.isMacOS
? ModifierKey.metaModifier
: ModifierKey.controlModifier,
],
keyDown: false,
);
await keyPressSimulator.simulateKeyPress(
key: LogicalKeyboardKey.keyV,
modifiers: [
UniPlatform.isMacOS
? ModifierKey.metaModifier
: ModifierKey.controlModifier,
],
);
await keyPressSimulator.simulateKeyPress(
key: LogicalKeyboardKey.keyV,
modifiers: [
UniPlatform.isMacOS
? ModifierKey.metaModifier
: ModifierKey.controlModifier,
],
keyDown: false,
);
}
Future<void> onShortcutKeyDownTranslateInputContent() async {}

@override
Future<void> onTrayIconMouseDown() async {
Expand Down
15 changes: 15 additions & 0 deletions apps/biyi_app/lib/extension/hotkey.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:flutter/widgets.dart';
import 'package:hotkey_manager/hotkey_manager.dart';

extension HotKeyExt on HotKey {
SingleActivator get singleActivator {
final activator = SingleActivator(
logicalKey,
shift: (modifiers ?? []).contains(HotKeyModifier.shift),
control: (modifiers ?? []).contains(HotKeyModifier.control),
alt: (modifiers ?? []).contains(HotKeyModifier.alt),
meta: (modifiers ?? []).contains(HotKeyModifier.meta),
);
return activator;
}
}
26 changes: 24 additions & 2 deletions apps/biyi_app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import 'dart:io';

import 'package:biyi_app/app/router_config.dart';
import 'package:biyi_app/extension/hotkey.dart';
import 'package:biyi_app/generated/codegen_loader.g.dart';
import 'package:biyi_app/services/local_db/local_db.dart';
import 'package:biyi_app/states/actions/translate_input_content.dart';
import 'package:biyi_app/states/settings.dart';
import 'package:biyi_app/utils/env.dart';
import 'package:biyi_app/utils/language_util.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:fluentui_system_icons/fluentui_system_icons.dart';
import 'package:hotkey_manager/hotkey_manager.dart';
import 'package:influxui/influxui.dart';
import 'package:influxui/themes.dart';
import 'package:launch_at_startup/launch_at_startup.dart';
Expand Down Expand Up @@ -42,6 +45,7 @@ Future<void> _ensureInitialized() async {
await EasyLocalization.ensureInitialized();
ExtendedIcons.iconLibrary = TablerIconLibrary();
if (UniPlatform.isLinux || UniPlatform.isMacOS || UniPlatform.isWindows) {
await hotKeyManager.unregisterAll();
await windowManager.ensureInitialized();
}

Expand Down Expand Up @@ -112,8 +116,7 @@ class MyApp extends StatefulWidget {
class _MyAppState extends State<MyApp> {
final botToastBuilder = BotToastInit();

@override
Widget build(BuildContext context) {
Widget _buildApp(BuildContext context) {
final settings = context.watch<Settings>();
if (context.locale != settings.locale) {
context.setLocale(settings.locale);
Expand Down Expand Up @@ -210,4 +213,23 @@ class _MyAppState extends State<MyApp> {
locale: context.locale,
);
}

@override
Widget build(BuildContext context) {
final boundShortcuts = Settings.instance.boundShortcuts;

final translateInputContentSingleActivator =
boundShortcuts.translateInputContent.singleActivator;
return Actions(
actions: <Type, Action<Intent>>{
TranslateInputContentIntent: TranslateInputContentAction(),
},
child: GlobalShortcuts(
shortcuts: {
translateInputContentSingleActivator: TranslateInputContentIntent(),
},
child: _buildApp(context),
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ShortcutService {
Future<void> start() async {
final boundShortcuts = Settings.instance.boundShortcuts;

await hotKeyManager.unregisterAll();
// await hotKeyManager.unregisterAll();
await hotKeyManager.register(
boundShortcuts.inputSubmitWithMetaEnter,
keyDownHandler: (_) {
Expand Down Expand Up @@ -66,14 +66,14 @@ class ShortcutService {
_listener?.onShortcutKeyDownExtractFromClipboard();
},
);
if (!UniPlatform.isLinux) {
await hotKeyManager.register(
boundShortcuts.translateInputContent,
keyDownHandler: (_) {
_listener?.onShortcutKeyDownTranslateInputContent();
},
);
}
// if (!UniPlatform.isLinux) {
// await hotKeyManager.register(
// boundShortcuts.translateInputContent,
// keyDownHandler: (_) {
// _listener?.onShortcutKeyDownTranslateInputContent();
// },
// );
// }
}

void stop() {
Expand Down
105 changes: 105 additions & 0 deletions apps/biyi_app/lib/states/actions/translate_input_content.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// TODO(lijy91): ModifierKey is deprecated, fix it later.
// ignore_for_file: deprecated_member_use

import 'package:biyi_app/services/translate_client/translate_client.dart';
import 'package:biyi_app/states/settings.dart';
import 'package:biyi_app/utils/language_util.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:keypress_simulator/keypress_simulator.dart';
import 'package:screen_text_extractor/screen_text_extractor.dart';
import 'package:uni_platform/uni_platform.dart';
import 'package:uni_translate_client/uni_translate_client.dart';

class TranslateInputContentIntent extends Intent {}

class TranslateInputContentAction extends Action<TranslateInputContentIntent> {
Future<void> _simulateSelectAll() async {
await keyPressSimulator.simulateKeyDown(
PhysicalKeyboardKey.keyA,
UniPlatform.select<List<ModifierKey>>(
macos: [ModifierKey.metaModifier],
otherwise: [ModifierKey.controlModifier],
),
);
await keyPressSimulator.simulateKeyUp(
PhysicalKeyboardKey.keyA,
UniPlatform.select<List<ModifierKey>>(
macos: [ModifierKey.metaModifier],
otherwise: [ModifierKey.controlModifier],
),
);
}

Future<void> _simulateCopy() async {
await keyPressSimulator.simulateKeyDown(
PhysicalKeyboardKey.keyC,
UniPlatform.select<List<ModifierKey>>(
macos: [ModifierKey.metaModifier],
otherwise: [ModifierKey.controlModifier],
),
);
await keyPressSimulator.simulateKeyUp(
PhysicalKeyboardKey.keyC,
UniPlatform.select<List<ModifierKey>>(
macos: [ModifierKey.metaModifier],
otherwise: [ModifierKey.controlModifier],
),
);
}

Future<void> _simulatePaste() async {
await keyPressSimulator.simulateKeyDown(
PhysicalKeyboardKey.keyC,
UniPlatform.select<List<ModifierKey>>(
macos: [ModifierKey.metaModifier],
otherwise: [ModifierKey.controlModifier],
),
);
await keyPressSimulator.simulateKeyUp(
PhysicalKeyboardKey.keyC,
UniPlatform.select<List<ModifierKey>>(
macos: [ModifierKey.metaModifier],
otherwise: [ModifierKey.controlModifier],
),
);
}

@override
Future<void> invoke(covariant TranslateInputContentIntent intent) async {
await _simulateSelectAll();
await _simulateCopy();

try {
ExtractedData? extractedData = await screenTextExtractor.extract(
mode: ExtractMode.screenSelection,
);

if ((extractedData?.text ?? '').isEmpty) {
throw Exception('Extracted text is empty');
}

TranslateResponse translateResponse = await translateClient
.use(Settings.instance.defaultTranslationEngineId!)
.translate(
TranslateRequest(
text: extractedData?.text ?? '',
sourceLanguage: kLanguageZH,
targetLanguage: kLanguageEN,
),
);

TextTranslation? textTranslation =
(translateResponse.translations).firstOrNull;

if (textTranslation != null) {
Clipboard.setData(ClipboardData(text: textTranslation.text));
}
} catch (error) {
return;
}

await _simulateSelectAll();
await _simulatePaste();
}
}
4 changes: 2 additions & 2 deletions apps/biyi_app/macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import audioplayers_darwin
import clipboard_watcher
import device_info_plus
import hotkey_manager_macos
import keypress_simulator
import keypress_simulator_macos
import ocr_engine_builtin
import package_info_plus
import path_provider_foundation
Expand All @@ -27,7 +27,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
ClipboardWatcherPlugin.register(with: registry.registrar(forPlugin: "ClipboardWatcherPlugin"))
DeviceInfoPlusMacosPlugin.register(with: registry.registrar(forPlugin: "DeviceInfoPlusMacosPlugin"))
HotkeyManagerMacosPlugin.register(with: registry.registrar(forPlugin: "HotkeyManagerMacosPlugin"))
KeypressSimulatorPlugin.register(with: registry.registrar(forPlugin: "KeypressSimulatorPlugin"))
KeypressSimulatorMacosPlugin.register(with: registry.registrar(forPlugin: "KeypressSimulatorMacosPlugin"))
OcrEngineBuiltinPlugin.register(with: registry.registrar(forPlugin: "OcrEngineBuiltinPlugin"))
FPPPackageInfoPlusPlugin.register(with: registry.registrar(forPlugin: "FPPPackageInfoPlusPlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
Expand Down
10 changes: 5 additions & 5 deletions apps/biyi_app/macos/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ PODS:
- hotkey_manager_macos (0.0.1):
- FlutterMacOS
- HotKey
- keypress_simulator (0.0.1):
- keypress_simulator_macos (0.0.1):
- FlutterMacOS
- ocr_engine_builtin (0.0.1):
- FlutterMacOS
Expand Down Expand Up @@ -43,7 +43,7 @@ DEPENDENCIES:
- device_info_plus (from `Flutter/ephemeral/.symlinks/plugins/device_info_plus/macos`)
- FlutterMacOS (from `Flutter/ephemeral`)
- hotkey_manager_macos (from `Flutter/ephemeral/.symlinks/plugins/hotkey_manager_macos/macos`)
- keypress_simulator (from `Flutter/ephemeral/.symlinks/plugins/keypress_simulator/macos`)
- keypress_simulator_macos (from `Flutter/ephemeral/.symlinks/plugins/keypress_simulator_macos/macos`)
- ocr_engine_builtin (from `Flutter/ephemeral/.symlinks/plugins/ocr_engine_builtin/macos`)
- package_info_plus (from `Flutter/ephemeral/.symlinks/plugins/package_info_plus/macos`)
- path_provider_foundation (from `Flutter/ephemeral/.symlinks/plugins/path_provider_foundation/darwin`)
Expand Down Expand Up @@ -71,8 +71,8 @@ EXTERNAL SOURCES:
:path: Flutter/ephemeral
hotkey_manager_macos:
:path: Flutter/ephemeral/.symlinks/plugins/hotkey_manager_macos/macos
keypress_simulator:
:path: Flutter/ephemeral/.symlinks/plugins/keypress_simulator/macos
keypress_simulator_macos:
:path: Flutter/ephemeral/.symlinks/plugins/keypress_simulator_macos/macos
ocr_engine_builtin:
:path: Flutter/ephemeral/.symlinks/plugins/ocr_engine_builtin/macos
package_info_plus:
Expand Down Expand Up @@ -103,7 +103,7 @@ SPEC CHECKSUMS:
FlutterMacOS: 8f6f14fa908a6fb3fba0cd85dbd81ec4b251fb24
HotKey: e96d8a2ddbf4591131e2bb3f54e69554d90cdca6
hotkey_manager_macos: 1e2edb0c7ae4fe67108af44a9d3445de41404160
keypress_simulator: c9697f52dc29725ce0185dcd52b6731718e0b9ca
keypress_simulator_macos: f8556f9101f9f2f175652e0bceddf0fe82a4c6b2
ocr_engine_builtin: 99c65f8f4a156830ceaf0da86940610fefe50f50
package_info_plus: 02d7a575e80f194102bef286361c6c326e4c29ce
path_provider_foundation: 2b6b4c569c0fb62ec74538f866245ac84301af46
Expand Down
4 changes: 2 additions & 2 deletions apps/biyi_app/macos/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@
"${BUILT_PRODUCTS_DIR}/clipboard_watcher/clipboard_watcher.framework",
"${BUILT_PRODUCTS_DIR}/device_info_plus/device_info_plus.framework",
"${BUILT_PRODUCTS_DIR}/hotkey_manager_macos/hotkey_manager_macos.framework",
"${BUILT_PRODUCTS_DIR}/keypress_simulator/keypress_simulator.framework",
"${BUILT_PRODUCTS_DIR}/keypress_simulator_macos/keypress_simulator_macos.framework",
"${BUILT_PRODUCTS_DIR}/ocr_engine_builtin/ocr_engine_builtin.framework",
"${BUILT_PRODUCTS_DIR}/package_info_plus/package_info_plus.framework",
"${BUILT_PRODUCTS_DIR}/path_provider_foundation/path_provider_foundation.framework",
Expand All @@ -298,7 +298,7 @@
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/clipboard_watcher.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/device_info_plus.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/hotkey_manager_macos.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/keypress_simulator.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/keypress_simulator_macos.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/ocr_engine_builtin.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/package_info_plus.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/path_provider_foundation.framework",
Expand Down
Loading

0 comments on commit 201b1c4

Please sign in to comment.