Skip to content
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

Implement TrayContainer and refactor LanternApp to use it #1210

Merged
merged 3 commits into from
Oct 10, 2024
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
93 changes: 50 additions & 43 deletions lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:internet_connection_checker_plus/internet_connection_checker_plu
import 'package:lantern/core/router/router.dart';
import 'package:lantern/core/widgtes/custom_bottom_bar.dart';
import 'package:lantern/features/messaging/messaging.dart';
import 'package:lantern/features/tray/tray_container.dart';
import 'package:lantern/features/vpn/vpn_notifier.dart';
import 'package:lantern/features/window/window_container.dart';

Expand Down Expand Up @@ -114,6 +115,54 @@ class _LanternAppState extends State<LanternApp>
}
}

Widget _buildMaterialApp(BuildContext context, String lang) {
final currentLocal = View.of(context).platformDispatcher.locale;
final app = MaterialApp.router(
locale: currentLocale(lang),
debugShowCheckedModeBanner: false,
theme: ThemeData(
useMaterial3: false,
fontFamily: _getLocaleBasedFont(currentLocal),
brightness: Brightness.light,
primarySwatch: Colors.grey,
appBarTheme: const AppBarTheme(
systemOverlayStyle: SystemUiOverlayStyle.dark,
),
colorScheme: ColorScheme.fromSwatch().copyWith(secondary: Colors.black),
),
title: 'app_name'.i18n,
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
routerConfig: globalRouter.config(
deepLinkBuilder: navigateToDeepLink,
),
supportedLocales: const [
Locale('ar', 'EG'),
Locale('fr', 'FR'),
Locale('en', 'US'),
Locale('fa', 'IR'),
Locale('th', 'TH'),
Locale('ms', 'MY'),
Locale('ru', 'RU'),
Locale('ur', 'IN'),
Locale('zh', 'CN'),
Locale('zh', 'HK'),
Locale('es', 'ES'),
Locale('es', 'CU'),
Locale('tr', 'TR'),
Locale('vi', 'VN'),
Locale('my', 'MM'),
],
);
if (isDesktop()) {
return WindowContainer(TrayContainer(app));
}
return app;
}

@override
Widget build(BuildContext context) {
final currentLocal = View.of(context).platformDispatcher.locale;
Expand Down Expand Up @@ -143,49 +192,7 @@ class _LanternAppState extends State<LanternApp>
child: I18n(
initialLocale: currentLocale(lang),
child: ScaffoldMessenger(
child: WindowContainer(
MaterialApp.router(
locale: currentLocale(lang),
debugShowCheckedModeBanner: false,
theme: ThemeData(
useMaterial3: false,
fontFamily: _getLocaleBasedFont(currentLocal),
brightness: Brightness.light,
primarySwatch: Colors.grey,
appBarTheme: const AppBarTheme(
systemOverlayStyle: SystemUiOverlayStyle.dark,
),
colorScheme: ColorScheme.fromSwatch()
.copyWith(secondary: Colors.black),
),
title: 'app_name'.i18n,
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
routerConfig: globalRouter.config(
deepLinkBuilder: navigateToDeepLink,
),
supportedLocales: const [
Locale('ar', 'EG'),
Locale('fr', 'FR'),
Locale('en', 'US'),
Locale('fa', 'IR'),
Locale('th', 'TH'),
Locale('ms', 'MY'),
Locale('ru', 'RU'),
Locale('ur', 'IN'),
Locale('zh', 'CN'),
Locale('zh', 'HK'),
Locale('es', 'ES'),
Locale('es', 'CU'),
Locale('tr', 'TR'),
Locale('vi', 'VN'),
Locale('my', 'MM'),
],
),
),
child: _buildMaterialApp(context, lang),
),
),
);
Expand Down
75 changes: 2 additions & 73 deletions lib/features/home/home.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import 'dart:ui' as ui;

import 'package:lantern/core/utils/common.dart';
import 'package:lantern/core/utils/common_desktop.dart';
import 'package:lantern/core/widgtes/custom_bottom_bar.dart';
import 'package:lantern/features/account/account_tab.dart';
import 'package:lantern/features/account/developer_settings.dart';
Expand All @@ -12,11 +9,8 @@ import 'package:lantern/features/messaging/protos_flutteronly/messaging.pb.dart'
import 'package:lantern/features/replica/replica_tab.dart';
import 'package:lantern/features/vpn/try_lantern_chat.dart';
import 'package:lantern/features/vpn/vpn.dart';
import 'package:lantern/features/vpn/vpn_notifier.dart';
import 'package:lantern/features/vpn/vpn_tab.dart';
import 'package:logger/logger.dart';
import 'package:tray_manager/tray_manager.dart';
import 'package:window_manager/window_manager.dart';

import '../messaging/messaging_model.dart';

