Skip to content
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
46 changes: 46 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1781,6 +1781,52 @@ make widgetbook # every screen and dialog, in every state, light and dark
- **`MacAppMenu.install()` calls `MacMenuBar.reset()` first, and must keep doing so.** A hot restart replaces the Dart isolate but not the engine, its plugins or the native `NSMenu`, so without it every menu is added again on each restart — and `_recentItemIds` comes back empty, so Open Recent duplicates too. `reset()` removes exactly the items this plugin added: those are the ones carrying a String `representedObject`, which is also how the plugin finds items by id, so stock MainMenu.xib entries cannot be stripped.
- **Never return `menuItem.isEnabled` from `validateMenuItem`** for an item the plugin overrode. Under automatic enabling AppKit *writes* `isEnabled` from the previous validation pass, so reading it back is a latch, not a query. MainMenu.xib ships `Preferences…` with no action at all, so AppKit disabled it before the override was installed and it could never re-enable — the Settings item was permanently greyed out. Overridden standard items are tracked in `overriddenItems` and validate to `true`; the Dart handler decides what happens when they fire.

## The requirements check — before anything else at launch

A computer below App Studio's requirements is told so **before first-run
setup**, which would otherwise download a few GB of tools onto a machine that
cannot run a project. `main()` creates `SystemRequirementsController` before
`runApp`, and `home` shows `SystemRequirementsScreen` ahead of the setup
screen and the shell while `blocksLaunch` is true.

- **The requirements are 16 GB of memory and 8 logical cores**
(`platform/system_requirements.dart`). Anything short of either raises the
same question — "App Studio is unlikely to run on this computer", listing
only what falls short, **Try Anyway** and **Quit** (Quit on the right, as
the default). One level, on purpose: there is no softer "may be slow" tier.
- **The memory bar is 13 GiB, not 16.** A 16 GB computer never reports all
of it where the figure is what the OS can use — Linux always — and
integrated graphics can hold back 2 GB; 12 GB, the next size down, reports
under 12. The dialog shows the figure rounded to the GB the computer was
sold with.
- **The core count is a proxy for speed, chosen over a benchmark.** It is
instant and the same on every start; a timed run varies with battery saver
and whatever else starts at login, and a wrong "unlikely to run" costs a
user. It passes every Apple Silicon Mac and four cores with two threads
each, and fails dual-core laptops and four-thread budget chips. It says
nothing about how fast a core is.
- **Reading the hardware** (`platform/system_hardware.dart`): Windows is asked
directly (`WindowsSystem.installedMemoryBytes` —
`GetPhysicallyInstalledSystemMemory`, then `GlobalMemoryStatusEx` where the
firmware does not say), macOS through `/usr/sbin/sysctl -n hw.memsize`,
Linux from `/proc/meminfo`. **A reading that fails or takes over two
seconds is null, and a null is never held against the computer**: the check
must not be what stops the app from starting.
- **Try Anyway is remembered with the readings it was made for**
(`system_requirements.tried_anyway` = `memory=8/16;processor=4/8`), so the
same computer never asks twice and one that reads differently later asks
again. It is a preference, so an uninstall forgets it.
- **While the question is up it owns the window, like setup.**
`_canOpenProjects` in `main.dart` is false for either gate, which is what
File → Open, Open Recent and Settings follow, and `_onStartPage` is false,
so no other launch question (Linux's add-to-applications, the update
check) lands on top. The screen asks again if its dialog is cleared away
rather than answered.
- **Quit is `quitApp()` (`exit(0)`)**, as in the setup dialogs: nothing has
been started yet that would need stopping.
- The Windows test VM is 8 GB and 4 vCPUs (`specs/windows_vm_testing.md`), so
every pass from a pristine snapshot starts with this question.

## First run

`main.dart` picks the whole screen from `toolchains.inventory.needsSetup`:
Expand Down
52 changes: 48 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'platform/afunix_settle.dart';
import 'platform/compact_window.dart';
import 'platform/host.dart';
import 'platform/linux/desktop_integration.dart';
import 'platform/system_requirements.dart';
import 'platform/window_close.dart';
import 'platform/windows/win32_system.dart';
import 'platform/windows/windows_roots.dart';
Expand Down Expand Up @@ -41,6 +42,7 @@ import 'ui/uninstall/uninstalled_screen.dart';
import 'uninstall/uninstaller.dart';
import 'ui/toolchain/toolchain_setup_screen.dart';
import 'ui/studio_shell.dart';
import 'ui/system_requirements_screen.dart';
import 'widgets/nothing_pushed.dart';

