mirror of
https://github.com/jonasbark/swiftcontrol.git
synced 2026-12-02 18:19:21 +01:00
macOS: open specific shortcut
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:bike_control/bluetooth/messages/notification.dart';
|
||||
import 'package:bike_control/gen/l10n.dart';
|
||||
@@ -20,6 +21,7 @@ import 'package:bike_control/widgets/ui/pro_badge.dart';
|
||||
import 'package:bike_control/widgets/ui/toast.dart';
|
||||
import 'package:bike_control/widgets/ui/warning.dart';
|
||||
import 'package:dartx/dartx.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:shadcn_flutter/shadcn_flutter.dart';
|
||||
import 'package:url_launcher/url_launcher_string.dart';
|
||||
@@ -258,6 +260,7 @@ class _ButtonEditPageState extends State<ButtonEditPage> {
|
||||
_keyPair.touchPosition = Offset.zero;
|
||||
_keyPair.logicalKey = null;
|
||||
_keyPair.androidAction = null;
|
||||
_keyPair.shortcutName = null;
|
||||
|
||||
setState(() {});
|
||||
widget.onUpdate();
|
||||
@@ -274,6 +277,7 @@ class _ButtonEditPageState extends State<ButtonEditPage> {
|
||||
_keyPair.touchPosition = Offset.zero;
|
||||
_keyPair.logicalKey = null;
|
||||
_keyPair.androidAction = null;
|
||||
_keyPair.shortcutName = null;
|
||||
|
||||
setState(() {});
|
||||
widget.onUpdate();
|
||||
@@ -290,6 +294,7 @@ class _ButtonEditPageState extends State<ButtonEditPage> {
|
||||
_keyPair.touchPosition = Offset.zero;
|
||||
_keyPair.logicalKey = null;
|
||||
_keyPair.androidAction = null;
|
||||
_keyPair.shortcutName = null;
|
||||
|
||||
setState(() {});
|
||||
widget.onUpdate();
|
||||
@@ -306,6 +311,7 @@ class _ButtonEditPageState extends State<ButtonEditPage> {
|
||||
_keyPair.touchPosition = Offset.zero;
|
||||
_keyPair.logicalKey = null;
|
||||
_keyPair.androidAction = null;
|
||||
_keyPair.shortcutName = null;
|
||||
|
||||
setState(() {});
|
||||
widget.onUpdate();
|
||||
@@ -322,6 +328,7 @@ class _ButtonEditPageState extends State<ButtonEditPage> {
|
||||
_keyPair.touchPosition = Offset.zero;
|
||||
_keyPair.logicalKey = null;
|
||||
_keyPair.androidAction = null;
|
||||
_keyPair.shortcutName = null;
|
||||
|
||||
setState(() {});
|
||||
widget.onUpdate();
|
||||
@@ -339,6 +346,7 @@ class _ButtonEditPageState extends State<ButtonEditPage> {
|
||||
_keyPair.touchPosition = Offset.zero;
|
||||
_keyPair.logicalKey = null;
|
||||
_keyPair.androidAction = null;
|
||||
_keyPair.shortcutName = null;
|
||||
|
||||
setState(() {});
|
||||
widget.onUpdate();
|
||||
@@ -392,6 +400,7 @@ class _ButtonEditPageState extends State<ButtonEditPage> {
|
||||
_keyPair.touchPosition = Offset.zero;
|
||||
_keyPair.inGameAction = null;
|
||||
_keyPair.inGameActionValue = null;
|
||||
_keyPair.shortcutName = null;
|
||||
setState(() {});
|
||||
widget.onUpdate();
|
||||
},
|
||||
@@ -424,6 +433,7 @@ class _ButtonEditPageState extends State<ButtonEditPage> {
|
||||
_keyPair.touchPosition = Offset.zero;
|
||||
_keyPair.inGameAction = null;
|
||||
_keyPair.inGameActionValue = null;
|
||||
_keyPair.shortcutName = null;
|
||||
setState(() {});
|
||||
widget.onUpdate();
|
||||
},
|
||||
@@ -431,6 +441,21 @@ class _ButtonEditPageState extends State<ButtonEditPage> {
|
||||
),
|
||||
],
|
||||
|
||||
if (!kIsWeb && (Platform.isMacOS || Platform.isIOS)) ...[
|
||||
SizedBox(height: 8),
|
||||
ColoredTitle(text: 'Other Actions'),
|
||||
SelectableCard(
|
||||
isProOnly: true,
|
||||
title: Text('Launch Shortcut'),
|
||||
icon: Icons.rocket_launch_outlined,
|
||||
isActive: _keyPair.shortcutName?.trim().isNotEmpty == true,
|
||||
value: _keyPair.shortcutName,
|
||||
onPressed: () async {
|
||||
await _showShortcutNameDialog(context);
|
||||
},
|
||||
),
|
||||
],
|
||||
|
||||
if (core.connection.accessories.isNotEmpty) ...[
|
||||
SizedBox(height: 8),
|
||||
ColoredTitle(text: 'Accessory Actions'),
|
||||
@@ -459,6 +484,7 @@ class _ButtonEditPageState extends State<ButtonEditPage> {
|
||||
_keyPair.inGameAction = InGameAction.headwindSpeed;
|
||||
_keyPair.inGameActionValue = value;
|
||||
_keyPair.androidAction = null;
|
||||
_keyPair.shortcutName = null;
|
||||
widget.onUpdate();
|
||||
setState(() {});
|
||||
},
|
||||
@@ -473,6 +499,7 @@ class _ButtonEditPageState extends State<ButtonEditPage> {
|
||||
_keyPair.inGameAction = InGameAction.headwindHeartRateMode;
|
||||
_keyPair.inGameActionValue = null;
|
||||
_keyPair.androidAction = null;
|
||||
_keyPair.shortcutName = null;
|
||||
widget.onUpdate();
|
||||
setState(() {});
|
||||
},
|
||||
@@ -508,6 +535,7 @@ class _ButtonEditPageState extends State<ButtonEditPage> {
|
||||
_keyPair.inGameAction = null;
|
||||
_keyPair.inGameActionValue = null;
|
||||
_keyPair.androidAction = null;
|
||||
_keyPair.shortcutName = null;
|
||||
widget.onUpdate();
|
||||
setState(() {});
|
||||
},
|
||||
@@ -549,6 +577,7 @@ class _ButtonEditPageState extends State<ButtonEditPage> {
|
||||
_keyPair.physicalKey = null;
|
||||
_keyPair.logicalKey = null;
|
||||
_keyPair.androidAction = null;
|
||||
_keyPair.shortcutName = null;
|
||||
_keyPair.inGameAction = action;
|
||||
_keyPair.inGameActionValue = ingame;
|
||||
_keyPair.isLongPress = _keyPair.isLongPress ? true : action.isLongPress;
|
||||
@@ -565,6 +594,7 @@ class _ButtonEditPageState extends State<ButtonEditPage> {
|
||||
_keyPair.physicalKey = null;
|
||||
_keyPair.logicalKey = null;
|
||||
_keyPair.androidAction = null;
|
||||
_keyPair.shortcutName = null;
|
||||
_keyPair.inGameAction = action;
|
||||
_keyPair.inGameActionValue = null;
|
||||
_keyPair.isLongPress = _keyPair.isLongPress ? true : action.isLongPress;
|
||||
@@ -578,6 +608,56 @@ class _ButtonEditPageState extends State<ButtonEditPage> {
|
||||
}).toList();
|
||||
}
|
||||
|
||||
Future<void> _showShortcutNameDialog(BuildContext context) async {
|
||||
final controller = TextEditingController(text: _keyPair.shortcutName ?? '');
|
||||
final result = await showDialog<String>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: Text('Launch Shortcut'),
|
||||
content: TextField(
|
||||
controller: controller,
|
||||
hintText: 'Shortcut name',
|
||||
autofocus: true,
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: Text(context.i18n.cancel),
|
||||
),
|
||||
if (_keyPair.shortcutName?.trim().isNotEmpty == true)
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, ''),
|
||||
child: Text('Clear'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, controller.text),
|
||||
child: Text('Save'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (result == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final shortcutName = result.trim();
|
||||
_keyPair.shortcutName = shortcutName.isEmpty ? null : shortcutName;
|
||||
|
||||
if (_keyPair.shortcutName != null) {
|
||||
_keyPair.physicalKey = null;
|
||||
_keyPair.logicalKey = null;
|
||||
_keyPair.modifiers = [];
|
||||
_keyPair.touchPosition = Offset.zero;
|
||||
_keyPair.inGameAction = null;
|
||||
_keyPair.inGameActionValue = null;
|
||||
_keyPair.androidAction = null;
|
||||
}
|
||||
|
||||
widget.onUpdate();
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
Future<bool> _ensureProForFeature(BuildContext context) async {
|
||||
if (IAPManager.instance.hasActiveSubscription) {
|
||||
return true;
|
||||
@@ -659,6 +739,7 @@ class _ButtonEditPageState extends State<ButtonEditPage> {
|
||||
_keyPair.inGameAction = keyPairAction.inGameAction;
|
||||
_keyPair.inGameActionValue = keyPairAction.inGameActionValue;
|
||||
_keyPair.androidAction = null;
|
||||
_keyPair.shortcutName = keyPairAction.shortcutName;
|
||||
setState(() {});
|
||||
},
|
||||
child: Column(
|
||||
@@ -703,6 +784,7 @@ class _ButtonEditPageState extends State<ButtonEditPage> {
|
||||
),
|
||||
);
|
||||
_keyPair.androidAction = null;
|
||||
_keyPair.shortcutName = null;
|
||||
setState(() {});
|
||||
widget.onUpdate();
|
||||
} else if (supportedMode == SupportedMode.touch) {
|
||||
@@ -712,6 +794,7 @@ class _ButtonEditPageState extends State<ButtonEditPage> {
|
||||
_keyPair.physicalKey = null;
|
||||
_keyPair.logicalKey = null;
|
||||
_keyPair.androidAction = null;
|
||||
_keyPair.shortcutName = null;
|
||||
await Navigator.of(context).push<bool?>(
|
||||
MaterialPageRoute(
|
||||
builder: (c) => TouchAreaSetupPage(
|
||||
|
||||
@@ -4,8 +4,10 @@ import 'package:bike_control/utils/core.dart';
|
||||
import 'package:bike_control/utils/iap/iap_manager.dart';
|
||||
import 'package:bike_control/utils/keymap/buttons.dart';
|
||||
import 'package:bike_control/widgets/ui/toast.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:keypress_simulator/keypress_simulator.dart';
|
||||
import 'package:shadcn_flutter/shadcn_flutter.dart';
|
||||
import 'package:url_launcher/url_launcher_string.dart';
|
||||
|
||||
class DesktopActions extends BaseActions {
|
||||
DesktopActions({super.supportedModes = const [SupportedMode.keyboard, SupportedMode.touch, SupportedMode.media]});
|
||||
@@ -20,6 +22,22 @@ class DesktopActions extends BaseActions {
|
||||
}
|
||||
final keyPair = supportedApp!.keymap.getKeyPair(button)!;
|
||||
|
||||
if (defaultTargetPlatform == TargetPlatform.macOS && keyPair.shortcutName?.trim().isNotEmpty == true) {
|
||||
if (!core.settings.getLocalEnabled()) {
|
||||
return Error('Enable local connection method first');
|
||||
}
|
||||
if (!isKeyDown) {
|
||||
return Ignored('Shortcut launch only runs on key down');
|
||||
}
|
||||
final shortcutName = Uri.encodeQueryComponent(keyPair.shortcutName!.trim());
|
||||
final launched = await launchUrlString('shortcuts://run-shortcut?name=$shortcutName');
|
||||
if (!launched) {
|
||||
return Error('Failed to launch shortcut: ${keyPair.shortcutName}');
|
||||
}
|
||||
await IAPManager.instance.incrementCommandCount();
|
||||
return Success('Shortcut launched: ${keyPair.shortcutName}');
|
||||
}
|
||||
|
||||
if (core.settings.getLocalEnabled()) {
|
||||
// Handle media keys
|
||||
if (keyPair.isSpecialKey) {
|
||||
|
||||
@@ -5,17 +5,40 @@ import 'package:bike_control/utils/actions/base_actions.dart';
|
||||
import 'package:bike_control/utils/core.dart';
|
||||
import 'package:bike_control/utils/keymap/buttons.dart';
|
||||
import 'package:bike_control/utils/keymap/keymap.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:url_launcher/url_launcher_string.dart';
|
||||
|
||||
import '../../gen/l10n.dart';
|
||||
import '../../widgets/keymap_explanation.dart';
|
||||
|
||||
class RemoteActions extends BaseActions {
|
||||
RemoteActions({super.supportedModes = const [SupportedMode.touch, SupportedMode.keyboard]});
|
||||
|
||||
@override
|
||||
Future<ActionResult> performAction(ControllerButton button, {required bool isKeyDown, required bool isKeyUp}) async {
|
||||
final keyPair = supportedApp!.keymap.getKeyPair(button);
|
||||
|
||||
if (keyPair == null || keyPair.hasNoAction) {
|
||||
return Error(AppLocalizations.current.noActionAssignedForButton(button.name.splitByUpperCase()));
|
||||
}
|
||||
|
||||
if (defaultTargetPlatform == TargetPlatform.iOS && keyPair.shortcutName?.trim().isNotEmpty == true) {
|
||||
if (!isKeyDown) {
|
||||
return Ignored('Shortcut launch only runs on key down');
|
||||
}
|
||||
final shortcutName = Uri.encodeQueryComponent(keyPair.shortcutName!.trim());
|
||||
final launched = await launchUrlString('shortcuts://run-shortcut?name=$shortcutName');
|
||||
if (!launched) {
|
||||
return Error('Failed to launch shortcut: ${keyPair.shortcutName}');
|
||||
}
|
||||
return Success('Shortcut launched: ${keyPair.shortcutName}');
|
||||
}
|
||||
|
||||
final superResult = await super.performAction(button, isKeyDown: isKeyDown, isKeyUp: isKeyUp);
|
||||
if (superResult is! NotHandled) {
|
||||
return superResult;
|
||||
}
|
||||
final keyPair = supportedApp!.keymap.getKeyPair(button)!;
|
||||
|
||||
if (!core.remotePairing.isConnected.value && !core.remoteKeyboardPairing.isConnected.value) {
|
||||
return Error('Not connected to a ${core.settings.getLastTarget()?.name ?? 'remote'} device');
|
||||
}
|
||||
|
||||
@@ -71,6 +71,7 @@ class Keymap {
|
||||
keyPair.inGameAction = null;
|
||||
keyPair.inGameActionValue = null;
|
||||
keyPair.androidAction = null;
|
||||
keyPair.shortcutName = null;
|
||||
}
|
||||
_updateStream.add(null);
|
||||
}
|
||||
@@ -117,6 +118,7 @@ class Keymap {
|
||||
isLongPress: buttonFromBase?.isLongPress ?? false,
|
||||
inGameActionValue: buttonFromBase?.inGameActionValue,
|
||||
androidAction: buttonFromBase?.androidAction,
|
||||
shortcutName: buttonFromBase?.shortcutName,
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -137,6 +139,7 @@ class KeyPair {
|
||||
InGameAction? inGameAction;
|
||||
int? inGameActionValue;
|
||||
AndroidSystemAction? androidAction;
|
||||
String? shortcutName;
|
||||
|
||||
KeyPair({
|
||||
required this.buttons,
|
||||
@@ -148,6 +151,7 @@ class KeyPair {
|
||||
this.inGameAction,
|
||||
this.inGameActionValue,
|
||||
this.androidAction,
|
||||
this.shortcutName,
|
||||
});
|
||||
|
||||
bool get isSpecialKey =>
|
||||
@@ -200,7 +204,8 @@ class KeyPair {
|
||||
physicalKey == null &&
|
||||
touchPosition == Offset.zero &&
|
||||
inGameAction == null &&
|
||||
androidAction == null;
|
||||
androidAction == null &&
|
||||
(shortcutName == null || shortcutName!.trim().isEmpty);
|
||||
|
||||
bool get hasActiveAction =>
|
||||
screenshotMode ||
|
||||
@@ -235,7 +240,8 @@ class KeyPair {
|
||||
core.zwiftMdnsEmulator.supportedActions.contains(inGameAction)) ||
|
||||
(inGameAction != null &&
|
||||
[InGameAction.headwindHeartRateMode, InGameAction.headwindSpeed].contains(inGameAction) &&
|
||||
(core.connection.accessories.isNotEmpty || kDebugMode));
|
||||
(core.connection.accessories.isNotEmpty || kDebugMode)) ||
|
||||
(shortcutName != null && shortcutName!.trim().isNotEmpty);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
@@ -249,6 +255,8 @@ class KeyPair {
|
||||
].joinToString(separator: ': ')
|
||||
: (androidAction != null && core.logic.showLocalControl && core.actionHandler is AndroidActions)
|
||||
? androidAction!.title
|
||||
: (shortcutName != null && shortcutName!.trim().isNotEmpty)
|
||||
? shortcutName!
|
||||
: (isSpecialKey && core.actionHandler.supportedModes.contains(SupportedMode.media))
|
||||
? switch (physicalKey) {
|
||||
PhysicalKeyboardKey.mediaPlayPause => AppLocalizations.current.playPause,
|
||||
@@ -316,6 +324,7 @@ class KeyPair {
|
||||
'inGameAction': inGameAction?.name,
|
||||
'inGameActionValue': inGameActionValue,
|
||||
'androidAction': androidAction?.name,
|
||||
'shortcutName': shortcutName,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -371,6 +380,8 @@ class KeyPair {
|
||||
.toList()
|
||||
: [];
|
||||
|
||||
final rawShortcutName = decoded['shortcutName']?.toString().trim();
|
||||
|
||||
return KeyPair(
|
||||
buttons: buttons,
|
||||
logicalKey: decoded.containsKey('logicalKey') && int.parse(decoded['logicalKey']) != 0
|
||||
@@ -389,6 +400,7 @@ class KeyPair {
|
||||
androidAction: decoded.containsKey('androidAction')
|
||||
? AndroidSystemAction.values.firstOrNullWhere((element) => element.name == decoded['androidAction'])
|
||||
: null,
|
||||
shortcutName: rawShortcutName != null && rawShortcutName.isNotEmpty ? rawShortcutName : null,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -404,7 +416,8 @@ class KeyPair {
|
||||
isLongPress == other.isLongPress &&
|
||||
inGameAction == other.inGameAction &&
|
||||
inGameActionValue == other.inGameActionValue &&
|
||||
androidAction == other.androidAction;
|
||||
androidAction == other.androidAction &&
|
||||
shortcutName == other.shortcutName;
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(
|
||||
@@ -416,5 +429,6 @@ class KeyPair {
|
||||
inGameAction,
|
||||
inGameActionValue,
|
||||
androidAction,
|
||||
shortcutName,
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user