Expand All @@ -28,25 +22,23 @@ class HomePage extends StatefulWidget {
_HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with TrayListener {
class _HomePageState extends State<HomePage> {
Function()? _cancelEventSubscription;
Function userNew = once<void>();

@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
_startupSequence();
});
super.initState();
}

void _startupSequence() {
if (isMobile()) {
channelListener();
return;
}
// This is a desktop device
_setupTrayManager();
}

void channelListener() {
Expand Down Expand Up @@ -138,75 +130,12 @@ class _HomePageState extends State<HomePage> with TrayListener {

@override
void dispose() {
if (isDesktop()) {
trayManager.removeListener(this);
}
if (_cancelEventSubscription != null) {
_cancelEventSubscription!();
}
super.dispose();
}

/// system tray methods
void _setupTrayManager() async {
trayManager.addListener(this);
final vpnNotifier = context.read<VPNChangeNotifier>();
await _updateTrayMenu();
vpnNotifier.vpnStatus.addListener(_updateTrayMenu);
}

/// this method updates the tray menu based on the current VPN status
Future<void> _updateTrayMenu() async {
final vpnNotifier = context.read<VPNChangeNotifier>();
final isConnected = vpnNotifier.isConnected();
await trayManager.setIcon(getSystemTrayIconPath(isConnected));
Menu menu = Menu(
items: [
MenuItem(
key: 'status',
disabled: true,
label: isConnected ? 'status_on'.i18n : 'status_off'.i18n,
),
MenuItem(
key: 'status',
label: isConnected ? 'disconnect'.i18n : 'connect'.i18n,
onClick: (item) => vpnNotifier.toggleConnection(),
),
MenuItem.separator(),
MenuItem(
key: 'show_window',
label: 'show'.i18n,
onClick: (item) {
windowManager.focus();
windowManager.setSkipTaskbar(false);
}),
MenuItem.separator(),
MenuItem(
key: 'exit',
label: 'exit'.i18n,
onClick: (item) async {
LanternFFI.exit();
await trayManager.destroy();
await windowManager.destroy();
exit(0);
},
),
],
);
await trayManager.setContextMenu(menu);
}

@override
Future<void> onTrayIconMouseDown() async {
windowManager.show();
trayManager.popUpContextMenu();
}

@override
void onTrayIconRightMouseDown() {
trayManager.popUpContextMenu();
}

@override
Widget build(BuildContext context) {
final tabModel = context.watch<BottomBarChangeNotifier>();
Expand Down
92 changes: 92 additions & 0 deletions lib/features/tray/tray_container.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import 'package:lantern/core/utils/common.dart';
import 'package:lantern/core/utils/common_desktop.dart';
import 'package:lantern/features/vpn/vpn_notifier.dart';
import 'package:tray_manager/tray_manager.dart';
import 'package:window_manager/window_manager.dart';

class TrayContainer extends StatefulWidget {
const TrayContainer(this.child, {super.key});

final Widget child;

@override
State<TrayContainer> createState() => _TrayContainerState();
}

class _TrayContainerState extends State<TrayContainer> with TrayListener {
@override
void initState() {
super.initState();
_initializeTray();
}

@override
void dispose() {
trayManager.removeListener(this);
super.dispose();
}

@override
Future<void> onTrayIconMouseDown() async {
windowManager.show();
trayManager.popUpContextMenu();
}

@override
void onTrayIconRightMouseDown() {
trayManager.popUpContextMenu();
}

/// system tray methods
Future<void> _initializeTray() async {
trayManager.addListener(this);
final vpnNotifier = context.read<VPNChangeNotifier>();
await _updateTrayMenu();
vpnNotifier.vpnStatus.addListener(_updateTrayMenu);
}

Future<void> _updateTrayMenu() async {
final vpnNotifier = context.read<VPNChangeNotifier>();
final isConnected = vpnNotifier.isConnected();
await trayManager.setIcon(getSystemTrayIconPath(isConnected));
Menu menu = Menu(
items: [
MenuItem(
key: 'status',
disabled: true,
label: isConnected ? 'status_on'.i18n : 'status_off'.i18n,
),
MenuItem(
key: 'status',
label: isConnected ? 'disconnect'.i18n : 'connect'.i18n,
onClick: (item) => vpnNotifier.toggleConnection(),
),
MenuItem.separator(),
MenuItem(
key: 'show_window',
label: 'show'.i18n,
onClick: (item) {
windowManager.focus();
windowManager.setSkipTaskbar(false);
}),
MenuItem.separator(),
MenuItem(
key: 'exit',
label: 'exit'.i18n,
onClick: (item) async {
LanternFFI.exit();
await trayManager.destroy();
await windowManager.destroy();
exit(0);
},
),
],
);
await trayManager.setContextMenu(menu);
}

@override
Widget build(BuildContext context) {
return widget.child;
}
}
Loading