Files
pipelines/app/lib/features/attention/notify/notification_bridge_io.dart
T
alan 8138cdc3d9 Linux desktop: optimized release build, installer, Linux gaps
Build: split Dart debug info out of libapp.so (13.96 -> 11.27 MB, symbols
kept in the cache) and fix icon tree shaking, which Flutter 3.44's Linux
CMake backend silently disables by quoting the define; the build image
patches its SDK copy (MaterialIcons 1.6 MB -> 25 KB). Impeller cannot be
enabled in a Linux release build and was slower in a profile-build bench.

Install: `./install --linux --install` installs the bundle into
<prefix>/lib/app-linux with com.pipelines.app.desktop (the GTK/Wayland
app id), its icon and a ~/.local/bin/pipelines-app link, through
scripts/install-linux-app.sh.

Linux gaps: notifications through the freedesktop daemon; HTML artifacts
explain the missing web view instead of throwing; no Share for files;
a GTK clipboard reader for pasting files and screenshots (Ctrl+V and
Omarchy's Super+V); GTK header bar only on GNOME; window icon.

Bench: native fixture frame log, wheel/mark steps and
tool/perf/linux_desktop_bench.py (private Xvfb); results in
app/notes/linux-desktop-optimized.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 12:54:18 +00:00

179 lines
5.7 KiB
Dart

import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'notification_bridge.dart';
import 'notification_models.dart';
/// System notifications from the native app: Android's notification shade (when
/// its background notification service is off), iOS's and macOS's
/// notification centre, and the Linux desktop's notification daemon (the
/// freedesktop D-Bus service: mako, dunst, GNOME Shell, Plasma). [close]
/// removes a delivered one by its tag. The permission is cached so
/// [environment] stays synchronous; it is refreshed when the bridge starts and after every request.
class NativeNotificationBridge implements NotificationBridge {
NativeNotificationBridge() {
_ready = _start();
}
final _plugin = FlutterLocalNotificationsPlugin();
final Map<int, void Function()> _taps = {};
/// The platform id each shown tag was posted with, so [close] can remove it.
final Map<String, int> _ids = {};
late final Future<void> _ready;
String _permission = 'default';
bool _started = false;
int _nextId = 1;
static const _channel = AndroidNotificationDetails(
'pipelines.attention',
'Runs and chats that need you',
channelDescription:
'A run waiting for a decision, a failed run, or a '
'chat question.',
importance: Importance.high,
priority: Priority.high,
);
static const _darwin = DarwinInitializationSettings(
requestAlertPermission: false,
requestBadgePermission: false,
requestSoundPermission: false,
);
/// The bundle's icon (installed by the Linux runner's CMake beside
/// `flutter_assets`), so a portable, uninstalled bundle has one too.
static final _linuxSettings = LinuxInitializationSettings(
defaultActionName: 'Open',
defaultIcon: FilePathLinuxIcon(
'${File(Platform.resolvedExecutable).parent.path}/data/app_icon.png',
),
);
/// The Linux app, by its target platform rather than the host's, so a test
/// on a Linux machine still sees the (Android) test platform.
static bool get _linux => defaultTargetPlatform == TargetPlatform.linux;
Future<void> _start() async {
try {
await _plugin.initialize(
settings: InitializationSettings(
android: AndroidInitializationSettings('@mipmap/ic_launcher'),
iOS: _darwin,
macOS: _darwin,
linux: _linuxSettings,
),
onDidReceiveNotificationResponse: (response) {
final id = response.id;
if (id == null) return;
_ids.removeWhere((_, posted) => posted == id);
_taps.remove(id)?.call();
},
);
_started = true;
await _refresh();
} catch (_) {
_started = false;
}
}
Future<void> _refresh() async {
if (_linux) {
// A notification daemon asks no permission.
_permission = 'granted';
} else if (Platform.isAndroid) {
final enabled = await _plugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin
>()
?.areNotificationsEnabled();
_permission = enabled == true ? 'granted' : 'default';
} else if (Platform.isMacOS) {
final options = await _plugin
.resolvePlatformSpecificImplementation<
MacOSFlutterLocalNotificationsPlugin
>()
?.checkPermissions();
_permission = options?.isEnabled == true ? 'granted' : 'default';
}
}
@override
NotificationEnvironment environment() => NotificationEnvironment(
supported:
Platform.isAndroid || Platform.isIOS || Platform.isMacOS || _linux,
// A native app needs no secure origin.
secureContext: true,
permission: _permission,
);
@override
Future<String> requestPermission() async {
await _ready;
if (!_started) return 'default';
bool? granted;
if (Platform.isAndroid) {
granted = await _plugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin
>()
?.requestNotificationsPermission();
} else if (Platform.isIOS) {
granted = await _plugin
.resolvePlatformSpecificImplementation<
IOSFlutterLocalNotificationsPlugin
>()
?.requestPermissions(alert: true, badge: true, sound: true);
} else if (Platform.isMacOS) {
granted = await _plugin
.resolvePlatformSpecificImplementation<
MacOSFlutterLocalNotificationsPlugin
>()
?.requestPermissions(alert: true, badge: true, sound: true);
}
_permission = granted == true
? 'granted'
: granted == false
? 'denied'
: 'default';
return _permission;
}
@override
bool show(NotificationMessage message, {void Function()? onTap}) {
if (!_started || _permission != 'granted') return false;
// The same tag reuses its platform id, so the new one replaces the old.
final id = _ids[message.tag] ??= _nextId++;
if (onTap != null) {
_taps[id] = onTap;
} else {
_taps.remove(id);
}
_plugin.show(
id: id,
title: message.title,
body: message.body,
notificationDetails: const NotificationDetails(
android: _channel,
iOS: DarwinNotificationDetails(),
macOS: DarwinNotificationDetails(),
),
payload: message.tag,
);
return true;
}
@override
void close(String tag) {
final id = _ids.remove(tag);
if (id == null || !_started) return;
_taps.remove(id);
// Removes the delivered notification (Android shade, Darwin centre).
_plugin.cancel(id: id).catchError((Object _) {});
}
}
NotificationBridge createNotificationBridge() => NativeNotificationBridge();