Exemplo n.º 1
0
    static async Task Main()
    {
        Logging.Init();

        var settings = await GetSettings();

        if (settings == null)
        {
            return;
        }
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        var tokenSource  = new CancellationTokenSource();
        var cancellation = tokenSource.Token;

        using var mutex = new Mutex(true, "DiffEngine", out var createdNew);
        if (!createdNew)
        {
            Log.Information("Mutex already exists. Exiting.");
            return;
        }

        using var icon = new NotifyIcon
              {
                  Icon    = Images.Default,
                  Visible = true,
                  Text    = "DiffEngine"
              };

        var showMenu = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic) !;

        icon.MouseUp += (sender, args) =>
        {
            if (args.Button == MouseButtons.Left)
            {
                showMenu.Invoke(icon, null);
            }
        };

        await using var tracker = new Tracker(
                        active: () => icon.Icon   = Images.Active,
                        inactive: () => icon.Icon = Images.Default);

        using var task = StartServer(tracker, cancellation);

        using var keyRegister = new KeyRegister(icon.Handle());
        ReBindKeys(settings, keyRegister, tracker);

        icon.ContextMenuStrip = MenuBuilder.Build(
            Application.Exit,
            async() => await OptionsFormLauncher.Launch(keyRegister, tracker),
            tracker);

        Application.Run();
        tokenSource.Cancel();
        await task;
    }