Skip to content

Commit

Permalink
TW-1787: Update context menu for pinned screen
Browse files Browse the repository at this point in the history
  • Loading branch information
hieutbui committed Jun 7, 2024
1 parent 9068e6e commit d224735
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 138 deletions.
8 changes: 4 additions & 4 deletions lib/pages/chat/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1468,16 +1468,16 @@ class ChatController extends State<Chat>
event,
);
_handleStateContextMenu();
final selectedAction = await showTwakeContextMenu(
final selectedActionIndex = await showTwakeContextMenu(
offset: offset,
context: context,
listActions: listContextMenuActions,
onClose: _handleStateContextMenu,
);

if (selectedAction != null) {
if (selectedActionIndex != null && selectedActionIndex is int) {
_handleClickOnContextMenuItem(
listPopupMenuActions[selectedAction],
listPopupMenuActions[selectedActionIndex],
event,
);
}
Expand Down Expand Up @@ -1815,7 +1815,7 @@ class ChatController extends State<Chat>
listActions: listContextMenuActions,
);

if (selectedActionIndex != null) {
if (selectedActionIndex != null && selectedActionIndex is int) {
final selectedAction = listAppBarActions[selectedActionIndex];
onSelectedAppBarActions(selectedAction);
}
Expand Down
79 changes: 60 additions & 19 deletions lib/pages/chat/chat_pinned_events/pinned_messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import 'package:fluffychat/utils/localized_exception_extension.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/event_extension.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/utils/twake_snackbar.dart';
import 'package:fluffychat/widgets/context_menu/context_menu_action.dart';
import 'package:fluffychat/widgets/matrix.dart';
import 'package:fluffychat/widgets/mixins/popup_context_menu_action_mixin.dart';
import 'package:fluffychat/widgets/mixins/popup_menu_widget_mixin.dart';
Expand Down Expand Up @@ -216,7 +217,26 @@ class PinnedMessagesController extends State<PinnedMessages>
builder: (context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: pinnedMessagesActionsList(context, event),
children: getPinnedMessagesActionsList(event).map((action) {
return popupItemByTwakeAppRouter(
context,
action.getTitle(
context,
unpin: event.isPinned,
isSelected: isSelected(event),
),
iconAction: action.getIconData(unpin: event.isPinned),
imagePath: action.getImagePath(unpin: event.isPinned),
colorIcon:
action == ChatContextMenuActions.pinChat && event.isPinned
? Theme.of(context).colorScheme.onSurface
: null,
onCallbackAction: () => _handleClickOnContextMenuItem(
action,
event,
),
);
}).toList(),
);
},
);
Expand Down Expand Up @@ -284,21 +304,12 @@ class PinnedMessagesController extends State<PinnedMessages>
openingPopupMenu.toggle();
}

