-
-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
191 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
apps/biyi_app/lib/states/actions/translate_input_content.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.