Example #1
0
        /// <summary>
        /// Gets selected/copied/all text, passes it through provided callback and puts it back.
        /// </summary>
        /// <param name="proc">Callback converting text.</param>
        /// <param name="type">What text to replace: selected/copied/all/auto.</param>
        /// <param name="cancellation">Cancellation token.</param>
        public async Task ReplaceAsync(ReplaceTextCallback proc, TextReplacementType type, CancellationToken cancellation)
        {
            using (type == TextReplacementType.Clipboard ? null : Backup()) {
                switch (type)
                {
                case TextReplacementType.Auto:
                    await PressAsync(Keys.RControlKey, Keys.C);

                    if (!Clipboard.ContainsText())
                    {
                        await PressAsync(Keys.RControlKey, Keys.A);
                        await PressAsync(Keys.RControlKey, Keys.C);
                    }
                    break;

                case TextReplacementType.All:
                    await PressAsync(Keys.RControlKey, Keys.A);
                    await PressAsync(Keys.RControlKey, Keys.C);

                    break;

                case TextReplacementType.Selected:
                    await PressAsync(Keys.RControlKey, Keys.C);

                    break;
                }

                if (cancellation.IsCancellationRequested)
                {
                    return;
                }

#if DEBUG
                var s = Stopwatch.StartNew();
#endif

                try {
                    var replacement = await proc(GetClipboardText(), cancellation);

                    if (cancellation.IsCancellationRequested)
                    {
                        return;
                    }

                    SetClipboardText(replacement);
                } catch (Exception e) {
                    TypoLogging.NonFatalErrorNotify("Can’t execute script", null, e);
                }

#if DEBUG
                TypoLogging.Write($"{s.Elapsed.TotalMilliseconds:F1} ms");
#endif

                if (type != TextReplacementType.Clipboard)
                {
                    await PressAsync(Keys.RControlKey, Keys.V);
                }
            }
        }
Example #2
0
 /// <summary>
 /// Gets selected/copied/all text, passes it through provided callback and puts it back.
 /// </summary>
 /// <param name="proc">Callback converting text.</param>
 /// <param name="modifiers">Depending on modifier keys, type of text replacement will be picked.</param>
 /// <param name="cancellation">Cancellation token.</param>
 public Task ReplaceAsync(ReplaceTextCallback proc, Keys modifiers, CancellationToken cancellation)
 {
     return(ReplaceAsync(proc, GetTextReplaceType(modifiers), cancellation));
 }