OSDN Git Service

発言詳細欄の更新間隔をThrottlingTimerで制御する, TweenMain._colorize を廃止
[opentween/open-tween.git] / OpenTween / Tween.cs
index 2d232d6..ac50534 100644 (file)
@@ -267,10 +267,10 @@ namespace OpenTween
         private bool osResumed = false;
 
         //////////////////////////////////////////////////////////////////////////////////////////////////////////
-        private bool _colorize = false;
 
         private System.Timers.Timer TimerTimeline = new System.Timers.Timer();
         private ThrottlingTimer RefreshThrottlingTimer;
+        private ThrottlingTimer selectionDebouncer;
 
         private string recommendedStatusFooter;
         private bool urlMultibyteSplit = false;
@@ -285,11 +285,11 @@ namespace OpenTween
 
         private List<urlUndo> urlUndoBuffer = null;
 
-        private struct ReplyChain
+        private readonly struct ReplyChain
         {
-            public long OriginalId;
-            public long InReplyToId;
-            public TabModel OriginalTab;
+            public readonly long OriginalId;
+            public readonly long InReplyToId;
+            public readonly TabModel OriginalTab;
 
             public ReplyChain(long originalId, long inReplyToId, TabModel originalTab)
             {
@@ -1114,8 +1114,8 @@ namespace OpenTween
             //タイマー設定
 
             var streamingRefreshInterval = TimeSpan.FromSeconds(SettingManager.Common.UserstreamPeriod);
-            this.RefreshThrottlingTimer = new ThrottlingTimer(streamingRefreshInterval,
-                () => this.InvokeAsync(() => this.RefreshTimeline()));
+            this.RefreshThrottlingTimer = ThrottlingTimer.Throttle(() => this.InvokeAsync(() => this.RefreshTimeline()), streamingRefreshInterval);
+            this.selectionDebouncer = ThrottlingTimer.Debounce(() => this.InvokeAsync(() => this.UpdateSelectedPost()), TimeSpan.FromMilliseconds(100));
 
             TimerTimeline.AutoReset = true;
             TimerTimeline.SynchronizingObject = this;
@@ -1282,7 +1282,7 @@ namespace OpenTween
             if (e.UserStream)
             {
                 var interval = TimeSpan.FromSeconds(SettingManager.Common.UserstreamPeriod);
-                var newTimer = new ThrottlingTimer(interval, () => this.InvokeAsync(() => this.RefreshTimeline()));
+                var newTimer = ThrottlingTimer.Throttle(() => this.InvokeAsync(() => this.RefreshTimeline()), interval);
                 var oldTimer = Interlocked.Exchange(ref this.RefreshThrottlingTimer, newTimer);
                 oldTimer.Dispose();
             }
@@ -1935,9 +1935,9 @@ namespace OpenTween
             this._statuses.SetReadAllTab(post.StatusId, read: true);
             //キャッシュの書き換え
             ChangeCacheStyleRead(true, index); // 既読へ(フォント、文字色)
-
             ColorizeList();
-            _colorize = true;
+
+            this.selectionDebouncer.Call();
         }
 
         private void ChangeCacheStyleRead(bool Read, int Index)
@@ -2734,8 +2734,8 @@ namespace OpenTween
                     {
                         this._statuses.AddPost(post);
                         this._statuses.DistributePosts();
-                        this.RefreshTimeline();
                     }
+                    this.RefreshTimeline();
                 }
             }
         }
@@ -5584,10 +5584,8 @@ namespace OpenTween
             }
         }
 
