Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revised Tooltip & ContextMenuItem for GC #1002

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Blish HUD/Common/UI/Views/BasicTooltipView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public BasicTooltipView(string text) {

protected override void Build(Container buildPanel) {
_tooltipLabel.Parent = buildPanel;

buildPanel.Hidden += (sender, args) => buildPanel.Dispose();
}

private void UpdateLabelValueAndWidth(string value) {
Expand Down
2 changes: 1 addition & 1 deletion Blish HUD/Controls/ContextMenuStrip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Blish_HUD.Controls {
/// <summary>
/// Represents a right-click shortcut menu. Can be assigned to <see cref="Control.Menu"/>.
/// </summary>
public class ContextMenuStrip : Container {
public class ContextMenuStrip : ReferenceCountedContainer {

private const int BORDER_PADDING = 2;

Expand Down
28 changes: 25 additions & 3 deletions Blish HUD/Controls/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ public static class StandardColors {

#endregion

~Control() {
// Pass false so the managed objects are not released
Dispose(false);
}

static Control() {
_defaultSpriteBatchParameters = new SpriteBatchParameters();

Expand Down Expand Up @@ -559,9 +564,14 @@ public Tooltip Tooltip {
get {
if (_tooltip != null && !_tooltip._disposedValue) return _tooltip;

return !string.IsNullOrWhiteSpace(_basicTooltipText)
? _tooltip = new Tooltip(new BasicTooltipView(_basicTooltipText))
: null;
if (!string.IsNullOrWhiteSpace(_basicTooltipText)) {
var tooltip = new Tooltip(new BasicTooltipView(_basicTooltipText));

if (SetProperty(ref _tooltip, tooltip))
return tooltip;
}

return null;
}
set => SetProperty(ref _tooltip, value);
}
Expand Down Expand Up @@ -959,6 +969,12 @@ private void Dispose(bool disposing) {
// Cancel any animations that were currently running on this object
Animation.Tweener.TargetCancel(this);

//Menu will be disposed if there are no other references to it
this.Tooltip = null;

//Menu will be disposed if there are no other references to it
this.Menu = null;

// Remove self from parent object
this.Parent = null;

Expand Down Expand Up @@ -989,8 +1005,14 @@ public void Dispose() {
protected bool SetProperty<T>(ref T property, T newValue, bool invalidateLayout = false, [CallerMemberName] string propertyName = null) {
if (Equals(property, newValue) || propertyName == null) return false;

if(property is IReferenceCountedObject currentRefProp)
currentRefProp.DecrementReference();

property = newValue;

if (property is IReferenceCountedObject newRefProp)
newRefProp.IncrementReference();

OnPropertyChanged(propertyName, invalidateLayout);

return true;
Expand Down
37 changes: 37 additions & 0 deletions Blish HUD/Controls/ReferenceCountedContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Threading;

namespace Blish_HUD.Controls {
public interface IReferenceCountedObject {
void IncrementReference();
void DecrementReference();
bool IsSafeToDispose();
}

public abstract class ReferenceCountedContainer : Container, IReferenceCountedObject {
private int _referenceCount;

public void IncrementReference() {
Interlocked.Increment(ref _referenceCount);
}

public void DecrementReference() {
//Something went wrong and the reference count was not incremented
if(_referenceCount == 0) return;

if (Interlocked.Decrement(ref _referenceCount) == 0) {
Dispose();
}
}

public bool IsSafeToDispose() {
return _referenceCount == 0;
}

protected override void DisposeControl() {
if (!IsSafeToDispose())
return;

base.DisposeControl();
}
}
}
10 changes: 7 additions & 3 deletions Blish HUD/Controls/Tooltip.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -9,7 +9,7 @@
using Microsoft.Xna.Framework.Graphics;

namespace Blish_HUD.Controls {
public class Tooltip : Container, IViewContainer {
public class Tooltip : ReferenceCountedContainer, IViewContainer {

internal const int MOUSE_VERTICAL_MARGIN = 36;

Expand Down Expand Up @@ -49,7 +49,8 @@ private static void HandleMouseMoved(object sender, MouseEventArgs e) {

private static void ControlOnActiveControlChanged(object sender, ControlActivatedEventArgs e) {
foreach (var tooltip in _allTooltips) {
tooltip.Hide();
if(tooltip.Visible)
tooltip.Hide();
}

if (_prevControl != null) {
Expand Down Expand Up @@ -248,6 +249,9 @@ public override void PaintBeforeChildren(SpriteBatch spriteBatch, Rectangle boun

protected override void DisposeControl() {
this.CurrentView?.DoUnload();

if(_allTooltips != null && _allTooltips.Contains(this))
_allTooltips.Remove(this);

foreach (var control in _children) {
control.Resized -= Invalidate;
Expand Down