mirror of
https://ghfast.top/https://github.com/StarCitizenToolBox/app.git
synced 2025-06-28 02:04:44 +08:00
feat: input_method_auto_translate
This commit is contained in:
@ -196,8 +196,8 @@ class _$HomeDownloaderUIStateImpl implements _HomeDownloaderUIState {
|
||||
.equals(other._waitingTasks, _waitingTasks) &&
|
||||
const DeepCollectionEquality()
|
||||
.equals(other._stoppedTasks, _stoppedTasks) &&
|
||||
(identical(other.globalStat, globalStat) ||
|
||||
other.globalStat == globalStat));
|
||||
const DeepCollectionEquality()
|
||||
.equals(other.globalStat, globalStat));
|
||||
}
|
||||
|
||||
@override
|
||||
@ -206,7 +206,7 @@ class _$HomeDownloaderUIStateImpl implements _HomeDownloaderUIState {
|
||||
const DeepCollectionEquality().hash(_tasks),
|
||||
const DeepCollectionEquality().hash(_waitingTasks),
|
||||
const DeepCollectionEquality().hash(_stoppedTasks),
|
||||
globalStat);
|
||||
const DeepCollectionEquality().hash(globalStat));
|
||||
|
||||
/// Create a copy of HomeDownloaderUIState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
|
@ -70,16 +70,28 @@ class InputMethodDialogUI extends HookConsumerWidget {
|
||||
placeholderStyle:
|
||||
TextStyle(color: Colors.white.withOpacity(.6)),
|
||||
style: TextStyle(fontSize: 16, color: Colors.white),
|
||||
onChanged: (str) {
|
||||
onChanged: (str) async {
|
||||
final text = model.onTextChange("src", str);
|
||||
destTextCtrl.text = text ?? "";
|
||||
if (text != null) {
|
||||
destTextCtrl.text = text;
|
||||
model.checkAutoTranslate();
|
||||
}
|
||||
},
|
||||
),
|
||||
SizedBox(height: 16),
|
||||
Center(
|
||||
child: Icon(FluentIcons.down),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
if (state.isAutoTranslateWorking)
|
||||
SizedBox(width: 24, height: 24, child: ProgressRing())
|
||||
else
|
||||
SizedBox(
|
||||
width: 24,
|
||||
height: 24,
|
||||
child: Icon(FluentIcons.down))
|
||||
],
|
||||
),
|
||||
),
|
||||
SizedBox(height: 16),
|
||||
TextFormBox(
|
||||
@ -101,6 +113,18 @@ class InputMethodDialogUI extends HookConsumerWidget {
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Text(S.current.input_method_auto_translate),
|
||||
SizedBox(width: 6),
|
||||
ToggleSwitch(
|
||||
checked: state.isEnableAutoTranslate,
|
||||
onChanged: (b) =>
|
||||
_onSwitchAutoTranslate(context, model, b),
|
||||
),
|
||||
],
|
||||
),
|
||||
SizedBox(width: 24),
|
||||
Row(
|
||||
children: [
|
||||
Text(S.current.input_method_remote_input_service),
|
||||
@ -214,4 +238,16 @@ class InputMethodDialogUI extends HookConsumerWidget {
|
||||
await serverModel.stopServer().unwrap(context: context);
|
||||
}
|
||||
}
|
||||
|
||||
_onSwitchAutoTranslate(
|
||||
BuildContext context, InputMethodDialogUIModel model, bool b) async {
|
||||
if (b) {
|
||||
final ok = await showConfirmDialogs(
|
||||
context,
|
||||
S.current.input_method_auto_translate_dialog_title,
|
||||
Text(S.current.input_method_auto_translate_dialog_title_content));
|
||||
if (ok != true) return;
|
||||
}
|
||||
model.toggleAutoTranslate(b);
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import 'package:flutter/widgets.dart';
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:starcitizen_doctor/api/api.dart';
|
||||
import 'package:starcitizen_doctor/common/utils/log.dart';
|
||||
import 'package:starcitizen_doctor/ui/home/localization/localization_ui_model.dart';
|
||||
|
||||
@ -18,6 +19,8 @@ class InputMethodDialogUIState with _$InputMethodDialogUIState {
|
||||
Map<String, String>? keyMaps,
|
||||
Map<String, String>? worldMaps, {
|
||||
@Default(false) bool enableAutoCopy,
|
||||
@Default(false) bool isEnableAutoTranslate,
|
||||
@Default(false) bool isAutoTranslateWorking,
|
||||
}) = _InputMethodDialogUIState;
|
||||
}
|
||||
|
||||
@ -43,10 +46,13 @@ class InputMethodDialogUIModel extends _$InputMethodDialogUIModel {
|
||||
final worldMaps = keyMaps?.map((key, value) => MapEntry(value.trim(), key));
|
||||
final appBox = await Hive.openBox("app_conf");
|
||||
final enableAutoCopy = appBox.get("enableAutoCopy", defaultValue: false);
|
||||
final isEnableAutoTranslate =
|
||||
appBox.get("isEnableAutoTranslate", defaultValue: false);
|
||||
state = state.copyWith(
|
||||
keyMaps: keyMaps,
|
||||
worldMaps: worldMaps,
|
||||
enableAutoCopy: enableAutoCopy,
|
||||
isEnableAutoTranslate: isEnableAutoTranslate,
|
||||
);
|
||||
}
|
||||
|
||||
@ -104,6 +110,7 @@ class InputMethodDialogUIModel extends _$InputMethodDialogUIModel {
|
||||
|
||||
// 打字结束后的1秒后自动复制,避免频繁复制
|
||||
void _handleAutoCopy(String text) {
|
||||
if (state.isEnableAutoTranslate) return;
|
||||
if (_autoCopyTimer != null) {
|
||||
_autoCopyTimer?.cancel();
|
||||
_autoCopyTimer = null;
|
||||
@ -135,8 +142,42 @@ class InputMethodDialogUIModel extends _$InputMethodDialogUIModel {
|
||||
_srcTextCtrl?.text = text;
|
||||
_destTextCtrl?.text = onTextChange("src", text) ?? "";
|
||||
if (_destTextCtrl?.text.isEmpty ?? true) return;
|
||||
if (autoCopy) {
|
||||
checkAutoTranslate(webMessage: true);
|
||||
if (autoCopy && !state.isAutoTranslateWorking) {
|
||||
Clipboard.setData(ClipboardData(text: _destTextCtrl?.text ?? ""));
|
||||
}
|
||||
}
|
||||
|
||||
toggleAutoTranslate(bool b) async {
|
||||
state = state.copyWith(isEnableAutoTranslate: b);
|
||||
final appConf = await Hive.openBox("app_conf");
|
||||
await appConf.put("isEnableAutoTranslate", b);
|
||||
}
|
||||
|
||||
Timer? _translateTimer;
|
||||
|
||||
Future<void> checkAutoTranslate({bool webMessage = false}) async {
|
||||
final sourceText = _srcTextCtrl?.text ?? "";
|
||||
final content = _destTextCtrl?.text ?? "";
|
||||
if (sourceText.trim().isEmpty) return;
|
||||
if (state.isEnableAutoTranslate) {
|
||||
if (_translateTimer != null) _translateTimer?.cancel();
|
||||
state = state.copyWith(isAutoTranslateWorking: true);
|
||||
_translateTimer =
|
||||
Timer(Duration(milliseconds: webMessage ? 1 : 400), () async {
|
||||
try {
|
||||
final r = await Api.doGoogleTranslate(sourceText);
|
||||
if (r != null) {
|
||||
_destTextCtrl?.text = "$content\n[en] $r";
|
||||
if (state.enableAutoCopy || webMessage) {
|
||||
Clipboard.setData(ClipboardData(text: _destTextCtrl?.text ?? ""));
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
dPrint("[InputMethodDialogUIModel] AutoTranslate error: $e");
|
||||
}
|
||||
state = state.copyWith(isAutoTranslateWorking: false);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,8 @@ mixin _$InputMethodDialogUIState {
|
||||
Map<String, String>? get keyMaps => throw _privateConstructorUsedError;
|
||||
Map<String, String>? get worldMaps => throw _privateConstructorUsedError;
|
||||
bool get enableAutoCopy => throw _privateConstructorUsedError;
|
||||
bool get isEnableAutoTranslate => throw _privateConstructorUsedError;
|
||||
bool get isAutoTranslateWorking => throw _privateConstructorUsedError;
|
||||
|
||||
/// Create a copy of InputMethodDialogUIState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@ -36,7 +38,9 @@ abstract class $InputMethodDialogUIStateCopyWith<$Res> {
|
||||
$Res call(
|
||||
{Map<String, String>? keyMaps,
|
||||
Map<String, String>? worldMaps,
|
||||
bool enableAutoCopy});
|
||||
bool enableAutoCopy,
|
||||
bool isEnableAutoTranslate,
|
||||
bool isAutoTranslateWorking});
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
@ -58,6 +62,8 @@ class _$InputMethodDialogUIStateCopyWithImpl<$Res,
|
||||
Object? keyMaps = freezed,
|
||||
Object? worldMaps = freezed,
|
||||
Object? enableAutoCopy = null,
|
||||
Object? isEnableAutoTranslate = null,
|
||||
Object? isAutoTranslateWorking = null,
|
||||
}) {
|
||||
return _then(_value.copyWith(
|
||||
keyMaps: freezed == keyMaps
|
||||
@ -72,6 +78,14 @@ class _$InputMethodDialogUIStateCopyWithImpl<$Res,
|
||||
? _value.enableAutoCopy
|
||||
: enableAutoCopy // ignore: cast_nullable_to_non_nullable
|
||||
as bool,
|
||||
isEnableAutoTranslate: null == isEnableAutoTranslate
|
||||
? _value.isEnableAutoTranslate
|
||||
: isEnableAutoTranslate // ignore: cast_nullable_to_non_nullable
|
||||
as bool,
|
||||
isAutoTranslateWorking: null == isAutoTranslateWorking
|
||||
? _value.isAutoTranslateWorking
|
||||
: isAutoTranslateWorking // ignore: cast_nullable_to_non_nullable
|
||||
as bool,
|
||||
) as $Val);
|
||||
}
|
||||
}
|
||||
@ -88,7 +102,9 @@ abstract class _$$InputMethodDialogUIStateImplCopyWith<$Res>
|
||||
$Res call(
|
||||
{Map<String, String>? keyMaps,
|
||||
Map<String, String>? worldMaps,
|
||||
bool enableAutoCopy});
|
||||
bool enableAutoCopy,
|
||||
bool isEnableAutoTranslate,
|
||||
bool isAutoTranslateWorking});
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
@ -109,6 +125,8 @@ class __$$InputMethodDialogUIStateImplCopyWithImpl<$Res>
|
||||
Object? keyMaps = freezed,
|
||||
Object? worldMaps = freezed,
|
||||
Object? enableAutoCopy = null,
|
||||
Object? isEnableAutoTranslate = null,
|
||||
Object? isAutoTranslateWorking = null,
|
||||
}) {
|
||||
return _then(_$InputMethodDialogUIStateImpl(
|
||||
freezed == keyMaps
|
||||
@ -123,6 +141,14 @@ class __$$InputMethodDialogUIStateImplCopyWithImpl<$Res>
|
||||
? _value.enableAutoCopy
|
||||
: enableAutoCopy // ignore: cast_nullable_to_non_nullable
|
||||
as bool,
|
||||
isEnableAutoTranslate: null == isEnableAutoTranslate
|
||||
? _value.isEnableAutoTranslate
|
||||
: isEnableAutoTranslate // ignore: cast_nullable_to_non_nullable
|
||||
as bool,
|
||||
isAutoTranslateWorking: null == isAutoTranslateWorking
|
||||
? _value.isAutoTranslateWorking
|
||||
: isAutoTranslateWorking // ignore: cast_nullable_to_non_nullable
|
||||
as bool,
|
||||
));
|
||||
}
|
||||
}
|
||||
@ -132,7 +158,9 @@ class __$$InputMethodDialogUIStateImplCopyWithImpl<$Res>
|
||||
class _$InputMethodDialogUIStateImpl implements _InputMethodDialogUIState {
|
||||
_$InputMethodDialogUIStateImpl(
|
||||
final Map<String, String>? keyMaps, final Map<String, String>? worldMaps,
|
||||
{this.enableAutoCopy = false})
|
||||
{this.enableAutoCopy = false,
|
||||
this.isEnableAutoTranslate = false,
|
||||
this.isAutoTranslateWorking = false})
|
||||
: _keyMaps = keyMaps,
|
||||
_worldMaps = worldMaps;
|
||||
|
||||
@ -159,10 +187,16 @@ class _$InputMethodDialogUIStateImpl implements _InputMethodDialogUIState {
|
||||
@override
|
||||
@JsonKey()
|
||||
final bool enableAutoCopy;
|
||||
@override
|
||||
@JsonKey()
|
||||
final bool isEnableAutoTranslate;
|
||||
@override
|
||||
@JsonKey()
|
||||
final bool isAutoTranslateWorking;
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'InputMethodDialogUIState(keyMaps: $keyMaps, worldMaps: $worldMaps, enableAutoCopy: $enableAutoCopy)';
|
||||
return 'InputMethodDialogUIState(keyMaps: $keyMaps, worldMaps: $worldMaps, enableAutoCopy: $enableAutoCopy, isEnableAutoTranslate: $isEnableAutoTranslate, isAutoTranslateWorking: $isAutoTranslateWorking)';
|
||||
}
|
||||
|
||||
@override
|
||||
@ -174,7 +208,11 @@ class _$InputMethodDialogUIStateImpl implements _InputMethodDialogUIState {
|
||||
const DeepCollectionEquality()
|
||||
.equals(other._worldMaps, _worldMaps) &&
|
||||
(identical(other.enableAutoCopy, enableAutoCopy) ||
|
||||
other.enableAutoCopy == enableAutoCopy));
|
||||
other.enableAutoCopy == enableAutoCopy) &&
|
||||
(identical(other.isEnableAutoTranslate, isEnableAutoTranslate) ||
|
||||
other.isEnableAutoTranslate == isEnableAutoTranslate) &&
|
||||
(identical(other.isAutoTranslateWorking, isAutoTranslateWorking) ||
|
||||
other.isAutoTranslateWorking == isAutoTranslateWorking));
|
||||
}
|
||||
|
||||
@override
|
||||
@ -182,7 +220,9 @@ class _$InputMethodDialogUIStateImpl implements _InputMethodDialogUIState {
|
||||
runtimeType,
|
||||
const DeepCollectionEquality().hash(_keyMaps),
|
||||
const DeepCollectionEquality().hash(_worldMaps),
|
||||
enableAutoCopy);
|
||||
enableAutoCopy,
|
||||
isEnableAutoTranslate,
|
||||
isAutoTranslateWorking);
|
||||
|
||||
/// Create a copy of InputMethodDialogUIState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@ -197,7 +237,9 @@ class _$InputMethodDialogUIStateImpl implements _InputMethodDialogUIState {
|
||||
abstract class _InputMethodDialogUIState implements InputMethodDialogUIState {
|
||||
factory _InputMethodDialogUIState(
|
||||
final Map<String, String>? keyMaps, final Map<String, String>? worldMaps,
|
||||
{final bool enableAutoCopy}) = _$InputMethodDialogUIStateImpl;
|
||||
{final bool enableAutoCopy,
|
||||
final bool isEnableAutoTranslate,
|
||||
final bool isAutoTranslateWorking}) = _$InputMethodDialogUIStateImpl;
|
||||
|
||||
@override
|
||||
Map<String, String>? get keyMaps;
|
||||
@ -205,6 +247,10 @@ abstract class _InputMethodDialogUIState implements InputMethodDialogUIState {
|
||||
Map<String, String>? get worldMaps;
|
||||
@override
|
||||
bool get enableAutoCopy;
|
||||
@override
|
||||
bool get isEnableAutoTranslate;
|
||||
@override
|
||||
bool get isAutoTranslateWorking;
|
||||
|
||||
/// Create a copy of InputMethodDialogUIState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
|
@ -7,7 +7,7 @@ part of 'input_method_dialog_ui_model.dart';
|
||||
// **************************************************************************
|
||||
|
||||
String _$inputMethodDialogUIModelHash() =>
|
||||
r'8c703de14c98fb6b2f26dbae04c2c9c06f50eb8c';
|
||||
r'ec8d0bb5118b74fa12341ed8048dde9335f57878';
|
||||
|
||||
/// See also [InputMethodDialogUIModel].
|
||||
@ProviderFor(InputMethodDialogUIModel)
|
||||
|
@ -1,6 +1,7 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:fluent_ui/fluent_ui.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
|
Reference in New Issue
Block a user