<commit_before>﻿using CefSharp;
using System.Windows.Forms;
using TweetDck.Core.Controls;
using TweetDck.Core.Utils;

namespace TweetDck.Core.Handling{
    class ContextMenuBrowser : ContextMenuBase{
        private const int MenuGlobal = 26600;
        private const int MenuMute = 26601;
        private const int MenuSettings = 26602;
        private const int MenuPlugins = 26003;
        private const int MenuAbout = 26604;
        
        private const int MenuOpenTweetUrl = 26610;
        private const int MenuCopyTweetUrl = 26611;
        private const int MenuOpenQuotedTweetUrl = 26612;
        private const int MenuCopyQuotedTweetUrl = 26613;

        private readonly FormBrowser form;

        public ContextMenuBrowser(FormBrowser form){
            this.form = form;
        }

        public override void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model){
            model.Remove(CefMenuCommand.Back);
            model.Remove(CefMenuCommand.Forward);
            model.Remove(CefMenuCommand.Print);
            model.Remove(CefMenuCommand.ViewSource);
            RemoveSeparatorIfLast(model);

            base.OnBeforeContextMenu(browserControl, browser, frame, parameters, model);

            if (!string.IsNullOrEmpty(TweetDeckBridge.LastHighlightedTweet) && (parameters.TypeFlags & (ContextMenuType.Editable | ContextMenuType.Selection)) == 0){
                model.AddItem((CefMenuCommand)MenuOpenTweetUrl, "Open tweet in browser");
                model.AddItem((CefMenuCommand)MenuCopyTweetUrl, "Copy tweet address");

                if (!string.IsNullOrEmpty(TweetDeckBridge.LastHighlightedQuotedTweet)){
                    model.AddSeparator();
                    model.AddItem((CefMenuCommand)MenuOpenQuotedTweetUrl, "Open quoted tweet in browser");
                    model.AddItem((CefMenuCommand)MenuCopyQuotedTweetUrl, "Copy quoted tweet address");
                }

                model.AddSeparator();
            }

            if ((parameters.TypeFlags & (ContextMenuType.Editable | ContextMenuType.Selection)) == 0){
                AddSeparator(model);

                IMenuModel globalMenu = model.Count == 0 ? model : model.AddSubMenu((CefMenuCommand)MenuGlobal, Program.BrandName);
            
                globalMenu.AddItem(CefMenuCommand.Reload, "Reload browser");
                globalMenu.AddCheckItem((CefMenuCommand)MenuMute, "Mute notifications");
                globalMenu.SetChecked((CefMenuCommand)MenuMute, Program.UserConfig.MuteNotifications);
                globalMenu.AddSeparator();

                globalMenu.AddItem((CefMenuCommand)MenuSettings, "Settings");
                globalMenu.AddItem((CefMenuCommand)MenuPlugins, "Plugins");
                globalMenu.AddItem((CefMenuCommand)MenuAbout, "About "+Program.BrandName);
            }
        }

        public override bool OnContextMenuCommand(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags){
            if (base.OnContextMenuCommand(browserControl, browser, frame, parameters, commandId, eventFlags)){
                return true;
            }

            switch((int)commandId){
                case (int)CefMenuCommand.Reload:
                    frame.ExecuteJavaScriptAsync("window.location.href = 'https://tweetdeck.twitter.com'");
                    return true;

                case MenuSettings:
                    form.InvokeSafe(form.OpenSettings);
                    return true;

                case MenuAbout:
                    form.InvokeSafe(form.OpenAbout);
                    return true;

                case MenuPlugins:
                    form.InvokeSafe(form.OpenPlugins);
                    return true;

                case MenuMute:
                    form.InvokeSafe(() => {
                        Program.UserConfig.MuteNotifications = !Program.UserConfig.MuteNotifications;
                        Program.UserConfig.Save();
                    });

                    return true;

                case MenuOpenTweetUrl:
                    BrowserUtils.OpenExternalBrowser(TweetDeckBridge.LastHighlightedTweet);
                    return true;

                case MenuCopyTweetUrl:
                    Clipboard.SetText(TweetDeckBridge.LastHighlightedTweet, TextDataFormat.UnicodeText);
                    return true;

                case MenuOpenQuotedTweetUrl:
                    BrowserUtils.OpenExternalBrowser(TweetDeckBridge.LastHighlightedQuotedTweet);
                    return true;

                case MenuCopyQuotedTweetUrl:
                    Clipboard.SetText(TweetDeckBridge.LastHighlightedQuotedTweet, TextDataFormat.UnicodeText);
                    return true;
            }

            return false;
        }
    }
}<commit_msg>Fix context menu state changing when moving mouse quickly<commit_after>﻿using CefSharp;
using System.Windows.Forms;
using TweetDck.Core.Controls;
using TweetDck.Core.Utils;

