Skip to content

Commit

Permalink
wip: cmd palette for searching/opening tags
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatNerdSquared committed Jan 17, 2024
1 parent 2066e93 commit 65d11cf
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 9 deletions.
10 changes: 7 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'model/entry_filter.dart';
import 'model/tag_data.dart';
import 'widgets/desktop_frame.dart';
import 'widgets/entry_list_view.dart';
import 'widgets/pret_command_palette.dart';
import 'widgets/sidebar.dart';

const uuID = Uuid();
Expand Down Expand Up @@ -96,11 +97,11 @@ class MyApp extends ConsumerWidget {
}
}

class PeregrineHomeView extends StatelessWidget {
class PeregrineHomeView extends ConsumerWidget {
const PeregrineHomeView({super.key});

@override
Widget build(BuildContext context) {
Widget build(BuildContext context, WidgetRef ref) {
final view = PretMainView(
leftSidebar: Sidebar(
searchBoxFocusNode: searchBoxFocusNode,
Expand All @@ -121,7 +122,10 @@ class PeregrineHomeView extends StatelessWidget {
body: DesktopFrame(
entryBoxFocusNode: entryBoxFocusNode,
searchBoxFocusNode: searchBoxFocusNode,
child: view,
child: PretCmdPaletteScope(
searchItems: ref.read(tagsProvider).keys.toList(),
child: view,
),
),
);
}
Expand Down
8 changes: 8 additions & 0 deletions lib/widgets/desktop_frame.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ class DesktopFrame extends ConsumerWidget {
),
onSelected: entryBoxFocusNode.requestFocus,
),
const PlatformMenuItem(
label: 'Quick Open Tag',
shortcut: SingleActivator(
LogicalKeyboardKey.keyP,
meta: true,
),
//onSelected: () => PretCmdPalette.of(context).togglePalette(),
),
],
),
],
Expand Down
18 changes: 18 additions & 0 deletions lib/widgets/peregrine_app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:pret_a_porter/pret_a_porter.dart';

import '../main.dart';
import 'pret_command_palette.dart';

class PeregrineAppBar extends ConsumerWidget {
final FocusNode searchBoxFocusNode;
Expand Down Expand Up @@ -45,6 +46,23 @@ class PeregrineAppBar extends ConsumerWidget {
filter.name,
style: Theme.of(context).textTheme.titleMedium,
)),
IconButton.filled(
style: IconButton.styleFrom(
iconSize: 20,
minimumSize: const Size(36, 36),
maximumSize: const Size(36, 36),
backgroundColor: const Color(0xffdac6b0),
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(PretConfig.thinBorderRounding),
)),
onPressed: () => PretPaletteToggle.of(context).togglePalette(),
icon: Icon(
Icons.tag,
color: Theme.of(context).colorScheme.surface,
),
),
const Padding(
padding: EdgeInsets.only(right: PretConfig.thinElementSpacing)),
IconButton.filled(
style: IconButton.styleFrom(
iconSize: 20,
Expand Down
15 changes: 13 additions & 2 deletions lib/widgets/peregrine_entry_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,26 @@ class PeregrineEntryBoxState extends ConsumerState<PeregrineEntryBox> {
_controller.text = '';
}

void insertIndent() {
final cursor = _controller.selection.base.offset;
final beforeCursor = _controller.text.substring(0, cursor);
final afterCursor = _controller.text.substring(cursor);
// this should probably be abstracted into a setting
_controller.text = '$beforeCursor $afterCursor';
}

@override
Widget build(BuildContext context) {
return ConstrainedBox(
constraints:
BoxConstraints(maxHeight: MediaQuery.of(context).size.height / 2),
child: CallbackShortcuts(
bindings: <ShortcutActivator, VoidCallback>{
const SingleActivator(LogicalKeyboardKey.enter, meta: true):
submitNewLogEntry
const SingleActivator(
LogicalKeyboardKey.enter,
meta: true,
): submitNewLogEntry,
const SingleActivator(LogicalKeyboardKey.tab): insertIndent
},
child: Row(children: [
Expanded(
Expand Down
109 changes: 109 additions & 0 deletions lib/widgets/pret_command_palette.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:pret_a_porter/pret_a_porter.dart';

import '../main.dart';

class PretCmdPaletteScope extends ConsumerStatefulWidget {
final Widget child;
final List<String> searchItems;

const PretCmdPaletteScope({
super.key,
required this.child,
required this.searchItems,
});

@override
PretCmdPaletteScopeState createState() => PretCmdPaletteScopeState();
}

class PretCmdPaletteScopeState extends ConsumerState<PretCmdPaletteScope> {
bool _isPaletteShown = false;

void togglePalette() => setState(() {
_isPaletteShown = !_isPaletteShown;
});

@override
Widget build(BuildContext context) => PretPaletteToggle(
isPaletteShown: _isPaletteShown,
togglePalette: togglePalette,
child: !_isPaletteShown
? widget.child
: LayoutBuilder(
builder: (context, constraints) => Container(
alignment: Alignment.topCenter,
child: Stack(
children: [
widget.child,
CallbackShortcuts(
bindings: <ShortcutActivator, VoidCallback>{
const SingleActivator(
LogicalKeyboardKey.escape,
): togglePalette
},
child: Container(
constraints: BoxConstraints(
maxHeight: min(0.8 * constraints.maxHeight, 600),
maxWidth: min(0.8 * constraints.maxWidth, 900),
),
child: PretCard(
child: Column(
children: [
TextFormField(
autofocus: true,
),
Expanded(
child: ListView.builder(
itemCount: widget.searchItems.length,
itemBuilder: (context, index) => ListTile(
title: Text(widget.searchItems[index]),
onTap: () {
togglePalette();
ref
.read(entryFilterProvider.notifier)
.setTagFilter(
widget.searchItems[index],
);
},
),
),
),
],
),
),
),
)
],
),
),
),
);
}

class PretPaletteToggle extends InheritedWidget {
final bool isPaletteShown;
final VoidCallback togglePalette;

const PretPaletteToggle({
super.key,
required this.isPaletteShown,
required this.togglePalette,
required Widget child,
}) : super(child: child);

static PretPaletteToggle of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<PretPaletteToggle>()!;
}

@override
bool updateShouldNotify(covariant InheritedWidget oldWidget) {
return false;
// TODO: implement updateShouldNotify
// throw UnimplementedError();
}
}
8 changes: 4 additions & 4 deletions lib/widgets/sidebar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ class Sidebar extends ConsumerWidget {
SliverToBoxAdapter(
child: SidebarToggleList(
toggleTitle: 'Tags',
items: tags.keys.map((tagName) {
return Container(
items: tags.keys.map(
(tagName) => Container(
padding: const EdgeInsets.only(
left: PretConfig.defaultElementSpacing,
right: PretConfig.defaultElementSpacing),
Expand Down Expand Up @@ -105,8 +105,8 @@ class Sidebar extends ConsumerWidget {
count: tags[tagName]!.count,
icon: Icons.tag,
),
));
}),
)),
),
)),
]);
}
Expand Down

0 comments on commit 65d11cf

Please sign in to comment.