Files
FlClash/lib/common/tray.dart
T
chen08209 aaf934c2d1 refactor(app): rebuild core client, providers, models, database, managers, and window handling
Rework the application layer end to end: core IPC client, providers,
models, database, managers, and shared utilities, with riverpod 3.4.2
and freezed 4.0.1. Keep a rejected profile selected, push the empty
config to Core when a profile fails to build, and fall back to the
default activity icon for package icons.

Add opt-in proxy authentication that injects credentials into the
generated profile and the runtime config, force-clears profile-provided
skip-auth-prefixes so loopback stays covered, sends the credentials with
the app's own proxied requests, and withholds the Android VPN system
proxy while credentials are set. HelperCoreLease treats an unreachable
Helper as confirmation once the OS reports the Core pid gone, and the
lifecycle reconcile retries a parked stop, so the Core comes back after
the Helper service dies. Launch arguments seed LinkManager so a clash://
link raises a silently started window, the access control app list is
fetched on every page entry, the desktop header matches a standard title
bar height, and the tray icons ship as resolution-aware assets scoped per
platform. The version moves to 0.8.97+2026090101.

Rule.parse mirrors mihomo's ParseRulePayload: the type is
case-insensitive, MATCH rules keep no content field, a two-field rule is
payload without a target except for comma-payload types whose lone field
is the target, params are matched whole, and the wildcard and
REMATCH-NAME types the core accepts get their own actions instead of
collapsing into DOMAIN. Window routes show, hide and toggle through one
serialized queue and defers the macOS accessory activation switch until
a second after the last regular switch, so hotkey bursts no longer leave
stray Dock icons. The hotkey manager registers through rust_api instead
of hotkey_manager, the window header restyles the caption buttons after
the native Windows title bar, and the proxies tab keeps its tab
controller alive while the empty state animates in. Bootstrap reads the
Android dynamic palette from the plugin channel without the deprecated
CorePalette, dynamic_color moves to its material_ui release,
navigator_resizable 3.1.0 fixes an assertion when popping
mid-transition, reorderable_grid goes, and the SDK floor rises to 3.10.

Changelog: Rework the app layer and window handling, and add proxy authentication
2026-09-10 12:43:00 +08:00

215 lines
5.7 KiB
Dart

import 'package:fl_clash/enum/enum.dart';
import 'package:fl_clash/models/models.dart';
import 'package:fl_clash/providers/providers.dart';
import 'package:flutter/foundation.dart' show visibleForTesting;
import 'package:flutter/services.dart';
import 'package:tray/tray.dart';
import 'app_localizations.dart';
import 'l10n_labels.dart';
import 'app_ports.dart';
import 'constant.dart';
import 'provider_reader.dart';
import 'system.dart';
import 'window.dart';
class AppTray implements TrayPort {
static AppTray? _instance;
final bool isMacOS;
final bool isWindows;
bool _isShutDown = false;
AppTray._internal({required this.isMacOS, required this.isWindows});
factory AppTray() {
_instance ??= AppTray._internal(
isMacOS: system.isMacOS,
isWindows: system.isWindows,
);
return _instance!;
}
@visibleForTesting
factory AppTray.forPlatform({
required bool isMacOS,
required bool isWindows,
}) {
return AppTray._internal(isMacOS: isMacOS, isWindows: isWindows);
}
String get _trayIconSuffix {
return isWindows ? 'ico' : 'png';
}
String get _trayIconDir {
return isWindows ? 'assets/images/tray/windows' : 'assets/images/tray/unix';
}
String getTrayIcon({required bool isStart, required bool tunEnable}) {
final status = switch ((isMacOS || !isStart, tunEnable)) {
(true, _) => 1,
(false, false) => 2,
(false, true) => 3,
};
return '$_trayIconDir/status_$status.$_trayIconSuffix';
}
@override
Future<void> shutdown() async {
_isShutDown = true;
await Tray.instance.hide();
}
@override
Future<void> update({
required TrayState trayState,
required Traffic traffic,
required ProviderReader read,
}) async {
if (_isShutDown) {
return;
}
await Tray.instance.show(
TraySpec(
icon: TrayIcon.asset(
getTrayIcon(
isStart: trayState.isStart,
tunEnable: trayState.tunEnable,
),
isTemplate: isMacOS,
),
toolTip: appName,
menu: _buildMenu(trayState: trayState, read: read),
),
);
await updateTitle(showTrayTitle: trayState.showTrayTitle, traffic: traffic);
}
Future<void> updateTitle({
required bool showTrayTitle,
required Traffic traffic,
}) async {
if (_isShutDown || !isMacOS) {
return;
}
await Tray.instance.setTitle(showTrayTitle ? traffic.trayTitle : '');
}
List<TrayMenuItem> _buildMenu({
required TrayState trayState,
required ProviderReader read,
}) {
final commonAction = read(commonActionProvider.notifier);
final systemAction = read(systemActionProvider.notifier);
final setupAction = read(setupActionProvider.notifier);
final appLocalizations = currentAppLocalizations;
return [
TrayMenuAction(
label: appLocalizations.show,
onSelected: () {
window?.show();
},
),
TrayMenuCheckbox(
label: trayState.isStart
? appLocalizations.stop
: appLocalizations.start,
checked: false,
onSelected: commonAction.toggleRunning,
),
if (isMacOS)
TrayMenuCheckbox(
label: appLocalizations.speedStatistics,
checked: trayState.showTrayTitle,
onSelected: commonAction.updateSpeedStatistics,
),
const TrayMenuSeparator(),
for (final mode in Mode.values)
TrayMenuCheckbox(
label: mode.label,
checked: mode == trayState.mode,
onSelected: () {
setupAction.changeMode(mode);
},
),
const TrayMenuSeparator(),
if (isMacOS) ..._buildGroupMenu(trayState: trayState, read: read),
if (trayState.isStart) ...[
TrayMenuCheckbox(
label: appLocalizations.tun,
checked: trayState.tunEnable,
onSelected: systemAction.updateTun,
),
TrayMenuCheckbox(
label: appLocalizations.systemProxy,
checked: trayState.systemProxy,
onSelected: systemAction.updateSystemProxy,
),
const TrayMenuSeparator(),
],
TrayMenuCheckbox(
label: appLocalizations.autoLaunch,
checked: trayState.autoLaunch,
onSelected: systemAction.updateAutoLaunch,
),
TrayMenuAction(
label: appLocalizations.copyEnvVar,
onSelected: () {
_copyEnv(trayState.port);
},
),
const TrayMenuSeparator(),
TrayMenuAction(
label: appLocalizations.exit,
onSelected: () {
systemAction.handleExit();
},
),
];
}
List<TrayMenuItem> _buildGroupMenu({
required TrayState trayState,
required ProviderReader read,
}) {
if (trayState.groups.isEmpty) {
return const [];
}
return [
for (final group in trayState.groups)
TrayMenuSubmenu(
label: group.name,
items: [
for (final proxy in group.all)
TrayMenuCheckbox(
label: proxy.name,
checked:
read(selectedProxyNameProvider(group.name)) == proxy.name,
onSelected: () {
read(
proxiesActionProvider.notifier,
).changeProxy(groupName: group.name, proxyName: proxy.name);
},
),
],
),
const TrayMenuSeparator(),
];
}
Future<void> _copyEnv(int port) async {
final url = 'http://127.0.0.1:$port';
final cmdline = isWindows
? 'set \$env:all_proxy=$url'
: 'export all_proxy=$url';
await Clipboard.setData(ClipboardData(text: cmdline));
}
}
final appTray = system.isDesktop ? AppTray() : null;