namespace TweetDck.Core.Handling{
    class ContextMenuBrowser : ContextMenuBase{
        private const int MenuGlobal = 26600;
        private const int MenuMute = 26601;
        private const int MenuSettings = 26602;
        private const int MenuPlugins = 26003;
        private const int MenuAbout = 26604;
        
        private const int MenuOpenTweetUrl = 26610;
        private const int MenuCopyTweetUrl = 26611;
        private const int MenuOpenQuotedTweetUrl = 26612;
        private const int MenuCopyQuotedTweetUrl = 26613;

        private readonly FormBrowser form;

        private string lastHighlightedTweet;
        private string lastHighlightedQuotedTweet;

        public ContextMenuBrowser(FormBrowser form){
            this.form = form;
        }

        public override void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model){
            model.Remove(CefMenuCommand.Back);
            model.Remove(CefMenuCommand.Forward);
            model.Remove(CefMenuCommand.Print);
            model.Remove(CefMenuCommand.ViewSource);
            RemoveSeparatorIfLast(model);

            base.OnBeforeContextMenu(browserControl, browser, frame, parameters, model);

            lastHighlightedTweet = TweetDeckBridge.LastHighlightedTweet;
            lastHighlightedQuotedTweet = TweetDeckBridge.LastHighlightedQuotedTweet;

            if (!string.IsNullOrEmpty(lastHighlightedTweet) && (parameters.TypeFlags & (ContextMenuType.Editable | ContextMenuType.Selection)) == 0){
                model.AddItem((CefMenuCommand)MenuOpenTweetUrl, "Open tweet in browser");
                model.AddItem((CefMenuCommand)MenuCopyTweetUrl, "Copy tweet address");

                if (!string.IsNullOrEmpty(lastHighlightedQuotedTweet)){
                    model.AddSeparator();
                    model.AddItem((CefMenuCommand)MenuOpenQuotedTweetUrl, "Open quoted tweet in browser");
                    model.AddItem((CefMenuCommand)MenuCopyQuotedTweetUrl, "Copy quoted tweet address");
                }

                model.AddSeparator();
            }

            if ((parameters.TypeFlags & (ContextMenuType.Editable | ContextMenuType.Selection)) == 0){
                AddSeparator(model);

                IMenuModel globalMenu = model.Count == 0 ? model : model.AddSubMenu((CefMenuCommand)MenuGlobal, Program.BrandName);
            
                globalMenu.AddItem(CefMenuCommand.Reload, "Reload browser");
                globalMenu.AddCheckItem((CefMenuCommand)MenuMute, "Mute notifications");
                globalMenu.SetChecked((CefMenuCommand)MenuMute, Program.UserConfig.MuteNotifications);
                globalMenu.AddSeparator();

                globalMenu.AddItem((CefMenuCommand)MenuSettings, "Settings");
                globalMenu.AddItem((CefMenuCommand)MenuPlugins, "Plugins");
                globalMenu.AddItem((CefMenuCommand)MenuAbout, "About "+Program.BrandName);
            }
        }

        public override bool OnContextMenuCommand(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags){
            if (base.OnContextMenuCommand(browserControl, browser, frame, parameters, commandId, eventFlags)){
                return true;
            }

            switch((int)commandId){
                case (int)CefMenuCommand.Reload:
                    frame.ExecuteJavaScriptAsync("window.location.href = 'https://tweetdeck.twitter.com'");
                    return true;

                case MenuSettings:
                    form.InvokeSafe(form.OpenSettings);
                    return true;

                case MenuAbout:
                    form.InvokeSafe(form.OpenAbout);
                    return true;

                case MenuPlugins:
                    form.InvokeSafe(form.OpenPlugins);
                    return true;

                case MenuMute:
                    form.InvokeSafe(() => {
                        Program.UserConfig.MuteNotifications = !Program.UserConfig.MuteNotifications;
                        Program.UserConfig.Save();
                    });

                    return true;

                case MenuOpenTweetUrl:
                    BrowserUtils.OpenExternalBrowser(lastHighlightedTweet);
                    return true;

                case MenuCopyTweetUrl:
                    Clipboard.SetText(lastHighlightedTweet, TextDataFormat.UnicodeText);
                    return true;

                case MenuOpenQuotedTweetUrl:
                    BrowserUtils.OpenExternalBrowser(lastHighlightedQuotedTweet);
                    return true;

                case MenuCopyQuotedTweetUrl:
                    Clipboard.SetText(lastHighlightedQuotedTweet, TextDataFormat.UnicodeText);
                    return true;
            }

            return false;
        }
    }
}
{'fraction_non_alphanumeric': 0.07562189054726368, 'fraction_numerical': 0.00955223880597015, 'mean_word_length': 2.106955177743431, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4804781', 'n_tokens_mistral': 2667, 'n_tokens_neox': 2411, 'n_words': 472}