Skip to content

Refactor code to prepare for more code. #3244

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 9 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion pkgs/dartpad_ui/lib/console.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import 'package:provider/provider.dart';

import 'enable_gen_ai.dart';
import 'model.dart';
import 'simple_widgets.dart';
Copy link
Member

Choose a reason for hiding this comment

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

Why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because of isMac. Now it is utils.dart

import 'suggest_fix.dart';
import 'theme.dart';
import 'utils.dart';
import 'widgets.dart';

class ConsoleWidget extends StatefulWidget {
final bool showDivider;
Expand Down
6 changes: 6 additions & 0 deletions pkgs/dartpad_ui/lib/enable_gen_ai.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
// Turn on or off gen-ai features in the client.
const bool genAiEnabled = true;

// Set to true to use GenUI instead of Gemini.
// This is a temporary flag to test GenUI in production.
// It will be removed once GenUI is fully integrated.
// The flag is set based on URL parameter in main.dart.
bool useGenUI = false;

/*

There are two options to use gen AI: Gemini and GenUI. Gemini is the default.
Expand Down
98 changes: 7 additions & 91 deletions pkgs/dartpad_ui/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ import 'keys.dart' as keys;
import 'local_storage/local_storage.dart';
import 'model.dart';
import 'problems.dart';
import 'prompt_dialog.dart';
import 'samples.g.dart';
import 'simple_widgets.dart';
import 'theme.dart';
import 'utils.dart';
import 'versions.dart';
import 'widgets.dart';

const appName = 'DartPad';
const smallScreenWidth = 720;
Expand Down Expand Up @@ -130,7 +131,7 @@ class _DartPadAppState extends State<DartPadApp> {
final channelParam = state.uri.queryParameters['channel'];
final embedMode = state.uri.queryParameters['embed'] == 'true';
final runOnLoad = state.uri.queryParameters['run'] == 'true';
final useGenui = state.uri.queryParameters['genui'] == 'true';
useGenUI = state.uri.queryParameters['genui'] == 'true';

return DartPadMainPage(
initialChannel: channelParam,
Expand All @@ -140,7 +141,6 @@ class _DartPadAppState extends State<DartPadApp> {
builtinSampleId: builtinSampleId,
flutterSampleId: flutterSampleId,
handleBrightnessChanged: handleBrightnessChanged,
useGenui: useGenui,
);
}

Expand Down Expand Up @@ -213,7 +213,6 @@ class DartPadMainPage extends StatefulWidget {
final String? gistId;
final String? builtinSampleId;
final String? flutterSampleId;
final bool useGenui;

DartPadMainPage({
required this.initialChannel,
Expand All @@ -223,7 +222,6 @@ class DartPadMainPage extends StatefulWidget {
this.gistId,
this.builtinSampleId,
this.flutterSampleId,
this.useGenui = false,
}) : super(
key: ValueKey(
'sample:$builtinSampleId gist:$gistId flutter:$flutterSampleId',
Expand Down Expand Up @@ -316,9 +314,7 @@ class _DartPadMainPageState extends State<DartPadMainPage>
}
});

debugPrint(
'initialized: useGenui = ${widget.useGenui}, channel = $channel.',
);
debugPrint('initialized: useGenui = $useGenUI, channel = $channel.');
}

@override
Expand Down Expand Up @@ -632,9 +628,10 @@ class DartPadAppBar extends StatelessWidget implements PreferredSizeWidget {
const SizedBox(width: denseSpacing),
GeminiMenu(
generateNewDartCode:
() => _generateNewCode(context, AppType.dart),
() => openCodeGenerationDialog(context, AppType.dart),
generateNewFlutterCode:
() => _generateNewCode(context, AppType.flutter),
() =>
openCodeGenerationDialog(context, AppType.flutter),
updateExistingCode: () => _updateExistingCode(context),
hideLabel: !wideLayout,
),
Expand Down Expand Up @@ -685,87 +682,6 @@ class DartPadAppBar extends StatelessWidget implements PreferredSizeWidget {
url_launcher.launchUrl(Uri.parse(response.firebaseStudioUrl));
}

Future<void> _generateNewCode(BuildContext context, AppType? appType) async {
final appModel = Provider.of<AppModel>(context, listen: false);
final appServices = Provider.of<AppServices>(context, listen: false);
final lastPrompt = LocalStorage.instance.getLastCreateCodePrompt();
final promptResponse = await showDialog<PromptDialogResponse>(
context: context,
builder:
(context) => PromptDialog(
title: 'Generate new code',
hint: 'Describe the code you want to generate',
initialAppType:
appType ?? LocalStorage.instance.getLastCreateCodeAppType(),
flutterPromptButtons: {
'to-do app':
'Generate a Flutter to-do app with add, remove, and complete task functionality',
'login screen':
'Generate a Flutter login screen with email and password fields, validation, and a submit button',
'tic-tac-toe':
'Generate a Flutter tic-tac-toe game with two players, win detection, and a reset button',
if (lastPrompt != null) 'your last prompt': lastPrompt,
},
dartPromptButtons: {
'hello, world': 'Generate a Dart hello world program',
'fibonacci':
'Generate a Dart program that prints the first 10 numbers in the Fibonacci sequence',
'factorial':
'Generate a Dart program that prints the factorial of 5',
if (lastPrompt != null) 'your last prompt': lastPrompt,
},
),
);

if (!context.mounted ||
promptResponse == null ||
promptResponse.prompt.isEmpty) {
return;
}

LocalStorage.instance.saveLastCreateCodeAppType(promptResponse.appType);
LocalStorage.instance.saveLastCreateCodePrompt(promptResponse.prompt);

try {
final Stream<String> stream;
if (widget.useGenui) {
stream = appServices.generateUi(
GenerateUiRequest(prompt: promptResponse.prompt),
);
} else {
stream = appServices.generateCode(
GenerateCodeRequest(
appType: promptResponse.appType,
prompt: promptResponse.prompt,
attachments: promptResponse.attachments,
),
);
}

final generateResponse = await showDialog<String>(
context: context,
builder:
(context) => GeneratingCodeDialog(
stream: stream,
title: 'Generating new code',
),
);

if (!context.mounted ||
generateResponse == null ||
generateResponse.isEmpty) {
return;
}

appModel.sourceCodeController.textNoScroll = generateResponse;
appServices.editorService!.focus();
appServices.performCompileAndReloadOrRun();
} catch (error) {
appModel.editorStatus.showToast('Error generating code');
appModel.appendError('Generating code issue: $error');
}
}

Future<void> _updateExistingCode(BuildContext context) async {
final appModel = Provider.of<AppModel>(context, listen: false);
final appServices = Provider.of<AppServices>(context, listen: false);
Expand Down
Loading