Skip to content

Instantly share code, notes, and snippets.

@mkrueger
Created March 19, 2021 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkrueger/2ee68c9428925ad34f92ba324e1a0d9b to your computer and use it in GitHub Desktop.
Save mkrueger/2ee68c9428925ad34f92ba324e1a0d9b to your computer and use it in GitHub Desktop.
//
// StatusView.cs
//
// Author:
// Mike Krüger <mikkrg@microsoft.com>
//
// Copyright (c) 2020 Microsoft Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Linq;
using AppKit;
using Foundation;
using MonoDevelop.Core;
namespace MonoDevelop.VersionControl.Views.Status
{
partial class StatusView
{
partial class StageArea
{
internal class NSObjectWrapper : NSObject
{
List<NSObjectWrapper> children = new List<NSObjectWrapper>();
public VersionInfo VersionInfo
{
get;
}
public IReadOnlyList<NSObjectWrapper> Children => children;
public NSObjectWrapper(VersionInfo versionInfo)
{
VersionInfo = versionInfo;
}
public FilePath FilePath { get; set; }
public NSObjectWrapper(FilePath filePath)
{
this.FilePath = filePath;
}
internal void Add(NSObjectWrapper child)
{
children.Add(child);
}
}
class VersionInfoDataSource : NSOutlineViewDataSource
{
readonly List<NSObjectWrapper> displayedItems = new List<NSObjectWrapper>();
public List<NSObjectWrapper> treeChildren = new List<NSObjectWrapper>();
public int Count => displayedItems.Count;
public event EventHandler ItemsChanged;
public bool TreeMode { get; set; }
public void Clear()
{
Runtime.AssertMainThread();
displayedItems.Clear();
treeChildren.Clear();
ItemsChanged?.Invoke(this, EventArgs.Empty);
}
public void AddItem(VersionInfo info)
{
Runtime.AssertMainThread();
displayedItems.Add(new NSObjectWrapper(info));
ItemsChanged?.Invoke(this, EventArgs.Empty);
foreach (var child in treeChildren)
{
if (child.FilePath == info.LocalPath.ParentDirectory)
{
AddItem(child, info);
return;
}
}
var newChild = new NSObjectWrapper(info.LocalPath.ParentDirectory);
treeChildren.Add(newChild);
AddItem(newChild, info);
}
private void AddItem(NSObjectWrapper child, VersionInfo info)
{
child.Add(new NSObjectWrapper(info));
}
internal VersionInfo GetItem(int i)
{
if (i < 0 || i >= displayedItems.Count)
throw new ArgumentOutOfRangeException(nameof(i), "should be >= 0 && < " + displayedItems.Count + " was:" + i);
return displayedItems[i].VersionInfo;
}
public override NSObject GetObjectValue(NSOutlineView outlineView, NSTableColumn tableColumn, NSObject item)
{
var wrappper = (NSObjectWrapper)item;
if (wrappper.VersionInfo != null)
return (NSString)wrappper.VersionInfo.LocalPath.FileName;
return (NSString)wrappper.FilePath.FileName;
}
public override nint GetChildrenCount(NSOutlineView outlineView, NSObject item)
{
if (!TreeMode)
{
return displayedItems.Count;
}
Console.WriteLine("children count : " + item);
if (item == null)
return treeChildren.Count;
var wrapper = (NSObjectWrapper)item;
return wrapper.Children.Count;
}
public override NSObject GetChild(NSOutlineView outlineView, nint childIndex, NSObject item)
{
if (!TreeMode)
{
if (item == null)
return displayedItems[(int)childIndex];
}
else
{
if (item == null)
{
if (treeChildren.Count > (int)childIndex)
return treeChildren[(int)childIndex];
return null;
}
var wrapper = (NSObjectWrapper)item;
if (wrapper.Children.Count > (int)childIndex)
{
return wrapper.Children[(int)childIndex];
}
}
return null;
}
public override bool ItemExpandable(NSOutlineView outlineView, NSObject item)
{
if (item != null)
{
var wrapper = (NSObjectWrapper)item;
Console.WriteLine("item is expandable:" + (wrapper.Children.Count > 0));
return wrapper.Children.Count > 0;
}
return displayedItems.Count > 0;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment