Giter Club home page Giter Club logo

shortcut_menu_extender's Introduction

shortcut_menu_extender

pub version

This plugin allows Flutter apps to Extending global shortcut menus.


English | 简体中文


Platform Support

Linux macOS Windows
✔️

Quick Start

Installation

Add this to your package's pubspec.yaml file:

dependencies:
  shortcut_menu_extender: ^0.1.1

Usage

Windows

Change the file windows/runner/main.cpp as follows:

#include <flutter/dart_project.h>
#include <flutter/flutter_view_controller.h>
#include <windows.h>

#include "flutter_window.h"
#include "utils.h"

+#include <shortcut_menu_extender_windows/shortcut_menu_extender_windows_plugin_c_api.h>

int APIENTRY wWinMain(_In_ HINSTANCE instance,
                      _In_opt_ HINSTANCE prev,
                      _In_ wchar_t* command_line,
                      _In_ int show_command) {
+  HANDLE instance_mutex =
+      CreateMutex(NULL, TRUE, L"shortcut_menu_extender_example");
+  if (GetLastError() == ERROR_ALREADY_EXISTS &&
+      !ShouldHandleByShortcutMenuExtenderCommand()) {
+    HWND hwnd = ::FindWindow(L"FLUTTER_RUNNER_WIN32_WINDOW",
+                             L"shortcut_menu_extender_example");
+    if (hwnd != NULL && ShouldHandleByShortcutMenuExtender()) {
+      DispatchToShortcutMenuExtender(hwnd);
+    }
+    CloseHandle(instance_mutex);
+    return EXIT_SUCCESS;
+  }

  // Attach to console when present (e.g., 'flutter run') or create a
  // new console when running with a debugger.
  if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) {
    CreateAndAttachConsole();
  }

  // Initialize COM, so that it is available for use in the library and/or
  // plugins.
  ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);

  flutter::DartProject project(L"data");

  std::vector<std::string> command_line_arguments = GetCommandLineArguments();

  project.set_dart_entrypoint_arguments(std::move(command_line_arguments));

  FlutterWindow window(project);
  Win32Window::Point origin(10, 10);
  Win32Window::Size size(1280, 720);
  if (!window.Create(L"shortcut_menu_extender_example", origin, size)) {
    return EXIT_FAILURE;
  }
  window.SetQuitOnClose(true);

  ::MSG msg;
  while (::GetMessage(&msg, nullptr, 0, 0)) {
    ::TranslateMessage(&msg);
    ::DispatchMessage(&msg);
  }

  ::CoUninitialize();
  return EXIT_SUCCESS;
}
import 'package:shortcut_menu_extender/shortcut_menu_extender.dart';

void main() async {
  // Must add this line.
  WidgetsFlutterBinding.ensureInitialized();

  if (shortcutMenuExtenderCommand.runIfNeeded(args)) exit(0);

  runApp(MyApp());
}

Register/Unregister

shortcutMenuExtender.register(
  'MyFlutterApp',
  name: 'Open With MyFlutterApp',
  executable: Platform.resolvedExecutable,
  useDefaultIcon: true,
);

shortcutMenuExtender.unregister(
  'MyFlutterApp',
);

Listening events

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with ShortcutMenuListener {
  @override
  void initState() {
    shortcutMenuExtender.addListener(this);
    super.initState();
  }

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

  @override
  Widget build(BuildContext context) {
    // ...
  }

  @override
  void onShortcutMenuClicked(String key, String path) {
    print('onShortcutMenuClicked: $key, $path');
  }
}

Please see the example app of this plugin for a full example.

Sponsors

cmlanche
cmlanche

License

MIT

shortcut_menu_extender's People

Contributors

lijy91 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.