diff --git a/frontend/appflowy_flutter/lib/ai/widgets/prompt_input/desktop_prompt_text_field.dart b/frontend/appflowy_flutter/lib/ai/widgets/prompt_input/desktop_prompt_text_field.dart index 97850f6a1c..a2676f2c15 100644 --- a/frontend/appflowy_flutter/lib/ai/widgets/prompt_input/desktop_prompt_text_field.dart +++ b/frontend/appflowy_flutter/lib/ai/widgets/prompt_input/desktop_prompt_text_field.dart @@ -23,6 +23,7 @@ class DesktopPromptInput extends StatefulWidget { required this.selectedSourcesNotifier, required this.onUpdateSelectedSources, this.hideDecoration = false, + this.hideFormats = false, this.extraBottomActionButton, }); @@ -34,6 +35,7 @@ class DesktopPromptInput extends StatefulWidget { final ValueNotifier> selectedSourcesNotifier; final void Function(List) onUpdateSelectedSources; final bool hideDecoration; + final bool hideFormats; final Widget? extraBottomActionButton; @override @@ -139,11 +141,11 @@ class _DesktopPromptInputState extends State { children: [ ConstrainedBox( constraints: getTextFieldConstraints( - state.showPredefinedFormats, + state.showPredefinedFormats && !widget.hideFormats, ), child: inputTextField(), ), - if (state.showPredefinedFormats) + if (state.showPredefinedFormats && !widget.hideFormats) Positioned.fill( bottom: null, child: TextFieldTapRegion( @@ -168,8 +170,9 @@ class _DesktopPromptInputState extends State { top: null, child: TextFieldTapRegion( child: _PromptBottomActions( - showPredefinedFormats: + showPredefinedFormatBar: state.showPredefinedFormats, + showPredefinedFormatButton: !widget.hideFormats, onTogglePredefinedFormatSection: () => context.read().add( AIPromptInputEvent @@ -571,7 +574,8 @@ class PromptInputTextField extends StatelessWidget { class _PromptBottomActions extends StatelessWidget { const _PromptBottomActions({ required this.sendButtonState, - required this.showPredefinedFormats, + required this.showPredefinedFormatBar, + required this.showPredefinedFormatButton, required this.onTogglePredefinedFormatSection, required this.onStartMention, required this.onSendPressed, @@ -581,7 +585,8 @@ class _PromptBottomActions extends StatelessWidget { this.extraBottomActionButton, }); - final bool showPredefinedFormats; + final bool showPredefinedFormatBar; + final bool showPredefinedFormatButton; final void Function() onTogglePredefinedFormatSection; final void Function() onStartMention; final SendButtonState sendButtonState; @@ -600,10 +605,12 @@ class _PromptBottomActions extends StatelessWidget { builder: (context, state) { return Row( children: [ - _predefinedFormatButton(), - const HSpace( - DesktopAIChatSizes.inputActionBarButtonSpacing, - ), + if (showPredefinedFormatButton) ...[ + _predefinedFormatButton(), + const HSpace( + DesktopAIChatSizes.inputActionBarButtonSpacing, + ), + ], SelectModelMenu( aiModelStateNotifier: context.read().aiModelStateNotifier, @@ -641,7 +648,7 @@ class _PromptBottomActions extends StatelessWidget { Widget _predefinedFormatButton() { return PromptInputDesktopToggleFormatButton( - showFormatBar: showPredefinedFormats, + showFormatBar: showPredefinedFormatBar, onTap: onTogglePredefinedFormatSection, ); } diff --git a/frontend/appflowy_flutter/lib/ai/widgets/prompt_input/select_model_menu.dart b/frontend/appflowy_flutter/lib/ai/widgets/prompt_input/select_model_menu.dart index 64a62b902c..eef2663370 100644 --- a/frontend/appflowy_flutter/lib/ai/widgets/prompt_input/select_model_menu.dart +++ b/frontend/appflowy_flutter/lib/ai/widgets/prompt_input/select_model_menu.dart @@ -173,7 +173,6 @@ class _ModelItem extends StatelessWidget { model.i18n, figmaLineHeight: 20, overflow: TextOverflow.ellipsis, - color: isSelected ? Theme.of(context).colorScheme.primary : null, ), if (model.desc.isNotEmpty) FlowyText( diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_page.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_page.dart index a6cff2ae07..ee3a852cc1 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_page.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_page.dart @@ -432,44 +432,50 @@ class _AppFlowyEditorPageState extends State ); } return Center( - child: FloatingToolbar( - floatingToolbarHeight: 40, - padding: EdgeInsets.symmetric(horizontal: 6), - style: FloatingToolbarStyle( - backgroundColor: Theme.of(context).cardColor, - toolbarActiveColor: Color(0xffe0f8fd), - ), - items: toolbarItems, - decoration: BoxDecoration( - borderRadius: BorderRadius.circular(8), - color: Theme.of(context).cardColor, - boxShadow: [ - BoxShadow( - offset: Offset(0, 4), - blurRadius: 24, - color: themeV2.shadow_medium, + child: BlocProvider.value( + value: context.read(), + child: FloatingToolbar( + floatingToolbarHeight: 40, + padding: EdgeInsets.symmetric(horizontal: 6), + style: FloatingToolbarStyle( + backgroundColor: Theme.of(context).cardColor, + toolbarActiveColor: Color(0xffe0f8fd), + ), + items: toolbarItems, + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(8), + color: Theme.of(context).cardColor, + boxShadow: [ + BoxShadow( + offset: Offset(0, 4), + blurRadius: 24, + color: themeV2.shadow_medium, + ), + ], + ), + toolbarBuilder: (_, child, onDismiss, isMetricsChanged) => + BlocProvider.value( + value: context.read(), + child: DesktopFloatingToolbar( + editorState: editorState, + onDismiss: onDismiss, + enableAnimation: false, + child: child, ), - ], - ), - toolbarBuilder: (context, child, onDismiss, isMetricsChanged) => - DesktopFloatingToolbar( + ), + placeHolderBuilder: (_) => customPlaceholderItem, editorState: editorState, - onDismiss: onDismiss, - enableAnimation: !isMetricsChanged, - child: child, + editorScrollController: editorScrollController, + textDirection: textDirection, + tooltipBuilder: (context, id, message, child) => + widget.styleCustomizer.buildToolbarItemTooltip( + context, + id, + message, + child, + ), + child: editor, ), - placeHolderBuilder: (_) => customPlaceholderItem, - editorState: editorState, - editorScrollController: editorScrollController, - textDirection: textDirection, - tooltipBuilder: (context, id, message, child) => - widget.styleCustomizer.buildToolbarItemTooltip( - context, - id, - message, - child, - ), - child: editor, ), ); } diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/ai_writer_block_component.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/ai_writer_block_component.dart index bfd7c24fea..f78f7d35fd 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/ai_writer_block_component.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/ai_writer_block_component.dart @@ -490,6 +490,12 @@ class MainContentArea extends StatelessWidget { return DesktopPromptInput( isStreaming: false, hideDecoration: true, + hideFormats: [ + AiWriterCommand.fixSpellingAndGrammar, + AiWriterCommand.improveWriting, + AiWriterCommand.makeLonger, + AiWriterCommand.makeShorter, + ].contains(state.command), textController: textController, onSubmitted: (message, format, _) { cubit.runCommand(state.command, message, format); diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/widgets/ai_writer_scroll_wrapper.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/widgets/ai_writer_scroll_wrapper.dart index 90bce596a0..6b5f27e028 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/widgets/ai_writer_scroll_wrapper.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/ai/widgets/ai_writer_scroll_wrapper.dart @@ -208,11 +208,26 @@ class _AiWriterScrollWrapperState extends State { throttler.call(() { if (aiWriterCubit.aiWriterNode != null) { final path = aiWriterCubit.aiWriterNode!.path; - if (path.isNotEmpty) { - widget.editorState.updateSelectionWithReason( - Selection.collapsed(Position(path: path)), - ); + + if (path.isEmpty) { + return; } + + if (path.previous.isNotEmpty) { + final node = widget.editorState.getNodeAtPath(path.previous); + if (node != null && node.delta != null && node.delta!.isNotEmpty) { + widget.editorState.updateSelectionWithReason( + Selection.collapsed( + Position(path: path, offset: node.delta!.length), + ), + ); + return; + } + } + + widget.editorState.updateSelectionWithReason( + Selection.collapsed(Position(path: path)), + ); } }); } diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/desktop_floating_toolbar.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/desktop_floating_toolbar.dart index daa066d50c..03fc12a37c 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/desktop_floating_toolbar.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/desktop_floating_toolbar.dart @@ -1,10 +1,9 @@ import 'package:appflowy/plugins/document/presentation/editor_plugins/base/toolbar_extension.dart'; +import 'package:appflowy/startup/startup.dart'; import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:flutter/material.dart'; -import 'package:flutter_bloc/flutter_bloc.dart'; import 'toolbar_animation.dart'; -import 'toolbar_cubit.dart'; class DesktopFloatingToolbar extends StatefulWidget { const DesktopFloatingToolbar({ @@ -28,6 +27,7 @@ class _DesktopFloatingToolbarState extends State { EditorState get editorState => widget.editorState; _Position? position; + final toolbarController = getIt(); @override void initState() { @@ -39,24 +39,32 @@ class _DesktopFloatingToolbarState extends State { final selectionRect = editorState.selectionRects(); if (selectionRect.isEmpty) return; position = calculateSelectionMenuOffset(selectionRect.first); + toolbarController._addCallback(dismiss); + } + + @override + void dispose() { + toolbarController._removeCallback(dismiss); + super.dispose(); } @override Widget build(BuildContext context) { if (position == null) return Container(); - return BlocProvider( - create: (_) => ToolbarCubit(widget.onDismiss), - child: Positioned( - left: position!.left, - top: position!.top, - right: position!.right, - child: widget.enableAnimation - ? ToolbarAnimationWidget(child: widget.child) - : widget.child, - ), + return Positioned( + left: position!.left, + top: position!.top, + right: position!.right, + child: widget.enableAnimation + ? ToolbarAnimationWidget(child: widget.child) + : widget.child, ); } + void dismiss() { + widget.onDismiss.call(); + } + _Position calculateSelectionMenuOffset( Rect rect, ) { @@ -92,3 +100,33 @@ class _Position { final double? top; final double? right; } + +class FloatingToolbarController { + final Set _dismissCallbacks = {}; + final Set _displayListeners = {}; + + void _addCallback(VoidCallback callback) { + _dismissCallbacks.add(callback); + for (final listener in Set.of(_displayListeners)) { + listener.call(); + } + } + + void _removeCallback(VoidCallback callback) => + _dismissCallbacks.remove(callback); + + bool get isToolbarShowing => _dismissCallbacks.isNotEmpty; + + void addDisplayListener(VoidCallback listener) => + _displayListeners.add(listener); + + void removeDisplayListener(VoidCallback listener) => + _displayListeners.remove(listener); + + void hideToolbar() { + if (_dismissCallbacks.isEmpty) return; + for (final callback in _dismissCallbacks) { + callback.call(); + } + } +} diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/link/link_create_menu.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/link/link_create_menu.dart index 2f5edc195d..bc14073193 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/link/link_create_menu.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/link/link_create_menu.dart @@ -19,11 +19,15 @@ class LinkCreateMenu extends StatefulWidget { required this.onSubmitted, required this.onDismiss, required this.alignment, + required this.currentViewId, + required this.initialText, }); final EditorState editorState; final void Function(String link, bool isPage) onSubmitted; final VoidCallback onDismiss; + final String currentViewId; + final String initialText; final LinkMenuAlignment alignment; @override @@ -32,6 +36,8 @@ class LinkCreateMenu extends StatefulWidget { class _LinkCreateMenuState extends State { late LinkSearchTextField searchTextField = LinkSearchTextField( + currentViewId: widget.currentViewId, + initialSearchText: widget.initialText, onEnter: () { searchTextField.onSearchResult( onLink: () => onSubmittedLink(), @@ -48,17 +54,28 @@ class _LinkCreateMenuState extends State { }, ); - bool get isButtonEnable => searchText.isNotEmpty; + bool get isTextfieldEnable => searchTextField.isTextfieldEnable; String get searchText => searchTextField.searchText; bool get showAtTop => widget.alignment.isTop; + bool showErrorText = false; + @override void initState() { super.initState(); searchTextField.requestFocus(); searchTextField.searchRecentViews(); + final focusNode = searchTextField.focusNode; + bool hasFocus = focusNode.hasFocus; + focusNode.addListener(() { + if (hasFocus != focusNode.hasFocus && mounted) { + setState(() { + hasFocus = focusNode.hasFocus; + }); + } + }); } @override @@ -98,32 +115,45 @@ class _LinkCreateMenuState extends State { Widget buildSearchContainer() { return Container( width: 320, - height: 48, decoration: buildToolbarLinkDecoration(context), padding: EdgeInsets.all(8), child: ValueListenableBuilder( valueListenable: searchTextField.textEditingController, builder: (context, _, __) { - return Row( + return Column( + crossAxisAlignment: CrossAxisAlignment.start, children: [ - Expanded(child: searchTextField.buildTextField()), - HSpace(8), - FlowyTextButton( - LocaleKeys.document_toolbar_insert.tr(), - mainAxisAlignment: MainAxisAlignment.center, - padding: EdgeInsets.zero, - constraints: BoxConstraints(maxWidth: 72, minHeight: 32), - fontSize: 14, - fontColor: - isButtonEnable ? Colors.white : LinkStyle.textTertiary, - fillColor: isButtonEnable - ? LinkStyle.fillThemeThick - : LinkStyle.borderColor, - hoverColor: LinkStyle.fillThemeThick, - lineHeight: 20 / 14, - fontWeight: FontWeight.w600, - onPressed: isButtonEnable ? () => onSubmittedLink() : null, + Row( + children: [ + Expanded( + child: searchTextField.buildTextField(context: context), + ), + HSpace(8), + FlowyTextButton( + LocaleKeys.document_toolbar_insert.tr(), + mainAxisAlignment: MainAxisAlignment.center, + padding: EdgeInsets.zero, + constraints: BoxConstraints(maxWidth: 72, minHeight: 32), + fontSize: 14, + fontColor: Colors.white, + fillColor: LinkStyle.fillThemeThick, + hoverColor: LinkStyle.fillThemeThick.withAlpha(200), + lineHeight: 20 / 14, + fontWeight: FontWeight.w600, + onPressed: onSubmittedLink, + ), + ], ), + if (showErrorText) + Padding( + padding: const EdgeInsets.only(top: 4), + child: FlowyText.regular( + LocaleKeys.document_plugins_file_networkUrlInvalid.tr(), + color: LinkStyle.textStatusError, + fontSize: 12, + figmaLineHeight: 16, + ), + ), ], ); }, @@ -131,7 +161,15 @@ class _LinkCreateMenuState extends State { ); } - void onSubmittedLink() => widget.onSubmitted(searchText, false); + void onSubmittedLink() { + if (!isTextfieldEnable) { + setState(() { + showErrorText = true; + }); + return; + } + widget.onSubmitted(searchText, false); + } void onSubmittedPageLink(ViewPB view) async { final workspaceId = context @@ -152,13 +190,16 @@ void showLinkCreateMenu( BuildContext context, EditorState editorState, Selection selection, + String currentViewId, ) { + if (!context.mounted) return; final (left, top, right, bottom, alignment) = _getPosition(editorState); final node = editorState.getNodeAtPath(selection.end.path); if (node == null) { return; } + final selectedText = editorState.getTextInSelection(selection).join(); OverlayEntry? overlay; @@ -178,12 +219,18 @@ void showLinkCreateMenu( builder: (context) { return LinkCreateMenu( alignment: alignment, + initialText: selectedText, + currentViewId: currentViewId, editorState: editorState, onSubmitted: (link, isPage) async { await editorState.formatDelta(selection, { BuiltInAttributeKey.href: link, kIsPageLink: isPage, }); + await editorState.updateSelectionWithReason( + null, + reason: SelectionUpdateReason.uiEvent, + ); dismissOverlay(); }, onDismiss: dismissOverlay, diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/link/link_edit_menu.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/link/link_edit_menu.dart index e10b46411b..b6a4ad89ba 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/link/link_edit_menu.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/link/link_edit_menu.dart @@ -3,6 +3,7 @@ import 'package:appflowy/generated/locale_keys.g.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/toolbar_item/custom_link_toolbar_item.dart'; import 'package:appflowy/plugins/shared/share/constants.dart'; import 'package:appflowy/user/application/user_service.dart'; +import 'package:appflowy/util/theme_extension.dart'; import 'package:appflowy/workspace/application/view/view_service.dart'; import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart'; import 'package:appflowy_editor/appflowy_editor.dart'; @@ -10,7 +11,8 @@ import 'package:appflowy_result/appflowy_result.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:flowy_infra_ui/flowy_infra_ui.dart'; import 'package:flutter/material.dart'; - +// ignore: implementation_imports +import 'package:appflowy_editor/src/editor/util/link_util.dart'; import 'link_create_menu.dart'; import 'link_search_text_field.dart'; import 'link_styles.dart'; @@ -22,12 +24,14 @@ class LinkEditMenu extends StatefulWidget { required this.onDismiss, required this.onApply, required this.onRemoveLink, + required this.currentViewId, }); final LinkInfo linkInfo; final ValueChanged onApply; - final VoidCallback onRemoveLink; + final ValueChanged onRemoveLink; final VoidCallback onDismiss; + final String currentViewId; @override State createState() => _LinkEditMenuState(); @@ -36,7 +40,7 @@ class LinkEditMenu extends StatefulWidget { class _LinkEditMenuState extends State { ValueChanged get onApply => widget.onApply; - VoidCallback get onRemoveLink => widget.onRemoveLink; + ValueChanged get onRemoveLink => widget.onRemoveLink; VoidCallback get onDismiss => widget.onDismiss; @@ -47,9 +51,7 @@ class _LinkEditMenuState extends State { late LinkSearchTextField searchTextField; bool isShowingSearchResult = false; ViewPB? currentView; - - bool get enableApply => - linkInfo.link.isNotEmpty && linkNameController.text.isNotEmpty; + bool showErrorText = false; @override void initState() { @@ -58,18 +60,9 @@ class _LinkEditMenuState extends State { if (isPageLink) getPageView(); searchTextField = LinkSearchTextField( initialSearchText: isPageLink ? '' : linkInfo.link, - onEnter: () { - searchTextField.onSearchResult( - onLink: onLinkSelected, - onRecentViews: () => - onPageSelected(searchTextField.currentRecentView), - onSearchViews: () => - onPageSelected(searchTextField.currentSearchedView), - onEmpty: () { - searchTextField.unfocus(); - }, - ); - }, + initialViewId: linkInfo.viewId, + currentViewId: widget.currentViewId, + onEnter: onConfirm, onEscape: () { if (isShowingSearchResult) { hideSearchResult(); @@ -95,6 +88,7 @@ class _LinkEditMenuState extends State { Widget build(BuildContext context) { final showingRecent = searchTextField.showingRecent && isShowingSearchResult; + final errorHeight = showErrorText ? 20.0 : 0.0; return GestureDetector( onTap: onDismiss, child: Container( @@ -107,9 +101,8 @@ class _LinkEditMenuState extends State { onTap: hideSearchResult, child: Container( width: 400, - height: 192, + height: 192 + errorHeight, decoration: buildToolbarLinkDecoration(context), - padding: EdgeInsets.fromLTRB(20, 16, 20, 16), ), ), Positioned( @@ -123,7 +116,7 @@ class _LinkEditMenuState extends State { ), ), Positioned( - top: 80, + top: 80 + errorHeight, left: 20, child: FlowyText.semibold( LocaleKeys.document_toolbar_linkName.tr(), @@ -133,12 +126,12 @@ class _LinkEditMenuState extends State { ), ), Positioned( - top: 152, + top: 144 + errorHeight, left: 20, child: buildButtons(), ), Positioned( - top: 108, + top: 100 + errorHeight, left: 20, child: buildNameTextField(), ), @@ -155,29 +148,53 @@ class _LinkEditMenuState extends State { Widget buildLinkField() { final showPageView = linkInfo.isPage && !isShowingSearchResult; - if (showPageView) return buildPageView(); - if (!isShowingSearchResult) return buildLinkView(); - return SizedBox( - width: 360, - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - SizedBox( - width: 360, - height: 32, - child: searchTextField.buildTextField( - autofocus: true, + Widget child; + if (showPageView) { + child = buildPageView(); + } else if (!isShowingSearchResult) { + child = buildLinkView(); + } else { + return SizedBox( + width: 360, + child: Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + SizedBox( + width: 360, + height: 32, + child: searchTextField.buildTextField( + autofocus: true, + context: context, + ), + ), + VSpace(6), + searchTextField.buildResultContainer( + context: context, + width: 360, + onPageLinkSelected: onPageSelected, + onLinkSelected: onLinkSelected, + ), + ], + ), + ); + } + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + mainAxisSize: MainAxisSize.min, + children: [ + child, + if (showErrorText) + Padding( + padding: const EdgeInsets.only(top: 4), + child: FlowyText.regular( + LocaleKeys.document_plugins_file_networkUrlInvalid.tr(), + color: LinkStyle.textStatusError, + fontSize: 12, + figmaLineHeight: 16, ), ), - VSpace(6), - searchTextField.buildResultContainer( - context: context, - width: 360, - onPageLinkSelected: onPageSelected, - onLinkSelected: onLinkSelected, - ), - ], - ), + ], ); } @@ -197,15 +214,15 @@ class _LinkEditMenuState extends State { preferBelow: false, decoration: BoxDecoration( borderRadius: BorderRadius.all(Radius.circular(8)), - border: Border.all(color: LinkStyle.borderColor), + border: Border.all(color: LinkStyle.borderColor(context)), ), - onPressed: onRemoveLink, + onPressed: () => onRemoveLink.call(linkInfo), ), Spacer(), DecoratedBox( decoration: BoxDecoration( borderRadius: BorderRadius.all(Radius.circular(8)), - border: Border.all(color: LinkStyle.borderColor), + border: Border.all(color: LinkStyle.borderColor(context)), ), child: FlowyTextButton( LocaleKeys.button_cancel.tr(), @@ -214,7 +231,9 @@ class _LinkEditMenuState extends State { constraints: BoxConstraints(maxWidth: 78, minHeight: 32), fontSize: 14, lineHeight: 20 / 14, - fontColor: LinkStyle.textPrimary, + fontColor: Theme.of(context).isLightMode + ? LinkStyle.textPrimary + : Theme.of(context).iconTheme.color, fillColor: Colors.transparent, fontWeight: FontWeight.w400, onPressed: onDismiss, @@ -232,14 +251,26 @@ class _LinkEditMenuState extends State { fontSize: 14, lineHeight: 20 / 14, hoverColor: LinkStyle.fillThemeThick.withAlpha(200), - fontColor: - enableApply ? Colors.white : LinkStyle.textTertiary, - fillColor: enableApply - ? LinkStyle.fillThemeThick - : LinkStyle.borderColor, + fontColor: Colors.white, + fillColor: LinkStyle.fillThemeThick, fontWeight: FontWeight.w400, - onPressed: - enableApply ? () => widget.onApply.call(linkInfo) : null, + onPressed: () { + if (isShowingSearchResult) { + onConfirm(); + return; + } + if (linkInfo.link.isEmpty) { + widget.onRemoveLink(linkInfo); + return; + } + if (linkInfo.link.isEmpty || !isUri(linkInfo.link)) { + setState(() { + showErrorText = true; + }); + return; + } + widget.onApply.call(linkInfo); + }, ); }, ), @@ -272,6 +303,7 @@ class _LinkEditMenuState extends State { }, decoration: LinkStyle.buildLinkTextFieldInputDecoration( LocaleKeys.document_toolbar_linkNameHint.tr(), + context, ), ), ); @@ -288,24 +320,34 @@ class _LinkEditMenuState extends State { ), ); } else { + final viewName = view.name; + final displayName = viewName.isEmpty + ? LocaleKeys.document_title_placeholder.tr() + : viewName; child = GestureDetector( onTap: showSearchResult, child: MouseRegion( cursor: SystemMouseCursors.click, - child: Container( - height: 32, - padding: EdgeInsets.fromLTRB(8, 6, 8, 6), - child: Row( - children: [ - searchTextField.buildIcon(view), - HSpace(8), - Flexible( - child: FlowyText.regular( - view.name, - overflow: TextOverflow.ellipsis, + child: FlowyTooltip( + preferBelow: false, + message: displayName, + child: Container( + height: 32, + padding: EdgeInsets.fromLTRB(8, 0, 8, 0), + child: Row( + children: [ + searchTextField.buildIcon(view), + HSpace(4), + Flexible( + child: FlowyText.regular( + displayName, + overflow: TextOverflow.ellipsis, + figmaLineHeight: 20, + fontSize: 14, + ), ), - ), - ], + ], + ), ), ), ), @@ -324,24 +366,28 @@ class _LinkEditMenuState extends State { width: 360, height: 32, decoration: buildDecoration(), - child: GestureDetector( - onTap: showSearchResult, - child: MouseRegion( - cursor: SystemMouseCursors.click, - child: Padding( - padding: EdgeInsets.fromLTRB(8, 6, 8, 6), - child: Row( - children: [ - FlowySvg(FlowySvgs.toolbar_link_earth_m), - HSpace(8), - Flexible( - child: FlowyText.regular( - linkInfo.link, - overflow: TextOverflow.ellipsis, - figmaLineHeight: 20, + child: FlowyTooltip( + preferBelow: false, + message: linkInfo.link, + child: GestureDetector( + onTap: showSearchResult, + child: MouseRegion( + cursor: SystemMouseCursors.click, + child: Padding( + padding: EdgeInsets.fromLTRB(8, 6, 8, 6), + child: Row( + children: [ + FlowySvg(FlowySvgs.toolbar_link_earth_m), + HSpace(8), + Flexible( + child: FlowyText.regular( + linkInfo.link, + overflow: TextOverflow.ellipsis, + figmaLineHeight: 20, + ), ), - ), - ], + ], + ), ), ), ), @@ -349,12 +395,21 @@ class _LinkEditMenuState extends State { ); } + void onConfirm() { + searchTextField.onSearchResult( + onLink: onLinkSelected, + onRecentViews: () => onPageSelected(searchTextField.currentRecentView), + onSearchViews: () => onPageSelected(searchTextField.currentSearchedView), + onEmpty: () { + searchTextField.unfocus(); + }, + ); + } + Future getPageView() async { if (!linkInfo.isPage) return; - final link = linkInfo.link; - final viewId = link.split('/').lastOrNull ?? ''; final (view, isInTrash, isDeleted) = - await ViewBackendService.getMentionPageStatus(viewId); + await ViewBackendService.getMentionPageStatus(linkInfo.viewId); if (mounted) { setState(() { currentView = view; @@ -413,7 +468,7 @@ class _LinkEditMenuState extends State { BoxDecoration buildDecoration() => BoxDecoration( borderRadius: BorderRadius.circular(8), - border: Border.all(color: LinkStyle.borderColor), + border: Border.all(color: LinkStyle.borderColor(context)), ); } @@ -426,4 +481,6 @@ class LinkInfo { Attributes toAttribute() => {AppFlowyRichTextKeys.href: link, kIsPageLink: isPage}; + + String get viewId => isPage ? link.split('/').lastOrNull ?? '' : ''; } diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/link/link_hover_menu.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/link/link_hover_menu.dart index e47386699b..b07d2949d9 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/link/link_hover_menu.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/link/link_hover_menu.dart @@ -3,20 +3,25 @@ import 'dart:math'; import 'package:appflowy/core/helpers/url_launcher.dart'; import 'package:appflowy/generated/flowy_svgs.g.dart'; import 'package:appflowy/generated/locale_keys.g.dart'; +import 'package:appflowy/plugins/document/application/document_bloc.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/copy_and_paste/clipboard_service.dart'; +import 'package:appflowy/plugins/document/presentation/editor_plugins/desktop_toolbar/desktop_floating_toolbar.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/mention/mention_page_block.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/toolbar_item/custom_link_toolbar_item.dart'; import 'package:appflowy/startup/startup.dart'; +import 'package:appflowy/util/theme_extension.dart'; import 'package:appflowy/workspace/application/view/view_service.dart'; +import 'package:appflowy/workspace/presentation/widgets/dialogs.dart'; +import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart'; import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:flowy_infra_ui/flowy_infra_ui.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; import 'link_create_menu.dart'; import 'link_edit_menu.dart'; -import 'link_styles.dart'; class LinkHoverTrigger extends StatefulWidget { const LinkHoverTrigger({ @@ -45,6 +50,7 @@ class LinkHoverTrigger extends StatefulWidget { class _LinkHoverTriggerState extends State { final hoverMenuController = PopoverController(); final editMenuController = PopoverController(); + final toolbarController = getIt(); bool isHoverMenuShowing = false; bool isHoverMenuHovering = false; bool isHoverTriggerHovering = false; @@ -57,12 +63,13 @@ class _LinkHoverTriggerState extends State { Attributes get attribute => widget.attribute; - HoverTriggerKey get triggerKey => HoverTriggerKey(widget.node.id, selection); + late HoverTriggerKey triggerKey = HoverTriggerKey(widget.node.id, selection); @override void initState() { super.initState(); getIt()._add(triggerKey, showLinkHoverMenu); + toolbarController.addDisplayListener(onToolbarShow); } @override @@ -70,6 +77,7 @@ class _LinkHoverTriggerState extends State { hoverMenuController.close(); editMenuController.close(); getIt()._remove(triggerKey, showLinkHoverMenu); + toolbarController.removeDisplayListener(onToolbarShow); super.dispose(); } @@ -132,9 +140,9 @@ class _LinkHoverTriggerState extends State { tryToDismissLinkHoverMenu(); }, onOpenLink: openLink, - onCopyLink: copyLink, + onCopyLink: () => copyLink(context), onEditLink: showLinkEditMenu, - onRemoveLink: () => removeLink(editorState, selection, true), + onRemoveLink: () => removeLink(editorState, selection), ), child: child, ); @@ -144,6 +152,7 @@ class _LinkHoverTriggerState extends State { final href = attribute.href ?? '', isPage = attribute.isPage, title = editorState.getTextInSelection(selection).join(); + final currentViewId = context.read()?.documentId ?? ''; return AppFlowyPopover( controller: editMenuController, direction: PopoverDirection.bottomWithLeftAligned, @@ -159,6 +168,7 @@ class _LinkHoverTriggerState extends State { minHeight: 282, ), popupBuilder: (context) => LinkEditMenu( + currentViewId: currentViewId, linkInfo: LinkInfo(name: title, link: href, isPage: isPage), onDismiss: () => editMenuController.close(), onApply: (info) async { @@ -173,14 +183,19 @@ class _LinkHoverTriggerState extends State { editMenuController.close(); await editorState.apply(transaction); }, - onRemoveLink: () => removeLink(editorState, selection, true), + onRemoveLink: (linkinfo) => + onRemoveAndReplaceLink(editorState, selection, linkinfo.name), ), child: child, ); } + void onToolbarShow() => hoverMenuController.close(); + void showLinkHoverMenu() { - if (isHoverMenuShowing) return; + if (isHoverMenuShowing || toolbarController.isToolbarShowing || !mounted) { + return; + } keepEditorFocusNotifier.increase(); hoverMenuController.show(); } @@ -219,20 +234,24 @@ class _LinkHoverTriggerState extends State { } } - Future copyLink() async { + Future copyLink(BuildContext context) async { final href = widget.attribute.href ?? ''; if (href.isEmpty) return; await getIt() .setData(ClipboardServiceData(plainText: href)); + if (context.mounted) { + showToastNotification( + context, + message: LocaleKeys.shareAction_copyLinkSuccess.tr(), + ); + } hoverMenuController.close(); } void removeLink( EditorState editorState, Selection selection, - bool isHref, ) { - if (!isHref) return; final node = editorState.getNodeAtPath(selection.end.path); if (node == null) { return; @@ -251,9 +270,34 @@ class _LinkHoverTriggerState extends State { ); editorState.apply(transaction); } + + void onRemoveAndReplaceLink( + EditorState editorState, + Selection selection, + String text, + ) { + final node = editorState.getNodeAtPath(selection.end.path); + if (node == null) { + return; + } + final index = selection.normalized.startIndex; + final length = selection.length; + final transaction = editorState.transaction + ..replaceText( + node, + index, + length, + text, + attributes: { + BuiltInAttributeKey.href: null, + kIsPageLink: null, + }, + ); + editorState.apply(transaction); + } } -class LinkHoverMenu extends StatelessWidget { +class LinkHoverMenu extends StatefulWidget { const LinkHoverMenu({ super.key, required this.attribute, @@ -275,17 +319,31 @@ class LinkHoverMenu extends StatelessWidget { final VoidCallback onEditLink; final VoidCallback onRemoveLink; + @override + State createState() => _LinkHoverMenuState(); +} + +class _LinkHoverMenuState extends State { + ViewPB? currentView; + late bool isPage = widget.attribute.isPage; + late String href = widget.attribute.href ?? ''; + + @override + void initState() { + super.initState(); + if (isPage) getPageView(); + } + @override Widget build(BuildContext context) { - final href = attribute.href ?? ''; return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ MouseRegion( - onEnter: onEnter, - onExit: onExit, + onEnter: widget.onEnter, + onExit: widget.onExit, child: SizedBox( - width: max(320, triggerSize.width), + width: max(320, widget.triggerSize.width), height: 48, child: Align( alignment: Alignment.centerLeft, @@ -293,21 +351,15 @@ class LinkHoverMenu extends StatelessWidget { width: 320, height: 48, decoration: buildToolbarLinkDecoration(context), - padding: EdgeInsets.all(8), + padding: EdgeInsets.fromLTRB(12, 8, 8, 8), child: Row( children: [ - Expanded( - child: FlowyText.regular( - href, - fontSize: 14, - figmaLineHeight: 20, - overflow: TextOverflow.ellipsis, - ), - ), + Expanded(child: buildLinkWidget()), Container( height: 20, width: 1, - color: LinkStyle.borderColor, + color: Color(0xffE8ECF3) + .withAlpha(Theme.of(context).isLightMode ? 255 : 40), margin: EdgeInsets.symmetric(horizontal: 6), ), FlowyIconButton( @@ -315,21 +367,21 @@ class LinkHoverMenu extends StatelessWidget { tooltipText: LocaleKeys.editor_copyLink.tr(), width: 36, height: 32, - onPressed: onCopyLink, + onPressed: widget.onCopyLink, ), FlowyIconButton( icon: FlowySvg(FlowySvgs.toolbar_link_edit_m), tooltipText: LocaleKeys.editor_editLink.tr(), width: 36, height: 32, - onPressed: onEditLink, + onPressed: widget.onEditLink, ), FlowyIconButton( icon: FlowySvg(FlowySvgs.toolbar_link_unlink_m), tooltipText: LocaleKeys.editor_removeLink.tr(), width: 36, height: 32, - onPressed: onRemoveLink, + onPressed: widget.onRemoveLink, ), ], ), @@ -339,13 +391,13 @@ class LinkHoverMenu extends StatelessWidget { ), MouseRegion( cursor: SystemMouseCursors.click, - onEnter: onEnter, - onExit: onExit, + onEnter: widget.onEnter, + onExit: widget.onExit, child: GestureDetector( - onTap: onOpenLink, + onTap: widget.onOpenLink, child: Container( - width: triggerSize.width, - height: triggerSize.height, + width: widget.triggerSize.width, + height: widget.triggerSize.height, color: Colors.black.withAlpha(1), ), ), @@ -353,6 +405,46 @@ class LinkHoverMenu extends StatelessWidget { ], ); } + + Future getPageView() async { + final viewId = href.split('/').lastOrNull ?? ''; + final (view, isInTrash, isDeleted) = + await ViewBackendService.getMentionPageStatus(viewId); + if (mounted) { + setState(() { + currentView = view; + }); + } + } + + Widget buildLinkWidget() { + final view = currentView; + if (isPage && view == null) { + return SizedBox.square( + dimension: 20, + child: CircularProgressIndicator(), + ); + } + String text = ''; + if (isPage && view != null) { + text = view.name; + if (text.isEmpty) { + text = LocaleKeys.document_title_placeholder.tr(); + } + } else { + text = href; + } + return FlowyTooltip( + message: text, + preferBelow: false, + child: FlowyText.regular( + text, + overflow: TextOverflow.ellipsis, + figmaLineHeight: 20, + fontSize: 14, + ), + ); + } } class HoverTriggerKey { @@ -367,7 +459,11 @@ class HoverTriggerKey { other is HoverTriggerKey && runtimeType == other.runtimeType && nodeId == other.nodeId && - selection == other.selection; + isSelectionSame(other.selection); + + bool isSelectionSame(Selection other) => + (selection.start == other.start && selection.end == other.end) || + (selection.start == other.end && selection.end == other.start); @override int get hashCode => nodeId.hashCode ^ selection.hashCode; diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/link/link_search_text_field.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/link/link_search_text_field.dart index 4fcd9e3b8b..97fd6abdad 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/link/link_search_text_field.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/link/link_search_text_field.dart @@ -12,6 +12,8 @@ import 'package:appflowy/workspace/application/view/view_service.dart'; import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart'; // ignore: implementation_imports import 'package:appflowy_editor/src/flutter/scrollable_positioned_list/scrollable_positioned_list.dart'; +// ignore: implementation_imports +import 'package:appflowy_editor/src/editor/util/link_util.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:flowy_infra_ui/flowy_infra_ui.dart'; import 'package:flutter/material.dart'; @@ -25,12 +27,16 @@ class LinkSearchTextField { this.onEscape, this.onEnter, this.onDataRefresh, + this.initialViewId = '', + required this.currentViewId, String? initialSearchText, }) : textEditingController = TextEditingController( - text: initialSearchText ?? '', + text: isUri(initialSearchText ?? '') ? initialSearchText : '', ); final TextEditingController textEditingController; + final String initialViewId; + final String currentViewId; final ItemScrollController searchController = ItemScrollController(); late FocusNode focusNode = FocusNode(onKeyEvent: onKeyEvent); final List searchedViews = []; @@ -43,7 +49,7 @@ class LinkSearchTextField { String get searchText => textEditingController.text; - bool get isButtonEnable => searchText.isNotEmpty; + bool get isTextfieldEnable => searchText.isNotEmpty && isUri(searchText); bool get showingRecent => searchText.isEmpty && recentViews.isNotEmpty; @@ -58,7 +64,11 @@ class LinkSearchTextField { recentViews.clear(); } - Widget buildTextField({bool autofocus = false}) { + Widget buildTextField({ + bool autofocus = false, + bool showError = false, + required BuildContext context, + }) { return TextFormField( autovalidateMode: AutovalidateMode.onUserInteraction, autofocus: autofocus, @@ -81,6 +91,8 @@ class LinkSearchTextField { }, decoration: LinkStyle.buildLinkTextFieldInputDecoration( LocaleKeys.document_toolbar_linkInputHint.tr(), + context, + showErrorBorder: showError, ), ); } @@ -177,29 +189,41 @@ class LinkSearchTextField { bool isSelected, ValueChanged? onSubmittedPageLink, ) { + final viewName = view.name; + final displayName = viewName.isEmpty + ? LocaleKeys.document_title_placeholder.tr() + : viewName; + final isCurrent = initialViewId == view.id; return SizedBox( height: 32, child: FlowyButton( isSelected: isSelected, - leftIcon: buildIcon(view), + leftIcon: buildIcon(view, padding: EdgeInsets.zero), text: FlowyText.regular( - view.name, + displayName, overflow: TextOverflow.ellipsis, fontSize: 14, figmaLineHeight: 20, ), + rightIcon: isCurrent ? FlowySvg(FlowySvgs.toolbar_check_m) : null, onTap: () => onSubmittedPageLink?.call(view), ), ); } - Widget buildIcon(ViewPB view) { + Widget buildIcon( + ViewPB view, { + EdgeInsetsGeometry padding = const EdgeInsets.only(top: 4), + }) { if (view.icon.value.isEmpty) return view.defaultIcon(size: Size(20, 20)); final iconData = view.icon.toEmojiIconData(); - return RawEmojiIconWidget( - emoji: iconData, - emojiSize: iconData.type == FlowyIconType.emoji ? 16 : 20, - lineHeight: 1, + return Padding( + padding: padding, + child: RawEmojiIconWidget( + emoji: iconData, + emojiSize: iconData.type == FlowyIconType.emoji ? 16 : 20, + lineHeight: 1, + ), ); } @@ -288,6 +312,7 @@ class LinkSearchTextField { final views = sectionViews .unique((e) => e.item.id) .map((e) => e.item) + .where((e) => e.id != currentViewId) .take(5) .toList(); recentViews.clear(); @@ -303,13 +328,14 @@ class LinkSearchTextField { ?.items .where( (view) => - view.name.toLowerCase().contains(search.toLowerCase()) || - (view.name.isEmpty && search.isEmpty) || - (view.name.isEmpty && - LocaleKeys.menuAppHeader_defaultNewPageName - .tr() - .toLowerCase() - .contains(search.toLowerCase())), + (view.id != currentViewId) && + (view.name.toLowerCase().contains(search.toLowerCase()) || + (view.name.isEmpty && search.isEmpty) || + (view.name.isEmpty && + LocaleKeys.menuAppHeader_defaultNewPageName + .tr() + .toLowerCase() + .contains(search.toLowerCase()))), ) .take(10) .toList(); diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/link/link_styles.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/link/link_styles.dart index ee63269dfc..f29583c9b4 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/link/link_styles.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/link/link_styles.dart @@ -1,19 +1,33 @@ +import 'package:appflowy/util/theme_extension.dart'; import 'package:flutter/material.dart'; class LinkStyle { - static const borderColor = Color(0xFFE8ECF3); static const textTertiary = Color(0xFF99A1A8); + static const textStatusError = Color(0xffE71D32); static const fillThemeThick = Color(0xFF00B5FF); static const shadowMedium = Color(0x1F22251F); static const textPrimary = Color(0xFF1F2329); - static InputDecoration buildLinkTextFieldInputDecoration(String hintText) { - const border = OutlineInputBorder( + static Color borderColor(BuildContext context) => + Theme.of(context).isLightMode ? Color(0xFFE8ECF3) : Color(0xffbdbdbd); + + static InputDecoration buildLinkTextFieldInputDecoration( + String hintText, + BuildContext context, { + bool showErrorBorder = false, + }) { + final border = OutlineInputBorder( borderRadius: BorderRadius.all(Radius.circular(8.0)), - borderSide: BorderSide(color: LinkStyle.borderColor), + borderSide: BorderSide( + color: borderColor(context), + ), ); final enableBorder = border.copyWith( - borderSide: BorderSide(color: LinkStyle.fillThemeThick), + borderSide: BorderSide( + color: showErrorBorder + ? LinkStyle.textStatusError + : LinkStyle.fillThemeThick, + ), ); const hintStyle = TextStyle( fontSize: 14, diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/toolbar_cubit.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/toolbar_cubit.dart deleted file mode 100644 index 8e114bf4c8..0000000000 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/desktop_toolbar/toolbar_cubit.dart +++ /dev/null @@ -1,17 +0,0 @@ -import 'dart:ui'; - -import 'package:bloc/bloc.dart'; - -class ToolbarCubit extends Cubit { - ToolbarCubit(this.onDismissCallback) : super(ToolbarState._()); - - final VoidCallback onDismissCallback; - - void dismiss() { - onDismissCallback.call(); - } -} - -class ToolbarState { - const ToolbarState._(); -} diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/toolbar_item/custom_format_toolbar_items.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/toolbar_item/custom_format_toolbar_items.dart index d665230a59..77d94451c8 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/toolbar_item/custom_format_toolbar_items.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/toolbar_item/custom_format_toolbar_items.dart @@ -2,6 +2,7 @@ import 'package:appflowy/generated/flowy_svgs.g.dart'; import 'package:appflowy/generated/locale_keys.g.dart'; import 'package:appflowy/plugins/document/presentation/editor_page.dart'; import 'package:appflowy/plugins/document/presentation/editor_style.dart'; +import 'package:appflowy/util/theme_extension.dart'; import 'package:appflowy_editor/appflowy_editor.dart'; // ignore: implementation_imports import 'package:appflowy_editor/src/editor/toolbar/desktop/items/utils/tooltip_util.dart'; @@ -68,7 +69,7 @@ class _FormatToolbarItem extends ToolbarItem { final hoverColor = isHighlight ? highlightColor : EditorStyleCustomizer.toolbarHoverColor(context); - final isDark = Theme.of(context).brightness == Brightness.dark; + final isDark = !Theme.of(context).isLightMode; final child = FlowyIconButton( width: 36, diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/toolbar_item/custom_hightlight_color_toolbar_item.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/toolbar_item/custom_hightlight_color_toolbar_item.dart index 2f2f75f0f0..8b32ebdde9 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/toolbar_item/custom_hightlight_color_toolbar_item.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/toolbar_item/custom_hightlight_color_toolbar_item.dart @@ -3,6 +3,7 @@ import 'package:appflowy/plugins/document/presentation/editor_page.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/desktop_toolbar/color_picker.dart'; import 'package:appflowy/plugins/document/presentation/editor_style.dart'; import 'package:appflowy_editor/appflowy_editor.dart' hide ColorPicker; +import 'package:flowy_infra/theme_extension_v2.dart'; import 'package:flowy_infra_ui/flowy_infra_ui.dart'; import 'package:flutter/material.dart'; @@ -78,7 +79,7 @@ class _HighlightColorPickerWidgetState } Widget buildChild(BuildContext context) { - final iconColor = Theme.of(context).iconTheme.color; + final iconColor = AFThemeExtensionV2.of(context).icon_primary; final child = FlowyIconButton( width: 36, diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/toolbar_item/custom_link_toolbar_item.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/toolbar_item/custom_link_toolbar_item.dart index fb241d5309..693c7a64ce 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/toolbar_item/custom_link_toolbar_item.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/toolbar_item/custom_link_toolbar_item.dart @@ -1,15 +1,17 @@ import 'package:appflowy/generated/flowy_svgs.g.dart'; +import 'package:appflowy/plugins/document/application/document_bloc.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/base/toolbar_extension.dart'; +import 'package:appflowy/plugins/document/presentation/editor_plugins/desktop_toolbar/desktop_floating_toolbar.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/desktop_toolbar/link/link_create_menu.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/desktop_toolbar/link/link_hover_menu.dart'; -import 'package:appflowy/plugins/document/presentation/editor_plugins/desktop_toolbar/toolbar_cubit.dart'; import 'package:appflowy/plugins/document/presentation/editor_style.dart'; import 'package:appflowy/startup/startup.dart'; +import 'package:appflowy/util/theme_extension.dart'; import 'package:appflowy_editor/appflowy_editor.dart'; +import 'package:flowy_infra/theme_extension_v2.dart'; import 'package:flowy_infra_ui/style_widget/icon_button.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; - import 'toolbar_id_enum.dart'; const kIsPageLink = 'is_page_link'; @@ -28,10 +30,10 @@ final customLinkItem = ToolbarItem( ); }); + final isDark = !Theme.of(context).isLightMode; final hoverColor = isHref ? highlightColor : EditorStyleCustomizer.toolbarHoverColor(context); - final toolbarCubit = context.read(); final child = FlowyIconButton( width: 36, @@ -41,16 +43,20 @@ final customLinkItem = ToolbarItem( icon: FlowySvg( FlowySvgs.toolbar_link_m, size: Size.square(20.0), - color: Theme.of(context).iconTheme.color, + color: (isDark && isHref) + ? Color(0xFF282E3A) + : AFThemeExtensionV2.of(context).icon_primary, ), onPressed: () { - toolbarCubit?.dismiss(); - if (isHref) { - getIt().call( - HoverTriggerKey(nodes.first.id, selection), - ); + getIt().hideToolbar(); + if (!isHref) { + final viewId = context.read()?.documentId ?? ''; + showLinkCreateMenu(context, editorState, selection, viewId); } else { - showLinkCreateMenu(context, editorState, selection); + WidgetsBinding.instance.addPostFrameCallback((_) { + getIt() + .call(HoverTriggerKey(nodes.first.id, selection)); + }); } }, ); diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/toolbar_item/custom_text_color_toolbar_item.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/toolbar_item/custom_text_color_toolbar_item.dart index 189cf64bd4..525eebe917 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/toolbar_item/custom_text_color_toolbar_item.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/toolbar_item/custom_text_color_toolbar_item.dart @@ -5,6 +5,7 @@ import 'package:appflowy/plugins/document/presentation/editor_plugins/desktop_to import 'package:appflowy/plugins/document/presentation/editor_style.dart'; import 'package:appflowy_editor/appflowy_editor.dart' hide ColorPicker; import 'package:easy_localization/easy_localization.dart'; +import 'package:flowy_infra/theme_extension_v2.dart'; import 'package:flowy_infra_ui/flowy_infra_ui.dart'; import 'package:flutter/material.dart'; import 'toolbar_id_enum.dart'; @@ -77,7 +78,7 @@ class _TextColorPickerWidgetState extends State { } Widget buildChild(BuildContext context) { - final iconColor = Theme.of(context).iconTheme.color; + final iconColor = AFThemeExtensionV2.of(context).icon_primary; final child = FlowyIconButton( width: 36, height: 32, diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/toolbar_item/more_option_toolbar_item.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/toolbar_item/more_option_toolbar_item.dart index fde59eee51..46b707a8d3 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/toolbar_item/more_option_toolbar_item.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/toolbar_item/more_option_toolbar_item.dart @@ -1,9 +1,10 @@ import 'package:appflowy/generated/flowy_svgs.g.dart'; import 'package:appflowy/generated/locale_keys.g.dart'; +import 'package:appflowy/plugins/document/application/document_bloc.dart'; import 'package:appflowy/plugins/document/presentation/editor_page.dart'; +import 'package:appflowy/plugins/document/presentation/editor_plugins/desktop_toolbar/desktop_floating_toolbar.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/desktop_toolbar/link/link_create_menu.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/desktop_toolbar/link/link_hover_menu.dart'; -import 'package:appflowy/plugins/document/presentation/editor_plugins/desktop_toolbar/toolbar_cubit.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart'; import 'package:appflowy/plugins/document/presentation/editor_style.dart'; import 'package:appflowy/startup/startup.dart'; @@ -288,7 +289,7 @@ class _MoreOptionActionListState extends State { popoverController: suggestionsPopoverController, popoverDirection: PopoverDirection.leftWithTopAligned, showOffset: Offset(-8, height), - onSelect: () => context.read()?.dismiss(), + onSelect: () => getIt().hideToolbar(), child: buildCommandItem( MoreOptionCommand.suggestions, rightIcon: FlowySvg(FlowySvgs.toolbar_arrow_right_m), @@ -308,7 +309,7 @@ class _MoreOptionActionListState extends State { popoverController: textAlignPopoverController, popoverDirection: PopoverDirection.leftWithTopAligned, showOffset: Offset(-8, 0), - onSelect: () => context.read()?.dismiss(), + onSelect: () => getIt().hideToolbar(), highlightColor: highlightColor, child: buildCommandItem( MoreOptionCommand.textAlign, @@ -379,13 +380,14 @@ enum MoreOptionCommand { (attributes) => attributes[AppFlowyRichTextKeys.href] != null, ); }); - context.read()?.dismiss(); + getIt().hideToolbar(); if (isHref) { getIt().call( HoverTriggerKey(nodes.first.id, selection), ); } else { - showLinkCreateMenu(context, editorState, selection); + final viewId = context.read()?.documentId ?? ''; + showLinkCreateMenu(context, editorState, selection, viewId); } } else if (this == strikethrough) { await editorState.toggleAttribute(name); diff --git a/frontend/appflowy_flutter/lib/startup/startup.dart b/frontend/appflowy_flutter/lib/startup/startup.dart index bf5dcfdafc..7a282b3856 100644 --- a/frontend/appflowy_flutter/lib/startup/startup.dart +++ b/frontend/appflowy_flutter/lib/startup/startup.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'dart:io'; import 'package:appflowy/env/cloud_env.dart'; +import 'package:appflowy/plugins/document/presentation/editor_plugins/desktop_toolbar/desktop_floating_toolbar.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/desktop_toolbar/link/link_hover_menu.dart'; import 'package:appflowy/util/expand_views.dart'; import 'package:appflowy/workspace/application/settings/prelude.dart'; @@ -187,6 +188,9 @@ Future initGetIt( getIt.registerSingleton(PluginSandbox()); getIt.registerSingleton(ViewExpanderRegistry()); getIt.registerSingleton(LinkHoverTriggers()); + getIt.registerSingleton( + FloatingToolbarController(), + ); await DependencyResolver.resolve(getIt, mode); } diff --git a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_bloc.dart b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_bloc.dart index 66ba7fe572..c4a4ee3afa 100644 --- a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_bloc.dart +++ b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_bloc.dart @@ -1,94 +1,145 @@ import 'dart:async'; import 'package:appflowy_backend/dispatch/dispatch.dart'; +import 'package:appflowy_backend/log.dart'; import 'package:appflowy_backend/protobuf/flowy-ai/entities.pb.dart'; -import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart'; import 'package:appflowy_result/appflowy_result.dart'; import 'package:bloc/bloc.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; + +import 'local_llm_listener.dart'; + part 'local_ai_bloc.freezed.dart'; -class LocalAIToggleBloc extends Bloc { - LocalAIToggleBloc() : super(const LocalAIToggleState()) { - on(_handleEvent); +class LocalAiPluginBloc extends Bloc { + LocalAiPluginBloc() : super(const LoadingLocalAiPluginState()) { + on(_handleEvent); + _startListening(); + _getLocalAiState(); + } + + final listener = LocalAIStateListener(); + + @override + Future close() async { + await listener.stop(); + return super.close(); } Future _handleEvent( - LocalAIToggleEvent event, - Emitter emit, + LocalAiPluginEvent event, + Emitter emit, ) async { await event.when( - started: () async { - final result = await AIEventGetLocalAIState().send(); - _handleResult(emit, result); + didReceiveAiState: (aiState) { + emit( + LocalAiPluginState.ready( + isEnabled: aiState.enabled, + version: aiState.pluginVersion, + runningState: aiState.state, + lackOfResource: + aiState.hasLackOfResource() ? aiState.lackOfResource : null, + ), + ); + }, + didReceiveLackOfResources: (resources) { + state.maybeMap( + ready: (readyState) { + emit(readyState.copyWith(lackOfResource: resources)); + }, + orElse: () {}, + ); }, toggle: () async { - emit( - state.copyWith( - pageIndicator: const LocalAIToggleStateIndicator.loading(), - ), - ); - unawaited( - AIEventToggleLocalAI().send().then( - (result) { - if (!isClosed) { - add(LocalAIToggleEvent.handleResult(result)); - } - }, - ), + emit(LocalAiPluginState.loading()); + await AIEventToggleLocalAI().send().fold( + (aiState) { + add(LocalAiPluginEvent.didReceiveAiState(aiState)); + }, + Log.error, ); }, - handleResult: (result) { - _handleResult(emit, result); + restart: () async { + emit(LocalAiPluginState.loading()); + await AIEventRestartLocalAI().send(); }, ); } - void _handleResult( - Emitter emit, - FlowyResult result, - ) { - result.fold( - (localAI) { - emit( - state.copyWith( - pageIndicator: - LocalAIToggleStateIndicator.isEnabled(localAI.enabled), - ), - ); + void _startListening() { + listener.start( + stateCallback: (pluginState) { + add(LocalAiPluginEvent.didReceiveAiState(pluginState)); }, - (err) { - emit( - state.copyWith( - pageIndicator: LocalAIToggleStateIndicator.error(err), - ), - ); + resourceCallback: (data) { + add(LocalAiPluginEvent.didReceiveLackOfResources(data)); }, ); } + + void _getLocalAiState() { + AIEventGetLocalAIState().send().fold( + (aiState) { + add(LocalAiPluginEvent.didReceiveAiState(aiState)); + }, + Log.error, + ); + } } @freezed -class LocalAIToggleEvent with _$LocalAIToggleEvent { - const factory LocalAIToggleEvent.started() = _Started; - const factory LocalAIToggleEvent.toggle() = _Toggle; - const factory LocalAIToggleEvent.handleResult( - FlowyResult result, - ) = _HandleResult; +class LocalAiPluginEvent with _$LocalAiPluginEvent { + const factory LocalAiPluginEvent.didReceiveAiState(LocalAIPB aiState) = + _DidReceiveAiState; + const factory LocalAiPluginEvent.didReceiveLackOfResources( + LackOfAIResourcePB resources, + ) = _DidReceiveLackOfResources; + const factory LocalAiPluginEvent.toggle() = _Toggle; + const factory LocalAiPluginEvent.restart() = _Restart; } @freezed -class LocalAIToggleState with _$LocalAIToggleState { - const factory LocalAIToggleState({ - @Default(LocalAIToggleStateIndicator.loading()) - LocalAIToggleStateIndicator pageIndicator, - }) = _LocalAIToggleState; -} +class LocalAiPluginState with _$LocalAiPluginState { + const LocalAiPluginState._(); -@freezed -class LocalAIToggleStateIndicator with _$LocalAIToggleStateIndicator { - // when start downloading the model - const factory LocalAIToggleStateIndicator.error(FlowyError error) = _OnError; - const factory LocalAIToggleStateIndicator.isEnabled(bool isEnabled) = _Ready; - const factory LocalAIToggleStateIndicator.loading() = _Loading; + const factory LocalAiPluginState.ready({ + required bool isEnabled, + required String version, + required RunningStatePB runningState, + required LackOfAIResourcePB? lackOfResource, + }) = ReadyLocalAiPluginState; + + const factory LocalAiPluginState.loading() = LoadingLocalAiPluginState; + + bool get isEnabled { + return maybeWhen( + ready: (isEnabled, _, __, ___) => isEnabled, + orElse: () => false, + ); + } + + bool get showIndicator { + return maybeWhen( + ready: (isEnabled, _, runningState, lackOfResource) => + runningState != RunningStatePB.Running || lackOfResource != null, + orElse: () => false, + ); + } + + bool get showSettings { + return maybeWhen( + ready: (isEnabled, _, runningState, lackOfResource) { + final isConnecting = [ + RunningStatePB.Connecting, + RunningStatePB.Connected, + ].contains(runningState); + + final resourcesReadyOrMissingModel = lackOfResource == null || + lackOfResource.resourceType == LackOfAIResourceTypePB.MissingModel; + + return !isConnecting && resourcesReadyOrMissingModel; + }, + orElse: () => false, + ); + } } diff --git a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_on_boarding_bloc.dart b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_on_boarding_bloc.dart index 2c1bf34a87..3bb26a182b 100644 --- a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_on_boarding_bloc.dart +++ b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_on_boarding_bloc.dart @@ -26,7 +26,7 @@ class LocalAIOnBoardingBloc _dispatch(); } - Future _onPaymentSuccessful() async { + void _onPaymentSuccessful() { if (isClosed) { return; } diff --git a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_setting_panel_bloc.dart b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_setting_panel_bloc.dart deleted file mode 100644 index 60c68b70c6..0000000000 --- a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/local_ai_setting_panel_bloc.dart +++ /dev/null @@ -1,92 +0,0 @@ -import 'dart:async'; - -import 'package:appflowy/workspace/application/settings/ai/local_llm_listener.dart'; -import 'package:appflowy_backend/dispatch/dispatch.dart'; -import 'package:appflowy_backend/log.dart'; -import 'package:appflowy_backend/protobuf/flowy-ai/entities.pb.dart'; -import 'package:appflowy_result/appflowy_result.dart'; -import 'package:bloc/bloc.dart'; -import 'package:freezed_annotation/freezed_annotation.dart'; - -part 'local_ai_setting_panel_bloc.freezed.dart'; - -class LocalAISettingPanelBloc - extends Bloc { - LocalAISettingPanelBloc() - : listener = LocalAIStateListener(), - super(const LocalAISettingPanelState()) { - on(_handleEvent); - - listener.start( - stateCallback: (newState) { - if (!isClosed) { - add(LocalAISettingPanelEvent.updateAIState(newState)); - } - }, - ); - - AIEventGetLocalAIState().send().fold( - (localAIState) { - if (!isClosed) { - add(LocalAISettingPanelEvent.updateAIState(localAIState)); - } - }, - Log.error, - ); - } - - final LocalAIStateListener listener; - - /// Handles incoming events and dispatches them to the appropriate handler. - Future _handleEvent( - LocalAISettingPanelEvent event, - Emitter emit, - ) async { - event.when( - updateAIState: (LocalAIPB pluginState) { - if (pluginState.pluginDownloaded) { - emit( - state.copyWith( - runningState: pluginState.state, - progressIndicator: const LocalAIProgress.checkPluginState(), - ), - ); - } else { - emit( - state.copyWith( - progressIndicator: const LocalAIProgress.downloadLocalAIApp(), - ), - ); - } - }, - ); - } - - @override - Future close() async { - await listener.stop(); - return super.close(); - } -} - -@freezed -class LocalAISettingPanelEvent with _$LocalAISettingPanelEvent { - const factory LocalAISettingPanelEvent.updateAIState( - LocalAIPB aiState, - ) = _UpdateAIState; -} - -@freezed -class LocalAISettingPanelState with _$LocalAISettingPanelState { - const factory LocalAISettingPanelState({ - LocalAIProgress? progressIndicator, - @Default(RunningStatePB.Connecting) RunningStatePB runningState, - }) = _LocalAIChatSettingState; -} - -@freezed -class LocalAIProgress with _$LocalAIProgress { - const factory LocalAIProgress.checkPluginState() = _CheckPluginStateProgress; - const factory LocalAIProgress.downloadLocalAIApp() = - _DownloadLocalAIAppProgress; -} diff --git a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/ollama_setting_bloc.dart b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/ollama_setting_bloc.dart index 8555d8cdc8..f5c4209028 100644 --- a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/ollama_setting_bloc.dart +++ b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/ollama_setting_bloc.dart @@ -80,11 +80,9 @@ class OllamaSettingBloc extends Bloc { } add(OllamaSettingEvent.updateSetting(setting)); AIEventUpdateLocalAISetting(setting).send().fold( - (_) { - Log.info('AI setting updated successfully'); - }, - (err) => Log.error("update ai setting failed: $err"), - ); + (_) => Log.info('AI setting updated successfully'), + (err) => Log.error("update ai setting failed: $err"), + ); }, ); } diff --git a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/plugin_state_bloc.dart b/frontend/appflowy_flutter/lib/workspace/application/settings/ai/plugin_state_bloc.dart deleted file mode 100644 index d91d7151ab..0000000000 --- a/frontend/appflowy_flutter/lib/workspace/application/settings/ai/plugin_state_bloc.dart +++ /dev/null @@ -1,150 +0,0 @@ -import 'dart:async'; - -import 'package:appflowy/workspace/application/settings/ai/local_llm_listener.dart'; -import 'package:appflowy_backend/dispatch/dispatch.dart'; -import 'package:appflowy_backend/log.dart'; -import 'package:appflowy_backend/protobuf/flowy-ai/entities.pb.dart'; -import 'package:bloc/bloc.dart'; -import 'package:freezed_annotation/freezed_annotation.dart'; - -part 'plugin_state_bloc.freezed.dart'; - -class PluginStateBloc extends Bloc { - PluginStateBloc() - : listener = LocalAIStateListener(), - super( - const PluginStateState( - action: PluginStateAction.unknown(), - ), - ) { - listener.start( - stateCallback: (pluginState) { - if (!isClosed) { - add(PluginStateEvent.updateLocalAIState(pluginState)); - } - }, - resourceCallback: (data) { - if (!isClosed) { - add(PluginStateEvent.resourceStateChange(data)); - } - }, - ); - - on(_handleEvent); - } - - final LocalAIStateListener listener; - - @override - Future close() async { - await listener.stop(); - return super.close(); - } - - Future _handleEvent( - PluginStateEvent event, - Emitter emit, - ) async { - await event.when( - started: () async { - final result = await AIEventGetLocalAIState().send(); - result.fold( - (pluginState) { - if (!isClosed) { - add(PluginStateEvent.updateLocalAIState(pluginState)); - } - }, - (err) => Log.error(err.toString()), - ); - }, - updateLocalAIState: (LocalAIPB aiState) { - // if the offline ai is not started, ask user to start it - if (aiState.hasLackOfResource()) { - emit( - PluginStateState( - action: PluginStateAction.lackOfResource(aiState.lackOfResource), - ), - ); - return; - } - - // Chech state of the plugin - switch (aiState.state) { - case RunningStatePB.ReadyToRun: - emit( - const PluginStateState( - action: PluginStateAction.readToRun(), - ), - ); - - case RunningStatePB.Connecting: - emit( - const PluginStateState( - action: PluginStateAction.initializingPlugin(), - ), - ); - case RunningStatePB.Connected: - emit( - const PluginStateState( - action: PluginStateAction.initializingPlugin(), - ), - ); - break; - case RunningStatePB.Running: - emit( - PluginStateState( - action: PluginStateAction.running(aiState.pluginVersion), - ), - ); - break; - case RunningStatePB.Stopped: - emit( - state.copyWith(action: const PluginStateAction.restartPlugin()), - ); - default: - break; - } - }, - restartLocalAI: () async { - emit( - const PluginStateState(action: PluginStateAction.restartPlugin()), - ); - unawaited(AIEventRestartLocalAI().send()); - }, - resourceStateChange: (data) { - emit( - PluginStateState( - action: PluginStateAction.lackOfResource(data.resourceDesc), - ), - ); - }, - ); - } -} - -@freezed -class PluginStateEvent with _$PluginStateEvent { - const factory PluginStateEvent.started() = _Started; - const factory PluginStateEvent.updateLocalAIState(LocalAIPB aiState) = - _UpdateLocalAIState; - const factory PluginStateEvent.restartLocalAI() = _RestartLocalAI; - const factory PluginStateEvent.resourceStateChange(LackOfAIResourcePB data) = - _ResourceStateChange; -} - -@freezed -class PluginStateState with _$PluginStateState { - const factory PluginStateState({ - required PluginStateAction action, - }) = _PluginStateState; -} - -@freezed -class PluginStateAction with _$PluginStateAction { - const factory PluginStateAction.unknown() = _Unknown; - const factory PluginStateAction.readToRun() = _ReadyToRun; - const factory PluginStateAction.initializingPlugin() = _InitializingPlugin; - const factory PluginStateAction.running(String version) = _PluginRunning; - const factory PluginStateAction.restartPlugin() = _RestartPlugin; - const factory PluginStateAction.lackOfResource(String desc) = _LackOfResource; -} diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/account/account_deletion.dart b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/account/account_deletion.dart index 5b11e2d139..31139492ad 100644 --- a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/account/account_deletion.dart +++ b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/account/account_deletion.dart @@ -14,7 +14,6 @@ import 'package:flowy_infra_ui/flowy_infra_ui.dart'; import 'package:flutter/material.dart'; import 'package:universal_platform/universal_platform.dart'; -const _confirmText = 'DELETE MY ACCOUNT'; const _acceptableConfirmTexts = [ 'delete my account', 'deletemyaccount', @@ -135,7 +134,8 @@ class _AccountDeletionDialog extends StatelessWidget { ), const VSpace(12.0), FlowyTextField( - hintText: _confirmText, + hintText: + LocaleKeys.newSettings_myAccount_deleteAccount_confirmHint3.tr(), controller: controller, ), const VSpace(16), @@ -176,7 +176,8 @@ class _AccountDeletionDialog extends StatelessWidget { bool _isConfirmTextValid(String text) { // don't convert the text to lower case or upper case, // just check if the text is in the list - return _acceptableConfirmTexts.contains(text); + return _acceptableConfirmTexts.contains(text) || + text == LocaleKeys.newSettings_myAccount_deleteAccount_confirmHint3.tr(); } Future deleteMyAccount( diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/init_local_ai.dart b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/init_local_ai.dart deleted file mode 100644 index 2de410a5e5..0000000000 --- a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/init_local_ai.dart +++ /dev/null @@ -1,82 +0,0 @@ -import 'package:appflowy/generated/flowy_svgs.g.dart'; -import 'package:appflowy/generated/locale_keys.g.dart'; -import 'package:appflowy/workspace/application/settings/ai/local_ai_setting_panel_bloc.dart'; -import 'package:appflowy_backend/protobuf/flowy-ai/entities.pb.dart'; -import 'package:easy_localization/easy_localization.dart'; -import 'package:flowy_infra_ui/style_widget/text.dart'; -import 'package:flowy_infra_ui/widget/spacing.dart'; -import 'package:flutter/material.dart'; -import 'package:flutter_bloc/flutter_bloc.dart'; - -class InitLocalAIIndicator extends StatelessWidget { - const InitLocalAIIndicator({super.key}); - - @override - Widget build(BuildContext context) { - return DecoratedBox( - decoration: const BoxDecoration( - color: Color(0xFFEDF7ED), - borderRadius: BorderRadius.all( - Radius.circular(4), - ), - ), - child: Padding( - padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 4), - child: BlocBuilder( - builder: (context, state) { - switch (state.runningState) { - case RunningStatePB.Connecting: - case RunningStatePB.Connected: - return Row( - children: [ - const HSpace(8), - Expanded( - child: FlowyText( - LocaleKeys.settings_aiPage_keys_localAIInitializing - .tr(), - fontSize: 11, - color: const Color(0xFF1E4620), - maxLines: 3, - ), - ), - ], - ); - case RunningStatePB.Running: - return SizedBox( - height: 30, - child: Row( - children: [ - const HSpace(8), - const FlowySvg( - FlowySvgs.download_success_s, - color: Color(0xFF2E7D32), - ), - const HSpace(6), - FlowyText( - LocaleKeys.settings_aiPage_keys_localAILoaded.tr(), - fontSize: 11, - color: const Color(0xFF1E4620), - ), - ], - ), - ); - case RunningStatePB.Stopped: - return Row( - children: [ - const HSpace(8), - FlowyText( - LocaleKeys.settings_aiPage_keys_localAIStopped.tr(), - fontSize: 11, - color: const Color(0xFFC62828), - ), - ], - ); - default: - return const SizedBox.shrink(); - } - }, - ), - ), - ); - } -} diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/local_ai_setting.dart b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/local_ai_setting.dart index 57a72b6ca1..751e7a6180 100644 --- a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/local_ai_setting.dart +++ b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/local_ai_setting.dart @@ -1,128 +1,150 @@ -import 'package:flutter/material.dart'; - import 'package:appflowy/generated/locale_keys.g.dart'; import 'package:appflowy/workspace/application/settings/ai/local_ai_bloc.dart'; -import 'package:appflowy/workspace/presentation/settings/pages/setting_ai_view/local_ai_setting_panel.dart'; import 'package:appflowy/workspace/presentation/widgets/dialogs.dart'; import 'package:appflowy/workspace/presentation/widgets/toggle/toggle.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:expandable/expandable.dart'; import 'package:flowy_infra_ui/style_widget/text.dart'; import 'package:flowy_infra_ui/widget/spacing.dart'; +import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; -class LocalAISetting extends StatelessWidget { +import 'ollama_setting.dart'; +import 'plugin_status_indicator.dart'; + +class LocalAISetting extends StatefulWidget { const LocalAISetting({super.key}); + @override + State createState() => _LocalAISettingState(); +} + +class _LocalAISettingState extends State { + final expandableController = ExpandableController(initialExpanded: false); + + @override + void dispose() { + expandableController.dispose(); + super.dispose(); + } + @override Widget build(BuildContext context) { return BlocProvider( - create: (context) => - LocalAIToggleBloc()..add(const LocalAIToggleEvent.started()), - child: Padding( - padding: const EdgeInsets.symmetric(vertical: 6), - child: ExpandableNotifier( - child: BlocListener( - listener: (context, state) { - final controller = - ExpandableController.of(context, required: true)!; - - state.pageIndicator.when( - error: (_) => controller.expanded = true, - isEnabled: (enabled) => controller.expanded = enabled, - loading: () => controller.expanded = true, - ); - }, - child: ExpandablePanel( - theme: const ExpandableThemeData( - headerAlignment: ExpandablePanelHeaderAlignment.center, - tapBodyToCollapse: false, - hasIcon: false, - tapBodyToExpand: false, - tapHeaderToExpand: false, - ), - header: const LocalAISettingHeader(), - collapsed: const SizedBox.shrink(), - expanded: Column( - children: [ - const VSpace(12), - DecoratedBox( - decoration: BoxDecoration( - color: - Theme.of(context).colorScheme.surfaceContainerHighest, - borderRadius: const BorderRadius.all(Radius.circular(4)), - ), - child: const Padding( - padding: - EdgeInsets.symmetric(horizontal: 12, vertical: 6), - child: LocalAISettingPanel(), - ), - ), - ], - ), + create: (context) => LocalAiPluginBloc(), + child: BlocConsumer( + listener: (context, state) { + expandableController.value = state.isEnabled; + }, + builder: (context, state) { + return ExpandablePanel( + controller: expandableController, + theme: ExpandableThemeData( + tapBodyToCollapse: false, + hasIcon: false, + tapBodyToExpand: false, + tapHeaderToExpand: false, ), - ), - ), + header: LocalAiSettingHeader( + isEnabled: state.isEnabled, + isToggleable: state is ReadyLocalAiPluginState, + ), + collapsed: const SizedBox.shrink(), + expanded: Padding( + padding: EdgeInsets.only(top: 12), + child: LocalAISettingPanel(), + ), + ); + }, ), ); } } -class LocalAISettingHeader extends StatelessWidget { - const LocalAISettingHeader({super.key}); +class LocalAiSettingHeader extends StatelessWidget { + const LocalAiSettingHeader({ + super.key, + required this.isEnabled, + required this.isToggleable, + }); + + final bool isEnabled; + final bool isToggleable; @override Widget build(BuildContext context) { - return BlocBuilder( + return Row( + children: [ + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + FlowyText.medium( + LocaleKeys.settings_aiPage_keys_localAIToggleTitle.tr(), + ), + const VSpace(4), + FlowyText( + LocaleKeys.settings_aiPage_keys_localAIToggleSubTitle.tr(), + maxLines: 3, + fontSize: 12, + ), + ], + ), + ), + IgnorePointer( + ignoring: !isToggleable, + child: Opacity( + opacity: isToggleable ? 1 : 0.5, + child: Toggle( + value: isEnabled, + onChanged: (_) => _onToggleChanged(context), + ), + ), + ), + ], + ); + } + + void _onToggleChanged(BuildContext context) { + if (isEnabled) { + showConfirmDialog( + context: context, + title: LocaleKeys.settings_aiPage_keys_disableLocalAITitle.tr(), + description: + LocaleKeys.settings_aiPage_keys_disableLocalAIDescription.tr(), + confirmLabel: LocaleKeys.button_confirm.tr(), + onConfirm: () { + context + .read() + .add(const LocalAiPluginEvent.toggle()); + }, + ); + } else { + context.read().add(const LocalAiPluginEvent.toggle()); + } + } +} + +class LocalAISettingPanel extends StatelessWidget { + const LocalAISettingPanel({super.key}); + + @override + Widget build(BuildContext context) { + return BlocBuilder( builder: (context, state) { - return state.pageIndicator.when( - error: (error) => SizedBox.shrink(), - loading: () => const SizedBox.shrink(), - isEnabled: (isEnabled) { - return Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Row( - children: [ - FlowyText.medium( - LocaleKeys.settings_aiPage_keys_localAIToggleTitle.tr(), - ), - const Spacer(), - Toggle( - value: isEnabled, - onChanged: (_) { - if (isEnabled) { - showConfirmDialog( - context: context, - title: LocaleKeys - .settings_aiPage_keys_disableLocalAITitle - .tr(), - description: LocaleKeys - .settings_aiPage_keys_disableLocalAIDescription - .tr(), - confirmLabel: LocaleKeys.button_confirm.tr(), - onConfirm: () => context - .read() - .add(const LocalAIToggleEvent.toggle()), - ); - } else { - context - .read() - .add(const LocalAIToggleEvent.toggle()); - } - }, - ), - ], - ), - const VSpace(4), - FlowyText( - LocaleKeys.settings_aiPage_keys_localAIToggleSubTitle.tr(), - maxLines: 3, - fontSize: 12, - ), - ], - ); - }, + if (state is! ReadyLocalAiPluginState) { + return const SizedBox.shrink(); + } + + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const LocalAIStatusIndicator(), + if (state.showSettings) ...[ + const VSpace(10), + OllamaSettingPage(), + ], + ], ); }, ); diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/local_ai_setting_panel.dart b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/local_ai_setting_panel.dart deleted file mode 100644 index 5357db5c91..0000000000 --- a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/local_ai_setting_panel.dart +++ /dev/null @@ -1,32 +0,0 @@ -import 'package:appflowy/workspace/application/settings/ai/local_ai_setting_panel_bloc.dart'; -import 'package:appflowy/workspace/presentation/settings/pages/setting_ai_view/ollma_setting.dart'; -import 'package:flowy_infra_ui/widget/spacing.dart'; -import 'package:flutter/material.dart'; -import 'package:flutter_bloc/flutter_bloc.dart'; -import 'plugin_state.dart'; - -class LocalAISettingPanel extends StatelessWidget { - const LocalAISettingPanel({super.key}); - - @override - Widget build(BuildContext context) { - return BlocProvider( - create: (context) => LocalAISettingPanelBloc(), - child: Padding( - padding: const EdgeInsets.symmetric(vertical: 6), - child: BlocBuilder( - builder: (context, state) { - return Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - OllamaSettingPage(), - VSpace(6), - PluginStateIndicator(), - ], - ); - }, - ), - ), - ); - } -} diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/model_selection.dart b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/model_selection.dart index 05db831e04..30ff4addde 100644 --- a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/model_selection.dart +++ b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/model_selection.dart @@ -20,25 +20,28 @@ class AIModelSelection extends StatelessWidget { buildWhen: (previous, current) => previous.availableModels != current.availableModels, builder: (context, state) { - if (state.availableModels == null) { + final models = state.availableModels?.models; + + if (models == null) { return const SizedBox( // Using same height as SettingsDropdown to avoid layout shift height: height, ); } + final localModels = models.where((model) => model.isLocal).toList(); + final cloudModels = models.where((model) => !model.isLocal).toList(); + return Padding( padding: const EdgeInsets.symmetric(vertical: 6), child: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - Flexible( + Expanded( child: FlowyText.medium( LocaleKeys.settings_aiPage_keys_llmModelType.tr(), - fontSize: 14, + overflow: TextOverflow.ellipsis, ), ), - const Spacer(), Flexible( child: SettingsDropdown( key: const Key('_AIModelSelection'), @@ -46,12 +49,13 @@ class AIModelSelection extends StatelessWidget { .read() .add(SettingsAIEvent.selectModel(model)), selectedOption: state.availableModels!.selectedModel, - options: state.availableModels!.models + options: [...localModels, ...cloudModels] .map( (model) => buildDropdownMenuEntry( context, value: model, - label: model.i18n, + label: + model.isLocal ? "${model.i18n} 🔐" : model.i18n, subLabel: model.desc, maximumHeight: height, ), diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/ollama_setting.dart b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/ollama_setting.dart new file mode 100644 index 0000000000..2abcb552d1 --- /dev/null +++ b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/ollama_setting.dart @@ -0,0 +1,114 @@ +import 'package:appflowy/workspace/application/settings/ai/ollama_setting_bloc.dart'; +import 'package:flowy_infra_ui/style_widget/button.dart'; +import 'package:flowy_infra_ui/style_widget/text.dart'; +import 'package:flowy_infra_ui/style_widget/text_field.dart'; +import 'package:flowy_infra_ui/widget/flowy_tooltip.dart'; +import 'package:flowy_infra_ui/widget/spacing.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; + +class OllamaSettingPage extends StatelessWidget { + const OllamaSettingPage({super.key}); + + @override + Widget build(BuildContext context) { + return BlocProvider( + create: (context) => + OllamaSettingBloc()..add(const OllamaSettingEvent.started()), + child: BlocBuilder( + buildWhen: (previous, current) => + previous.inputItems != current.inputItems || + previous.isEdited != current.isEdited, + builder: (context, state) { + return Container( + decoration: BoxDecoration( + color: Theme.of(context).colorScheme.surfaceContainerHighest, + borderRadius: const BorderRadius.all(Radius.circular(8.0)), + ), + padding: EdgeInsets.all(12), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + spacing: 10, + children: [ + for (final item in state.inputItems) + _SettingItemWidget(item: item), + _SaveButton(isEdited: state.isEdited), + ], + ), + ); + }, + ), + ); + } +} + +class _SettingItemWidget extends StatelessWidget { + const _SettingItemWidget({required this.item}); + + final SettingItem item; + + @override + Widget build(BuildContext context) { + return Column( + key: ValueKey(item.content + item.settingType.title), + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + FlowyText( + item.settingType.title, + fontSize: 12, + figmaLineHeight: 16, + ), + const VSpace(4), + SizedBox( + height: 32, + child: FlowyTextField( + hintText: item.hintText, + text: item.content, + onChanged: (content) { + context.read().add( + OllamaSettingEvent.onEdit(content, item.settingType), + ); + }, + ), + ), + ], + ); + } +} + +class _SaveButton extends StatelessWidget { + const _SaveButton({required this.isEdited}); + + final bool isEdited; + + @override + Widget build(BuildContext context) { + return Align( + alignment: AlignmentDirectional.centerEnd, + child: FlowyTooltip( + message: isEdited ? null : 'No changes', + child: SizedBox( + child: FlowyButton( + text: FlowyText( + 'Apply', + figmaLineHeight: 20, + color: Theme.of(context).colorScheme.onPrimary, + ), + disable: !isEdited, + expandText: false, + margin: EdgeInsets.symmetric(horizontal: 16.0, vertical: 6.0), + backgroundColor: Theme.of(context).colorScheme.primary, + hoverColor: Theme.of(context).colorScheme.primary.withAlpha(200), + onTap: () { + if (isEdited) { + context + .read() + .add(const OllamaSettingEvent.submit()); + } + }, + ), + ), + ), + ); + } +} diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/ollma_setting.dart b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/ollma_setting.dart deleted file mode 100644 index 8af4e35914..0000000000 --- a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/ollma_setting.dart +++ /dev/null @@ -1,168 +0,0 @@ -import 'package:appflowy/core/helpers/url_launcher.dart'; -import 'package:appflowy/generated/locale_keys.g.dart'; -import 'package:appflowy/workspace/application/settings/ai/ollama_setting_bloc.dart'; -import 'package:easy_localization/easy_localization.dart'; -import 'package:flowy_infra/size.dart'; -import 'package:flowy_infra_ui/style_widget/button.dart'; -import 'package:flowy_infra_ui/style_widget/text.dart'; -import 'package:flowy_infra_ui/style_widget/text_field.dart'; -import 'package:flowy_infra_ui/widget/flowy_tooltip.dart'; -import 'package:flowy_infra_ui/widget/spacing.dart'; -import 'package:flutter/gestures.dart'; -import 'package:flutter/material.dart'; -import 'package:flutter_bloc/flutter_bloc.dart'; - -class OllamaSettingPage extends StatelessWidget { - const OllamaSettingPage({super.key}); - - @override - Widget build(BuildContext context) { - return BlocProvider( - create: (context) => - OllamaSettingBloc()..add(const OllamaSettingEvent.started()), - child: BlocBuilder( - buildWhen: (previous, current) => - previous.inputItems != current.inputItems || - previous.isEdited != current.isEdited, - builder: (context, state) { - return Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - ListView.separated( - physics: NeverScrollableScrollPhysics(), - shrinkWrap: true, - itemCount: state.inputItems.length, - separatorBuilder: (_, __) => const VSpace(10), - itemBuilder: (context, index) { - final item = state.inputItems[index]; - return _SettingItemWidget(item: item); - }, - ), - const VSpace(6), - Opacity( - opacity: 0.6, - child: _InstallOllamaInstruction(), - ), - _SaveButton(isEdited: state.isEdited), - ], - ); - }, - ), - ); - } -} - -class _SettingItemWidget extends StatelessWidget { - const _SettingItemWidget({required this.item}); - final SettingItem item; - - @override - Widget build(BuildContext context) { - return Column( - key: ValueKey(item.content + item.settingType.title), - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - FlowyText(item.settingType.title), - const VSpace(8), - FlowyTextField( - hintText: item.hintText, - text: item.content, - onChanged: (content) { - context.read().add( - OllamaSettingEvent.onEdit(content, item.settingType), - ); - }, - ), - ], - ); - } -} - -class _SaveButton extends StatelessWidget { - const _SaveButton({required this.isEdited}); - - final bool isEdited; - - @override - Widget build(BuildContext context) { - final tooltipMessage = isEdited ? 'Click to apply changes' : 'No changes'; - return SizedBox( - height: 50, - child: Row( - children: [ - const Spacer(), - SizedBox( - width: 120, - child: FlowyTooltip( - message: tooltipMessage, - child: Opacity( - opacity: isEdited ? 1 : 0.5, - child: FlowyTextButton( - 'Apply', - mainAxisAlignment: MainAxisAlignment.center, - onPressed: isEdited - ? () { - context - .read() - .add(const OllamaSettingEvent.submit()); - } - : null, - ), - ), - ), - ), - ], - ), - ); - } -} - -class _InstallOllamaInstruction extends StatelessWidget { - const _InstallOllamaInstruction(); - - @override - Widget build(BuildContext context) { - return Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - RichText( - maxLines: 3, - textAlign: TextAlign.left, - text: TextSpan( - children: [ - TextSpan( - text: - "${LocaleKeys.settings_aiPage_keys_localAISetupInstruction1.tr()} ", - style: Theme.of(context) - .textTheme - .bodySmall! - .copyWith(height: 1.5), - ), - TextSpan( - text: - " ${LocaleKeys.settings_aiPage_keys_localAISetupInstruction2.tr()} ", - style: Theme.of(context).textTheme.bodyMedium!.copyWith( - fontSize: FontSizes.s14, - color: Theme.of(context).colorScheme.primary, - height: 1.5, - ), - recognizer: TapGestureRecognizer() - ..onTap = () => afLaunchUrlString( - "https://appflowy.com/guide/appflowy-local-ai-ollama", - ), - ), - TextSpan( - text: - " ${LocaleKeys.settings_aiPage_keys_localAISetupInstruction3.tr()} ", - style: Theme.of(context) - .textTheme - .bodySmall! - .copyWith(height: 1.5), - ), - ], - ), - ), - ], - ); - } -} diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/plugin_state.dart b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/plugin_state.dart deleted file mode 100644 index 12ce391a29..0000000000 --- a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/plugin_state.dart +++ /dev/null @@ -1,168 +0,0 @@ -import 'package:appflowy/generated/flowy_svgs.g.dart'; -import 'package:appflowy/generated/locale_keys.g.dart'; -import 'package:appflowy/workspace/application/settings/ai/plugin_state_bloc.dart'; -import 'package:appflowy/workspace/presentation/settings/pages/setting_ai_view/init_local_ai.dart'; -import 'package:easy_localization/easy_localization.dart'; -import 'package:flowy_infra_ui/style_widget/button.dart'; -import 'package:flowy_infra_ui/style_widget/text.dart'; -import 'package:flowy_infra_ui/widget/spacing.dart'; -import 'package:flutter/material.dart'; -import 'package:flutter_bloc/flutter_bloc.dart'; - -class PluginStateIndicator extends StatelessWidget { - const PluginStateIndicator({super.key}); - - @override - Widget build(BuildContext context) { - return BlocProvider( - create: (context) => - PluginStateBloc()..add(const PluginStateEvent.started()), - child: BlocBuilder( - builder: (context, state) { - return state.action.when( - unknown: () => const SizedBox.shrink(), - readToRun: () => const _PrepareRunning(), - initializingPlugin: () => const InitLocalAIIndicator(), - running: (version) => _LocalAIRunning( - key: ValueKey(version), - version: version, - ), - restartPlugin: () => const _RestartPluginButton(), - lackOfResource: (desc) => _LackOfResource(desc: desc), - ); - }, - ), - ); - } -} - -class _PrepareRunning extends StatelessWidget { - const _PrepareRunning(); - - @override - Widget build(BuildContext context) { - return Row( - children: [ - Expanded( - child: FlowyText( - LocaleKeys.settings_aiPage_keys_localAIStart.tr(), - maxLines: 3, - ), - ), - ], - ); - } -} - -class _RestartPluginButton extends StatelessWidget { - const _RestartPluginButton(); - - @override - Widget build(BuildContext context) { - return Row( - children: [ - const FlowySvg( - FlowySvgs.download_warn_s, - color: Color(0xFFC62828), - ), - const HSpace(6), - FlowyText(LocaleKeys.settings_aiPage_keys_failToLoadLocalAI.tr()), - const Spacer(), - SizedBox( - height: 30, - child: FlowyButton( - useIntrinsicWidth: true, - text: - FlowyText(LocaleKeys.settings_aiPage_keys_restartLocalAI.tr()), - onTap: () { - context.read().add( - const PluginStateEvent.restartLocalAI(), - ); - }, - ), - ), - ], - ); - } -} - -class _LocalAIRunning extends StatelessWidget { - const _LocalAIRunning({required this.version, super.key}); - - final String version; - - @override - Widget build(BuildContext context) { - return DecoratedBox( - decoration: const BoxDecoration( - color: Color(0xFFEDF7ED), - borderRadius: BorderRadius.all( - Radius.circular(4), - ), - ), - child: Padding( - padding: const EdgeInsets.symmetric(vertical: 4), - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - Flexible( - child: Row( - children: [ - const HSpace(8), - const FlowySvg( - FlowySvgs.download_success_s, - color: Color(0xFF2E7D32), - ), - const HSpace(6), - if (version.isNotEmpty) - Flexible( - child: FlowyText( - "($version) ", - fontSize: 11, - color: const Color(0xFF1E4620), - ), - ), - Flexible( - child: FlowyText( - LocaleKeys.settings_aiPage_keys_localAIRunning.tr(), - fontSize: 11, - color: const Color(0xFF1E4620), - maxLines: 3, - ), - ), - ], - ), - ), - ], - ), - ), - ); - } -} - -class _LackOfResource extends StatelessWidget { - const _LackOfResource({required this.desc}); - - final String desc; - - @override - Widget build(BuildContext context) { - return Row( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - FlowySvg( - FlowySvgs.toast_warning_filled_s, - size: const Size.square(20.0), - blendMode: null, - ), - const HSpace(6), - Expanded( - child: FlowyText( - desc, - maxLines: 3, - ), - ), - ], - ); - } -} diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/plugin_status_indicator.dart b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/plugin_status_indicator.dart new file mode 100644 index 0000000000..fdf03bb9ca --- /dev/null +++ b/frontend/appflowy_flutter/lib/workspace/presentation/settings/pages/setting_ai_view/plugin_status_indicator.dart @@ -0,0 +1,359 @@ +import 'package:appflowy/core/helpers/url_launcher.dart'; +import 'package:appflowy/generated/flowy_svgs.g.dart'; +import 'package:appflowy/generated/locale_keys.g.dart'; +import 'package:appflowy/util/theme_extension.dart'; +import 'package:appflowy/workspace/application/settings/ai/local_ai_bloc.dart'; +import 'package:appflowy_backend/protobuf/flowy-ai/protobuf.dart'; +import 'package:easy_localization/easy_localization.dart'; +import 'package:flowy_infra_ui/style_widget/text.dart'; +import 'package:flowy_infra_ui/widget/spacing.dart'; +import 'package:flutter/gestures.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; + +class LocalAIStatusIndicator extends StatelessWidget { + const LocalAIStatusIndicator({super.key}); + + @override + Widget build(BuildContext context) { + return BlocBuilder( + builder: (context, state) { + return state.maybeWhen( + ready: (_, version, runningState, lackOfResource) { + if (lackOfResource != null) { + return _LackOfResource(resource: lackOfResource); + } + + return switch (runningState) { + RunningStatePB.ReadyToRun => const _ReadyToRun(), + RunningStatePB.Connecting || + RunningStatePB.Connected => + _Initializing(), + RunningStatePB.Running => _LocalAIRunning(version: version), + RunningStatePB.Stopped => const _RestartPluginButton(), + _ => const SizedBox.shrink(), + }; + }, + orElse: () => const SizedBox.shrink(), + ); + }, + ); + } +} + +class _ReadyToRun extends StatelessWidget { + const _ReadyToRun(); + + @override + Widget build(BuildContext context) { + return Padding( + padding: const EdgeInsets.symmetric(vertical: 8.0), + child: Row( + children: [ + const SizedBox.square( + dimension: 20.0, + child: CircularProgressIndicator( + strokeWidth: 2.0, + strokeAlign: BorderSide.strokeAlignInside, + ), + ), + const HSpace(8.0), + Expanded( + child: FlowyText( + LocaleKeys.settings_aiPage_keys_localAIStart.tr(), + maxLines: 3, + overflow: TextOverflow.ellipsis, + ), + ), + ], + ), + ); + } +} + +class _Initializing extends StatelessWidget { + const _Initializing(); + + @override + Widget build(BuildContext context) { + return Padding( + padding: const EdgeInsets.symmetric(vertical: 8.0), + child: Row( + children: [ + const SizedBox.square( + dimension: 20.0, + child: CircularProgressIndicator( + strokeWidth: 2.0, + strokeAlign: BorderSide.strokeAlignInside, + ), + ), + HSpace(8), + Expanded( + child: FlowyText( + LocaleKeys.settings_aiPage_keys_localAIInitializing.tr(), + maxLines: 3, + overflow: TextOverflow.ellipsis, + ), + ), + ], + ), + ); + } +} + +class _RestartPluginButton extends StatelessWidget { + const _RestartPluginButton(); + + @override + Widget build(BuildContext context) { + final textStyle = + Theme.of(context).textTheme.bodyMedium?.copyWith(height: 1.5); + + return Container( + decoration: BoxDecoration( + color: Theme.of(context).isLightMode + ? const Color(0x80FFE7EE) + : const Color(0x80591734), + borderRadius: BorderRadius.all(Radius.circular(8.0)), + ), + padding: const EdgeInsets.all(8.0), + child: Row( + children: [ + const FlowySvg( + FlowySvgs.toast_error_filled_s, + size: Size.square(20.0), + blendMode: null, + ), + const HSpace(8), + Expanded( + child: RichText( + maxLines: 3, + text: TextSpan( + children: [ + TextSpan( + text: + LocaleKeys.settings_aiPage_keys_failToLoadLocalAI.tr(), + style: textStyle, + ), + TextSpan( + text: ' ', + style: textStyle, + ), + TextSpan( + text: LocaleKeys.settings_aiPage_keys_restartLocalAI.tr(), + style: textStyle?.copyWith( + fontWeight: FontWeight.w600, + decoration: TextDecoration.underline, + ), + recognizer: TapGestureRecognizer() + ..onTap = () { + context + .read() + .add(const LocalAiPluginEvent.restart()); + }, + ), + ], + ), + ), + ), + ], + ), + ); + } +} + +class _LocalAIRunning extends StatelessWidget { + const _LocalAIRunning({ + required this.version, + }); + + final String version; + + @override + Widget build(BuildContext context) { + final runningText = LocaleKeys.settings_aiPage_keys_localAIRunning.tr(); + final text = version.isEmpty ? runningText : "$runningText ($version)"; + + return Container( + decoration: const BoxDecoration( + color: Color(0xFFEDF7ED), + borderRadius: BorderRadius.all(Radius.circular(8.0)), + ), + padding: const EdgeInsets.all(8.0), + child: Row( + children: [ + const FlowySvg( + FlowySvgs.download_success_s, + color: Color(0xFF2E7D32), + ), + const HSpace(6), + Expanded( + child: FlowyText( + text, + color: const Color(0xFF1E4620), + maxLines: 3, + ), + ), + ], + ), + ); + } +} + +class _LackOfResource extends StatelessWidget { + const _LackOfResource({required this.resource}); + + final LackOfAIResourcePB resource; + + @override + Widget build(BuildContext context) { + return Container( + decoration: BoxDecoration( + color: Theme.of(context).isLightMode + ? const Color(0x80FFE7EE) + : const Color(0x80591734), + borderRadius: BorderRadius.all(Radius.circular(8.0)), + ), + padding: const EdgeInsets.all(8.0), + child: Row( + children: [ + FlowySvg( + FlowySvgs.toast_error_filled_s, + size: const Size.square(20.0), + blendMode: null, + ), + const HSpace(8), + Expanded( + child: switch (resource.resourceType) { + LackOfAIResourceTypePB.PluginExecutableNotReady => + _buildNoLAI(context), + LackOfAIResourceTypePB.OllamaServerNotReady => + _buildNoOllama(context), + LackOfAIResourceTypePB.MissingModel => + _buildNoModel(context, resource.missingModelNames), + _ => const SizedBox.shrink(), + }, + ), + ], + ), + ); + } + + TextStyle? _textStyle(BuildContext context) { + return Theme.of(context).textTheme.bodyMedium?.copyWith(height: 1.5); + } + + Widget _buildNoLAI(BuildContext context) { + final textStyle = _textStyle(context); + return RichText( + maxLines: 3, + text: TextSpan( + children: [ + TextSpan( + text: LocaleKeys.settings_aiPage_keys_laiNotReady.tr(), + style: textStyle, + ), + TextSpan(text: ' ', style: _textStyle(context)), + ..._downloadInstructions(textStyle), + ], + ), + ); + } + + Widget _buildNoOllama(BuildContext context) { + final textStyle = _textStyle(context); + return RichText( + maxLines: 3, + text: TextSpan( + children: [ + TextSpan( + text: LocaleKeys.settings_aiPage_keys_ollamaNotReady.tr(), + style: textStyle, + ), + TextSpan(text: ' ', style: textStyle), + ..._downloadInstructions(textStyle), + ], + ), + ); + } + + Widget _buildNoModel(BuildContext context, List modelNames) { + final textStyle = _textStyle(context); + + return RichText( + maxLines: 3, + text: TextSpan( + children: [ + TextSpan( + text: LocaleKeys.settings_aiPage_keys_modelsMissing.tr(), + style: textStyle, + ), + TextSpan( + text: ' ', + style: textStyle, + ), + TextSpan( + text: LocaleKeys.settings_aiPage_keys_pleaseFollowThese.tr(), + style: textStyle, + ), + TextSpan( + text: ' ', + style: textStyle, + ), + TextSpan( + text: LocaleKeys.settings_aiPage_keys_instructions.tr(), + style: textStyle?.copyWith( + fontWeight: FontWeight.w600, + decoration: TextDecoration.underline, + ), + recognizer: TapGestureRecognizer() + ..onTap = () { + afLaunchUrlString( + "https://appflowy.com/guide/appflowy-local-ai-ollama", + ); + }, + ), + TextSpan( + text: ' ', + style: textStyle, + ), + TextSpan( + text: LocaleKeys.settings_aiPage_keys_downloadModel.tr(), + style: textStyle, + ), + ], + ), + ); + } + + List _downloadInstructions(TextStyle? textStyle) { + return [ + TextSpan( + text: LocaleKeys.settings_aiPage_keys_pleaseFollowThese.tr(), + style: textStyle, + ), + TextSpan( + text: ' ', + style: textStyle, + ), + TextSpan( + text: LocaleKeys.settings_aiPage_keys_instructions.tr(), + style: textStyle?.copyWith( + fontWeight: FontWeight.w600, + decoration: TextDecoration.underline, + ), + recognizer: TapGestureRecognizer() + ..onTap = () { + afLaunchUrlString( + "https://appflowy.com/guide/appflowy-local-ai-ollama", + ); + }, + ), + TextSpan(text: ' ', style: textStyle), + TextSpan( + text: LocaleKeys.settings_aiPage_keys_installOllamaLai.tr(), + style: textStyle, + ), + ]; + } +} diff --git a/frontend/resources/translations/ar-SA.json b/frontend/resources/translations/ar-SA.json index e6a8be240c..bf7e9f4d6a 100644 --- a/frontend/resources/translations/ar-SA.json +++ b/frontend/resources/translations/ar-SA.json @@ -875,9 +875,9 @@ "activeOfflineAI": "نشط", "downloadOfflineAI": "التنزيل", "openModelDirectory": "افتح المجلد", - "localAISetupInstruction1": "اتبع هؤلاء", - "localAISetupInstruction2": "التعليمات", - "localAISetupInstruction3": "لإعداد Ollama وAppFlowy Local AI. تخطَّ هذه الخطوة إذا كنت قد قمت بإعدادها بالفعل", + "pleaseFollowThese": "اتبع هؤلاء", + "instructions": "التعليمات", + "installOllamaLai": "لإعداد Ollama وAppFlowy Local AI. تخطَّ هذه الخطوة إذا كنت قد قمت بإعدادها بالفعل", "startLocalAI": "قد يستغرق الأمر بضع ثوانٍ لبدء تشغيل الذكاء الاصطناعي المحلي" } }, @@ -2610,12 +2610,12 @@ "dialogTitle": "حذف الحساب", "dialogContent1": "هل أنت متأكد أنك تريد حذف حسابك نهائياً؟", "dialogContent2": "لا يمكن التراجع عن هذا الإجراء، وسوف يؤدي إلى إزالة الوصول من جميع مساحات العمل، ومسح حسابك بالكامل، بما في ذلك مساحات العمل الخاصة، وإزالتك من جميع مساحات العمل المشتركة.", - "confirmHint1": "من فضلك اكتب \"حذف حسابي\" للتأكيد.", + "confirmHint1": "من فضلك اكتب \"@:newSettings.myAccount.deleteAccount.confirmHint3\" للتأكيد.", "confirmHint2": "أفهم أن هذا الإجراء لا رجعة فيه وسيؤدي إلى حذف حسابي وجميع البيانات المرتبطة به بشكل دائم.", "confirmHint3": "حذف حسابي", "checkToConfirmError": "يجب عليك تحديد المربع لتأكيد الحذف", "failedToGetCurrentUser": "فشل في الحصول على البريد الإلكتروني الحالي للمستخدم", - "confirmTextValidationFailed": "نص التأكيد الخاص بك لا يتطابق مع \"حذف حسابي\"", + "confirmTextValidationFailed": "نص التأكيد الخاص بك لا يتطابق مع \"@:newSettings.myAccount.deleteAccount.confirmHint3\"", "deleteAccountSuccess": "تم حذف الحساب بنجاح" } }, @@ -3217,4 +3217,4 @@ "rewrite": "إعادة كتابة", "insertBelow": "أدخل أدناه" } -} \ No newline at end of file +} diff --git a/frontend/resources/translations/de-DE.json b/frontend/resources/translations/de-DE.json index 996ed03b7d..e95b40d26d 100644 --- a/frontend/resources/translations/de-DE.json +++ b/frontend/resources/translations/de-DE.json @@ -2517,11 +2517,11 @@ "dialogTitle": "Benutzerkonto löschen", "dialogContent1": "Bist du sicher, dass du dein Benutzerkonto unwiderruflich löschen möchtest?", "dialogContent2": "Diese Aktion kann nicht rückgängig gemacht werden und führt dazu, dass der Zugriff auf alle Teambereiche aufgehoben wird, dein gesamtes Benutzerkonto, einschließlich privater Arbeitsbereiche, gelöscht wird und du aus allen freigegebenen Arbeitsbereichen entfernt wirst.", - "confirmHint1": "Geben Sie zur Bestätigung bitte „MEIN KONTO LÖSCHEN“ ein.", + "confirmHint1": "Geben Sie zur Bestätigung bitte „@:newSettings.myAccount.deleteAccount.confirmHint3“ ein.", "confirmHint3": "MEIN KONTO LÖSCHEN", "checkToConfirmError": "Sie müssen das Kontrollkästchen aktivieren, um das Löschen zu bestätigen", "failedToGetCurrentUser": "Aktuelle Benutzer-E-Mail konnte nicht abgerufen werden.", - "confirmTextValidationFailed": "Ihr Bestätigungstext stimmt nicht mit „MEIN KONTO LÖSCHEN“ überein.", + "confirmTextValidationFailed": "Ihr Bestätigungstext stimmt nicht mit „@:newSettings.myAccount.deleteAccount.confirmHint3“ überein.", "deleteAccountSuccess": "Konto erfolgreich gelöscht" } }, diff --git a/frontend/resources/translations/en.json b/frontend/resources/translations/en.json index e93f5273e5..e0c735e2ff 100644 --- a/frontend/resources/translations/en.json +++ b/frontend/resources/translations/en.json @@ -854,16 +854,16 @@ "downloadAIModelButton": "Download", "downloadingModel": "Downloading", "localAILoaded": "Local AI Model successfully added and ready to use", - "localAIStart": "Local AI is starting. If it’s slow, try toggling it off and on", + "localAIStart": "Local AI is starting. If it's slow, try toggling it off and on", "localAILoading": "Local AI Chat Model is loading...", "localAIStopped": "Local AI stopped", "localAIRunning": "Local AI is running", - "localAIInitializing": "Local AI is loading and may take a few seconds, depending on your device", "localAINotReadyRetryLater": "Local AI is initializing, please retry later", "localAIDisabled": "You are using local AI, but it is disabled. Please go to settings to enable it or try different model", + "localAIInitializing": "Local AI is loading. This may take a few minutes depending on your device", "localAINotReadyTextFieldPrompt": "You can not edit while Local AI is loading", - "failToLoadLocalAI": "Failed to start local AI", - "restartLocalAI": "Restart Local AI", + "failToLoadLocalAI": "Failed to start local AI.", + "restartLocalAI": "Restart", "disableLocalAITitle": "Disable local AI", "disableLocalAIDescription": "Do you want to disable local AI?", "localAIToggleTitle": "AppFlowy Local AI (LAI)", @@ -877,10 +877,13 @@ "activeOfflineAI": "Active", "downloadOfflineAI": "Download", "openModelDirectory": "Open folder", - "localAISetupInstruction1": "Follow these", - "localAISetupInstruction2": "instructions", - "localAISetupInstruction3": "to set up Ollama and AppFlowy Local AI. Skip if you've already set it up", - "startLocalAI": "It may take a few seconds to start the local AI" + "laiNotReady": "The Local AI app was not installed correctly.", + "ollamaNotReady": "The Ollama server is not ready.", + "pleaseFollowThese": "Please follow these", + "instructions": "instructions", + "installOllamaLai": "to set up Ollama and AppFlowy Local AI.", + "modelsMissing": "Cannot find the required models.", + "downloadModel": "to download them." } }, "planPage": { @@ -2596,12 +2599,12 @@ "dialogTitle": "Delete account", "dialogContent1": "Are you sure you want to permanently delete your account?", "dialogContent2": "This action cannot be undone, and will remove access from all workspaces, erasing your entire account, including private workspaces, and removing you from all shared workspaces.", - "confirmHint1": "Please type \"DELETE MY ACCOUNT\" to confirm.", + "confirmHint1": "Please type \"@:newSettings.myAccount.deleteAccount.confirmHint3\" to confirm.", "confirmHint2": "I understand that this action is irreversible and will permanently delete my account and all associated data.", "confirmHint3": "DELETE MY ACCOUNT", "checkToConfirmError": "You must check the box to confirm deletion", "failedToGetCurrentUser": "Failed to get current user email", - "confirmTextValidationFailed": "Your confirmation text does not match \"DELETE MY ACCOUNT\"", + "confirmTextValidationFailed": "Your confirmation text does not match \"@:newSettings.myAccount.deleteAccount.confirmHint3\"", "deleteAccountSuccess": "Account deleted successfully" } }, @@ -3204,4 +3207,4 @@ "rewrite": "Rewrite", "insertBelow": "Insert below" } -} \ No newline at end of file +} diff --git a/frontend/resources/translations/fr-FR.json b/frontend/resources/translations/fr-FR.json index 270b92d72f..0d542e916b 100644 --- a/frontend/resources/translations/fr-FR.json +++ b/frontend/resources/translations/fr-FR.json @@ -2519,12 +2519,12 @@ "dialogTitle": "Supprimer le compte", "dialogContent1": "Êtes-vous sûr de vouloir supprimer définitivement votre compte ?", "dialogContent2": "Cette action ne peut pas être annulée et supprimera l'accès à tous les espaces d'équipe, effaçant l'intégralité de votre compte, y compris les espaces de travail privés, et vous supprimant de tous les espaces de travail partagés.", - "confirmHint1": "Veuillez taper « SUPPRIMER MON COMPTE » pour confirmer.", + "confirmHint1": "Veuillez taper « @:newSettings.myAccount.deleteAccount.confirmHint3 » pour confirmer.", "confirmHint2": "Je comprends que cette action est irréversible et supprimera définitivement mon compte et toutes les données associées.", "confirmHint3": "SUPPRIMER MON COMPTE", "checkToConfirmError": "Vous devez cocher la case pour confirmer la suppression", "failedToGetCurrentUser": "Impossible d'obtenir l'e-mail de l'utilisateur actuel", - "confirmTextValidationFailed": "Votre texte de confirmation ne correspond pas à « SUPPRIMER MON COMPTE »", + "confirmTextValidationFailed": "Votre texte de confirmation ne correspond pas à « @:newSettings.myAccount.deleteAccount.confirmHint3 »", "deleteAccountSuccess": "Compte supprimé avec succès" } }, diff --git a/frontend/resources/translations/ko-KR.json b/frontend/resources/translations/ko-KR.json index 635efe92de..66240d954a 100644 --- a/frontend/resources/translations/ko-KR.json +++ b/frontend/resources/translations/ko-KR.json @@ -868,9 +868,9 @@ "activeOfflineAI": "활성화됨", "downloadOfflineAI": "다운로드", "openModelDirectory": "폴더 열기", - "localAISetupInstruction1": "이 지침을 따르세요", - "localAISetupInstruction2": "지침", - "localAISetupInstruction3": "Ollama 및 AppFlowy 로컬 AI를 설정합니다. 이미 설정한 경우 건너뛰세요", + "pleaseFollowThese": "지침", + "instructions": "이 지침을 따르세요", + "installOllamaLai": "Ollama 및 AppFlowy 로컬 AI를 설정합니다. 이미 설정한 경우 건너뛰세요", "startLocalAI": "로컬 AI를 시작하는 데 몇 초가 소요될 수 있습니다" } }, @@ -2579,12 +2579,12 @@ "dialogTitle": "계정 삭제", "dialogContent1": "계정을 영구적으로 삭제하시겠습니까?", "dialogContent2": "이 작업은 되돌릴 수 없으며, 모든 작업 공간에서 액세스를 제거하고, 개인 작업 공간을 포함한 전체 계정을 삭제하며, 모든 공유 작업 공간에서 제거됩니다.", - "confirmHint1": "\"내 계정 삭제\"를 입력하여 확인하세요.", + "confirmHint1": "\"@:newSettings.myAccount.deleteAccount.confirmHint3\"를 입력하여 확인하세요.", "confirmHint2": "이 작업은 되돌릴 수 없으며, 계정과 모든 관련 데이터를 영구적으로 삭제합니다.", "confirmHint3": "내 계정 삭제", "checkToConfirmError": "삭제를 확인하려면 확인란을 선택해야 합니다", "failedToGetCurrentUser": "현재 사용자 이메일을 가져오지 못했습니다", - "confirmTextValidationFailed": "확인 텍스트가 \"내 계정 삭제\"와 일치하지 않습니다", + "confirmTextValidationFailed": "확인 텍스트가 \"@:newSettings.myAccount.deleteAccount.confirmHint3\"와 일치하지 않습니다", "deleteAccountSuccess": "계정이 성공적으로 삭제되었습니다" } }, @@ -3185,4 +3185,4 @@ "rewrite": "다시 작성", "insertBelow": "아래에 삽입" } -} \ No newline at end of file +} diff --git a/frontend/resources/translations/tr-TR.json b/frontend/resources/translations/tr-TR.json index d23566e512..346742f295 100644 --- a/frontend/resources/translations/tr-TR.json +++ b/frontend/resources/translations/tr-TR.json @@ -2518,12 +2518,12 @@ "dialogTitle": "Hesabı sil", "dialogContent1": "Hesabınızı kalıcı olarak silmek istediğinizden emin misiniz?", "dialogContent2": "Bu işlem GERİ ALINAMAZ ve tüm çalışma alanlarından erişiminizi kaldıracak, özel çalışma alanları dahil tüm hesabınızı silecek ve sizi tüm paylaşılan çalışma alanlarından çıkaracaktır.", - "confirmHint1": "Onaylamak için lütfen \"HESABIMI SİL\" yazın.", + "confirmHint1": "Onaylamak için lütfen \"@:newSettings.myAccount.deleteAccount.confirmHint3\" yazın.", "confirmHint2": "Bu işlemin GERİ ALINAMAZ olduğunu ve hesabımı ve ilişkili tüm verileri kalıcı olarak sileceğini anlıyorum.", "confirmHint3": "HESABIMI SİL", "checkToConfirmError": "Silme işlemini onaylamak için kutuyu işaretlemelisiniz", "failedToGetCurrentUser": "Mevcut kullanıcı e-postası alınamadı", - "confirmTextValidationFailed": "Onay metniniz \"HESABIMI SİL\" ile eşleşmiyor", + "confirmTextValidationFailed": "Onay metniniz \"@:newSettings.myAccount.deleteAccount.confirmHint3\" ile eşleşmiyor", "deleteAccountSuccess": "Hesap başarıyla silindi" } }, diff --git a/frontend/resources/translations/vi-VN.json b/frontend/resources/translations/vi-VN.json index a506a84db3..7e27e8a648 100644 --- a/frontend/resources/translations/vi-VN.json +++ b/frontend/resources/translations/vi-VN.json @@ -2270,12 +2270,12 @@ "dialogTitle": "Xóa tài khoản", "dialogContent1": "Bạn có chắc chắn muốn xóa vĩnh viễn tài khoản của mình không?", "dialogContent2": "Không thể hoàn tác hành động này và sẽ xóa quyền truy cập khỏi mọi không gian làm việc nhóm, xóa toàn bộ tài khoản của bạn, bao gồm cả không gian làm việc riêng tư và xóa bạn khỏi mọi không gian làm việc được chia sẻ.", - "confirmHint1": "Vui lòng nhập \"XÓA TÀI KHOẢN CỦA TÔI\" để xác nhận.", + "confirmHint1": "Vui lòng nhập \"@:newSettings.myAccount.deleteAccount.confirmHint3\" để xác nhận.", "confirmHint2": "Tôi hiểu rằng hành động này là không thể đảo ngược và sẽ xóa vĩnh viễn tài khoản của tôi cùng mọi dữ liệu liên quan.", "confirmHint3": "XÓA TÀI KHOẢN CỦA TÔI", "checkToConfirmError": "Bạn phải đánh dấu vào ô để xác nhận việc xóa", "failedToGetCurrentUser": "Không lấy được email người dùng hiện tại", - "confirmTextValidationFailed": "Văn bản xác nhận của bạn không khớp với \"XÓA TÀI KHOẢN CỦA TÔI\"", + "confirmTextValidationFailed": "Văn bản xác nhận của bạn không khớp với \"@:newSettings.myAccount.deleteAccount.confirmHint3\"", "deleteAccountSuccess": "Tài khoản đã được xóa thành công" } }, diff --git a/frontend/resources/translations/zh-CN.json b/frontend/resources/translations/zh-CN.json index ce0c760f3d..557916b5b5 100644 --- a/frontend/resources/translations/zh-CN.json +++ b/frontend/resources/translations/zh-CN.json @@ -1920,7 +1920,14 @@ "deleteMyAccount": "删除我的账户", "dialogTitle": "删除帐户", "dialogContent1": "你确定要永久删除您的帐户吗?", - "dialogContent2": "此操作无法撤消,并且将删除所有团队空间的访问权限,删除你的整个帐户(包括私人工作区),并将你从所有共享工作区中删除。" + "dialogContent2": "此操作无法撤消,并且将删除所有团队空间的访问权限,删除你的整个帐户(包括私人工作区),并将你从所有共享工作区中删除。", + "confirmHint1": "请输入 \"@:newSettings.myAccount.deleteAccount.confirmHint3\" 以确认。", + "confirmHint2": "我理解此操作是不可逆的,并且将永久删除我的帐户和所有关联数据。", + "confirmHint3": "删除我的账户", + "checkToConfirmError": "你必须勾选以确认删除。", + "failedToGetCurrentUser": "获取当前用户邮箱失败", + "confirmTextValidationFailed": "你的确认文本不匹配 \"@:newSettings.myAccount.deleteAccount.confirmHint3\"", + "deleteAccountSuccess": "账户删除成功" } }, "workplace": { diff --git a/frontend/rust-lib/flowy-ai/src/entities.rs b/frontend/rust-lib/flowy-ai/src/entities.rs index 30a3e5dd28..0075e35c04 100644 --- a/frontend/rust-lib/flowy-ai/src/entities.rs +++ b/frontend/rust-lib/flowy-ai/src/entities.rs @@ -586,7 +586,7 @@ pub struct LocalAIPB { pub enabled: bool, #[pb(index = 2, one_of)] - pub lack_of_resource: Option, + pub lack_of_resource: Option, #[pb(index = 3)] pub state: RunningStatePB, @@ -721,5 +721,35 @@ impl From for LocalAISetting { #[derive(Default, ProtoBuf, Clone, Debug)] pub struct LackOfAIResourcePB { #[pb(index = 1)] - pub resource_desc: String, + pub resource_type: LackOfAIResourceTypePB, + + #[pb(index = 2)] + pub missing_model_names: Vec, +} + +#[derive(Debug, Default, Clone, ProtoBuf_Enum)] +pub enum LackOfAIResourceTypePB { + #[default] + PluginExecutableNotReady = 0, + OllamaServerNotReady = 1, + MissingModel = 2, +} + +impl From for LackOfAIResourcePB { + fn from(value: PendingResource) -> Self { + match value { + PendingResource::PluginExecutableNotReady => Self { + resource_type: LackOfAIResourceTypePB::PluginExecutableNotReady, + missing_model_names: vec![], + }, + PendingResource::OllamaServerNotReady => Self { + resource_type: LackOfAIResourceTypePB::OllamaServerNotReady, + missing_model_names: vec![], + }, + PendingResource::MissingModel(model_name) => Self { + resource_type: LackOfAIResourceTypePB::MissingModel, + missing_model_names: vec![model_name], + }, + } + } } diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/controller.rs b/frontend/rust-lib/flowy-ai/src/local_ai/controller.rs index 03b014c89d..b63401b829 100644 --- a/frontend/rust-lib/flowy-ai/src/local_ai/controller.rs +++ b/frontend/rust-lib/flowy-ai/src/local_ai/controller.rs @@ -1,5 +1,5 @@ use crate::ai_manager::AIUserService; -use crate::entities::{LackOfAIResourcePB, LocalAIPB, RunningStatePB}; +use crate::entities::{LocalAIPB, RunningStatePB}; use crate::local_ai::resource::{LLMResourceService, LocalAIResourceController}; use crate::notification::{ chat_notification_builder, ChatNotification, APPFLOWY_AI_NOTIFICATION_KEY, @@ -539,9 +539,7 @@ async fn initialize_ai_plugin( APPFLOWY_AI_NOTIFICATION_KEY, ChatNotification::LocalAIResourceUpdated, ) - .payload(LackOfAIResourcePB { - resource_desc: lack_of_resource, - }) + .payload(lack_of_resource) .send(); return Ok(()); diff --git a/frontend/rust-lib/flowy-ai/src/local_ai/resource.rs b/frontend/rust-lib/flowy-ai/src/local_ai/resource.rs index 3eddcf2039..c40a9a9b88 100644 --- a/frontend/rust-lib/flowy-ai/src/local_ai/resource.rs +++ b/frontend/rust-lib/flowy-ai/src/local_ai/resource.rs @@ -47,22 +47,13 @@ pub enum WatchDiskEvent { Remove, } +#[derive(Debug, Clone)] pub enum PendingResource { PluginExecutableNotReady, OllamaServerNotReady, MissingModel(String), } -impl PendingResource { - pub fn desc(self) -> String { - match self { - PendingResource::PluginExecutableNotReady => "The Local AI app was not installed correctly. Please follow the instructions to install the Local AI application".to_string(), - PendingResource::OllamaServerNotReady => "Ollama is not ready. Please follow the instructions to install Ollama".to_string(), - PendingResource::MissingModel(model) => format!("Cannot find the model: {}. Please use the ollama pull command to install the model", model), - } - } -} - pub struct LocalAIResourceController { user_service: Arc, resource_service: Arc, @@ -128,10 +119,10 @@ impl LocalAIResourceController { return false; } - match self.calculate_pending_resources().await { - Ok(res) => res.is_empty(), - Err(_) => false, - } + self + .calculate_pending_resources() + .await + .is_ok_and(|r| r.is_none()) } pub async fn get_plugin_download_link(&self) -> FlowyResult { @@ -147,36 +138,35 @@ impl LocalAIResourceController { #[instrument(level = "info", skip_all, err)] pub async fn set_llm_setting(&self, setting: LocalAISetting) -> FlowyResult<()> { self.resource_service.store_setting(setting)?; - let mut resources = self.calculate_pending_resources().await?; - if let Some(resource) = resources.pop() { + if let Some(resource) = self.calculate_pending_resources().await? { chat_notification_builder( APPFLOWY_AI_NOTIFICATION_KEY, ChatNotification::LocalAIResourceUpdated, ) - .payload(LackOfAIResourcePB { - resource_desc: resource.desc(), - }) + .payload(LackOfAIResourcePB::from(resource)) .send(); } Ok(()) } - pub async fn get_lack_of_resource(&self) -> Option { - let mut resources = self.calculate_pending_resources().await.ok()?; - resources.pop().map(|r| r.desc()) + pub async fn get_lack_of_resource(&self) -> Option { + self + .calculate_pending_resources() + .await + .ok()? + .map(Into::into) } - pub async fn calculate_pending_resources(&self) -> FlowyResult> { - let mut resources = vec![]; + pub async fn calculate_pending_resources(&self) -> FlowyResult> { let app_path = ollama_plugin_path(); if !is_plugin_ready() { trace!("[LLM Resource] offline app not found: {:?}", app_path); - resources.push(PendingResource::PluginExecutableNotReady); - return Ok(resources); + return Ok(Some(PendingResource::PluginExecutableNotReady)); } let setting = self.get_llm_setting(); let client = Client::builder().timeout(Duration::from_secs(5)).build()?; + match client.get(&setting.ollama_server_url).send().await { Ok(resp) if resp.status().is_success() => { info!( @@ -189,8 +179,7 @@ impl LocalAIResourceController { "[LLM Resource] Ollama server is not responding at {}", setting.ollama_server_url ); - resources.push(PendingResource::OllamaServerNotReady); - return Ok(resources); + return Ok(Some(PendingResource::OllamaServerNotReady)); }, } @@ -201,12 +190,8 @@ impl LocalAIResourceController { match client.get(&tags_url).send().await { Ok(resp) if resp.status().is_success() => { - let tags: TagsResponse = resp.json().await.map_err(|e| { - log::error!( - "[LLM Resource] Failed to parse /api/tags JSON response: {:?}", - e - ); - e + let tags: TagsResponse = resp.json().await.inspect_err(|e| { + log::error!("[LLM Resource] Failed to parse /api/tags JSON response: {e:?}") })?; // Check each required model is present in the response. for required in &required_models { @@ -215,9 +200,7 @@ impl LocalAIResourceController { "[LLM Resource] required model '{}' not found in API response", required ); - resources.push(PendingResource::MissingModel(required.clone())); - // Optionally, you could continue checking all models rather than returning early. - return Ok(resources); + return Ok(Some(PendingResource::MissingModel(required.clone()))); } } }, @@ -226,12 +209,11 @@ impl LocalAIResourceController { "[LLM Resource] Failed to fetch models from {} (GET /api/tags)", setting.ollama_server_url ); - resources.push(PendingResource::OllamaServerNotReady); - return Ok(resources); + return Ok(Some(PendingResource::OllamaServerNotReady)); }, } - Ok(resources) + Ok(None) } #[instrument(level = "info", skip_all)]