-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: custom shortcut manager and registry
- Loading branch information
Showing
4 changed files
with
235 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import 'package:flemozi/components/ui/vertical_tabs.dart'; | ||
import 'package:flemozi/pages/settings/settings.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/services.dart'; | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
import 'package:super_hot_key/super_hot_key.dart'; | ||
|
||
enum ShortcutType { | ||
application, | ||
system, | ||
} | ||
|
||
typedef ProviderReader = T Function<T>(ProviderListenable<T> provider); | ||
|
||
abstract class ShortcutActions { | ||
static void openFlemozi(ProviderReader read, BuildContext context) { | ||
print("Open Flemozi"); | ||
} | ||
|
||
static void switchTabs(ProviderReader read, BuildContext context) { | ||
const length = 3; | ||
final tabs = read(tabsIndex); | ||
read(tabsIndex.notifier).state = (tabs + 1) % length; | ||
} | ||
|
||
static void openSettings(ProviderReader read, BuildContext context) { | ||
Navigator.of(context).push( | ||
MaterialPageRoute( | ||
builder: (context) => const Settings(), | ||
), | ||
); | ||
} | ||
} | ||
|
||
enum FlemoziShortcuts { | ||
openFlemozi( | ||
"Open Flemozi", | ||
ShortcutActions.openFlemozi, | ||
ShortcutType.system, | ||
), | ||
|
||
switchTabs( | ||
"Switch Tabs", | ||
ShortcutActions.switchTabs, | ||
ShortcutType.application, | ||
), | ||
|
||
openSettings( | ||
"Open Settings", | ||
ShortcutActions.openSettings, | ||
ShortcutType.application, | ||
); | ||
|
||
final String title; | ||
final void Function(ProviderReader read, BuildContext context) action; | ||
final ShortcutType type; | ||
const FlemoziShortcuts( | ||
this.title, | ||
this.action, | ||
this.type, | ||
); | ||
} | ||
|
||
class FlemoziShortcutKey { | ||
final KeyboardKey trigger; | ||
final bool alt; | ||
final bool shift; | ||
final bool control; | ||
final bool meta; | ||
const FlemoziShortcutKey( | ||
this.trigger, { | ||
this.alt = false, | ||
this.shift = false, | ||
this.control = false, | ||
this.meta = false, | ||
}); | ||
|
||
HotKeyDefinition toHotKeyDefinition() { | ||
return HotKeyDefinition( | ||
key: trigger as PhysicalKeyboardKey, | ||
alt: alt, | ||
shift: shift, | ||
control: control, | ||
meta: meta, | ||
); | ||
} | ||
|
||
SingleActivator toSingleActivator() { | ||
return SingleActivator( | ||
trigger as LogicalKeyboardKey, | ||
alt: alt, | ||
shift: shift, | ||
control: control, | ||
meta: meta, | ||
); | ||
} | ||
|
||
Set<String> get keyLabels => { | ||
if (shift == true) 'Shift', | ||
if (control == true) 'Ctrl', | ||
if (alt == true) 'Alt', | ||
if (meta == true) 'Meta', | ||
trigger.toString(), | ||
}; | ||
} | ||
|
||
final Map<FlemoziShortcuts, FlemoziShortcutKey> defaultFlemoziShortcuts = { | ||
FlemoziShortcuts.openFlemozi: const FlemoziShortcutKey( | ||
PhysicalKeyboardKey.period, | ||
alt: true, | ||
control: true, | ||
), | ||
FlemoziShortcuts.switchTabs: const FlemoziShortcutKey( | ||
LogicalKeyboardKey.tab, | ||
control: true, | ||
), | ||
FlemoziShortcuts.openSettings: const FlemoziShortcutKey( | ||
LogicalKeyboardKey.comma, | ||
control: true, | ||
), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import 'package:flemozi/collections/shortcuts.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
import 'package:super_hot_key/super_hot_key.dart'; | ||
|
||
class ShortcutNotifier | ||
extends StateNotifier<Map<FlemoziShortcuts, FlemoziShortcutKey>> { | ||
final Ref ref; | ||
ShortcutNotifier(super.state, this.ref); | ||
|
||
void initialize(BuildContext context) async { | ||
final globalShortcuts = FlemoziShortcuts.values | ||
.where((shortcut) => shortcut.type == ShortcutType.system) | ||
.toList(); | ||
|
||
for (var shortcut in globalShortcuts) { | ||
await HotKey.create( | ||
definition: state[shortcut]!.toHotKeyDefinition(), | ||
callback: () { | ||
return shortcut.action(ref.read, context); | ||
}, | ||
); | ||
} | ||
} | ||
|
||
static final provider = StateNotifierProvider<ShortcutNotifier, | ||
Map<FlemoziShortcuts, FlemoziShortcutKey>>( | ||
(ref) => ShortcutNotifier( | ||
defaultFlemoziShortcuts, | ||
ref, | ||
), | ||
); | ||
} |