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

Gives Option in popUpContextMenu() to Foreground App or Not #58

Merged
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
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
Loading