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
3 changes: 3 additions & 0 deletions lib/ide/terminal/terminal_instance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import 'package:path/path.dart' as p;

import '../../settings/user_settings.dart';
import '../../theme/code_font.dart';
import '../../theme/linux_code_fonts.dart';
import 'pty.dart';
import 'shell_integration/shell_integration.dart';
import 'shell_integration/shell_integration_injection.dart';
Expand Down Expand Up @@ -210,6 +211,7 @@ class TerminalInstance extends ChangeNotifier {
);
_colorTheme.addListener(_updateTheme);
CodeFont.families.addListener(_updateFont);
LinuxCodeFonts.resolved.addListener(_updateFont);
source = TerminalCoreSource(terminal, decorationService: decorations);
clipboard = TerminalClipboard(
selection: selection,
Expand Down Expand Up @@ -830,6 +832,7 @@ class TerminalInstance extends ChangeNotifier {
_disposed = true;
_colorTheme.removeListener(_updateTheme);
CodeFont.families.removeListener(_updateFont);
LinuxCodeFonts.resolved.removeListener(_updateFont);
if (!_exited) _pty?.kill();
unawaited(_printing?.cancel());
unawaited(_output.close());
Expand Down
16 changes: 7 additions & 9 deletions lib/ide/terminal/terminal_render_theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -416,16 +416,14 @@ ITheme vscodeTerminalTheme([TerminalColorTheme? theme]) =>
(theme ?? terminalColorTheme.value).toXtermTheme();

/// The terminal font, as VS Code's `getFont` resolves it without a
/// `terminal.integrated.fontFamily`: the editor's families (this app's
/// editor draws in [CodeFont.families]), then `monospace` and, on macOS,
/// AppleBraille. On Windows the app's fallbacks ([AppFonts.windowsFallbacks],
/// ending in `monospace`) follow them as the theme puts them.
/// `terminal.integrated.fontFamily`: the editor's ([AppFonts.mono] and
/// [AppFonts.monoFallbacks], as this app's editor draws code: on Windows the
/// app's fallbacks, ending in `monospace`; on Linux the families as loaded),
/// then `monospace` where they have not it and, on macOS, AppleBraille.
String vscodeTerminalFontFamily() {
final families = [
...CodeFont.families.value,
if (AppPlatform.isWindows) ...AppFonts.windowsFallbacks else 'monospace',
if (AppPlatform.isMacOS) 'AppleBraille',
];
final families = [AppFonts.mono, ...AppFonts.monoFallbacks];
if (!families.contains('monospace')) families.add('monospace');
if (AppPlatform.isMacOS) families.add('AppleBraille');
return families.map((f) => f.contains(' ') ? "'$f'" : f).join(', ');
}

Expand Down
3 changes: 3 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ import 'telemetry/telemetry_service.dart';
import 'telemetry/telemetry_store.dart';
import 'theme/app_theme.dart';
import 'theme/code_font.dart';
import 'theme/linux_code_fonts.dart';
import 'theme/pixel_snapped_text_scaler.dart';
import 'theme/ui_zoom.dart';
import 'theme/window_blur.dart';
Expand Down Expand Up @@ -208,6 +209,7 @@ Future<void> main(List<String> arguments) async {
(key) => files.settings[key],
).timeout(const Duration(seconds: 1), onTimeout: () {});
followSettingsFiles(files);
await LinuxCodeFonts.start();
// The channel build's first start: set up before anything else shows.
await _beginChannelSetup(files);
_openChannelGate(files);
Expand Down Expand Up @@ -884,6 +886,7 @@ class _BaoCodeAppState extends State<BaoCodeApp>
/// windows' blur, which every surface over it is tinted for.
static final Listenable _codeFont = Listenable.merge([
CodeFont.families,
LinuxCodeFonts.resolved,
CodeFont.size,
CodeFont.ligatures,
WindowBlur.enabled,
Expand Down
53 changes: 25 additions & 28 deletions lib/settings/pages/appearance_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import '../../l10n/l10n.dart';
import '../../platform/event_log.dart' show appEvent;
import '../../theme/app_theme.dart';
import '../../theme/code_font.dart';
import '../../theme/linux_code_fonts.dart';
import '../../theme/window_blur.dart';
import '../../theme/workbench_theme.dart' show ThemeSettingDefaults;
import '../user_settings.dart';
Expand Down Expand Up @@ -109,17 +110,6 @@ class AppearanceSettingsPage extends StatelessWidget {
}),
);

/// The families the dropdown offers: each is put first, then the defaults.
static const _codeFontPresets = <String>[
'JetBrains Mono',
'Fira Code',
'Cascadia Code',
'Consolas',
'Menlo',
'Monaco',
'Courier New',
];

/// The step of [steps] nearest [value]: the slider's position.
static int _nearest<T extends num>(List<T> steps, num value) {
var nearest = 0;
Expand Down Expand Up @@ -403,6 +393,7 @@ class AppearanceSettingsPage extends StatelessWidget {
WindowBlur.enabled,
for (final role in PixelLoaderRole.values) ...[role.style, role.speed],
CodeFont.families,
LinuxCodeFonts.resolved,
CodeFont.size,
CodeFont.ligatures,
CodeFont.uiScale,
Expand Down Expand Up @@ -688,25 +679,30 @@ class AppearanceSettingsPage extends StatelessWidget {
semanticLabel: l10n.appearanceSettingsCodeFontLabel(
_familyName(context, families),
),
entries: () => ideMenuGroups([
[
IdeMenuAction(
l10n.appearanceSettingsCodeFontDefault,
checked: listEquals(
families,
CodeFont.defaultFamilies,
),
onSelected: () =>
_selectFamilies(CodeFont.defaultFamilies),
),
for (final name in _codeFontPresets)
entries: () {
// On Linux, what it offers loaded ahead of a pick.
unawaited(LinuxCodeFonts.preloadPresets());
return ideMenuGroups([
[
IdeMenuAction(
name,
checked: listEquals(families, _presetOf(name)),
onSelected: () => _selectFamilies(_presetOf(name)),
l10n.appearanceSettingsCodeFontDefault,
checked: listEquals(
families,
CodeFont.defaultFamilies,
),
onSelected: () =>
_selectFamilies(CodeFont.defaultFamilies),
),
],
]),
for (final name in CodeFont.familyPresets)
IdeMenuAction(
name,
checked: listEquals(families, _presetOf(name)),
onSelected: () =>
_selectFamilies(_presetOf(name)),
),
],
]);
},
),
),
// Not const: it must redraw as the font, size or ligatures
Expand Down Expand Up @@ -827,6 +823,7 @@ class _TerminalFontPreview extends StatelessWidget {
listenable: Listenable.merge([
terminalColorTheme,
CodeFont.families,
LinuxCodeFonts.resolved,
CodeFont.terminalSize,
]),
builder: (context, _) {
Expand Down
16 changes: 12 additions & 4 deletions lib/theme/app_theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import '../ide/terminal/terminal_colors.dart';
import '../platform/app_platform.dart';
import 'code_font.dart';
import 'linux_code_fonts.dart';
import 'window_blur.dart';
import 'workbench_theme.dart' hide ColorScheme;

Expand Down Expand Up @@ -253,14 +254,21 @@ abstract final class AppMetrics {

abstract final class AppFonts {
/// The font code, paths and commands are drawn in: the first of
/// [CodeFont.families], which the user may change.
static String get mono => CodeFont.families.value.first;
/// [CodeFont.families], which the user may change; on Linux the first
/// installed, as loaded ([LinuxCodeFonts.resolved]).
static String get mono =>
LinuxCodeFonts.resolved.value?.first ?? CodeFont.families.value.first;

/// What code falls back on, in order: the families after [mono], then on
/// Windows [windowsFallbacks]. A family not installed is passed over.
/// Windows [windowsFallbacks]. A family not installed is passed over. On
/// Linux the rest as loaded, [LinuxCodeFonts.fallbacks] among them, or
/// while they are not, by their names.
static List<String> get monoFallbacks => [
...CodeFont.families.value.skip(1),
...(LinuxCodeFonts.resolved.value?.skip(1) ??
CodeFont.families.value.skip(1)),
if (AppPlatform.isWindows) ...windowsFallbacks,
if (AppPlatform.isLinux && LinuxCodeFonts.resolved.value == null)
...LinuxCodeFonts.fallbacks,
];

/// A style for code drawn at [size], as the code's size is moved (see
Expand Down
12 changes: 12 additions & 0 deletions lib/theme/code_font.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ abstract final class CodeFont {
'Cascadia Mono',
];

/// The families the Appearance page's menu offers: each is put first,
/// then the defaults.
static const familyPresets = <String>[
'JetBrains Mono',
'Fira Code',
'Cascadia Code',
'Consolas',
'Menlo',
'Monaco',
'Courier New',
];

/// The size the editor draws code at on macOS. Code is drawn from it:
/// every other code size is moved by the same amount the user moves this
/// one (see [sized]).
Expand Down
135 changes: 135 additions & 0 deletions lib/theme/linux_code_fonts.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import 'dart:io';
import 'dart:ui' as ui;

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import '../platform/app_platform.dart';
import 'code_font.dart';

/// Code drawn on Linux in the families set ([CodeFont.families]), each
/// loaded from the files Fontconfig matches for it, under a name of the
/// app's own.
///
/// By their own names, Skia may draw them in another face under a CJK
/// locale (#1304): its Fontconfig lookup gathers the faces of every family
/// the configuration puts with the name (a CJK one, first, for `zh`), then
/// picks among them by style and the locale's language, not by family, and
/// the CJK face comes out, proportional at times. Skia keeps the family
/// since a4d18ba482 (flutter/flutter#195102); with a Flutter whose Skia has
/// it, this can go. `FC_LANG=en` works around it too, but for the whole app
/// and what it starts, and the language is not the app's to change.
abstract final class LinuxCodeFonts {
/// What code falls back on after the families set: monospaced ones most
/// Linux systems have, then whichever Fontconfig calls monospaced.
static const fallbacks = [
'DejaVu Sans Mono',
'Liberation Mono',
'Noto Sans Mono',
'monospace',
];

/// The window's channel (WindowControls'), whose Linux host answers
/// `codeFontFiles` (see my_application.cc).
static const _channel = MethodChannel('baocode/window');

/// Each family asked for: the name to draw it by (the one it was loaded
/// under; its own, installed where it cannot be loaded), or null, not
/// installed.
static final _faces = <String, Future<String?>>{};

static int _aliases = 0;

static final _resolved = ValueNotifier<List<String>?>(null);

/// The families code is drawn in, by the names to draw them by: those set,
/// then [fallbacks], the ones not installed left out. Null off Linux, and
/// while none is known.
static ValueListenable<List<String>?> get resolved => _resolved;

/// Loads the families set, before the first frame, and those set later.
static Future<void> start() async {
if (!AppPlatform.isLinux) return;
CodeFont.families.addListener(_refresh);
await _refresh();
}

/// Loads the families the Appearance page's menu offers, as it opens: a
/// font loaded has Flutter lay all its text out again, which picked from
/// the menu left the old glyphs on screen a long while.
static Future<void> preloadPresets() async {
if (!AppPlatform.isLinux) return;
await _load({...CodeFont.defaultFamilies, ...CodeFont.familyPresets});
}

static Future<void> _refresh() async {
final configured = CodeFont.families.value;
final families = {...configured, ...fallbacks};
await _load(families);
final faces = [for (final name in families) ?await _faces[name]];
// Set again meanwhile: that one's to tell.
if (!identical(configured, CodeFont.families.value)) return;
final next = faces.isEmpty ? null : faces;
if (!listEquals(_resolved.value, next)) _resolved.value = next;
}

/// Asks for the files of those of [families] not asked for yet, and loads
/// them. Asked again later where the host could not answer.
static Future<void> _load(Set<String> families) async {
final asking = [
for (final name in families)
if (!_faces.containsKey(name)) name,
];
if (asking.isNotEmpty) {
final Map<String, Object?>? answer;
try {
answer = await _channel.invokeMapMethod<String, Object?>(
'codeFontFiles',
asking,
);
} on PlatformException catch (error) {
debugPrint('Code fonts could not be resolved: $error');
return;
} on MissingPluginException {
// A host without the Linux runner's channel.
return;
}
for (final name in asking) {
final entry = answer?[name];
final (files, installed) = entry is Map
? (
[...?(entry['files'] as List?)?.cast<String>()],
entry['installed'] == true,
)
: (const <String>[], false);
_faces.putIfAbsent(name, () => _register(name, files, installed));
}
}
await Future.wait([for (final name in families) ?_faces[name]]);
}

/// The name to draw [family] by: its [files] loaded under one of the
/// app's own; where none could be and it is [installed] (in a font
/// collection but its first face), its own name, Skia's to find as before.
static Future<String?> _register(
String family,
List<String> files,
bool installed,
) async {
final alias = 'BaoCodeCodeFont${_aliases++}';
var loaded = false;
for (final path in files.toSet()) {
try {
await ui.loadFontFromList(
await File(path).readAsBytes(),
fontFamily: alias,
);
loaded = true;
} on FileSystemException {
// Removed since, or not readable.
}
}
if (loaded) return alias;
return installed ? family : null;
}
}
2 changes: 2 additions & 0 deletions linux/runner/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ add_definitions(-DAPPLICATION_ID="${APPLICATION_ID}")
# Add dependency libraries. Add any application-specific dependencies here.
target_link_libraries(${BINARY_NAME} PRIVATE flutter)
target_link_libraries(${BINARY_NAME} PRIVATE PkgConfig::GTK)
pkg_check_modules(FONTCONFIG REQUIRED IMPORTED_TARGET fontconfig)
target_link_libraries(${BINARY_NAME} PRIVATE PkgConfig::FONTCONFIG)

# WebKitGTK, for the channel build's login window (login_window.cc): only in
# the channel build (BAOCODE_CHANNEL=true in the environment, as
Expand Down
Loading