-        private async Task Colorize()
+        private async Task UpdateSelectedPost()
         {
-            _colorize = false;
-            await this.DispSelectedPost();
             //件数関連の場合、タイトル即時書き換え
             if (SettingManager.Common.DispLatestPost != MyCommon.DispTitleEnum.None &&
                SettingManager.Common.DispLatestPost != MyCommon.DispTitleEnum.Post &&
@@ -5599,11 +5597,11 @@ namespace OpenTween
             if (!StatusLabelUrl.Text.StartsWith("http", StringComparison.OrdinalIgnoreCase))
                 SetStatusLabelUrl();
 
-            foreach (var (tab, index) in this._statuses.Tabs.WithIndex())
+            if (SettingManager.Common.TabIconDisp)
             {
-                if (tab.UnreadCount == 0)
+                foreach (var (tab, index) in this._statuses.Tabs.WithIndex())
                 {
-                    if (SettingManager.Common.TabIconDisp)
+                    if (tab.UnreadCount == 0)
                     {
                         var tabPage = this.ListTab.TabPages[index];
                         if (tabPage.ImageIndex == 0)
@@ -5611,7 +5609,12 @@ namespace OpenTween
                     }
                 }
             }
-            if (!SettingManager.Common.TabIconDisp) ListTab.Refresh();
+            else
+            {
+                this.ListTab.Refresh();
+            }
+
+            await this.DispSelectedPost();
         }
 
         public string createDetailHtml(string orgdata)
@@ -5870,7 +5873,7 @@ namespace OpenTween
 
                 ShortcutCommand.Create(Keys.Control | Keys.Home, Keys.Control | Keys.End)
                     .FocusedOn(FocusedControl.ListTab)
-                    .Do(() => this._colorize = true, preventDefault: false),
+                    .Do(() => this.selectionDebouncer.Call(), preventDefault: false),
 
                 ShortcutCommand.Create(Keys.Control | Keys.N)
                     .FocusedOn(FocusedControl.ListTab)
@@ -7745,11 +7748,8 @@ namespace OpenTween
         private static bool blink = false;
         private static bool idle = false;
 
-        private async Task RefreshTasktrayIcon()
+        private void RefreshTasktrayIcon()
         {
-            if (_colorize)
-                await this.Colorize();
-
             if (!TimerRefreshIcon.Enabled) return;
             //Static usCheckCnt As int = 0
 
@@ -7833,8 +7833,8 @@ namespace OpenTween
             }
         }
 
-        private async void TimerRefreshIcon_Tick(object sender, EventArgs e)
-            => await this.RefreshTasktrayIcon(); // 200ms
+        private void TimerRefreshIcon_Tick(object sender, EventArgs e)
+            => this.RefreshTasktrayIcon(); // 200ms
 
         private void ContextMenuTabProperty_Opening(object sender, CancelEventArgs e)
         {
@@ -8456,83 +8456,56 @@ namespace OpenTween
         {
             var linkElements = this.tweetDetailsView.GetLinkElements();
 
-            if (linkElements.Length > 0)
+            if (linkElements.Length == 0)
+                return;
+
+            var links = new List<OpenUrlItem>(linkElements.Length);
+
+            foreach (var linkElm in linkElements)
             {
-                UrlDialog.ClearUrl();
+                var displayUrl = linkElm.GetAttribute("title");
+                var href = linkElm.GetAttribute("href");
+                var linkedText = linkElm.InnerText;
 
-                string openUrlStr = "";
+                if (string.IsNullOrEmpty(displayUrl))
+                    displayUrl = href;
 
-                if (linkElements.Length == 1)
-                {
-                    // ツイートに含まれる URL が 1 つのみの場合
-                    //   => OpenURL ダイアログを表示せずにリンクを開く
+                links.Add(new OpenUrlItem(linkedText, displayUrl, href));
+            }
 
-                    string urlStr = "";
-                    try
-                    {
-                        urlStr = MyCommon.IDNEncode(linkElements[0].GetAttribute("href"));
-                    }
-                    catch (ArgumentException)
-                    {
-                        //変なHTML?
-                        return;
-                    }
-                    catch (Exception)
-                    {
-                        return;
-                    }
-                    if (string.IsNullOrEmpty(urlStr)) return;
-                    openUrlStr = MyCommon.urlEncodeMultibyteChar(urlStr);
+            string selectedUrl;
+            bool isReverseSettings;
 
-                    // Ctrl+E で呼ばれた場合を考慮し isReverseSettings の判定を行わない
-                    await this.OpenUriAsync(new Uri(openUrlStr));
-                }
-                else
-                {
-                    // ツイートに含まれる URL が複数ある場合
-                    //   => OpenURL を表示しユーザーが選択したリンクを開く
+            if (links.Count == 1)
+            {
+                // ツイートに含まれる URL が 1 つのみの場合
+                //   => OpenURL ダイアログを表示せずにリンクを開く
+                selectedUrl = links[0].Href;
 
-                    foreach (var linkElm in linkElements)
-                    {
-                        string urlStr = "";
-                        string linkText = "";
-                        string href = "";
-                        try
-                        {
-                            urlStr = linkElm.GetAttribute("title");
-                            href = MyCommon.IDNEncode(linkElm.GetAttribute("href"));
-                            if (string.IsNullOrEmpty(urlStr)) urlStr = href;
-                            linkText = linkElm.InnerText;
-                        }
-                        catch (ArgumentException)
-                        {
-                            //変なHTML?
-                            return;
-                        }
-                        catch (Exception)
-                        {
-                            return;
-                        }
-                        if (string.IsNullOrEmpty(urlStr)) continue;
-                        UrlDialog.AddUrl(new OpenUrlItem(linkText, MyCommon.urlEncodeMultibyteChar(urlStr), href));
-                    }
-                    try
-                    {
-                        if (UrlDialog.ShowDialog() == DialogResult.OK)
-                        {
-                            openUrlStr = UrlDialog.SelectedUrl;
+                // Ctrl+E で呼ばれた場合を考慮し isReverseSettings の判定を行わない
+                isReverseSettings = false;
+            }
+            else
+            {
+                // ツイートに含まれる URL が複数ある場合
+                //   => OpenURL を表示しユーザーが選択したリンクを開く
+                this.UrlDialog.ClearUrl();
 
-                            // Ctrlを押しながらリンクを開いた場合は、設定と逆の動作をするフラグを true としておく
-                            await this.OpenUriAsync(new Uri(openUrlStr), MyCommon.IsKeyDown(Keys.Control));
-                        }
-                    }
-                    catch (Exception)
-                    {
-                        return;
-                    }
-                    this.TopMost = SettingManager.Common.AlwaysTop;
-                }
+                foreach (var link in links)
+                    this.UrlDialog.AddUrl(link);
+
+                if (this.UrlDialog.ShowDialog(this) != DialogResult.OK)
+                    return;
+
+                this.TopMost = SettingManager.Common.AlwaysTop;
+
+                selectedUrl = this.UrlDialog.SelectedUrl;
+
+                // Ctrlを押しながらリンクを開いた場合は、設定と逆の動作をするフラグを true としておく
+                isReverseSettings = MyCommon.IsKeyDown(Keys.Control);
             }
+
+            await this.OpenUriAsync(new Uri(selectedUrl), isReverseSettings);
         }
 
         private void ClearTabMenuItem_Click(object sender, EventArgs e)
@@ -9114,10 +9087,10 @@ namespace OpenTween
                     }
                     else if (Converter_Type != MyCommon.UrlConverter.Nicoms)
                     {
-                        //短縮URL変換 日本語を含むかもしれないのでURLエンコードする
+                        // 短縮URL変換
                         try
                         {
-                            var srcUri = new Uri(MyCommon.urlEncodeMultibyteChar(tmp));
+                            var srcUri = new Uri(tmp);
                             var resultUri = await ShortUrl.Instance.ShortenUrlAsync(Converter_Type, srcUri);
                             result = resultUri.AbsoluteUri;
                         }
@@ -9188,10 +9161,10 @@ namespace OpenTween
                     }
                     else if (Converter_Type != MyCommon.UrlConverter.Nicoms)
                     {
-                        //短縮URL変換 日本語を含むかもしれないのでURLエンコードする
+                        // 短縮URL変換
                         try
                         {
-                            var srcUri = new Uri(MyCommon.urlEncodeMultibyteChar(tmp));
+                            var srcUri = new Uri(tmp);
                             var resultUri = await ShortUrl.Instance.ShortenUrlAsync(Converter_Type, srcUri);
                             result = resultUri.AbsoluteUri;
                         }
@@ -11352,7 +11325,7 @@ namespace OpenTween
 
             this._statuses.DistributePosts();
 
-            this.RefreshThrottlingTimer.Invoke();
+            this.RefreshThrottlingTimer.Call();
         }
 
         private async void tw_UserStreamStarted(object sender, EventArgs e)