Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into atavism/ios
Browse files Browse the repository at this point in the history
  • Loading branch information
atavism committed May 26, 2024
2 parents d309c99 + 193e842 commit 0d1a752
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 90 deletions.
51 changes: 1 addition & 50 deletions lib/common/common_desktop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,56 +12,7 @@ export 'dart:ffi'; // For FFI
export 'package:ffi/ffi.dart';
export 'package:ffi/src/utf8.dart';
export 'package:lantern/ffi.dart';
export 'package:lantern/common/tray_handler.dart';
export 'package:lantern/common/ui/websocket.dart';
export 'package:web_socket_channel/io.dart';
export 'package:web_socket_channel/web_socket_channel.dart';

// Include resources here just for desktop compatibility or use

String systemTrayIcon(bool connected) {
if (connected) {
return Platform.isWindows
? 'assets/images/lantern_connected_32.ico'
: 'assets/images/lantern_connected_32.png';
}
return Platform.isWindows
? 'assets/images/lantern_disconnected_32.ico'
: 'assets/images/lantern_disconnected_32.png';
}

// void setSelectedTab(BuildContext context, String name) {
// final tab = name.toNativeUtf8();
// final currentTab = ffiSelectedTab().toDartString();
// setSelectTab(tab);
// // when the user clicks on the active tab again, do nothing
// if (currentTab == name) return;
// // context.pushRoute(Home());
// }

Future<void> setupMenu(bool isConnected) async {
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,
),
MenuItem.separator(),
MenuItem(
key: 'show_window',
label: 'show'.i18n,
),
MenuItem.separator(),
MenuItem(
key: 'exit',
label: 'exit'.i18n,
),
],
);
await trayManager.setContextMenu(menu);
await trayManager.setIcon(systemTrayIcon(isConnected));
}
97 changes: 97 additions & 0 deletions lib/common/tray_handler.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import 'dart:ui';

import 'package:lantern/common/common.dart';
import 'package:lantern/common/common_desktop.dart';
import 'package:tray_manager/tray_manager.dart';
import 'package:window_manager/window_manager.dart';

String systemTrayIcon(bool connected) {
if (connected) {
return Platform.isWindows
? 'assets/images/lantern_connected_32.ico'
: 'assets/images/lantern_connected_32.png';
}
return Platform.isWindows
? 'assets/images/lantern_disconnected_32.ico'
: 'assets/images/lantern_disconnected_32.png';
}


class TrayHandler with TrayListener {
factory TrayHandler() => _getInstance();

static TrayHandler get instance => _getInstance();
static TrayHandler? _instance;

static TrayHandler _getInstance() {
_instance ??= TrayHandler._internal();
return _instance!;
}

TrayHandler._internal() {
if (isDesktop()) {
setupTray(false);
}
}

Future<void> setupTray(bool isConnected) async {
await trayManager.setIcon(systemTrayIcon(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,
),
MenuItem.separator(),
MenuItem(
key: 'show_window',
label: 'show'.i18n,
),
MenuItem.separator(),
MenuItem(
key: 'exit',
label: 'exit'.i18n,
),
],
);
await trayManager.setContextMenu(menu);
}

@override
void onTrayMenuItemClick(MenuItem menuItem) {
switch (menuItem.key) {
case 'show':
windowManager.focus();
windowManager.setSkipTaskbar(false);
case 'exit':
windowManager.destroy();
ffiExit();
case 'status':
final status = ffiVpnStatus().toDartString();
bool isConnected = status == "connected";
if (isConnected) {
sysProxyOff();
setupTray(false);
} else {
sysProxyOn();
setupTray(true);
}
}
}

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

@override
void onTrayIconRightMouseDown() {
trayManager.popUpContextMenu();
}
}
36 changes: 2 additions & 34 deletions lib/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,39 +101,7 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
}

void setupTrayManager() async {
trayManager.addListener(this);
await setupMenu(false);
}

@override
void onTrayIconMouseDown() {
windowManager.show();
trayManager.popUpContextMenu();
}

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

@override
void onTrayMenuItemClick(MenuItem menuItem) async {
switch (menuItem.key) {
case 'show':
windowManager.focus();
case 'exit':
ffiExit();
case 'status':
final status = ffiVpnStatus().toDartString();
bool isConnected = status == "connected";
if (isConnected) {
sysProxyOff();
await setupMenu(false);
} else {
sysProxyOn();
await setupMenu(true);
}
}
trayManager.addListener(TrayHandler.instance);
}

@override
Expand Down Expand Up @@ -185,7 +153,7 @@ class _HomePageState extends State<HomePage> with TrayListener, WindowListener {
@override
void dispose() {
if (isDesktop()) {
trayManager.removeListener(this);
trayManager.removeListener(TrayHandler.instance);
windowManager.removeListener(this);
}
if (_cancelEventSubscription != null) {
Expand Down
4 changes: 2 additions & 2 deletions lib/vpn/vpn_switch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ class _VPNSwitchState extends State<VPNSwitch> {
bool isConnected = vpnStatus == 'connected';
if (isConnected) {
sysProxyOff();
await setupMenu(false);
await TrayHandler.instance.setupTray(false);
} else {
sysProxyOn();
await setupMenu(true);
await TrayHandler.instance.setupTray(true);
}
}

Expand Down
8 changes: 4 additions & 4 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1670,10 +1670,10 @@ packages:
dependency: "direct main"
description:
name: tray_manager
sha256: e0ac9a88b2700f366b8629b97e8663b6ef450a2f169560a685dc167bfe9c9c29
sha256: c9a63fd88bd3546287a7eb8ccc978d707eef82c775397af17dda3a4f4c039e64
url: "https://pub.dev"
source: hosted
version: "0.2.2"
version: "0.2.3"
typed_data:
dependency: transitive
description:
Expand Down Expand Up @@ -1982,10 +1982,10 @@ packages:
dependency: "direct main"
description:
name: window_manager
sha256: b3c895bdf936c77b83c5254bec2e6b3f066710c1f89c38b20b8acc382b525494
sha256: "8699323b30da4cdbe2aa2e7c9de567a6abd8a97d9a5c850a3c86dcd0b34bbfbf"
url: "https://pub.dev"
source: hosted
version: "0.3.8"
version: "0.3.9"
xdg_directories:
dependency: transitive
description:
Expand Down

0 comments on commit 0d1a752

Please sign in to comment.