arblant
firefox-vpn-client / app / lib / desktop / desktop_bridge.dart
desktop_bridge.dart130 lines · 3.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
import 'package:flutter/services.dart';

/// A tray menu entry; ids start at 1. An entry with [children] is a
/// submenu.
class TrayItem {
  const TrayItem(
    this.id,
    this.label, {
    this.enabled = true,
    this.checked = false,
    this.children = const [],
  }) : separator = false;

  const TrayItem.submenu(this.label, this.children, {this.enabled = true})
    : id = 0,
      checked = false,
      separator = false;

  const TrayItem.separator()
    : id = 0,
      label = '',
      enabled = false,
      checked = false,
      separator = true,
      children = const [];

  final int id;
  final String label;
  final bool enabled;
  final bool checked;
  final bool separator;
  final List<TrayItem> children;

  Map<String, Object> toMap() => {
    'id': id,
    'label': label,
    'enabled': enabled,
    'checked': checked,
    'separator': separator,
    if (children.isNotEmpty) 'children': [for (final c in children) c.toMap()],
  };

  /// Identifies what the menu shows, to skip redundant updates.
  String get signature => separator
      ? '-'
      : '$id:$label:$enabled:$checked(${children.map((c) => c.signature).join(',')})';
}

/// The native side of the window's close button and the tray icon
/// (windows/runner/desktop_bridge.cpp, linux/runner/desktop_bridge.cc, and
/// DesktopBridge in macos/Runner/MainFlutterWindow.swift; channel
/// "app/desktop").
class DesktopBridge {
  DesktopBridge({
    required this.onCloseRequested,
    required this.onTrayClicked,
    required this.onTrayMenu,
    required this.onShown,
  }) {
    _channel.setMethodCallHandler((call) async {
      switch (call.method) {
        case 'closeRequested':
          onCloseRequested();
        case 'trayClicked':
          onTrayClicked();
        case 'trayMenu':
          onTrayMenu(call.arguments as int);
        case 'shown':
          onShown();
      }
    });
  }

  static const _channel = MethodChannel('app/desktop');

  final void Function() onCloseRequested;
  final void Function() onTrayClicked;
  final void Function(int id) onTrayMenu;

  /// A second launch brought the window up.
  final void Function() onShown;

  Future<void> setTray({
    required String icon,
    required String tooltip,
    required List<TrayItem> items,
    bool rtl = false,
  }) => _channel.invokeMethod('setTray', {
    'icon': icon,
    'tooltip': tooltip,
    'rtl': rtl,
    'items': [for (final i in items) i.toMap()],
  });

  /// A notification from the tray icon (a Windows toast; elsewhere, the
  /// system's notifications).
  Future<void> notify(String title, String body) =>
      _channel.invokeMethod('notify', {'title': title, 'body': body});

  /// Starts [command] at sign-in (the current user's Run key; on Linux, an
  /// XDG autostart entry; on macOS, a LaunchAgent for this app), or not.
  Future<void> setAutostart({required bool enabled, required String command}) =>
      _channel.invokeMethod('setAutostart', {
        'enabled': enabled,
        'command': command,
      });

  /// Opens an http or https [url] in the default browser through the
  /// shell (Windows only; the runner refuses other schemes).
  static Future<void> openUrl(String url) =>
      _channel.invokeMethod('openUrl', url);

  /// The program file the user picks in the system's open dialog titled
  /// [title] (on Windows an .exe, of the kind [label] names), or null
  /// (Windows and Linux).
  static Future<String?> pickProgram({
    required String title,
    required String label,
  }) => _channel.invokeMethod<String>('pickProgram', {
    'title': title,
    'label': label,
  });

  Future<void> show() => _channel.invokeMethod('show');

  Future<void> hide() => _channel.invokeMethod('hide');

  /// Removes the tray icon and closes the window, which ends the app.
  Future<void> quit() => _channel.invokeMethod('quit');
}