From: anis774 Date: Tue, 14 Sep 2010 12:50:17 +0000 (+0000) Subject: アイコンキャッシュ機構を破棄。 X-Git-Tag: Tween_v0.9.6.0~61 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=52a5f6a4d3c9bb7e23e6bfd3aeea876467ac6112;p=opentween%2Fopen-tween.git アイコンキャッシュ機構を破棄。 git-svn-id: http://svn.sourceforge.jp/svnroot/tween/trunk@871 e39ad16e-3079-482e-bb30-4b4d378143b6 --- diff --git a/Tween/DetailsListView.vb b/Tween/DetailsListView.vb index 03a25694..924dac02 100644 --- a/Tween/DetailsListView.vb +++ b/Tween/DetailsListView.vb @@ -36,8 +36,6 @@ Namespace TweenCustomControl Private multiSelected As Boolean Private _handlers As New System.ComponentModel.EventHandlerList() - Public Event Scrolled As System.EventHandler - Public Sub New() View = Windows.Forms.View.Details FullRowSelect = True @@ -163,24 +161,19 @@ Namespace TweenCustomControl Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) Const WM_ERASEBKGND As Integer = &H14 Const WM_PAINT As Integer = &HF - Const WM_HSCROLL As Integer = &H114 - Const WM_VSCROLL As Integer = &H115 - - Select Case m.Msg - Case WM_ERASEBKGND - If Me.changeBounds <> Rectangle.Empty Then - m.Msg = 0 - End If - Case WM_PAINT - If Me.changeBounds <> Rectangle.Empty Then - Win32Api.ValidateRect(Me.Handle, IntPtr.Zero) - Me.Invalidate(Me.changeBounds) - Me.changeBounds = Rectangle.Empty - End If - Case WM_HSCROLL, WM_VSCROLL - RaiseEvent Scrolled(Me, EventArgs.Empty) - End Select + If m.Msg = WM_ERASEBKGND Then + If Me.changeBounds <> Rectangle.Empty Then + m.Msg = 0 + End If + End If + If m.Msg = WM_PAINT Then + If Me.changeBounds <> Rectangle.Empty Then + Win32Api.ValidateRect(Me.Handle, IntPtr.Zero) + Me.Invalidate(Me.changeBounds) + Me.changeBounds = Rectangle.Empty + End If + End If Try MyBase.WndProc(m) Catch ex As ArgumentOutOfRangeException diff --git a/Tween/ImageCacheDictionary.vb b/Tween/ImageCacheDictionary.vb deleted file mode 100644 index 459934e5..00000000 --- a/Tween/ImageCacheDictionary.vb +++ /dev/null @@ -1,305 +0,0 @@ -Imports System.Drawing -Imports System.IO - -Public Class ImageCacheDictionary - Implements IDictionary(Of String, Image), IDisposable - - Private ReadOnly lockObject As New Object() - - Private cacheDiractoryPath As String - Private memoryCacheCount As Integer - Private innerDictionary As Dictionary(Of String, CachedImage) - Private sortedKeyList As List(Of String) '古いもの順 - Private fileCacheProcList As New Queue(Of Threading.ThreadStart)() - - Public Sub New(ByVal cacheDirectory As String, ByVal memoryCacheCount As Integer) - SyncLock Me.lockObject - Me.innerDictionary = New Dictionary(Of String, CachedImage)(memoryCacheCount + 1) - Me.sortedKeyList = New List(Of String)(memoryCacheCount + 1) - Me.memoryCacheCount = memoryCacheCount - Me.cacheDiractoryPath = cacheDirectory - If Not Directory.Exists(Me.cacheDiractoryPath) Then - Directory.CreateDirectory(Me.cacheDiractoryPath) - End If - End SyncLock - End Sub - - Public Sub Add(ByVal item As System.Collections.Generic.KeyValuePair(Of String, Image)) Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of String, Image)).Add - Me.Add(item.Key, item.Value) - End Sub - - Public Sub Add(ByVal key As String, ByVal value As Image) Implements System.Collections.Generic.IDictionary(Of String, Image).Add - SyncLock Me.lockObject - Me.innerDictionary.Add(key, New CachedImage(value, Me.cacheDiractoryPath)) - Me.sortedKeyList.Add(key) - - If Me.innerDictionary.Count > Me.memoryCacheCount Then - Me.innerDictionary(Me.sortedKeyList(Me.sortedKeyList.Count - Me.memoryCacheCount - 1)).Cache() - End If - End SyncLock - - While Me.fileCacheProcList.Count > 0 - Me.fileCacheProcList.Dequeue().Invoke() - End While - End Sub - - Public Function Remove(ByVal item As System.Collections.Generic.KeyValuePair(Of String, Image)) As Boolean Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of String, Image)).Remove - Return Me.Remove(item.Key) - End Function - - Public Function Remove(ByVal key As String) As Boolean Implements System.Collections.Generic.IDictionary(Of String, Image).Remove - SyncLock Me.lockObject - Me.sortedKeyList.Remove(key) - Me.innerDictionary(key).Dispose() - Return Me.innerDictionary.Remove(key) - End SyncLock - End Function - - Default Public Property Item(ByVal key As String) As Image Implements System.Collections.Generic.IDictionary(Of String, Image).Item - Get - SyncLock Me.lockObject - Me.sortedKeyList.Remove(key) - Me.sortedKeyList.Add(key) - If Me.sortedKeyList.Count > Me.memoryCacheCount Then - Dim imgObj As CachedImage = Me.innerDictionary(Me.sortedKeyList(Me.sortedKeyList.Count - Me.memoryCacheCount - 1)) - If Not imgObj.Cached Then - Me.fileCacheProcList.Enqueue(Sub() - If Me.innerDictionary.ContainsValue(imgObj) Then - imgObj.Cache() - End If - End Sub) - End If - End If - Return Me.innerDictionary(key).Image - End SyncLock - End Get - Set(ByVal value As Image) - SyncLock Me.lockObject - Me.sortedKeyList.Remove(key) - Me.sortedKeyList.Add(key) - If Me.sortedKeyList.Count > Me.memoryCacheCount Then - Me.innerDictionary(Me.sortedKeyList(Me.sortedKeyList.Count - Me.memoryCacheCount - 1)).Cache() - End If - Me.innerDictionary(key).Dispose() - Me.innerDictionary(key) = New CachedImage(value, Me.cacheDiractoryPath) - End SyncLock - End Set - End Property - - Public Sub Clear() Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of String, Image)).Clear - SyncLock Me.lockObject - For Each value As CachedImage In Me.innerDictionary.Values - value.Dispose() - Next - - Me.innerDictionary.Clear() - Me.sortedKeyList.Clear() - End SyncLock - End Sub - - Public Function Contains(ByVal item As System.Collections.Generic.KeyValuePair(Of String, Image)) As Boolean Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of String, Image)).Contains - SyncLock Me.lockObject - Return Me.innerDictionary.ContainsKey(item.Key) AndAlso Me.innerDictionary(item.Key) Is item.Value - End SyncLock - End Function - - Public Sub CopyTo(ByVal array() As System.Collections.Generic.KeyValuePair(Of String, Image), ByVal arrayIndex As Integer) Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of String, Image)).CopyTo - SyncLock Me.lockObject - Dim index As Integer = arrayIndex - For Each Item As KeyValuePair(Of String, CachedImage) In Me.innerDictionary - If array.Length - 1 < index Then - Exit For - End If - - array(index) = New KeyValuePair(Of String, Image)(Item.Key, Item.Value.Image) - index += 1 - Next - End SyncLock - End Sub - - Public ReadOnly Property Count As Integer Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of String, Image)).Count - Get - SyncLock Me.lockObject - Return Me.innerDictionary.Count - End SyncLock - End Get - End Property - - Public ReadOnly Property IsReadOnly As Boolean Implements System.Collections.Generic.ICollection(Of System.Collections.Generic.KeyValuePair(Of String, Image)).IsReadOnly - Get - Return False - End Get - End Property - - Public Function ContainsKey(ByVal key As String) As Boolean Implements System.Collections.Generic.IDictionary(Of String, Image).ContainsKey - Return Me.innerDictionary.ContainsKey(key) - End Function - - Public ReadOnly Property Keys As System.Collections.Generic.ICollection(Of String) Implements System.Collections.Generic.IDictionary(Of String, Image).Keys - Get - SyncLock Me.lockObject - Return Me.innerDictionary.Keys - End SyncLock - End Get - End Property - - Public Function TryGetValue(ByVal key As String, ByRef value As Image) As Boolean Implements System.Collections.Generic.IDictionary(Of String, Image).TryGetValue - SyncLock Me.lockObject - If Me.innerDictionary.ContainsKey(key) Then - value = Me.innerDictionary(key).Image - Return True - Else - Return False - End If - End SyncLock - End Function - - Public ReadOnly Property Values As System.Collections.Generic.ICollection(Of Image) Implements System.Collections.Generic.IDictionary(Of String, Image).Values - Get - SyncLock Me.lockObject - Dim imgList As New List(Of Image)(Me.memoryCacheCount) - For Each value As CachedImage In Me.innerDictionary.Values - imgList.Add(value.Image) - Next - Return imgList - End SyncLock - End Get - End Property - - Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of System.Collections.Generic.KeyValuePair(Of String, Image)) Implements System.Collections.Generic.IEnumerable(Of System.Collections.Generic.KeyValuePair(Of String, Image)).GetEnumerator - Throw New NotImplementedException() - End Function - - Public Function GetEnumerator1() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator - Throw New NotImplementedException() - End Function - - Public Sub Dispose() Implements IDisposable.Dispose - SyncLock Me.lockObject - For Each item As CachedImage In Me.innerDictionary.Values - If item.img IsNot Nothing Then - item.img.Dispose() - End If - Next - - Dim di As New DirectoryInfo(Me.cacheDiractoryPath) - di.Delete(True) - End SyncLock - End Sub - - Public Function GetItemCopy(ByVal key As String) As Image - SyncLock Me.lockObject - Me.sortedKeyList.Remove(key) - Me.sortedKeyList.Add(key) - If Me.sortedKeyList.Count > Me.memoryCacheCount Then - Dim imgObj As CachedImage = Me.innerDictionary(Me.sortedKeyList(Me.sortedKeyList.Count - Me.memoryCacheCount - 1)) - If Not imgObj.Cached Then - Me.fileCacheProcList.Enqueue(Sub() - If Me.innerDictionary.ContainsValue(imgObj) Then - imgObj.Cache() - End If - End Sub) - End If - End If - - If Me.innerDictionary(key).Image IsNot Nothing Then - Return New Bitmap(Me.innerDictionary(key).Image) - Else - Return Nothing - End If - End SyncLock - End Function - - Private Class CachedImage - Implements IDisposable - - Private ReadOnly lockObject As New Object() - - Public img As Image = Nothing - Private tmpFilePath As String = Nothing - Private cacheDirectoryPath As String - - Public Sub New(ByVal img As Image, ByVal cacheDirectory As String) - SyncLock Me.lockObject - Me.img = img - Me.cacheDirectoryPath = cacheDirectory - End SyncLock - End Sub - - Public ReadOnly Property Image As Image - Get - SyncLock Me.lockObject - If Me.img Is Nothing AndAlso Me.tmpFilePath IsNot Nothing Then - Try - Dim tempImage As Image = Nothing - Using fs As New FileStream(Me.tmpFilePath, FileMode.Open, FileAccess.Read) - tempImage = Bitmap.FromStream(fs) - End Using - Me.img = New Bitmap(tempImage) - Catch ex As OutOfMemoryException - Dim filePath As String = Path.Combine(Application.StartupPath, Path.GetFileName(Me.tmpFilePath)) - File.Copy(Me.tmpFilePath, filePath) - Throw ex - End Try - End If - - Return Me.img - End SyncLock - End Get - End Property - - Public Sub Cache() - SyncLock Me.lockObject - If Me.tmpFilePath Is Nothing AndAlso Me.img IsNot Nothing Then - Dim tmpFile As String = Nothing - - Dim err As Boolean = False - Do - Try - err = False - tmpFile = Path.Combine(Me.cacheDirectoryPath, Path.GetRandomFileName().Remove(8, 1)) - - Using fs As New FileStream(tmpFile, FileMode.CreateNew, FileAccess.Write) - Me.img.Save(fs, Imaging.ImageFormat.Bmp) - fs.Flush() - End Using - Catch ex As InvalidOperationException - err = True - Catch ex As IOException - err = True - Catch ex As Exception - File.Delete(tmpFile) - Me.tmpFilePath = Nothing - Exit Sub - End Try - Loop While err - Me.tmpFilePath = tmpFile - End If - If Me.img IsNot Nothing Then - Me.img.Dispose() - Me.img = Nothing - End If - End SyncLock - End Sub - - Public ReadOnly Property Cached As Boolean - Get - SyncLock Me.lockObject - Return Me.tmpFilePath IsNot Nothing - End SyncLock - End Get - End Property - - Public Sub Dispose() Implements IDisposable.Dispose - SyncLock Me.lockObject - If Me.img IsNot Nothing Then - Me.img.Dispose() - End If - - If Me.tmpFilePath IsNot Nothing Then - File.Delete(Me.tmpFilePath) - End If - End SyncLock - End Sub - End Class -End Class \ No newline at end of file diff --git a/Tween/ImageListViewItem.vb b/Tween/ImageListViewItem.vb deleted file mode 100644 index ade2c439..00000000 --- a/Tween/ImageListViewItem.vb +++ /dev/null @@ -1,17 +0,0 @@ -Public Class ImageListViewItem - Inherits ListViewItem - - Public Sub New(ByVal items() As String, ByVal imageKey As String) - MyBase.New(items, imageKey) - End Sub - - Public Property Image As Image - - Protected Overrides Sub Finalize() - If Me.Image IsNot Nothing Then - Me.Image.Dispose() - Me.Image = Nothing - End If - MyBase.Finalize() - End Sub -End Class diff --git a/Tween/Tween.vb b/Tween/Tween.vb index 7e480b95..00aa41e7 100644 --- a/Tween/Tween.vb +++ b/Tween/Tween.vb @@ -319,6 +319,11 @@ Public Class TweenMain fDialog.Dispose() UrlDialog.Dispose() _spaceKeyCanceler.Dispose() + If TIconDic IsNot Nothing AndAlso TIconDic.Keys.Count > 0 Then + For Each value As Image In TIconDic.Values + value.Dispose() + Next + End If If NIconAt IsNot Nothing Then NIconAt.Dispose() If NIconAtRed IsNot Nothing Then NIconAtRed.Dispose() If NIconAtSmoke IsNot Nothing Then NIconAtSmoke.Dispose() @@ -356,7 +361,6 @@ Public Class TweenMain If _bwFollower IsNot Nothing Then _bwFollower.Dispose() End If - DirectCast(TIconDic, IDisposable).Dispose() End Sub Private Sub LoadIcon(ByRef IconInstance As Icon, ByVal FileName As String) @@ -981,7 +985,7 @@ Public Class TweenMain End If 'アイコンリスト作成 - TIconDic = New ImageCacheDictionary(Path.Combine(Path.GetTempPath(), "Tween" + Environment.TickCount.ToString()), 3000) + TIconDic = New Dictionary(Of String, Image) tw.DetailIcon = TIconDic @@ -3250,7 +3254,6 @@ Public Class TweenMain AddHandler _listCustom.CacheVirtualItems, AddressOf MyList_CacheVirtualItems AddHandler _listCustom.RetrieveVirtualItem, AddressOf MyList_RetrieveVirtualItem AddHandler _listCustom.DrawSubItem, AddressOf MyList_DrawSubItem - AddHandler _listCustom.Scrolled, AddressOf MyList_Scrolled InitColumnText() _colHd1.Text = ColumnText(0) @@ -3413,7 +3416,6 @@ Public Class TweenMain RemoveHandler _listCustom.CacheVirtualItems, AddressOf MyList_CacheVirtualItems RemoveHandler _listCustom.RetrieveVirtualItem, AddressOf MyList_RetrieveVirtualItem RemoveHandler _listCustom.DrawSubItem, AddressOf MyList_DrawSubItem - RemoveHandler _listCustom.Scrolled, AddressOf MyList_Scrolled TabDialog.RemoveTab(TabName) @@ -3683,7 +3685,7 @@ Public Class TweenMain Catch ex As Exception '不正な要求に対する間に合わせの応答 Dim sitem() As String = {"", "", "", "", "", "", "", ""} - e.Item = New ImageListViewItem(sitem, "") + e.Item = New ListViewItem(sitem, "") End Try End If End Sub @@ -3714,17 +3716,13 @@ Public Class TweenMain If Post.IsMark Then mk += "♪" If Post.IsProtect Then mk += "Ю" If Post.InReplyToId > 0 Then mk += "⇒" - Dim itm As ImageListViewItem + Dim itm As ListViewItem If Post.RetweetedId = 0 Then Dim sitem() As String = {"", Post.Nickname, Post.Data, Post.PDate.ToString(SettingDialog.DateTimeFormat), Post.Name, "", mk, Post.Source} - itm = New ImageListViewItem(sitem, Post.ImageUrl) + itm = New ListViewItem(sitem, Post.ImageUrl) Else Dim sitem() As String = {"", Post.Nickname, Post.Data, Post.PDate.ToString(SettingDialog.DateTimeFormat), Post.Name + "(RT:" + Post.RetweetedBy + ")", "", mk, Post.Source} - itm = New ImageListViewItem(sitem, Post.ImageUrl) - End If - - If tw.DetailIcon.ContainsKey(Post.ImageUrl) Then - itm.Image = DirectCast(tw.DetailIcon, ImageCacheDictionary).GetItemCopy(Post.ImageUrl) + itm = New ListViewItem(sitem, Post.ImageUrl) End If Dim read As Boolean = Post.IsRead @@ -3853,11 +3851,9 @@ Public Class TweenMain End Sub Private Sub DrawListViewItemIcon(ByVal e As DrawListViewSubItemEventArgs) - Dim item As ImageListViewItem = DirectCast(e.Item, ImageListViewItem) - - If item.Image IsNot Nothing Then + If Not String.IsNullOrEmpty(e.Item.ImageKey) AndAlso Me.TIconDic.ContainsKey(e.Item.ImageKey) Then 'e.Bounds.Leftが常に0を指すから自前で計算 - Dim itemRect As Rectangle = item.Bounds + Dim itemRect As Rectangle = e.Item.Bounds itemRect.Width = e.Item.ListView.Columns(0).Width For Each clm As ColumnHeader In e.Item.ListView.Columns @@ -3866,10 +3862,10 @@ Public Class TweenMain End If Next - Dim iconRect As Rectangle = Rectangle.Intersect(item.GetBounds(ItemBoundsPortion.Icon), itemRect) + Dim iconRect As Rectangle = Rectangle.Intersect(e.Item.GetBounds(ItemBoundsPortion.Icon), itemRect) If iconRect.Width > 0 Then e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.High - e.Graphics.DrawImage(item.Image, iconRect) + e.Graphics.DrawImage(Me.TIconDic(e.Item.ImageKey), iconRect) End If End If End Sub @@ -7135,11 +7131,6 @@ RETRY: 'End If End Sub - Private Sub MyList_Scrolled(ByVal sender As Object, ByVal e As EventArgs) - Dim listView As DetailsListView = DirectCast(sender, DetailsListView) - listView.Refresh() - End Sub - Public Function WebBrowser_GetSelectionText(ByRef ComponentInstance As WebBrowser) As String '発言詳細で「選択文字列をコピー」を行う 'WebBrowserコンポーネントのインスタンスを渡す diff --git a/Tween/Tween.vbproj b/Tween/Tween.vbproj index 168ac082..679fd6cb 100644 --- a/Tween/Tween.vbproj +++ b/Tween/Tween.vbproj @@ -148,8 +148,6 @@ Form - - InputTabName.vb diff --git a/Tween/Twitter.vb b/Tween/Twitter.vb index 680c10af..9301c409 100644 --- a/Tween/Twitter.vb +++ b/Tween/Twitter.vb @@ -249,7 +249,6 @@ Public Class Twitter Dim httpVar As New HttpVarious img = httpVar.GetImage(post.ImageUrl, 10000) If img Is Nothing Then - 'post.ImageUrl = Nothing TabInformations.GetInstance.AddPost(post) Exit Sub End If @@ -262,13 +261,10 @@ Public Class Twitter _dIcon.Add(post.ImageUrl, img) Catch ex As InvalidOperationException 'タイミングにより追加できない場合がある?(キー重複ではない) - 'post.ImageUrl = Nothing Catch ex As System.OverflowException '不正なアイコン?DrawImageに失敗する場合あり - 'post.ImageUrl = Nothing Catch ex As OutOfMemoryException 'DrawImageで発生 - 'post.ImageUrl = Nothing End Try End If End SyncLock