Skip to content

Adds a basic dialog for opening a file #3342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions packages/devtools_app/lib/src/debugger/codeview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1056,16 +1056,11 @@ class ScriptPopupMenuOption {
}
}

final defaultScriptPopupMenuOptions = openFileDialogEnabled
? [
copyScriptNameOption,
goToLineOption,
openFileOption,
]
: [
copyScriptNameOption,
goToLineOption,
];
final defaultScriptPopupMenuOptions = [
copyScriptNameOption,
goToLineOption,
if (openFileDialogEnabled) openFileOption,
];

final copyScriptNameOption = ScriptPopupMenuOption(
label: 'Copy filename',
Expand Down Expand Up @@ -1098,9 +1093,10 @@ void showOpenFileDialog(BuildContext context, DebuggerController controller) {
}

const openFileOption = ScriptPopupMenuOption(
label: 'Open file (⌘ P)',
icon: Icons.folder_open,
onSelected: showOpenFileDialog);
label: 'Open file (⌘ P)',
icon: Icons.folder_open,
onSelected: showOpenFileDialog,
);

class GoToLineDialog extends StatelessWidget {
const GoToLineDialog(this._debuggerController);
Expand Down Expand Up @@ -1158,13 +1154,13 @@ class OpenFileDialog extends StatelessWidget {
title: dialogTitleText(Theme.of(context), 'Open file'),
includeDivider: false,
content: Container(
alignment: Alignment.topCenter,
height: 325,
width: 500,
child: FileSearchField(
controller: _debuggerController,
handleClose: () =>
Navigator.of(context).pop(dialogDefaultContext))),
alignment: Alignment.topCenter,
height: 325,
width: 500,
child: FileSearchField(
controller: _debuggerController,
),
),
actions: const [
DialogCancelButton(),
],
Expand Down
64 changes: 30 additions & 34 deletions packages/devtools_app/lib/src/debugger/file_search.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// Copyright 2020 The Chromium Authors. All rights reserved.
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:vm_service/vm_service.dart';

import '../auto_dispose_mixin.dart';
import '../common_widgets.dart';
import '../dialogs.dart';
import '../theme.dart';
import '../ui/search.dart';
import '../utils.dart';
import 'debugger_controller.dart';
Expand All @@ -16,11 +20,9 @@ const int numOfMatchesToShow = 6;
class FileSearchField extends StatefulWidget {
const FileSearchField({
@required this.controller,
@required this.handleClose,
});

final DebuggerController controller;
final Function handleClose;

@override
_FileSearchFieldState createState() => _FileSearchFieldState();
Expand All @@ -29,32 +31,35 @@ class FileSearchField extends StatefulWidget {
class _FileSearchFieldState extends State<FileSearchField>
with SearchFieldMixin, AutoDisposeMixin {
AutoCompleteController _autoCompleteController;
final Map<String, ScriptRef> _scriptsCache = {};

final _scriptsCache = <String, ScriptRef>{};

final fileSearchFieldKey = GlobalKey(debugLabel: 'fileSearchFieldKey');

@override
void initState() {
super.initState();

_autoCompleteController = AutoCompleteController();
_autoCompleteController.currentDefaultIndex = 0;
_autoCompleteController = AutoCompleteController()..currentDefaultIndex = 0;

addAutoDisposeListener(
_autoCompleteController.selectTheSearchNotifier, _handleSearch);
addAutoDisposeListener(
_autoCompleteController.searchNotifier, _handleSearch);
addAutoDisposeListener(_autoCompleteController.searchAutoCompleteNotifier,
_handleAutoCompleteOverlay);

_autoCompleteController.selectTheSearch = true;
SchedulerBinding.instance.addPostFrameCallback((_) => _handleSearch());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a comment explaining why this needs to be in a post frame callback?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks!

}

void _handleSearch() {
final query = _autoCompleteController.search;
final matches = findMatches(query, widget.controller.sortedScripts.value);
matches.forEach(_addScriptRefToCache);
_autoCompleteController.searchAutoComplete.value =
matches.map((scriptRef) => scriptRef.uri).toList();
_handleAutoCompleteOverlay();
if (matches.isEmpty) {
_autoCompleteController.searchAutoComplete.value = ['No files found.'];
} else {
matches.forEach(_addScriptRefToCache);
_autoCompleteController.searchAutoComplete.value =
matches.map((scriptRef) => scriptRef.uri).toList();
}
}

void _handleAutoCompleteOverlay() {
Expand All @@ -66,9 +71,7 @@ class _FileSearchFieldState extends State<FileSearchField>
}

void _addScriptRefToCache(ScriptRef scriptRef) {
if (!_scriptsCache.containsKey(scriptRef.uri)) {
_scriptsCache[scriptRef.uri] = scriptRef;
}
_scriptsCache.putIfAbsent(scriptRef.uri, () => scriptRef);
}

@override
Expand All @@ -78,24 +81,17 @@ class _FileSearchFieldState extends State<FileSearchField>
return Container(
decoration: BoxDecoration(
border: Border(
top: BorderSide(color: theme.focusColor),
top: defaultBorderSide(theme),
),
),
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
const SizedBox(width: 8.0),
Expanded(
child: buildAutoCompleteSearchField(
controller: _autoCompleteController,
searchFieldKey: fileSearchFieldKey,
searchFieldEnabled: true,
shouldRequestFocus: true,
closeOverlayOnEscape: false,
onSelection: _onSelection,
),
),
],
padding: const EdgeInsets.all(denseSpacing),
child: buildAutoCompleteSearchField(
controller: _autoCompleteController,
searchFieldKey: fileSearchFieldKey,
searchFieldEnabled: true,
shouldRequestFocus: true,
closeOverlayOnEscape: false,
onSelection: _onSelection,
),
);
}
Expand All @@ -104,7 +100,7 @@ class _FileSearchFieldState extends State<FileSearchField>
final scriptRef = _scriptsCache[scriptUri];
widget.controller.showScriptLocation(ScriptLocation(scriptRef));
_scriptsCache.clear();
widget.handleClose();
Navigator.of(context).pop(dialogDefaultContext);
}

@override
Expand Down Expand Up @@ -134,7 +130,7 @@ List<ScriptRef> findMatches(
.where((scriptRef) => scriptRef.uri.caseInsensitiveFuzzyMatch(query))
.toList();

return takeTopMatches([...exactMatches, ...fuzzyMatches, ...scriptRefs]);
return takeTopMatches([...exactMatches, ...fuzzyMatches]);
}

List<ScriptRef> takeTopMatches(List<ScriptRef> allMatches) {
Expand Down