// Used for "Right Click" Context Menu
List<Widget> pinnedMessagesActionsList(
BuildContext context,
List<ChatContextMenuActions> actions,
Event event,
) {
final listAction = [
ChatContextMenuActions.pinChat,
ChatContextMenuActions.select,
ChatContextMenuActions.jumpToMessage,
ChatContextMenuActions.copyMessage,
ChatContextMenuActions.forward,
if (PlatformInfos.isWeb && event.hasAttachment)
ChatContextMenuActions.downloadFile,
];
return listAction.map((action) {
return actions.map((action) {
return popupItemByTwakeAppRouter(
context,
action.getTitle(
Expand All @@ -319,13 +330,36 @@ class PinnedMessagesController extends State<PinnedMessages>
}).toList();
}

// Used for "More" Context Menu
List<Widget> _pinnedMessagesActionsTileList(
List<ChatContextMenuActions> getPinnedMessagesActionsList(Event event) {
final listAction = [
ChatContextMenuActions.pinChat,
ChatContextMenuActions.select,
ChatContextMenuActions.jumpToMessage,
ChatContextMenuActions.copyMessage,
ChatContextMenuActions.forward,
if (PlatformInfos.isWeb && event.hasAttachment)
ChatContextMenuActions.downloadFile,
];
return listAction;
}

List<ContextMenuAction> pinnedMessagesContextMenuActionsList(
BuildContext context,
Event event,
) {
final actionTiles = pinnedMessagesActionsList(context, event).map((action) {
return action;
final actionTiles = getPinnedMessagesActionsList(event).map((action) {
return ContextMenuAction(
name: action.getTitle(
context,
unpin: event.isPinned,
isSelected: isSelected(event),
),
icon: action.getIconData(unpin: event.isPinned),
imagePath: action.getImagePath(unpin: event.isPinned),
colorIcon: action == ChatContextMenuActions.pinChat && event.isPinned
? Theme.of(context).colorScheme.onSurface
: null,
);
}).toList();
return actionTiles;
}
Expand Down Expand Up @@ -382,15 +416,22 @@ class PinnedMessagesController extends State<PinnedMessages>
BuildContext context,
Event event,
TapDownDetails tapDownDetails,
) {
) async {
final offset = tapDownDetails.globalPosition;
final listActions = pinnedMessagesContextMenuActionsList(context, event);
_handleStateContextMenu();
showTwakeContextMenu(
final selectedActionIndex = await showTwakeContextMenu(
context: context,
offset: offset,
builder: (context) => _pinnedMessagesActionsTileList(context, event),
listActions: listActions,
onClose: _handleStateContextMenu,
);
if (selectedActionIndex != null && selectedActionIndex is int) {
_handleClickOnContextMenuItem(
getPinnedMessagesActionsList(event)[selectedActionIndex],
event,
);
}
}

void _listenRoomUpdateEvent() {
Expand Down
13 changes: 11 additions & 2 deletions lib/pages/chat/chat_pinned_events/pinned_messages_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,22 @@ class PinnedMessagesScreen extends StatelessWidget {
selectMode: selectedEvents.isNotEmpty,
onSelect: controller.onSelectMessage,
selected: controller.isSelected(event),
menuChildren: (context) => controller
.pinnedMessagesActionsList(context, event),
menuChildren: (context) =>
controller.pinnedMessagesActionsList(
context,
controller.getPinnedMessagesActionsList(event),
event,
),
onLongPress: (event) =>
controller.onLongPressMessage(
context,
event,
),
listAction: controller
.pinnedMessagesContextMenuActionsList(
context,
event,
),
);
},
);
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/chat_list/chat_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ class ChatListController extends State<ChatList>
context: context,
listActions: listContextActions,
);
if (selectedActionIndex != null) {
if (selectedActionIndex != null && selectedActionIndex is int) {
_handleClickOnContextMenuItem(
listPopupActions[selectedActionIndex],
room,
Expand Down
30 changes: 21 additions & 9 deletions lib/widgets/context_menu/context_menu_action.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';

class ContextMenuAction {
String name;
IconData? icon;
String? imagePath;
Color? colorIcon;
double? iconSize;
TextStyle? styleName;
EdgeInsets? padding;
class ContextMenuAction extends Equatable {
final String name;
final IconData? icon;
final String? imagePath;
final Color? colorIcon;
final double? iconSize;
final TextStyle? styleName;
final EdgeInsets? padding;

ContextMenuAction({
const ContextMenuAction({
required this.name,
this.icon,
this.imagePath,
Expand All @@ -18,4 +19,15 @@ class ContextMenuAction {
this.styleName,
this.padding,
});

@override
List<Object?> get props => [
name,
icon,
imagePath,
colorIcon,
iconSize,
styleName,
padding,
];
}
12 changes: 6 additions & 6 deletions lib/widgets/context_menu/twake_context_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ const double _kMinTileHeight = 24;
/// If you just want to use a normal [TwakeContextMenu], please use [TwakeContextMenuArea].
class TwakeContextMenu extends StatefulWidget {
/// The [BuildContext] of the dialog/modal that will display the [TwakeContextMenu]. This is used to close the dialog/modal when the [TwakeContextMenu] is closed.
final BuildContext dialogContext;

/// The list of items to be displayed in the [TwakeContextMenu]. This is used to build the UI of items
final List<ContextMenuAction> listActions;

/// The [Offset] from coordinate origin the [TwakeContextMenu] will be displayed at.
final Offset position;

/// The builder for the items to be displayed. [ListTile] is very useful in most cases.
// final ContextMenuBuilder builder;

/// The padding value at the top an bottom between the edge of the [TwakeContextMenu] and the first / last item
final double? verticalPadding;

Expand Down Expand Up @@ -136,7 +136,7 @@ class TwakeContextMenuState extends State<TwakeContextMenu>
child: action,
closeMenuAction: () {
closeContextMenu(
indexOfAction: widget.listActions
popResult: widget.listActions
.indexOf(action),
);
},
Expand Down Expand Up @@ -164,9 +164,9 @@ class TwakeContextMenuState extends State<TwakeContextMenu>
);
}

void closeContextMenu({int? indexOfAction}) {
void closeContextMenu({dynamic popResult}) {
_animationController.reverse().whenComplete(() {
Navigator.of(widget.dialogContext).pop<int>(indexOfAction);
Navigator.of(widget.dialogContext).pop<dynamic>(popResult);
});
}

Expand Down
2 changes: 2 additions & 0 deletions lib/widgets/context_menu/twake_context_menu_area.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ typedef ContextMenuBuilder = List<Widget> Function(BuildContext context);
/// with the corresponding location [Offset].
class TwakeContextMenuArea extends StatelessWidget with TwakeContextMenuMixin {
/// The list of items to be displayed in the [TwakeContextMenu]. This is used to build the UI of items
final List<ContextMenuAction> listActions;

/// The widget displayed inside the [TwakeContextMenuArea]
final Widget child;

Expand Down
Loading

0 comments on commit d224735

Please sign in to comment.