Skip to content

Commit

Permalink
use more subtle progress when reloading (#3147)
Browse files Browse the repository at this point in the history
  • Loading branch information
devoncarew authored Feb 20, 2025
1 parent d8a8842 commit 9d052dd
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 48 deletions.
24 changes: 14 additions & 10 deletions pkgs/dartpad_ui/lib/execution/frame.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ class ExecutionServiceImpl implements ExecutionService {
}

return _send(reload ? 'executeReload' : 'execute', {
'js': _decorateJavaScript(javaScript,
modulesBaseUrl: modulesBaseUrl,
isNewDDC: isNewDDC,
reload: reload,
isFlutter: isFlutter),
'js': _decorateJavaScript(
javaScript,
modulesBaseUrl: modulesBaseUrl,
isNewDDC: isNewDDC,
reload: reload,
isFlutter: isFlutter,
),
if (engineVersion != null)
'canvasKitBaseUrl': _canvasKitUrl(engineVersion),
});
Expand All @@ -61,11 +63,13 @@ class ExecutionServiceImpl implements ExecutionService {
@override
Future<void> tearDown() => _reset();

String _decorateJavaScript(String javaScript,
{String? modulesBaseUrl,
required bool isNewDDC,
required bool reload,
required bool isFlutter}) {
String _decorateJavaScript(
String javaScript, {
String? modulesBaseUrl,
required bool isNewDDC,
required bool reload,
required bool isFlutter,
}) {
if (reload) return javaScript;

final script = StringBuffer();
Expand Down
28 changes: 15 additions & 13 deletions pkgs/dartpad_ui/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -442,12 +442,12 @@ class _DartPadMainPageState extends State<DartPadMainPage>
child: CallbackShortcuts(
bindings: <ShortcutActivator, VoidCallback>{
keys.runKeyActivator1: () {
if (!appModel.compilingBusy.value) {
if (!appModel.compilingBusy.value.busy) {
appServices.performCompileAndRun();
}
},
keys.runKeyActivator2: () {
if (!appModel.compilingBusy.value) {
if (!appModel.compilingBusy.value.busy) {
appServices.performCompileAndRun();
}
},
Expand Down Expand Up @@ -513,7 +513,7 @@ class _DartPadMainPageState extends State<DartPadMainPage>
void _handleRunStarted() {
setState(() {
// Switch to the application output tab.]
if (appModel.compilingBusy.value) {
if (appModel.compilingBusy.value != CompilingState.none) {
tabController.animateTo(1);
}
});
Expand All @@ -531,19 +531,20 @@ class LoadingOverlay extends StatelessWidget {
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return ValueListenableBuilder<bool>(
return ValueListenableBuilder<CompilingState>(
valueListenable: appModel.compilingBusy,
builder: (_, bool compiling, __) {
builder: (_, compiling, __) {
final color = theme.colorScheme.surface;

// If reloading, show a progress spinner. If restarting, also display a
// semi-opaque overlay.
return AnimatedContainer(
color: color.withValues(alpha: compiling ? 0.8 : 0),
color: color.withValues(
alpha: compiling == CompilingState.compiling ? 0.8 : 0),
duration: animationDelay,
curve: animationCurve,
child: compiling
? const GoldenRatioCenter(
child: CircularProgressIndicator(),
)
child: compiling.busy
? const GoldenRatioCenter(child: CircularProgressIndicator())
: const SizedBox(width: 1),
);
},
Expand Down Expand Up @@ -731,12 +732,13 @@ class EditorWithButtons extends StatelessWidget {
}),
const SizedBox(width: defaultSpacing),
// Run action
ValueListenableBuilder<bool>(
ValueListenableBuilder<CompilingState>(
valueListenable: appModel.compilingBusy,
builder: (_, bool value, __) {
builder: (_, compiling, __) {
return PointerInterceptor(
child: RunButton(
onPressed: value ? null : onCompileAndRun,
onPressed:
compiling.busy ? null : onCompileAndRun,
),
);
},
Expand Down
67 changes: 42 additions & 25 deletions pkgs/dartpad_ui/lib/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class AppModel {
final ValueNotifier<String> consoleOutput = ValueNotifier('');

final ValueNotifier<bool> formattingBusy = ValueNotifier(false);
final ValueNotifier<bool> compilingBusy = ValueNotifier(false);
final ValueNotifier<CompilingState> compilingBusy =
ValueNotifier(CompilingState.none);
final ValueNotifier<bool> docHelpBusy = ValueNotifier(false);

final ValueNotifier<bool> hasRun = ValueNotifier(false);
Expand All @@ -76,23 +77,25 @@ class AppModel {
final ValueNotifier<bool> vimKeymapsEnabled = ValueNotifier(false);

bool _consoleShowingError = false;

final ValueNotifier<bool> showReload = ValueNotifier(false);
final ValueNotifier<bool> _useNewDDC = ValueNotifier(false);
final ValueNotifier<bool> useNewDDC = ValueNotifier(false);
final ValueNotifier<String?> currentDeltaDill = ValueNotifier(null);

AppModel() {
consoleOutput.addListener(_recalcLayout);
void updateCanReload() => canReload.value =
hasRun.value && !compilingBusy.value && currentDeltaDill.value != null;
void updateCanReload() => canReload.value = hasRun.value &&
!compilingBusy.value.busy &&
currentDeltaDill.value != null;
hasRun.addListener(updateCanReload);
compilingBusy.addListener(updateCanReload);
currentDeltaDill.addListener(updateCanReload);

void updateShowReload() {
showReload.value = _useNewDDC.value && (_appIsFlutter.value ?? false);
showReload.value = useNewDDC.value && (_appIsFlutter.value ?? false);
}

_useNewDDC.addListener(updateShowReload);
useNewDDC.addListener(updateShowReload);
_appIsFlutter.addListener(updateShowReload);

_splitSubscription =
Expand Down Expand Up @@ -187,7 +190,7 @@ class AppServices {
appModel.analysisIssues.addListener(_updateEditorProblemsStatus);

void updateUseNewDDC() {
appModel._useNewDDC.value =
appModel.useNewDDC.value =
_hotReloadableChannels.contains(_channel.value);
}

Expand Down Expand Up @@ -344,13 +347,15 @@ class AppServices {
}

Future<void> _performCompileAndAction({required bool reload}) async {
final willUseReload = reload && appModel.useNewDDC.value;

final source = appModel.sourceCodeController.text;
final progress =
appModel.editorStatus.showMessage(initialText: 'Compiling…');
final progress = appModel.editorStatus
.showMessage(initialText: willUseReload ? 'Reloading…' : 'Compiling…');

try {
CompileDDCResponse response;
if (!appModel._useNewDDC.value) {
if (!appModel.useNewDDC.value) {
response = await _compileDDC(CompileRequest(source: source));
} else if (reload) {
response = await _compileNewDDCReload(CompileRequest(
Expand All @@ -366,7 +371,7 @@ class AppServices {
modulesBaseUrl: response.modulesBaseUrl,
engineVersion: appModel.runtimeVersions.value?.engineVersion,
dartSource: source,
isNewDDC: appModel._useNewDDC.value,
isNewDDC: appModel.useNewDDC.value,
reload: reload,
);
appModel.currentDeltaDill.value = response.deltaDill;
Expand Down Expand Up @@ -411,38 +416,38 @@ class AppServices {

Future<CompileResponse> compile(CompileRequest request) async {
try {
appModel.compilingBusy.value = true;
appModel.compilingBusy.value = CompilingState.compiling;
return await services.compile(request);
} finally {
appModel.compilingBusy.value = false;
appModel.compilingBusy.value = CompilingState.none;
}
}

Future<CompileDDCResponse> _compileDDC(CompileRequest request) async {
try {
appModel.compilingBusy.value = true;
appModel.compilingBusy.value = CompilingState.compiling;
return await services.compileDDC(request);
} finally {
appModel.compilingBusy.value = false;
appModel.compilingBusy.value = CompilingState.none;
}
}

Future<CompileDDCResponse> _compileNewDDC(CompileRequest request) async {
try {
appModel.compilingBusy.value = true;
appModel.compilingBusy.value = CompilingState.compiling;
return await services.compileNewDDC(request);
} finally {
appModel.compilingBusy.value = false;
appModel.compilingBusy.value = CompilingState.none;
}
}

Future<CompileDDCResponse> _compileNewDDCReload(
CompileRequest request) async {
try {
appModel.compilingBusy.value = true;
appModel.compilingBusy.value = CompilingState.reloading;
return await services.compileNewDDCReload(request);
} finally {
appModel.compilingBusy.value = false;
appModel.compilingBusy.value = CompilingState.none;
}
}

Expand Down Expand Up @@ -477,12 +482,14 @@ class AppServices {
appModel._usesPackageWeb = hasPackageWebImport(dartSource);
appModel._recalcLayout();

_executionService?.execute(javaScript,
modulesBaseUrl: modulesBaseUrl,
engineVersion: engineVersion,
reload: reload,
isNewDDC: isNewDDC,
isFlutter: appIsFlutter);
_executionService?.execute(
javaScript,
modulesBaseUrl: modulesBaseUrl,
engineVersion: engineVersion,
reload: reload,
isNewDDC: isNewDDC,
isFlutter: appIsFlutter,
);
}

void dispose() {
Expand Down Expand Up @@ -579,3 +586,13 @@ class SplitDragStateManager {
}

enum SplitDragState { inactive, active }

enum CompilingState {
none(false),
compiling(true),
reloading(true);

final bool busy;

const CompilingState(this.busy);
}

0 comments on commit 9d052dd

Please sign in to comment.