Skip to content

Commit

Permalink
popUpContextMenu() does not automatically foreground app, but leaves …
Browse files Browse the repository at this point in the history
…the option for that behavior - working on Windows
  • Loading branch information
EthanBarlow committed Oct 28, 2024
1 parent 6c4935b commit 5ea730e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 19 deletions.
29 changes: 24 additions & 5 deletions packages/tray_manager/example/lib/pages/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class HomePage extends StatefulWidget {
}

class _HomePageState extends State<HomePage> with TrayListener {
ValueNotifier<bool> shouldForegroundOnContextMenu = ValueNotifier(false);
String _iconType = _kIconTypeOriginal;
Menu? _menu;

Expand Down Expand Up @@ -251,10 +252,28 @@ class _HomePageState extends State<HomePage> with TrayListener {
},
),
const Divider(height: 0),
ListTile(
title: const Text('popUpContextMenu'),
onTap: () async {
await trayManager.popUpContextMenu();
ValueListenableBuilder(
valueListenable: shouldForegroundOnContextMenu,
builder: (context, bool bringToForeground, Widget? child) {
return ListTile(
title: const Text('popUpContextMenu'),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text('Should bring app to foreground'),
Switch(
value: bringToForeground,
onChanged: (value) {
shouldForegroundOnContextMenu.value = !bringToForeground;
},
),
],
),
onTap: () async {
await trayManager
.popUpContextMenu(shouldForegroundOnContextMenu.value);
},
);
},
),
const Divider(height: 0),
Expand Down Expand Up @@ -290,7 +309,7 @@ class _HomePageState extends State<HomePage> with TrayListener {
if (kDebugMode) {
print('onTrayIconMouseDown');
}
trayManager.popUpContextMenu();
trayManager.popUpContextMenu(shouldForegroundOnContextMenu.value);
}

@override
Expand Down
20 changes: 17 additions & 3 deletions packages/tray_manager/lib/src/tray_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,23 @@ class TrayManager {
await _channel.invokeMethod('setContextMenu', arguments);
}

/// Pops up the context menu of the tray icon.
Future<void> popUpContextMenu() async {
await _channel.invokeMethod('popUpContextMenu');
/// Pops up the context menu of the tray icon and can foreground the app if `true` is passed as an argument.
Future<void> popUpContextMenu([bool bringAppToForeground = false]) async {
if (bringAppToForeground) {
_popUpContextMenuAndForegroundApp();
} else {
_popUpContextMenuAndNotForegroundApp();
}
}

// Pops up the context menu of the tray icon and do not bring app to the foreground.
Future<void> _popUpContextMenuAndNotForegroundApp() async {
await _channel.invokeMethod('popUpContextMenuAndNotForegroundApp');
}

// Pops up the context menu of the tray icon and bring the app to the foreground.
Future<void> _popUpContextMenuAndForegroundApp() async {
await _channel.invokeMethod('popUpContextMenuAndForegroundApp');
}

/// The bounds of this tray icon.
Expand Down
28 changes: 17 additions & 11 deletions packages/tray_manager/windows/tray_manager_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ class TrayManagerPlugin : public flutter::Plugin {
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
void TrayManagerPlugin::PopUpContextMenu(
const flutter::MethodCall<flutter::EncodableValue>& method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result,
bool bringAppToFront);
void TrayManagerPlugin::GetBounds(
const flutter::MethodCall<flutter::EncodableValue>& method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
Expand Down Expand Up @@ -185,14 +186,12 @@ std::optional<LRESULT> TrayManagerPlugin::HandleWindowProc(HWND hWnd,
} else if (message == WM_MYMESSAGE) {
switch (lParam) {
case WM_LBUTTONUP:
channel->InvokeMethod(
"onTrayIconMouseDown",
std::make_unique<flutter::EncodableValue>());
channel->InvokeMethod("onTrayIconMouseDown",
std::make_unique<flutter::EncodableValue>());
break;
case WM_RBUTTONUP:
channel->InvokeMethod(
"onTrayIconRightMouseDown",
std::make_unique<flutter::EncodableValue>());
channel->InvokeMethod("onTrayIconRightMouseDown",
std::make_unique<flutter::EncodableValue>());
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
Expand Down Expand Up @@ -287,7 +286,8 @@ void TrayManagerPlugin::SetContextMenu(

void TrayManagerPlugin::PopUpContextMenu(
const flutter::MethodCall<flutter::EncodableValue>& method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result,
bool bringAppToFront) {
HWND hWnd = GetMainWindow();

double x, y;
Expand All @@ -303,7 +303,9 @@ void TrayManagerPlugin::PopUpContextMenu(
x = cursorPos.x;
y = cursorPos.y;

SetForegroundWindow(hWnd);
if (bringAppToFront) {
SetForegroundWindow(hWnd);
}
TrackPopupMenu(hMenu, TPM_BOTTOMALIGN | TPM_LEFTALIGN, static_cast<int>(x),
static_cast<int>(y), 0, hWnd, NULL);
result->Success(flutter::EncodableValue(true));
Expand Down Expand Up @@ -352,8 +354,12 @@ void TrayManagerPlugin::HandleMethodCall(
SetToolTip(method_call, std::move(result));
} else if (method_call.method_name().compare("setContextMenu") == 0) {
SetContextMenu(method_call, std::move(result));
} else if (method_call.method_name().compare("popUpContextMenu") == 0) {
PopUpContextMenu(method_call, std::move(result));
} else if (method_call.method_name().compare(
"popUpContextMenuAndNotForegroundApp") == 0) {
PopUpContextMenu(method_call, std::move(result), false);
} else if (method_call.method_name().compare(
"popUpContextMenuAndForegroundApp") == 0) {
PopUpContextMenu(method_call, std::move(result), true);
} else if (method_call.method_name().compare("getBounds") == 0) {
GetBounds(method_call, std::move(result));
} else {
Expand Down

0 comments on commit 5ea730e

Please sign in to comment.