add dynamic hinting and latching for PMPs

This commit is contained in:
Henry Hiles 2026-11-26 16:06:08 -05:00
commit c786344bf2
Signed by: Henry-Hiles
SSH key fingerprint: SHA256:VKQUdS31Q90KvX7EkKMHMBpUspcmItAh86a+v7PGiIs
9 changed files with 120 additions and 37 deletions

View file

@ -87,26 +87,22 @@ class AccountDataController extends Notifier<AccountData> {
Future<void> setRoomProfile(String roomId, String profileId) async {
const type = AccountData.perMessageProfilesKey;
final current = ref
final rooms = ref.read(RoomsController.provider.notifier);
final previous = ref
.read(RoomsController.provider)[roomId]
?.accountData[type];
final content = {...?previous, "default_profile_id": profileId};
// Shown straight away, rather than once it comes down sync
rooms.setAccountData(roomId, type, content);
try {
await ref
.read(ClientController.provider.notifier)
.setAccountData(
.new(
type: type,
roomId: roomId,
content: {...?current, "default_profile_id": profileId},
),
);
await ref
.read(RoomsController.provider.notifier)
.awaitRooms(
(rooms) =>
rooms[roomId]?.accountData[type]?["default_profile_id"] ==
profileId,
);
.setAccountData(.new(type: type, roomId: roomId, content: content));
} catch (_) {
rooms.setAccountData(roomId, type, previous);
rethrow;
}
}
/// Saves [rule] of [kind], creating it if it's new, or otherwise changing only

View file

@ -136,6 +136,24 @@ class RoomsController extends Notifier<IMap<String, Room>> {
.add(event.eventId, event.rowId);
});
void setAccountData(
String roomId,
String type,
Map<String, dynamic>? content,
) {
if (state[roomId] case final room?) {
state = state.add(
roomId,
room.copyWith(
accountData: switch (content) {
final content? => room.accountData.add(type, content),
null => room.accountData.remove(type),
},
),
);
}
}
void update(
IMap<String, Room> rooms, [
ISet<String> leftRooms = const .empty(),

View file

@ -467,6 +467,21 @@ class SettingsSectionsController
),
),
if (settings.subprofiles) ...[
.new(
title: "Latch Triggered Subprofiles",
description: "After you send a message with a subprofile's trigger, keep sending as it in that room until you choose another.",
icon: Icons.push_pin,
builder: (title, description, icon) => SwitchListTile(
title: Text(title),
subtitle: Text(description),
secondary: Icon(icon),
value: settings.latchSubprofiles,
onChanged: (value) => ref
.read(SettingsController.provider.notifier)
.set(settings.copyWith(latchSubprofiles: value))
.onError(showError),
),
),
if (subprofiles.profiles.isNotEmpty)
.new(
title: "Default Subprofile",

View file

@ -6,11 +6,7 @@ import "package:nexus/controllers/account_data.dart";
import "package:nexus/controllers/rooms.dart";
import "package:nexus/models/account_data.dart";
import "package:nexus/models/per_message_profiles.dart";
typedef RoomSubprofiles = ({
IList<StoredProfile> profiles,
StoredProfile? current,
});
import "package:nexus/models/room_subprofiles.dart";
class SubprofilesController(final String roomId)
extends Notifier<RoomSubprofiles> {
@ -44,8 +40,10 @@ class SubprofilesController(final String roomId)
);
final currentId = room?.defaultProfileId ?? global.defaultProfileId;
return (
return .new(
profiles: profiles,
triggerOrder: (room?.profiles ?? const IList<StoredProfile>.empty())
.addAll(global.profiles),
current: currentId == null || currentId.isEmpty
? null
: profiles.firstWhereOrNull((profile) => profile.id == currentId),

View file

@ -41,6 +41,11 @@ class const ProfileTrigger({
final String suffix = "",
final bool keepTrigger = false,
}) with _$ProfileTrigger {
bool matches(String text) =>
text.length >= prefix.length + suffix.length &&
text.startsWith(prefix) &&
text.endsWith(suffix);
Map<String, Object?> toJson() => _$ProfileTriggerToJson(this);
factory ProfileTrigger.fromJson(Map<String, Object?> json) =>

View file

@ -0,0 +1,20 @@
import "package:collection/collection.dart";
import "package:fast_immutable_collections/fast_immutable_collections.dart";
import "package:freezed_annotation/freezed_annotation.dart";
import "package:nexus/models/per_message_profiles.dart";
part "room_subprofiles.freezed.dart";
@freezed
class const RoomSubprofiles({
required final IList<StoredProfile> profiles,
// Each trigger is tried in this order, as gomuks does when sending
required final IList<StoredProfile> triggerOrder,
final StoredProfile? current,
}) with _$RoomSubprofiles {
StoredProfile? triggeredBy(String text) => triggerOrder.firstWhereOrNull(
(profile) =>
profile.id.isNotEmpty &&
profile.triggers.any((trigger) => trigger.matches(text)),
);
}

View file

@ -24,6 +24,7 @@ class const Settings({
final NameColor nameColor = .powerLevel,
final ShowAndSend typingIndicators = .showAndSend,
final bool subprofiles = false,
final bool latchSubprofiles = false,
/// When not sending, read receipts are still sent privately, so that the
/// user's other devices know what has been read.

View file

@ -27,6 +27,9 @@ import "package:hooks_riverpod/hooks_riverpod.dart";
import "package:nexus/controllers/attachment.dart";
import "package:nexus/controllers/client.dart";
import "package:nexus/controllers/settings.dart";
import "package:nexus/controllers/account_data.dart";
import "package:nexus/controllers/subprofiles.dart";
import "package:nexus/models/settings.dart";
import "package:nexus/controllers/image_picker.dart";
import "package:nexus/helpers/file_dialog_helper.dart";
import "package:image_picker/image_picker.dart";
@ -433,6 +436,20 @@ class const Composer(
attachment != null && attachment.$2 == null) {
return;
}
if (ref.read(SettingsController.provider).value case Settings(
subprofiles: true,
latchSubprofiles: true,
)) {
final subprofiles = ref.read(SubprofilesController.provider(roomId));
if (subprofiles.triggeredBy(controller.text) case final triggered?
when triggered.id != subprofiles.current?.id) {
ref
.read(AccountDataController.provider.notifier)
.setRoomProfile(roomId, triggered.id)
.onError(showError);
}
}
onSend(
"${TextCommand.prefixOf(textCommands)}${controller.formattedText}",
shouldMention: shouldMention.value,
@ -566,7 +583,7 @@ class const Composer(
),
)
? [
SubprofileButton(roomId),
SubprofileButton(roomId, controller: controller),
IconButton(
onPressed: hasReplacement
? null

View file

@ -1,15 +1,21 @@
import "package:flutter_riverpod/flutter_riverpod.dart";
import "package:flutter_hooks/flutter_hooks.dart";
import "package:hooks_riverpod/hooks_riverpod.dart";
import "package:material_adaptive_sheets/material_adaptive_sheets.dart";
import "package:material_ui/material_ui.dart";
import "package:nexus/controllers/account_data.dart";
import "package:nexus/controllers/subprofiles.dart";
import "package:nexus/controllers/settings.dart";
import "package:nexus/main.dart";
import "package:nexus/models/room_subprofiles.dart";
import "package:nexus/widgets/avatars/avatar_or_hash.dart";
/// Chooses the subprofile messages are sent as in [roomId], if subprofiles are on.
class const SubprofileButton(final String roomId, {super.key})
extends ConsumerWidget {
/// Chooses the subprofile messages are sent as in [roomId], if subprofiles are
/// on. It shows the one the text in [controller] would trigger, if any.
class const SubprofileButton(
final String roomId, {
required final TextEditingController controller,
super.key,
}) extends HookConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final enabled = ref.watch(
@ -17,10 +23,14 @@ class const SubprofileButton(final String roomId, {super.key})
(settings) => settings.value?.subprofiles ?? false,
),
);
final (:profiles, :current) = ref.watch(
SubprofilesController.provider(roomId),
final subprofiles = ref.watch(SubprofilesController.provider(roomId));
final RoomSubprofiles(:profiles, :current) = subprofiles;
final triggered = useListenableSelector(
controller,
() => subprofiles.triggeredBy(controller.text),
);
if (!enabled || profiles.isEmpty) return const SizedBox.shrink();
final shown = triggered ?? current;
Future<void> choose(String profileId) => ref
.read(AccountDataController.provider.notifier)
@ -45,14 +55,17 @@ class const SubprofileButton(final String roomId, {super.key})
}
return AdaptiveMenuButton<void>(
tooltip: switch (current) {
final current? => "Sending as ${current.displayName ?? current.id}",
null => "Choose subprofile",
tooltip: switch ((triggered, current)) {
(final triggered?, _) =>
"Sending this message as ${triggered.displayName ?? triggered.id}",
(null, final current?) =>
"Sending as ${current.displayName ?? current.id}",
(null, null) => "Choose subprofile",
},
icon: switch (current) {
final current? => AvatarOrHash(
current.avatarUrl,
current.displayName ?? current.id,
icon: switch (shown) {
final shown? => AvatarOrHash(
shown.avatarUrl,
shown.displayName ?? shown.id,
),
null => Icon(Icons.person),
},