Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bf5c6996b4 | |||
| a87379d2eb | |||
| 91c954da14 | |||
| d3a6b51410 | |||
| 32b641c281 | |||
|
ffbb0ad9e2 |
|||
|
d5f29dc344 |
|||
|
05d25869ef |
118 changed files with 1027 additions and 750 deletions
31
lib/controllers/emoji_shortcodes.dart
Normal file
31
lib/controllers/emoji_shortcodes.dart
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import "package:collection/collection.dart";
|
||||
import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
||||
import "package:flutter_riverpod/flutter_riverpod.dart";
|
||||
import "package:material_emoji_picker/material_emoji_picker.dart";
|
||||
|
||||
/// Maps each unicode emoji, without variation selectors, to its shortcode.
|
||||
class EmojiShortcodesController extends Notifier<IMap<String, String>> {
|
||||
static final _variationSelectors = RegExp("[\uFE0E\uFE0F]");
|
||||
|
||||
static String normalize(String emoji) =>
|
||||
emoji.replaceAll(_variationSelectors, "");
|
||||
|
||||
@override
|
||||
IMap<String, String> build() => .fromEntries(
|
||||
ref
|
||||
.watch(EmojiController.provider)
|
||||
.requireValue
|
||||
.map((category) => category.emojis)
|
||||
.flattened
|
||||
.where((emoji) => emoji.aliases.isNotEmpty)
|
||||
.map(
|
||||
(emoji) =>
|
||||
MapEntry(normalize(emoji.value), ":${emoji.aliases.first}:"),
|
||||
),
|
||||
);
|
||||
|
||||
static final provider =
|
||||
NotifierProvider<EmojiShortcodesController, IMap<String, String>>(
|
||||
EmojiShortcodesController.new,
|
||||
);
|
||||
}
|
||||
|
|
@ -1,14 +1,18 @@
|
|||
import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
||||
import "package:flutter_riverpod/flutter_riverpod.dart";
|
||||
import "package:nexus/controllers/client.dart";
|
||||
import "package:nexus/controllers/emoji_shortcodes.dart";
|
||||
import "package:nexus/controllers/rooms.dart";
|
||||
import "package:nexus/models/configs/event.dart";
|
||||
import "package:nexus/models/content/reaction.dart";
|
||||
import "package:nexus/models/reaction_group.dart";
|
||||
|
||||
class ReactionsController(final EventConfig config)
|
||||
extends AsyncNotifier<IMap<String, IList<String>>> {
|
||||
extends AsyncNotifier<IMap<String, ReactionGroup>> {
|
||||
@override
|
||||
Future<IMap<String, IList<String>>> build() async {
|
||||
Future<IMap<String, ReactionGroup>> build() async {
|
||||
final unicodeShortcodes = ref.watch(EmojiShortcodesController.provider);
|
||||
|
||||
final eventInfo = ref.watch(
|
||||
RoomsController.provider.select((value) {
|
||||
final event = value[config.roomId]?.events[config.eventRowId];
|
||||
|
|
@ -30,12 +34,25 @@ class ReactionsController(final EventConfig config)
|
|||
|
||||
return reactionEvents
|
||||
?.where((event) => event.redactedBy == null)
|
||||
.fold<IMap<String, IList<String>>>(.new(), (acc, event) {
|
||||
if (event.content case ReactionContent(:final key?)) {
|
||||
.fold<IMap<String, ReactionGroup>>(.new(), (acc, event) {
|
||||
if (event.content case ReactionContent(
|
||||
:final key?,
|
||||
:final shortcode,
|
||||
)) {
|
||||
return acc.update(
|
||||
key,
|
||||
(list) => list.add(event.sender),
|
||||
ifAbsent: () => .new([event.sender]),
|
||||
(group) => group.copyWith(
|
||||
senders: group.senders.add(event.sender),
|
||||
shortcode: group.shortcode ?? shortcode,
|
||||
),
|
||||
ifAbsent: () => .new(
|
||||
senders: .new([event.sender]),
|
||||
shortcode:
|
||||
shortcode ??
|
||||
unicodeShortcodes[EmojiShortcodesController.normalize(
|
||||
key,
|
||||
)],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -47,7 +64,7 @@ class ReactionsController(final EventConfig config)
|
|||
static final provider =
|
||||
AsyncNotifierProvider.family<
|
||||
ReactionsController,
|
||||
IMap<String, IList<String>>,
|
||||
IMap<String, ReactionGroup>,
|
||||
EventConfig
|
||||
>(ReactionsController.new);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,9 +36,9 @@ import "package:nexus/models/join_rule.dart";
|
|||
import "package:nexus/models/power_level_permission.dart";
|
||||
import "package:nexus/models/setting.dart";
|
||||
import "package:nexus/models/settings_category.dart";
|
||||
import "package:nexus/widgets/avatar_or_hash.dart";
|
||||
import "package:nexus/widgets/divider_text.dart";
|
||||
import "package:nexus/widgets/image_pack_avatar.dart";
|
||||
import "package:nexus/widgets/avatars/avatar_or_hash.dart";
|
||||
import "package:nexus/widgets/common/divider_text.dart";
|
||||
import "package:nexus/widgets/avatars/image_pack_avatar.dart";
|
||||
import "package:nexus/widgets/settings/add_users_power_level_sheet.dart";
|
||||
import "package:nexus/widgets/settings/aliases_sheet.dart";
|
||||
import "package:nexus/widgets/settings/allowed_rooms_sheet.dart";
|
||||
|
|
|
|||
|
|
@ -35,15 +35,15 @@ import "package:nexus/models/settings.dart";
|
|||
import "package:nexus/models/settings_category.dart";
|
||||
import "package:nexus/models/show_and_send.dart";
|
||||
import "package:nexus/main.dart";
|
||||
import "package:nexus/widgets/avatar_or_hash.dart";
|
||||
import "package:nexus/widgets/divider_text.dart";
|
||||
import "package:nexus/widgets/image_pack_avatar.dart";
|
||||
import "package:nexus/widgets/avatars/avatar_or_hash.dart";
|
||||
import "package:nexus/widgets/common/divider_text.dart";
|
||||
import "package:nexus/widgets/avatars/image_pack_avatar.dart";
|
||||
import "package:nexus/widgets/settings/block_user_field.dart";
|
||||
import "package:nexus/widgets/settings/dialog_list_tile.dart";
|
||||
import "package:nexus/widgets/settings/enable_image_packs_sheet.dart";
|
||||
import "package:nexus/widgets/settings/keyword_field.dart";
|
||||
import "package:nexus/widgets/user_bottom_sheet.dart";
|
||||
import "package:nexus/widgets/reset_encryption_sheet.dart";
|
||||
import "package:nexus/widgets/users/user_bottom_sheet.dart";
|
||||
import "package:nexus/widgets/encryption/reset_encryption_sheet.dart";
|
||||
|
||||
class SettingsSectionsController
|
||||
extends AsyncNotifier<IMap<String, IList<SettingsCategory>>> {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import "package:flutter/widgets.dart";
|
||||
import "package:flutter_riverpod/flutter_riverpod.dart";
|
||||
import "package:nexus/widgets/error_dialog.dart";
|
||||
import "package:nexus/widgets/loading.dart";
|
||||
import "package:nexus/widgets/common/error_dialog.dart";
|
||||
import "package:nexus/widgets/common/loading.dart";
|
||||
|
||||
extension BetterWhen<T> on AsyncValue<T> {
|
||||
Widget betterWhen({
|
||||
|
|
|
|||
|
|
@ -15,8 +15,8 @@ import "package:nexus/helpers/extensions/event_id_is_fake.dart";
|
|||
import "package:nexus/models/content/message.dart";
|
||||
import "package:nexus/models/event.dart";
|
||||
import "package:nexus/models/relation_type.dart";
|
||||
import "package:nexus/widgets/emoji_picker.dart";
|
||||
import "package:nexus/widgets/mxc_emoji.dart";
|
||||
import "package:nexus/widgets/emoji/emoji_picker.dart";
|
||||
import "package:nexus/widgets/emoji/mxc_emoji.dart";
|
||||
import "package:nexus/main.dart";
|
||||
|
||||
extension BuildEventOptions on Event {
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
|||
import "package:material_emoji_picker/material_emoji_picker.dart";
|
||||
import "package:nexus/models/content/image_pack.dart";
|
||||
import "package:nexus/models/image_pack.dart";
|
||||
import "package:nexus/widgets/mxc_emoji.dart";
|
||||
import "package:nexus/widgets/emoji/mxc_emoji.dart";
|
||||
|
||||
extension ImagePackEmoji on ImagePack {
|
||||
EmojiCategory? get emojiCategory => _category(emoticons);
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import "package:nexus/helpers/extensions/focus_room.dart";
|
|||
import "package:nexus/helpers/extensions/get_link_info.dart";
|
||||
import "package:nexus/helpers/extensions/show_user_popover.dart";
|
||||
import "package:nexus/models/room_summary.dart";
|
||||
import "package:nexus/widgets/room_summary_sheet.dart";
|
||||
import "package:nexus/widgets/rooms/room_summary_sheet.dart";
|
||||
|
||||
extension OpenLinkInfo on WidgetRef {
|
||||
Future<void> openLinkInfo(
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
|||
import "package:material_ui/material_ui.dart";
|
||||
import "package:navigation_rail_m3e/navigation_rail_m3e.dart";
|
||||
import "package:nexus/models/room.dart";
|
||||
import "package:nexus/widgets/avatar_or_hash.dart";
|
||||
import "package:nexus/widgets/avatars/avatar_or_hash.dart";
|
||||
|
||||
extension RoomDestination on Room {
|
||||
NavigationRailM3EDestination toDestination({
|
||||
|
|
|
|||
|
|
@ -20,6 +20,8 @@ extension SchemeToTheme on ColorScheme {
|
|||
shape: RoundedRectangleBorder(borderRadius: .circular(16)),
|
||||
color: surfaceContainerHigh,
|
||||
),
|
||||
// So ink at the edges of a sheet doesn't spill past its rounded corners
|
||||
bottomSheetTheme: .new(clipBehavior: .antiAlias),
|
||||
appBarTheme: AppBarTheme(
|
||||
titleSpacing: 0,
|
||||
backgroundColor: surfaceContainerLow,
|
||||
|
|
|
|||
83
lib/helpers/extensions/show_image_viewer.dart
Normal file
83
lib/helpers/extensions/show_image_viewer.dart
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
import "package:flutter_riverpod/flutter_riverpod.dart";
|
||||
import "package:m3e_buttons/m3e_buttons.dart";
|
||||
import "package:material_ui/material_ui.dart";
|
||||
import "package:nexus/helpers/extensions/save_attachment.dart";
|
||||
import "package:nexus/helpers/extensions/size_to_string.dart";
|
||||
import "package:nexus/helpers/mxc_image.dart";
|
||||
import "package:nexus/models/requests/download_media.dart";
|
||||
import "package:nexus/widgets/common/error_dialog.dart";
|
||||
|
||||
extension ShowImageViewer on BuildContext {
|
||||
/// Shows the image from [request] fullscreen, zoomable.
|
||||
///
|
||||
/// If [downloadFilename] is set, a download button is shown, suggesting it.
|
||||
void showImageViewer(
|
||||
WidgetRef ref,
|
||||
DownloadMediaRequest request, {
|
||||
String? downloadFilename,
|
||||
int? size,
|
||||
}) => showDialog(
|
||||
context: this,
|
||||
builder: (context) => SafeArea(
|
||||
child: Stack(
|
||||
children: [
|
||||
Positioned.fill(
|
||||
child: GestureDetector(
|
||||
onTap: Navigator.of(context).pop,
|
||||
child: InteractiveViewer(
|
||||
maxScale: 10,
|
||||
child: Image(
|
||||
errorBuilder: (_, error, stackTrace) => ErrorDialog(
|
||||
"Loading failed for ${request.mxc}\nError: $error",
|
||||
stackTrace,
|
||||
),
|
||||
image: MxcImage(ref, request),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
if (size != null)
|
||||
Align(
|
||||
alignment: .topLeft,
|
||||
child: Padding(
|
||||
padding: .all(32),
|
||||
child: Card(
|
||||
margin: .zero,
|
||||
child: Padding(
|
||||
padding: .symmetric(horizontal: 12, vertical: 8),
|
||||
child: Text(size.sizeAsString),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
Align(
|
||||
alignment: .topRight,
|
||||
child: Padding(
|
||||
padding: .all(32),
|
||||
child: Row(
|
||||
mainAxisSize: .min,
|
||||
spacing: 8,
|
||||
children: [
|
||||
if (downloadFilename != null)
|
||||
M3EButton(
|
||||
onPressed: () => context.saveAttachment(
|
||||
ref,
|
||||
request.mxc,
|
||||
encrypted: request.encrypted,
|
||||
filename: downloadFilename,
|
||||
),
|
||||
child: Icon(Icons.download),
|
||||
),
|
||||
M3EButton(
|
||||
onPressed: Navigator.of(context).pop,
|
||||
child: Icon(Icons.close),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
import "package:material_ui/material_ui.dart";
|
||||
import "package:material_adaptive_sheets/material_adaptive_sheets.dart";
|
||||
import "package:nexus/widgets/user_bottom_sheet.dart";
|
||||
import "package:nexus/widgets/users/user_bottom_sheet.dart";
|
||||
|
||||
extension ShowUserPopover on BuildContext {
|
||||
void showUserPopover(String userId, {String? roomId, Rect? anchor}) =>
|
||||
|
|
|
|||
|
|
@ -22,9 +22,9 @@ import "package:nexus/widgets/pages/select_server.dart";
|
|||
import "package:nexus/widgets/pages/settings.dart";
|
||||
import "package:nexus/widgets/pages/verify.dart";
|
||||
import "package:nexus/widgets/account_scope.dart";
|
||||
import "package:nexus/widgets/appbar.dart";
|
||||
import "package:nexus/widgets/error_dialog.dart";
|
||||
import "package:nexus/widgets/loading.dart";
|
||||
import "package:nexus/widgets/common/appbar.dart";
|
||||
import "package:nexus/widgets/common/error_dialog.dart";
|
||||
import "package:nexus/widgets/common/loading.dart";
|
||||
import "package:window_manager/window_manager.dart";
|
||||
import "package:material_ui/material_ui.dart";
|
||||
|
||||
|
|
|
|||
10
lib/models/reaction_group.dart
Normal file
10
lib/models/reaction_group.dart
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
||||
import "package:freezed_annotation/freezed_annotation.dart";
|
||||
|
||||
part "reaction_group.freezed.dart";
|
||||
|
||||
@Freezed(toJson: false, fromJson: false)
|
||||
class const ReactionGroup({
|
||||
required final IList<String> senders,
|
||||
final String? shortcode,
|
||||
}) with _$ReactionGroup;
|
||||
42
lib/widgets/avatars/avatar_or_hash.dart
Normal file
42
lib/widgets/avatars/avatar_or_hash.dart
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import "package:color_hash/color_hash.dart";
|
||||
import "package:material_ui/material_ui.dart";
|
||||
import "package:flutter_riverpod/flutter_riverpod.dart";
|
||||
import "package:nexus/helpers/mxc_image.dart";
|
||||
|
||||
final class const AvatarOrHash(
|
||||
final Uri? avatar,
|
||||
final String title, {
|
||||
final Widget? fallback,
|
||||
final double dimension = 24,
|
||||
super.key,
|
||||
}) extends ConsumerWidget {
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final box = ColoredBox(
|
||||
color: ColorHash(title).color,
|
||||
child: Center(child: Icon(Icons.person, size: dimension / 2)),
|
||||
);
|
||||
|
||||
return SizedBox(
|
||||
width: dimension,
|
||||
height: dimension,
|
||||
child: Center(
|
||||
child: ClipRRect(
|
||||
borderRadius: .all(.circular((dimension - 8) / 2.5)),
|
||||
child: SizedBox.square(
|
||||
dimension: dimension,
|
||||
child: avatar == null
|
||||
? fallback ?? box
|
||||
: Image(
|
||||
image: MxcImage(ref, .new(mxc: avatar!, isAvatar: true)),
|
||||
fit: .cover,
|
||||
loadingBuilder: (_, child, loadingProgress) =>
|
||||
loadingProgress == null ? child : fallback ?? box,
|
||||
errorBuilder: (_, _, _) => fallback ?? box,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
39
lib/widgets/avatars/avatar_ring.dart
Normal file
39
lib/widgets/avatars/avatar_ring.dart
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import "package:material_ui/material_ui.dart";
|
||||
|
||||
class const AvatarRing({
|
||||
required final String tooltip,
|
||||
required final VoidCallback onTap,
|
||||
required final Widget child,
|
||||
final double dimension = 24,
|
||||
super.key,
|
||||
}) extends StatelessWidget {
|
||||
static const ringWidth = 1.5;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Tooltip(
|
||||
message: tooltip,
|
||||
child: Container(
|
||||
padding: .all(ringWidth),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
shape: .circle,
|
||||
),
|
||||
child: ClipOval(
|
||||
child: SizedBox.square(
|
||||
dimension: dimension,
|
||||
child: Stack(
|
||||
fit: .expand,
|
||||
children: [
|
||||
child,
|
||||
// Over the child, so the ripple shows on top of avatars
|
||||
Material(
|
||||
type: .transparency,
|
||||
child: InkWell(onTap: onTap),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
86
lib/widgets/avatars/editable_avatar.dart
Normal file
86
lib/widgets/avatars/editable_avatar.dart
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
import "package:hooks_riverpod/hooks_riverpod.dart";
|
||||
import "package:material_ui/material_ui.dart";
|
||||
import "package:material_adaptive_sheets/material_adaptive_sheets.dart";
|
||||
import "package:nexus/controllers/client.dart";
|
||||
import "package:nexus/helpers/file_dialog_helper.dart";
|
||||
import "package:nexus/helpers/hooks/saving.dart";
|
||||
import "package:nexus/main.dart";
|
||||
import "package:nexus/models/content/message.dart";
|
||||
|
||||
/// Shows [child], an avatar, with a button to upload a new one or remove it.
|
||||
class const EditableAvatar({
|
||||
required final Widget child,
|
||||
|
||||
/// Whether there's an avatar to remove.
|
||||
required final bool hasAvatar,
|
||||
|
||||
/// Called with the uploaded image, or null when the avatar is removed.
|
||||
required final Future<void> Function(Uri? url) onChanged,
|
||||
final double dimension = 200,
|
||||
super.key,
|
||||
}) extends HookConsumerWidget {
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final saving = Saving.use();
|
||||
Future<void> save(Future<void> Function() action) =>
|
||||
saving.run(() => action().onError(showError));
|
||||
|
||||
Future<void> pick() async {
|
||||
final file = await ref
|
||||
.read(FileDialogHelper.provider)
|
||||
.pickFile(images: true);
|
||||
if (file == null || !context.mounted) return;
|
||||
|
||||
await save(() async {
|
||||
final content = await ref
|
||||
.read(ClientController.provider.notifier)
|
||||
.uploadMedia(.new(path: file.path, encrypt: false));
|
||||
if (content case ImageMessageContent(:final url)) {
|
||||
await onChanged(url);
|
||||
} else {
|
||||
throw Exception("The file isn't an image");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return SizedBox.square(
|
||||
dimension: dimension,
|
||||
child: Stack(
|
||||
children: [
|
||||
child,
|
||||
if (saving.active) const Center(child: CircularProgressIndicator()),
|
||||
Positioned(
|
||||
right: dimension * 0.01,
|
||||
bottom: dimension * 0.01,
|
||||
child: Builder(
|
||||
builder: (context) => IconButton.filled(
|
||||
tooltip: "Edit avatar",
|
||||
icon: Icon(Icons.edit),
|
||||
onPressed: saving.active
|
||||
? null
|
||||
: !hasAvatar
|
||||
? pick
|
||||
: () => context.showAdaptiveMenu([
|
||||
PopupMenuItem(
|
||||
onTap: pick,
|
||||
child: ListTile(
|
||||
leading: Icon(Icons.upload),
|
||||
title: Text("Upload new avatar"),
|
||||
),
|
||||
),
|
||||
PopupMenuItem(
|
||||
onTap: () => save(() => onChanged(null)),
|
||||
child: ListTile(
|
||||
leading: Icon(Icons.delete),
|
||||
title: Text("Remove avatar"),
|
||||
),
|
||||
),
|
||||
]),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
14
lib/widgets/avatars/image_pack_avatar.dart
Normal file
14
lib/widgets/avatars/image_pack_avatar.dart
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import "package:material_ui/material_ui.dart";
|
||||
import "package:nexus/widgets/emoji/mxc_emoji.dart";
|
||||
|
||||
class const ImagePackAvatar(final Uri? icon, {super.key})
|
||||
extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) => SizedBox.square(
|
||||
dimension: 40,
|
||||
child: switch (icon) {
|
||||
final icon? => MxcEmoji(icon),
|
||||
null => Icon(Icons.emoji_emotions),
|
||||
},
|
||||
);
|
||||
}
|
||||
133
lib/widgets/common/account_switcher.dart
Normal file
133
lib/widgets/common/account_switcher.dart
Normal file
|
|
@ -0,0 +1,133 @@
|
|||
import "package:collection/collection.dart";
|
||||
import "package:flutter_riverpod/flutter_riverpod.dart";
|
||||
import "package:material_ui/material_ui.dart";
|
||||
import "package:material_adaptive_sheets/material_adaptive_sheets.dart";
|
||||
import "package:nexus/controllers/accounts.dart";
|
||||
import "package:nexus/controllers/active_account.dart";
|
||||
import "package:nexus/helpers/extensions/log_out.dart";
|
||||
import "package:nexus/main.dart";
|
||||
import "package:nexus/models/account.dart";
|
||||
import "package:nexus/widgets/avatars/avatar_or_hash.dart";
|
||||
|
||||
/// Switches between, adds and logs out of accounts. With [onlyWithOthers], it's
|
||||
/// hidden when there's only one account.
|
||||
class const AccountSwitcher({final bool onlyWithOthers = false, super.key})
|
||||
extends ConsumerWidget {
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final danger = Theme.of(context).colorScheme.error;
|
||||
final accounts = ref.watch(AccountsController.provider).value;
|
||||
final activeId = ref.watch(ActiveAccountController.provider);
|
||||
if (accounts == null || (onlyWithOthers && accounts.length < 2)) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
|
||||
String nameOf(Account account) =>
|
||||
account.displayName ?? account.userId ?? "New account";
|
||||
|
||||
Widget avatarOf(Account account, double dimension) => AvatarOrHash(
|
||||
account.avatar,
|
||||
nameOf(account),
|
||||
dimension: dimension,
|
||||
fallback: account.userId == null
|
||||
? Icon(Icons.account_circle, size: dimension)
|
||||
: null,
|
||||
);
|
||||
|
||||
final active = accounts.firstWhereOrNull(
|
||||
(account) => account.id == activeId,
|
||||
);
|
||||
|
||||
void switchTo(String accountId) => ref
|
||||
.read(ActiveAccountController.provider.notifier)
|
||||
.switchTo(accountId)
|
||||
.onError(showError);
|
||||
|
||||
final loggingOut = ref.watch(LogOut.mutation).isPending;
|
||||
|
||||
return AdaptiveMenuButton<void>(
|
||||
tooltip: loggingOut ? "Logging out..." : "Switch account",
|
||||
enabled: !loggingOut,
|
||||
icon: loggingOut
|
||||
? const SizedBox.square(
|
||||
dimension: 24,
|
||||
child: CircularProgressIndicator(),
|
||||
)
|
||||
: active == null
|
||||
? const Icon(Icons.account_circle)
|
||||
: avatarOf(active, 28),
|
||||
itemBuilder: (_) => [
|
||||
for (final account in accounts)
|
||||
PopupMenuItem(
|
||||
onTap: () => switchTo(account.id),
|
||||
child: ListTile(
|
||||
selected: account.id == activeId,
|
||||
leading: avatarOf(account, 32),
|
||||
title: Text(nameOf(account)),
|
||||
subtitle: switch (account) {
|
||||
Account(displayName: _?, :final userId?) => Text(userId),
|
||||
_ => null,
|
||||
},
|
||||
trailing: account.userId == null
|
||||
? null
|
||||
// Inside the menu, so it can close it
|
||||
: Builder(
|
||||
builder: (menuContext) => IconButton(
|
||||
tooltip: "Log out",
|
||||
icon: Icon(Icons.logout, color: danger),
|
||||
onPressed: () async {
|
||||
Navigator.of(menuContext).pop();
|
||||
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (dialogContext) => AlertDialog(
|
||||
title: Text("Log out of ${nameOf(account)}?"),
|
||||
content: Text(
|
||||
account.id == activeId
|
||||
? "You'll be switched to another account, or returned to the login page if this is your only one."
|
||||
: "You'll stop receiving notifications for this account.",
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () =>
|
||||
Navigator.of(dialogContext).pop(false),
|
||||
child: const Text("Cancel"),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () =>
|
||||
Navigator.of(dialogContext).pop(true),
|
||||
style: TextButton.styleFrom(
|
||||
foregroundColor: danger,
|
||||
),
|
||||
child: const Text("Log out"),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
if (confirmed != true) return;
|
||||
|
||||
await ref.logOut(account.id).onError(showError);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
const PopupMenuDivider(),
|
||||
PopupMenuItem(
|
||||
onTap: () {
|
||||
final id = DateTime.now().millisecondsSinceEpoch.toString();
|
||||
ref
|
||||
.read(AccountsController.provider.notifier)
|
||||
.save(.new(id: id))
|
||||
.then((_) => switchTo(id))
|
||||
.onError(showError);
|
||||
},
|
||||
child: const ListTile(
|
||||
leading: Icon(Icons.person_add),
|
||||
title: Text("Add account"),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
96
lib/widgets/common/appbar.dart
Normal file
96
lib/widgets/common/appbar.dart
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
import "dart:io";
|
||||
|
||||
import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
||||
import "package:material_ui/material_ui.dart";
|
||||
import "package:hooks_riverpod/hooks_riverpod.dart";
|
||||
import "package:nexus/controllers/settings.dart";
|
||||
import "package:window_manager/window_manager.dart";
|
||||
|
||||
final class const Appbar({
|
||||
final Widget? leading,
|
||||
final Widget? title,
|
||||
final Color? backgroundColor,
|
||||
final double? scrolledUnderElevation,
|
||||
final IList<Widget> actions = const .empty(),
|
||||
final VoidCallback? onTap,
|
||||
|
||||
/// Whether to show the minimize, maximize and close buttons on desktop.
|
||||
final bool windowControls = true,
|
||||
super.key,
|
||||
}) extends ConsumerWidget implements PreferredSizeWidget {
|
||||
@override
|
||||
Size get preferredSize => const .fromHeight(kToolbarHeight);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final isDesktop = !(Platform.isAndroid || Platform.isIOS);
|
||||
|
||||
void maximize() {
|
||||
final window = WindowManager.instance.getCurrent();
|
||||
if (window == null) return;
|
||||
|
||||
if (window.isMaximized) {
|
||||
return window.unmaximize();
|
||||
}
|
||||
|
||||
return window.maximize();
|
||||
}
|
||||
|
||||
return GestureDetector(
|
||||
onPanStart: ref
|
||||
.watch(SettingsController.provider)
|
||||
.whenOrNull(
|
||||
data: (settings) => !isDesktop || settings.linuxMobileMode
|
||||
? null
|
||||
: (_) => WindowManager.instance.getCurrent()?.startDragging(),
|
||||
),
|
||||
child: AppBar(
|
||||
leading: leading == null ? null : InkWell(onTap: onTap, child: leading),
|
||||
backgroundColor: backgroundColor,
|
||||
scrolledUnderElevation: scrolledUnderElevation,
|
||||
actionsPadding: const .symmetric(horizontal: 8),
|
||||
title: InkWell(
|
||||
onTap: onTap,
|
||||
child: IgnorePointer(child: title),
|
||||
),
|
||||
flexibleSpace: GestureDetector(
|
||||
onDoubleTap: isDesktop ? maximize : null,
|
||||
),
|
||||
actions: [
|
||||
...actions,
|
||||
if (windowControls && isDesktop) ...[
|
||||
if (ref
|
||||
.watch(SettingsController.provider)
|
||||
.whenOrNull(
|
||||
data: (settings) => settings.showMinimizeButton,
|
||||
) ??
|
||||
false)
|
||||
IconButton(
|
||||
tooltip: "Minimize window",
|
||||
onPressed: () =>
|
||||
WindowManager.instance.getCurrent()?.minimize(),
|
||||
icon: const Icon(Icons.minimize),
|
||||
),
|
||||
if (!Platform.isLinux)
|
||||
IconButton(
|
||||
tooltip: "Maximize window",
|
||||
onPressed: maximize,
|
||||
icon: const Icon(Icons.fullscreen),
|
||||
),
|
||||
if (ref
|
||||
.watch(SettingsController.provider)
|
||||
.whenOrNull(
|
||||
data: (settings) => !settings.linuxMobileMode,
|
||||
) ??
|
||||
true)
|
||||
IconButton(
|
||||
tooltip: "Close window",
|
||||
onPressed: () => exit(0),
|
||||
icon: const Icon(Icons.close),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue