Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:desktop toolbar custom style #519

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/src/editor/toolbar/desktop/floating_toolbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ class FloatingToolbarStyle {
const FloatingToolbarStyle({
this.backgroundColor = Colors.black,
this.toolbarActiveColor = Colors.lightBlue,
this.toolbarIconColor = Colors.white,
this.toolbarShadowColor,
this.toolbarElevation = 0,
});

final Color backgroundColor;
final Color toolbarActiveColor;
final Color toolbarIconColor;
final Color? toolbarShadowColor;
final double toolbarElevation;
}

/// A floating toolbar that displays at the top of the editor when the selection
Expand Down Expand Up @@ -124,6 +130,7 @@ class _FloatingToolbarState extends State<FloatingToolbar>
}

final String _debounceKey = 'show the toolbar';

void _clear() {
Debounce.cancel(_debounceKey);

Expand Down Expand Up @@ -181,6 +188,9 @@ class _FloatingToolbarState extends State<FloatingToolbar>
editorState: editorState,
backgroundColor: widget.style.backgroundColor,
toolbarActiveColor: widget.style.toolbarActiveColor,
toolbarIconColor: widget.style.toolbarIconColor,
toolbarElevation: widget.style.toolbarElevation,
toolbarShadowColor: widget.style.toolbarShadowColor,
textDirection: widget.textDirection ?? Directionality.of(context),
);
return _toolbarWidget!;
Expand Down
9 changes: 9 additions & 0 deletions lib/src/editor/toolbar/desktop/floating_toolbar_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class FloatingToolbarWidget extends StatefulWidget {
super.key,
this.backgroundColor = Colors.black,
required this.toolbarActiveColor,
this.toolbarIconColor,
this.toolbarElevation = 0,
this.toolbarShadowColor,
required this.items,
required this.editorState,
required this.textDirection,
Expand All @@ -23,6 +26,9 @@ class FloatingToolbarWidget extends StatefulWidget {
final List<ToolbarItem> items;
final Color backgroundColor;
final Color toolbarActiveColor;
final Color? toolbarIconColor;
final double toolbarElevation;
final Color? toolbarShadowColor;
final EditorState editorState;
final TextDirection textDirection;

Expand All @@ -40,6 +46,8 @@ class _FloatingToolbarWidgetState extends State<FloatingToolbarWidget> {
return Material(
borderRadius: BorderRadius.circular(8.0),
color: widget.backgroundColor,
shadowColor: widget.toolbarShadowColor,
elevation: widget.toolbarElevation,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: SizedBox(
Expand All @@ -59,6 +67,7 @@ class _FloatingToolbarWidgetState extends State<FloatingToolbarWidget> {
context,
widget.editorState,
widget.toolbarActiveColor,
widget.toolbarIconColor,
),
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class _AlignmentToolbarItem extends ToolbarItem {
id: 'editor.$id',
group: 6,
isActive: onlyShowInTextType,
builder: (context, editorState, highlightColor) {
builder: (context, editorState, highlightColor, iconColor) {
final selection = editorState.selection!;
final nodes = editorState.getNodesInSelection(selection);
final isHighlight = nodes.every(
Expand All @@ -42,6 +42,7 @@ class _AlignmentToolbarItem extends ToolbarItem {
iconName: 'toolbar/$name',
isHighlight: isHighlight,
highlightColor: highlightColor,
iconColor: iconColor,
tooltip: tooltip,
onPressed: () => editorState.updateNode(
selection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ final ToolbarItem bulletedListItem = ToolbarItem(
id: 'editor.bulleted_list',
group: 3,
isActive: onlyShowInSingleSelectionAndTextType,
builder: (context, editorState, highlightColor) {
builder: (context, editorState, highlightColor, iconColor) {
final selection = editorState.selection!;
final node = editorState.getNodeAtPath(selection.start.path)!;
final isHighlight = node.type == 'bulleted_list';
return SVGIconItemWidget(
iconName: 'toolbar/bulleted_list',
isHighlight: isHighlight,
highlightColor: highlightColor,
iconColor: iconColor,
tooltip: AppFlowyEditorLocalizations.current.bulletedList,
onPressed: () => editorState.formatNode(
selection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ToolbarItem buildHighlightColorItem({List<ColorOption>? colorOptions}) {
id: 'editor.highlightColor',
group: 4,
isActive: onlyShowInTextType,
builder: (context, editorState, highlightColor) {
builder: (context, editorState, highlightColor, iconColor) {
String? highlightColorHex;

final selection = editorState.selection!;
Expand All @@ -20,6 +20,7 @@ ToolbarItem buildHighlightColorItem({List<ColorOption>? colorOptions}) {
iconName: 'toolbar/highlight_color',
isHighlight: isHighlight,
highlightColor: highlightColor,
iconColor: iconColor,
tooltip: AppFlowyEditorLocalizations.current.highlightColor,
onPressed: () {
bool showClearButton = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ToolbarItem buildTextColorItem({
id: 'editor.textColor',
group: 4,
isActive: onlyShowInTextType,
builder: (context, editorState, highlightColor) {
builder: (context, editorState, highlightColor, iconColor) {
String? textColorHex;
final selection = editorState.selection!;
final nodes = editorState.getNodesInSelection(selection);
Expand All @@ -21,6 +21,7 @@ ToolbarItem buildTextColorItem({
iconName: 'toolbar/text_color',
isHighlight: isHighlight,
highlightColor: highlightColor,
iconColor: iconColor,
tooltip: AppFlowyEditorLocalizations.current.textColor,
onPressed: () {
bool showClearButton = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class _FormatToolbarItem extends ToolbarItem {
id: 'editor.$id',
group: 2,
isActive: onlyShowInTextType,
builder: (context, editorState, highlightColor) {
builder: (context, editorState, highlightColor, iconColor) {
final selection = editorState.selection!;
final nodes = editorState.getNodesInSelection(selection);
final isHighlight = nodes.allSatisfyInSelection(selection, (delta) {
Expand All @@ -55,6 +55,7 @@ class _FormatToolbarItem extends ToolbarItem {
iconName: 'toolbar/$name',
isHighlight: isHighlight,
highlightColor: highlightColor,
iconColor: iconColor,
tooltip: tooltip,
onPressed: () => editorState.toggleAttribute(name),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class _HeadingToolbarItem extends ToolbarItem {
id: 'editor.h$level',
group: 1,
isActive: onlyShowInSingleSelectionAndTextType,
builder: (context, editorState, highlightColor) {
builder: (context, editorState, highlightColor, iconColor) {
final selection = editorState.selection!;
final node = editorState.getNodeAtPath(selection.start.path)!;
final isHighlight =
Expand All @@ -22,6 +22,7 @@ class _HeadingToolbarItem extends ToolbarItem {
iconName: 'toolbar/h$level',
isHighlight: isHighlight,
highlightColor: highlightColor,
iconColor: iconColor,
tooltip: levelToTooltips(level),
onPressed: () => editorState.formatNode(
selection,
Expand Down
4 changes: 3 additions & 1 deletion lib/src/editor/toolbar/desktop/items/icon_item_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class SVGIconItemWidget extends StatelessWidget {
this.iconBuilder,
required this.isHighlight,
required this.highlightColor,
this.iconColor,
this.tooltip,
this.onPressed,
});
Expand All @@ -20,6 +21,7 @@ class SVGIconItemWidget extends StatelessWidget {
final WidgetBuilder? iconBuilder;
final bool isHighlight;
final Color highlightColor;
final Color? iconColor;
final String? tooltip;
final VoidCallback? onPressed;

Expand All @@ -29,7 +31,7 @@ class SVGIconItemWidget extends StatelessWidget {
? iconBuilder!(context)
: EditorSvg(
name: iconName,
color: isHighlight ? highlightColor : null,
color: isHighlight ? highlightColor : iconColor,
width: iconSize.width,
height: iconSize.height,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final linkItem = ToolbarItem(
id: 'editor.link',
group: 4,
isActive: onlyShowInSingleSelectionAndTextType,
builder: (context, editorState, highlightColor) {
builder: (context, editorState, highlightColor, iconColor) {
final selection = editorState.selection!;
final nodes = editorState.getNodesInSelection(selection);
final isHref = nodes.allSatisfyInSelection(selection, (delta) {
Expand All @@ -23,6 +23,7 @@ final linkItem = ToolbarItem(
iconName: 'toolbar/link',
isHighlight: isHref,
highlightColor: highlightColor,
iconColor: iconColor,
tooltip: AppFlowyEditorLocalizations.current.link,
onPressed: () {
showLinkMenu(context, editorState, selection, isHref);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ final ToolbarItem numberedListItem = ToolbarItem(
id: 'editor.numbered_list',
group: 3,
isActive: onlyShowInSingleSelectionAndTextType,
builder: (context, editorState, highlightColor) {
builder: (context, editorState, highlightColor, iconColor) {
final selection = editorState.selection!;
final node = editorState.getNodeAtPath(selection.start.path)!;
final isHighlight = node.type == 'numbered_list';
return SVGIconItemWidget(
iconName: 'toolbar/numbered_list',
isHighlight: isHighlight,
highlightColor: highlightColor,
iconColor: iconColor,
tooltip: AppFlowyEditorLocalizations.current.numberedList,
onPressed: () => editorState.formatNode(
selection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ final ToolbarItem paragraphItem = ToolbarItem(
id: 'editor.paragraph',
group: 1,
isActive: onlyShowInSingleSelectionAndTextType,
builder: (context, editorState, highlightColor) {
builder: (context, editorState, highlightColor, iconColor) {
final selection = editorState.selection!;
final node = editorState.getNodeAtPath(selection.start.path)!;
final isHighlight = node.type == 'paragraph';
Expand All @@ -13,6 +13,7 @@ final ToolbarItem paragraphItem = ToolbarItem(
iconName: 'toolbar/text',
isHighlight: isHighlight,
highlightColor: highlightColor,
iconColor: iconColor,
tooltip: AppFlowyEditorLocalizations.current.text,
onPressed: () => editorState.formatNode(
selection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ final ToolbarItem placeholderItem = ToolbarItem(
id: placeholderItemId,
group: -1,
isActive: (editorState) => true,
builder: (_, __, ___) {
builder: (_, __, ___, ____) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 5),
child: Container(
Expand Down
3 changes: 2 additions & 1 deletion lib/src/editor/toolbar/desktop/items/quote_toolbar_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ final ToolbarItem quoteItem = ToolbarItem(
id: 'editor.quote',
group: 3,
isActive: onlyShowInSingleSelectionAndTextType,
builder: (context, editorState, highlightColor) {
builder: (context, editorState, highlightColor, iconColor) {
final selection = editorState.selection!;
final node = editorState.getNodeAtPath(selection.start.path)!;
final isHighlight = node.type == 'quote';
return SVGIconItemWidget(
iconName: 'toolbar/quote',
isHighlight: isHighlight,
highlightColor: highlightColor,
iconColor: iconColor,
tooltip: AppFlowyEditorLocalizations.current.quote,
onPressed: () => editorState.formatNode(
selection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class _TextDirectionToolbarItem extends ToolbarItem {
id: 'editor.$id',
group: 7,
isActive: onlyShowInTextType,
builder: (context, editorState, highlightColor) {
builder: (context, editorState, highlightColor, iconColor) {
final selection = editorState.selection!;
final nodes = editorState.getNodesInSelection(selection);
final isHighlight = nodes.every(
Expand All @@ -41,6 +41,7 @@ class _TextDirectionToolbarItem extends ToolbarItem {
iconName: 'toolbar/$iconName',
isHighlight: isHighlight,
highlightColor: highlightColor,
iconColor: iconColor,
tooltip: tooltip,
onPressed: () => editorState.updateNode(
selection,
Expand Down
1 change: 1 addition & 0 deletions lib/src/render/toolbar/toolbar_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ToolbarItem {
BuildContext context,
EditorState editorState,
Color highlightColor,
Color? iconColor,
)? builder;

// deprecated
Expand Down
14 changes: 10 additions & 4 deletions lib/src/render/toolbar/toolbar_widget.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import 'package:appflowy_editor/src/editor_state.dart';
import 'package:appflowy_editor/src/flutter/overlay.dart';
import 'package:appflowy_editor/src/render/toolbar/toolbar_item.dart';
import 'package:appflowy_editor/src/render/toolbar/toolbar_item_widget.dart';
import 'package:flutter/material.dart' hide Overlay, OverlayEntry;

import 'package:appflowy_editor/src/editor_state.dart';

mixin ToolbarMixin<T extends StatefulWidget> on State<T> {
void hide();
}
Expand All @@ -16,6 +15,7 @@ class ToolbarWidget extends StatefulWidget {
required this.layerLink,
required this.offset,
required this.highlightColor,
this.iconColor,
required this.items,
this.alignment = Alignment.topLeft,
}) : super(key: key);
Expand All @@ -24,6 +24,7 @@ class ToolbarWidget extends StatefulWidget {
final LayerLink layerLink;
final Offset offset;
final Color highlightColor;
final Color? iconColor;

final List<ToolbarItem> items;

Expand All @@ -46,7 +47,7 @@ class _ToolbarWidgetState extends State<ToolbarWidget> with ToolbarMixin {
showWhenUnlinked: true,
offset: widget.offset,
followerAnchor: widget.alignment,
child: _buildToolbar(context, widget.highlightColor),
child: _buildToolbar(context, widget.highlightColor, widget.iconColor),
),
);
}
Expand All @@ -57,7 +58,11 @@ class _ToolbarWidgetState extends State<ToolbarWidget> with ToolbarMixin {
_listToolbarOverlay = null;
}

Widget _buildToolbar(BuildContext context, Color highlightColor) {
Widget _buildToolbar(
BuildContext context,
Color highlightColor,
Color? iconColor,
) {
return Material(
borderRadius: BorderRadius.circular(8.0),
child: Padding(
Expand All @@ -73,6 +78,7 @@ class _ToolbarWidgetState extends State<ToolbarWidget> with ToolbarMixin {
context,
widget.editorState,
highlightColor,
iconColor,
) ??
item.itemBuilder?.call(context, widget.editorState) ??
ToolbarItemWidget(
Expand Down