Exemplo n.º 1
0
    static async Task <IReadOnlyList <string> > Save(KeyRegister keyRegister, Tracker tracker, Settings settings)
    {
        if (!settings.IsValidate(out var errors))
        {
            return(errors);
        }

        var saveErrors = new List <string>();

        AddHotKey(keyRegister, settings.AcceptAllHotKey, KeyBindingIds.AcceptAll, tracker.AcceptAll, saveErrors);
        AddHotKey(keyRegister, settings.AcceptOpenHotKey, KeyBindingIds.AcceptOpen, tracker.AcceptOpen, saveErrors);

        if (saveErrors.Any())
        {
            return(saveErrors);
        }

        if (settings.RunAtStartup)
        {
            Startup.Add();
        }
        else
        {
            Startup.Remove();
        }

        await SettingsHelper.Write(settings);

        return(new List <string>());
    }
Exemplo n.º 2
0
    static void ReBindKeys(Settings settings, KeyRegister keyRegister, Tracker tracker)
    {
        var acceptAllHotKey = settings.AcceptAllHotKey;

        if (acceptAllHotKey != null)
        {
            keyRegister.TryAddBinding(
                KeyBindingIds.AcceptAll,
                acceptAllHotKey.Shift,
                acceptAllHotKey.Control,
                acceptAllHotKey.Alt,
                acceptAllHotKey.Key,
                tracker.AcceptAll);
        }

        var acceptOpenHotKey = settings.AcceptOpenHotKey;

        if (acceptOpenHotKey != null)
        {
            keyRegister.TryAddBinding(
                KeyBindingIds.AcceptAll,
                acceptOpenHotKey.Shift,
                acceptOpenHotKey.Control,
                acceptOpenHotKey.Alt,
                acceptOpenHotKey.Key,
                tracker.AcceptOpen);
        }
    }
    public static async Task Launch(KeyRegister keyRegister, Tracker tracker)
    {
        var settings = await SettingsHelper.Read();

        using OptionsForm form = new(settings, async newSettings => await Save(keyRegister, tracker, newSettings));
        form.ShowDialog();
    }
Exemplo n.º 4
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;
    }
Exemplo n.º 5
0
    public static async Task Launch(IMessageBox messageBox, KeyRegister keyRegister, Tracker tracker)
    {
        var settings = await SettingsHelper.Read();

        var form = new OptionsForm(
            messageBox,
            settings,
            async newSettings => await Save(keyRegister, tracker, newSettings));

        form.ShowDialog();
    }
Exemplo n.º 6
0
 void Awake()
 {
     if (Instance == null)
     {
         Instance = this;
         DontDestroyOnLoad(gameObject);
     }
     else
     {
         Destroy(gameObject);
     }
 }
Exemplo n.º 7
0
    static void AddHotKey(KeyRegister keyRegister, HotKey?hotKey, int id, Action action, List <string> saveErrors)
    {
        keyRegister.ClearBinding(id);
        if (hotKey == null)
        {
            return;
        }

        if (!keyRegister.TryAddBinding(id, hotKey.Shift, hotKey.Control, hotKey.Alt, hotKey.Key, action))
        {
            saveErrors.Add("Binding already registered");
        }
    }
Exemplo n.º 8
0
    public static async Task Launch(KeyRegister keyRegister, Tracker tracker)
    {
        if (instance != null)
        {
            instance.BringToFront();
            return;
        }

        var settings = await SettingsHelper.Read();

        using var form = new OptionsForm(settings, newSettings => Save(keyRegister, tracker, newSettings));
        instance       = form;
        form.Closed   += formOnClosed;
        form.ShowDialog();
    }