Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
0.7.4
  • Loading branch information
TNorbury committed Oct 30, 2023
2 parents e62a83e + c3ccd8a commit 48669e8
Show file tree
Hide file tree
Showing 14 changed files with 231 additions and 32 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ on:
- main
- development
pull_request:
types:
- ready_for_review
- synchronize
- opened
types: [synchronize, opened, reopened]

env:
flutter_version: 3.10.0
Expand Down
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.7.4 - 2023-10-30
### Added
- Add FocusNode parameter which is passed to underlying Focus widget
- Added input as a type for an action. This type of action will give the user a text field and accept the processed text, passing it to a callback

## 0.7.3 - 2023-05-12
### Changed
- Upgrade to Flutter 3.10.0
Expand Down Expand Up @@ -91,4 +96,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## 0.1.0 - 2021-11-03
### Added
- initial release
- initial release
10 changes: 10 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ class _MyHomePageState extends State<MyHomePage> {
),
],
),
CommandPaletteAction.input(
id: "new-user",
label: "New User",
shortcut: ["ctrl", "shift", "n"],
leading: Icon(Icons.add),
onConfirmInput: (value) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Created user: $value')));
},
),
CommandPaletteAction.nested(
id: 1, // or numbers (or really anything...)
label: "Set User",
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.7.3"
version: "0.7.4"
fake_async:
dependency: transitive
description:
Expand Down
8 changes: 7 additions & 1 deletion lib/src/command_palette.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class CommandPalette extends InheritedWidget {
/// functional configuration
final CommandPaletteConfig config;

final FocusNode? focusNode;

late final _CommandPaletteToggler _toggler;

late final CommandPaletteController _controller;
Expand All @@ -33,6 +35,7 @@ class CommandPalette extends InheritedWidget {
CommandPaletteConfig? config,
required this.actions,
required Widget child,
this.focusNode,
}) : config = config ?? CommandPaletteConfig(),
super(
key: key,
Expand All @@ -45,6 +48,7 @@ class CommandPalette extends InheritedWidget {
),
config: config ?? CommandPaletteConfig(),
toggler: _CommandPaletteToggler(false),
focusNode: focusNode,
child: child,
),
) {
Expand Down Expand Up @@ -109,14 +113,15 @@ class _CommandPaletteInner extends StatefulWidget {
final CommandPaletteConfig config;
final _CommandPaletteToggler toggler;
final CommandPaletteController controller;

final FocusNode? focusNode;
const _CommandPaletteInner({
Key? key,
required this.child,
required this.actions,
required this.config,
required this.toggler,
required this.controller,
required this.focusNode,
}) : super(key: key);

@override
Expand Down Expand Up @@ -239,6 +244,7 @@ class _CommandPaletteInnerState extends State<_CommandPaletteInner> {
)
},
child: Focus(
focusNode: widget.focusNode,
autofocus: true,
child: widget.child,
),
Expand Down
26 changes: 23 additions & 3 deletions lib/src/controller/command_palette_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ class CommandPaletteController extends ChangeNotifier {

/// Listens to [textEditingController] and is called whenever it changes.
void _onTextControllerChange() {
if (currentlySelectedAction?.actionType == CommandPaletteActionType.input) {
return;
}

if (_enteredQuery != textEditingController.text) {
_enteredQuery = textEditingController.text;
_actionsNeedRefiltered = true;
Expand All @@ -96,7 +100,8 @@ class CommandPaletteController extends ChangeNotifier {
CommandPaletteAction? get currentlySelectedAction => _currentlySelectedAction;
set currentlySelectedAction(CommandPaletteAction? newAction) {
assert(newAction == null ||
newAction.actionType == CommandPaletteActionType.nested);
newAction.actionType == CommandPaletteActionType.nested ||
newAction.actionType == CommandPaletteActionType.input);
_currentlySelectedAction = newAction;
_actionsNeedRefiltered = true;
textEditingController.clear();
Expand All @@ -118,6 +123,9 @@ class CommandPaletteController extends ChangeNotifier {
if (currentlySelectedAction?.actionType ==
CommandPaletteActionType.nested) {
filteredActions = currentlySelectedAction!.childrenActions!;
} else if (currentlySelectedAction?.actionType ==
CommandPaletteActionType.input) {
filteredActions = [];
} else {
filteredActions = actions;
}
Expand Down Expand Up @@ -193,14 +201,26 @@ class CommandPaletteController extends ChangeNotifier {

// nested items we set this item as the selected which in turn
// will display its children.
else if (action.actionType == CommandPaletteActionType.nested) {
else {
currentlySelectedAction = action;
}
}

void handleActionInput(BuildContext context) {
_currentlySelectedAction?.onConfirmInput!(textEditingController.text);
if (Navigator.of(context).canPop()) {
Navigator.of(context).pop();
}
}

/// performs the action which is currently selected by [highlightedAction]
void performHighlightedAction(BuildContext context) {
handleAction(context, action: _filteredActionsCache[highlightedAction]);
if (_currentlySelectedAction?.actionType ==
CommandPaletteActionType.input) {
handleActionInput(context);
} else {
handleAction(context, action: _filteredActionsCache[highlightedAction]);
}
}
}

Expand Down
24 changes: 22 additions & 2 deletions lib/src/models/command_palette_action.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ enum CommandPaletteActionType {

/// Upon being selected a nested action will change the state of the command
/// palette so that it only shows its children
nested
nested,

/// Upon being selected the user will be given a text field that they can enter text into and submit.
/// The entered text will then be passed to the relevant callback
input
}

/// Action that is presented in the command palette. These are the things the
Expand All @@ -25,6 +29,10 @@ class CommandPaletteAction {
/// Specifies what type of action this is
final CommandPaletteActionType actionType;

/// Required when [actionType] set to [CommandPaletteActionType.input]. This
/// function is called when the action is confirmed
ValueChanged<String>? onConfirmInput;

/// Required when [actionType] set to [CommandPaletteActionType.single]. This
/// function is called when the action is selected
VoidCallback? onSelect;
Expand Down Expand Up @@ -68,13 +76,16 @@ class CommandPaletteAction {
required this.actionType,
this.onSelect,
this.childrenActions,
this.onConfirmInput,
this.shortcut,
this.id,
this.leading,
}) : assert((actionType == CommandPaletteActionType.single &&
onSelect != null) ||
(actionType == CommandPaletteActionType.nested &&
(childrenActions?.isNotEmpty ?? false))) {
(childrenActions?.isNotEmpty ?? false)) ||
(actionType == CommandPaletteActionType.input &&
(onConfirmInput != null))) {
// give all our children "us" as a parent.
if (actionType == CommandPaletteActionType.nested) {
for (final child in childrenActions!) {
Expand Down Expand Up @@ -106,6 +117,15 @@ class CommandPaletteAction {
}
}

CommandPaletteAction.input({
required this.label,
this.description,
required this.onConfirmInput,
this.shortcut,
this.id,
this.leading,
}) : actionType = CommandPaletteActionType.input;

@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/models/command_palette_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class CommandPaletteConfig {
/// keyboard
///
/// The current instructions are:
/// * enter/return: to select
/// * enter/return: to select, (or if an input action) to confirm
/// * up/down arrow: to navigate
/// * escape: to close
///
Expand Down
1 change: 1 addition & 0 deletions lib/src/models/matched_command_palette_action.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class MatchedCommandPaletteAction extends CommandPaletteAction {
childrenActions: action.childrenActions,
description: action.description,
onSelect: action.onSelect,
onConfirmInput: action.onConfirmInput,
shortcut: action.shortcut,
id: action.id,
leading: action.leading,
Expand Down
35 changes: 21 additions & 14 deletions lib/src/widgets/command_palette_instructions.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:command_palette/src/controller/command_palette_controller.dart';
import 'package:command_palette/src/models/command_palette_action.dart';
import 'package:command_palette/src/widgets/keyboard_key_icon.dart';
import 'package:flutter/material.dart';

Expand Down Expand Up @@ -34,21 +35,27 @@ class CommandPaletteInstructions extends StatelessWidget {
color: color,
),
],
instruction: "to select",
),
_KeyboardInstruction(
icons: [
KeyboardKeyIcon(
icon: Icons.arrow_upward,
color: color,
),
KeyboardKeyIcon(
icon: Icons.arrow_downward,
color: color,
),
],
instruction: "to navigate",
instruction:
(controller.currentlySelectedAction?.actionType ==
CommandPaletteActionType.input)
? "to confirm"
: "to select",
),
if (controller.currentlySelectedAction?.actionType !=
CommandPaletteActionType.input)
_KeyboardInstruction(
icons: [
KeyboardKeyIcon(
icon: Icons.arrow_upward,
color: color,
),
KeyboardKeyIcon(
icon: Icons.arrow_downward,
color: color,
),
],
instruction: "to navigate",
),
if (controller.currentlySelectedAction != null)
_KeyboardInstruction(
icons: [
Expand Down
6 changes: 4 additions & 2 deletions lib/src/widgets/command_palette_text_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ class _CommandPaletteTextFieldState extends State<CommandPaletteTextField> {
inputDecoration.prefixText == null;
if (styleHasNoPrefix &&
style.prefixNestedActions &&
controller.currentlySelectedAction?.actionType ==
CommandPaletteActionType.nested) {
(controller.currentlySelectedAction?.actionType ==
CommandPaletteActionType.nested ||
controller.currentlySelectedAction?.actionType ==
CommandPaletteActionType.input)) {
inputDecoration = inputDecoration.copyWith(
prefixText: "${controller.currentlySelectedAction!.label}: ",
hintText: "",
Expand Down
3 changes: 2 additions & 1 deletion lib/src/widgets/options/command_palette_body.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class _CommandPaletteBodyState extends State<CommandPaletteBody> {
double posToScrollTo = -1;
if (selectedItemTop < scrollViewTopOffset) {
posToScrollTo = selectedItemTop;
} else if (selectedItemBottom > scrollViewBottomOffset) {
} else if (scrollViewBottomOffset != 0 &&
selectedItemBottom > scrollViewBottomOffset) {
// align bottom of item to bottom
posToScrollTo = selectedItemBottom - scrollViewHeight;
}
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: command_palette
description: Flutter implementation of a Command Palette. Can be brought up via a keyboard shortcut.
version: 0.7.3
version: 0.7.4
homepage: https://github.com/TNorbury/command_palette
issue_tracker: https://github.com/TNorbury/command_palette/issues
repository: https://github.com/TNorbury/command_palette
Expand All @@ -14,7 +14,7 @@ platforms:


environment:
sdk: ">=2.12.0 <3.0.0"
sdk: ">=2.12.0 <4.0.0"
flutter: ">=1.17.0"

dependencies:
Expand Down
Loading

0 comments on commit 48669e8

Please sign in to comment.