Skip to content

Commit

Permalink
Reimplement shortcuts for search
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Dec 15, 2024
1 parent 6563df8 commit 0780e9b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
16 changes: 14 additions & 2 deletions app/lib/views/app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ import 'navigator/view.dart';
class PadAppBar extends StatelessWidget implements PreferredSizeWidget {
final GlobalKey viewportKey;
final ToolbarSize size;
final SearchController searchController;

PadAppBar({super.key, required this.viewportKey, required this.size});
PadAppBar({
super.key,
required this.viewportKey,
required this.size,
required this.searchController,
});

late final windowTitleBar = _buildWindowTitleBar();

Expand Down Expand Up @@ -67,6 +73,7 @@ class PadAppBar extends StatelessWidget implements PreferredSizeWidget {
MediaQuery.of(context).size.width < LeapBreakpoints.compact;
return _AppBarTitle(
isMobile: isMobile,
searchController: searchController,
);
}),
);
Expand All @@ -76,8 +83,11 @@ class PadAppBar extends StatelessWidget implements PreferredSizeWidget {
}

class _AppBarTitle extends StatefulWidget {
final SearchController searchController;

const _AppBarTitle({
required this.isMobile,
required this.searchController,
});

final bool isMobile;
Expand Down Expand Up @@ -298,7 +308,9 @@ class _AppBarTitleState extends State<_AppBarTitle> {
state.location.fileType.icon(PhosphorIconsStyle.light)),
tooltip: AppLocalizations.of(context).export,
onPressed: () => context.read<ImportService>().export()),
SearchButton(),
SearchButton(
controller: widget.searchController,
),
if (state.location.path != '' && state.embedding == null) ...[
IconButton(
icon: const PhosphorIcon(PhosphorIconsLight.folder),
Expand Down
21 changes: 20 additions & 1 deletion app/lib/views/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'package:butterfly/views/toolbar/view.dart';
import 'package:butterfly/views/edit.dart';
import 'package:butterfly/views/error.dart';
import 'package:butterfly/views/property.dart';
import 'package:butterfly/widgets/search.dart';
import 'package:butterfly_api/butterfly_api.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -87,6 +88,7 @@ class _ProjectPageState extends State<ProjectPage> {
ExternalStorage? _remote;
ImportService? _importService;
ExportService? _exportService;
final SearchController _searchController = SearchController();
late final CloseSubscription _closeSubscription;
final GlobalKey _viewportKey = GlobalKey();
final _actions = <Type, Action<Intent>>{
Expand Down Expand Up @@ -286,6 +288,7 @@ class _ProjectPageState extends State<ProjectPage> {
widget.embedding?.handler.unregister();
_closeSubscription.dispose();
_bloc?.dispose();
_searchController.dispose();
}

@override
Expand Down Expand Up @@ -331,7 +334,19 @@ class _ProjectPageState extends State<ProjectPage> {
previous.toolbarSize != current.toolbarSize,
builder: (context, settings) {
return Actions(
actions: _actions,
actions: {
..._actions,
SearchIntent: CallbackAction<SearchIntent>(
onInvoke: (_) {
if (_searchController.isOpen) {
_searchController.closeView(null);
return null;
}
_searchController.openView();
return null;
},
),
},
child: Shortcuts(
shortcuts: {
LogicalKeySet(LogicalKeyboardKey.control,
Expand Down Expand Up @@ -364,6 +379,8 @@ class _ProjectPageState extends State<ProjectPage> {
LogicalKeySet(LogicalKeyboardKey.control,
LogicalKeyboardKey.keyA):
SelectAllIntent(context),
LogicalKeySet(LogicalKeyboardKey.control,
LogicalKeyboardKey.keyK): SearchIntent(),
if (widget.embedding == null) ...{
LogicalKeySet(LogicalKeyboardKey.control,
LogicalKeyboardKey.keyE):
Expand Down Expand Up @@ -441,6 +458,8 @@ class _ProjectPageState extends State<ProjectPage> {
: PadAppBar(
viewportKey: _viewportKey,
size: settings.toolbarSize,
searchController:
_searchController,
),
drawer: state is DocumentLoadSuccess
? const DocumentNavigator(
Expand Down
29 changes: 22 additions & 7 deletions app/lib/widgets/search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:material_leap/material_leap.dart';
import 'package:phosphor_flutter/phosphor_flutter.dart';

class SearchIntent extends Intent {
const SearchIntent();
}

Future<List<SearchResult>> _searchIsolate(NoteData noteData, String currentPage,
DocumentPage page, String query) =>
Isolate.run(() => noteData
.search(RegExp(query, caseSensitive: false), currentPage, page)
.toList());

class SearchButton extends StatelessWidget {
const SearchButton({super.key});
final SearchController controller;
const SearchButton({super.key, required this.controller});

IconGetter _getIcon(SearchResult item) => switch (item) {
ElementResult e => e.element.icon,
Expand Down Expand Up @@ -49,13 +54,23 @@ class SearchButton extends StatelessWidget {
Widget build(BuildContext context) {
final bloc = context.read<DocumentBloc>();
return SearchAnchor(
builder: (BuildContext context, SearchController controller) =>
IconButton(
icon: const Icon(PhosphorIconsLight.magnifyingGlass),
tooltip: AppLocalizations.of(context).search,
onPressed: () {
controller.openView();
searchController: controller,
builder: (BuildContext context, SearchController controller) => Actions(
actions: {
SearchIntent: CallbackAction<SearchIntent>(
onInvoke: (_) {
controller.openView();
return null;
},
),
},
child: IconButton(
icon: const Icon(PhosphorIconsLight.magnifyingGlass),
tooltip: AppLocalizations.of(context).search,
onPressed: () {
controller.openView();
},
),
),
suggestionsBuilder: (context, controller) async {
final state = bloc.state;
Expand Down

0 comments on commit 0780e9b

Please sign in to comment.