Skip to content

Commit

Permalink
発言一覧の選択状態を更新する際に選択範囲の差分のみ反映する
Browse files Browse the repository at this point in the history
  • Loading branch information
upsilon committed Mar 7, 2020
1 parent 21ea2ae commit d62e270
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
36 changes: 30 additions & 6 deletions OpenTween/DetailsListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,39 @@ public int SelectionMark

public void SelectItems(int[] indices)
{
foreach (var index in indices)
{
if (index < 0 || index >= this.VirtualListSize)
throw new ArgumentOutOfRangeException(nameof(indices));
var listSize = this.VirtualListSize;
if (indices.Any(x => x < 0 || x >= listSize))
throw new ArgumentOutOfRangeException(nameof(indices));

NativeMethods.SelectItem(this, index);
if (indices.Length == 0)
{
this.SelectedIndices.Clear();
}
else if (indices.Length == 1)
{
this.SelectedIndices.Clear();
this.SelectedIndices.Add(indices[0]);
}
else
{
var currentSelectedIndices = this.SelectedIndices.Cast<int>().ToArray();
var selectIndices = indices.Except(currentSelectedIndices).ToArray();
var deselectIndices = currentSelectedIndices.Except(indices).ToArray();

this.OnSelectedIndexChanged(EventArgs.Empty);
if (selectIndices.Length + deselectIndices.Length > currentSelectedIndices.Length)
{
// Clearして選択し直した方が早い場合
this.SelectedIndices.Clear();
selectIndices = indices;
deselectIndices = Array.Empty<int>();
}

foreach (var index in selectIndices)
NativeMethods.SelectItem(this, index, selected: true);

foreach (var index in deselectIndices)
NativeMethods.SelectItem(this, index, selected: false);
}
}

public void SelectAllItems()
Expand Down
5 changes: 3 additions & 2 deletions OpenTween/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ private struct LVITEM
[Flags]
private enum LVIS : uint
{
None = 0,
SELECTED = 0x02,
}

Expand All @@ -130,13 +131,13 @@ private enum LVIS : uint
/// <param name="listView">対象となる ListView</param>
/// <param name="index">選択するアイテムのインデックス</param>
/// <returns>成功した場合は true、それ以外の場合は false</returns>
public static bool SelectItem(ListView listView, int index)
public static bool SelectItem(ListView listView, int index, bool selected = true)
{
// LVM_SETITEMSTATE では stateMask, state 以外のメンバーは無視される
var lvitem = new LVITEM
{
stateMask = LVIS.SELECTED,
state = LVIS.SELECTED,
state = selected ? LVIS.SELECTED : LVIS.None,
};

var ret = (int)SendMessage(listView.Handle, SendMessageType.LVM_SETITEMSTATE, (IntPtr)index, ref lvitem);
Expand Down
5 changes: 0 additions & 5 deletions OpenTween/Tween.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9598,11 +9598,6 @@ private void SelectListItem(DetailsListView LView , int[]? Index, int focusedInd

if (Index != null)
{
do
{
LView.SelectedIndices.Clear();
}
while (LView.SelectedIndices.Count > 0);
LView.SelectItems(Index);
}
if (selectionMarkIndex > -1 && LView.VirtualListSize > selectionMarkIndex)
Expand Down

0 comments on commit d62e270

Please sign in to comment.