Future<void> main() async {
Expand All @@ -58,6 +60,11 @@ Future<void> main() async {
final themeController = ThemeController();
await themeController.load();

// Whether this computer is up to running a project. Read before anything
// is shown, because a computer that falls short hears so first — ahead of
// first-run setup and its downloads.
final requirements = await SystemRequirementsController.create();

final toolchains = await ToolchainController.create();
final session = ProjectSession();
await session.loadRecents();
Expand Down Expand Up @@ -111,6 +118,7 @@ Future<void> main() async {
runApp(
AppStudioApp(
themeController: themeController,
requirements: requirements,
toolchains: toolchains,
session: session,
process: process,
Expand All @@ -130,6 +138,7 @@ class AppStudioApp extends StatefulWidget {
const AppStudioApp({
super.key,
required this.themeController,
required this.requirements,
required this.toolchains,
required this.session,
required this.process,
Expand All @@ -144,6 +153,10 @@ class AppStudioApp extends StatefulWidget {
});

final ThemeController themeController;

/// Whether the computer meets App Studio's requirements, and the launch
/// gate for one that does not.
final SystemRequirementsController requirements;
final ToolchainController toolchains;
final ProjectSession session;
final ServerpodProcessController process;
Expand Down Expand Up @@ -234,7 +247,7 @@ class _AppStudioAppState extends State<AppStudioApp> {
onSetAutomaticUpdates: (enabled) =>
unawaited(widget.updates.setAutomaticallyChecks(enabled)),
automaticUpdatesEnabled: () => widget.updates.state.automaticallyChecks,
canOpenProjects: () => !_needsToolchainSetup,
canOpenProjects: () => _canOpenProjects,
onCreateAndApplyMigrations: () => _withNavigatorContext(
(context) => createAndApplyMigrations(context, widget.process),
),
Expand All @@ -250,6 +263,7 @@ class _AppStudioAppState extends State<AppStudioApp> {
// re-sync when the last install lands.
_menuCanOpenProjects = !_needsToolchainSetup;
widget.toolchains.addListener(_onToolchainsChanged);
widget.requirements.addListener(_onRequirementsChanged);
_noticedProjectPath = widget.session.projectPath;
widget.session.addListener(_onSessionChanged);
_lifecycle = AppLifecycleListener(onExitRequested: _onExitRequested);
Expand All @@ -261,7 +275,7 @@ class _AppStudioAppState extends State<AppStudioApp> {
unawaited(_macMenu!.install());
// With setup done, the start page is the first screen: offer once it is
// up. Otherwise the end of setup offers (`_onToolchainsChanged`).
if (!_needsToolchainSetup) {
if (!_needsToolchainSetup && !_belowRequirements) {
WidgetsBinding.instance.addPostFrameCallback(
(_) => _offerAddToApplications(),
);
Expand Down Expand Up @@ -350,7 +364,7 @@ class _AppStudioAppState extends State<AppStudioApp> {
onSetAutomaticUpdates: (enabled) =>
unawaited(widget.updates.setAutomaticallyChecks(enabled)),
automaticUpdatesEnabled: () => widget.updates.state.automaticallyChecks,
canOpenProjects: () => !_needsToolchainSetup,
canOpenProjects: () => _canOpenProjects,
onCreateAndApplyMigrations: () => _withNavigatorContext(
(context) => createAndApplyMigrations(context, widget.process),
),
Expand All @@ -359,6 +373,7 @@ class _AppStudioAppState extends State<AppStudioApp> {
),
changes: Listenable.merge([
widget.updates,
widget.requirements,
widget.toolchains,
widget.previewMode,
widget.desktopIntegration,
Expand Down Expand Up @@ -399,14 +414,27 @@ class _AppStudioAppState extends State<AppStudioApp> {
// current for exactly that reason: a second pop here would take `home` with
// it and leave an empty navigator.
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) return;
// Behind the requirements question nothing is swapped: that screen
// stays until it is answered, and the clear would take its question.
if (!mounted || _belowRequirements) return;
_clearPushedRoutes();
// After the clear, or it would take the question with it.
if (canOpen) _offerAddToApplications();
});
_syncMenu();
}

/// "Try Anyway" on the requirements question: the gate lifts and setup or
/// the start page takes the screen, as if the app had just opened on it.
void _onRequirementsChanged() {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted || _belowRequirements) return;
_clearPushedRoutes();
if (!_needsToolchainSetup) _offerAddToApplications();
});
_syncMenu();
}

/// Linux, as an AppImage nobody added to the app menu: asks whether to,
/// once per run, over the start page and never among setup's dialogs.
///
Expand All @@ -428,6 +456,7 @@ class _AppStudioAppState extends State<AppStudioApp> {
bool _onStartPage() =>
mounted &&
widget.session.projectPath == null &&
!_belowRequirements &&
!_needsToolchainSetup &&
_uninstalled == null &&
nothingPushedOver(_navigatorKey);
Expand All @@ -445,6 +474,13 @@ class _AppStudioAppState extends State<AppStudioApp> {
/// [ToolchainController.needsSetup].
bool get _needsToolchainSetup => widget.toolchains.needsSetup;

/// The requirements question owns the window: the computer falls short and
/// the user has not yet chosen to go on.
bool get _belowRequirements => widget.requirements.blocksLaunch;

/// What File → Open, Open Recent and Settings follow: neither gate is up.
bool get _canOpenProjects => !_needsToolchainSetup && !_belowRequirements;

/// Opening a project auto-starts the server, so this is the moment the long
/// first compile begins — say so before it looks like a hang.
void _onSessionChanged() {
Expand Down Expand Up @@ -608,6 +644,7 @@ class _AppStudioAppState extends State<AppStudioApp> {
_lifecycle?.dispose();
widget.updates.removeListener(_syncMenu);
widget.toolchains.removeListener(_onToolchainsChanged);
widget.requirements.removeListener(_onRequirementsChanged);
widget.session.removeListener(_onSessionChanged);
_macMenu?.dispose();
// Same teardown Sparkle runs before it relaunches, so the two paths cannot
Expand All @@ -633,6 +670,7 @@ class _AppStudioAppState extends State<AppStudioApp> {
widget.previewMode.dispose();
widget.logView.dispose();
widget.desktopIntegration.dispose();
widget.requirements.dispose();
super.dispose();
}

Expand All @@ -641,6 +679,7 @@ class _AppStudioAppState extends State<AppStudioApp> {
return ListenableBuilder(
listenable: Listenable.merge([
widget.themeController,
widget.requirements,
widget.toolchains,
widget.previewMode,
]),
Expand Down Expand Up @@ -674,6 +713,11 @@ class _AppStudioAppState extends State<AppStudioApp> {
// moment; the shell would otherwise keep rendering panes for a
// session that is being dismantled underneath it.
null when _uninstalling => const UninstallingScreen(),
// First of the two gates: a computer that cannot run a project
// should not be taken through setup's downloads to find out.
null when _belowRequirements => SystemRequirementsScreen(
requirements: widget.requirements,
),
// Before the start page, not over it: there is nothing to create or
// open a project with yet.
null when _needsToolchainSetup => ToolchainSetupScreen(
Expand Down
93 changes: 93 additions & 0 deletions lib/platform/system_hardware.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import 'dart:async';
import 'dart:io';

import 'package:flutter/foundation.dart';

import 'host.dart';
import 'windows/win32_system.dart';
import 'windows/windows_system.dart';

/// What the computer has to run App Studio with, as far as it could be read.
/// A null is a reading that failed, never a zero.
@immutable
class SystemHardware {
const SystemHardware({this.memoryBytes, this.logicalCores});

/// The RAM fitted, or on Linux the RAM the kernel can use — a little less.
final int? memoryBytes;

/// Processor cores as the OS counts them: a core with two threads is two.
final int? logicalCores;
}

/// Runs a short system command. Replaceable so tests start no process.
typedef SystemCommand =
Future<ProcessResult> Function(String executable, List<String> arguments);

/// Reads the computer's memory and processor count.
///
/// Each OS has its own source for the memory, and none needs a tool that
/// might be missing: Windows is asked directly ([WindowsSystem]), macOS
/// through `sysctl`, which ships with it, and Linux from `/proc/meminfo`.
///
/// Never throws and never takes long: launch waits on it. A reading that
/// fails or stalls comes back null, and a null is not held against the
/// computer.
Future<SystemHardware> readSystemHardware({
HostPlatform? host,
WindowsSystem? windows,
SystemCommand? run,
String meminfoPath = '/proc/meminfo',
int? logicalCores,
Duration timeout = const Duration(seconds: 2),
}) async {
final platform = host ?? currentHost;
int? memory;
try {
memory = await _readMemory(
platform,
windows: windows,
run: run ?? (exe, args) => Process.run(exe, args),
meminfoPath: meminfoPath,
).timeout(timeout);
} catch (_) {
memory = null;
}
int? cores;
try {
cores = logicalCores ?? Platform.numberOfProcessors;
} catch (_) {
cores = null;
}
return SystemHardware(
memoryBytes: memory != null && memory > 0 ? memory : null,
logicalCores: cores != null && cores > 0 ? cores : null,
);
}

Future<int?> _readMemory(
HostPlatform host, {
required WindowsSystem? windows,
required SystemCommand run,
required String meminfoPath,
}) async {
if (host.isWindows) {
return (windows ?? Win32System()).installedMemoryBytes();
}
if (host.isMacOS) {
final result = await run('/usr/sbin/sysctl', ['-n', 'hw.memsize']);
return result.exitCode == 0 ? int.tryParse('${result.stdout}'.trim()) : null;
}
return parseMeminfoTotal(await File(meminfoPath).readAsString());
}

/// `MemTotal` from `/proc/meminfo`, in bytes: `MemTotal: 16303428 kB`.
int? parseMeminfoTotal(String meminfo) {
final match = RegExp(
r'^MemTotal:\s+(\d+)\s*kB',
multiLine: true,
caseSensitive: false,
).firstMatch(meminfo);
final kilobytes = match == null ? null : int.tryParse(match[1]!);
return kilobytes == null ? null : kilobytes * 1024;
}
Loading
Loading