xptracker: fix out-of-sync add to canvas menu (#9983)

Changes so that `XpInfoBox` uses same method of determining whether to show `Add to canvas` or `Remove from canvas` button by calling `hasOverlay`. Previously `XpInfoBox` would rely solely on its own state which meant that if canvas is added from anywhere else (e.g. skill context menu) this action is not known by the `XpInfoBox`. 

Fixes #9788
This commit is contained in:
Ignas Maslinskas
2019-10-08 10:59:34 +03:00
committed by Tomas Slusny
parent 478df41331
commit a46e1c133d
2 changed files with 31 additions and 7 deletions

View File

@@ -41,6 +41,8 @@ import javax.swing.JPopupMenu;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import lombok.AccessLevel;
import lombok.Getter;
import net.runelite.api.Client;
@@ -139,18 +141,34 @@ class XpInfoBox extends JPanel
popupMenu.add(resetOthers);
popupMenu.add(pauseSkill);
popupMenu.add(canvasItem);
popupMenu.addPopupMenuListener(new PopupMenuListener()
{
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent popupMenuEvent)
{
canvasItem.setText(xpTrackerPlugin.hasOverlay(skill) ? REMOVE_STATE : ADD_STATE);
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent popupMenuEvent)
{
}
@Override
public void popupMenuCanceled(PopupMenuEvent popupMenuEvent)
{
}
});
canvasItem.addActionListener(e ->
{
if (canvasItem.getText().equals(REMOVE_STATE))
{
xpTrackerPlugin.removeOverlay(skill);
canvasItem.setText(ADD_STATE);
}
else
{
xpTrackerPlugin.addOverlay(skill);
canvasItem.setText(REMOVE_STATE);
}
});

View File

@@ -262,6 +262,17 @@ public class XpTrackerPlugin extends Plugin
overlayManager.removeIf(e -> e instanceof XpInfoBoxOverlay && ((XpInfoBoxOverlay) e).getSkill() == skill);
}
/**
* Check if there is an overlay on the canvas for the skill.
*
* @param skill the skill which should have an overlay.
* @return true if the skill has an overlay.
*/
boolean hasOverlay(final Skill skill)
{
return overlayManager.anyMatch(o -> o instanceof XpInfoBoxOverlay && ((XpInfoBoxOverlay) o).getSkill() == skill);
}
/**
* Reset internal state and re-initialize all skills with XP currently cached by the RS client
* This is called by the user manually clicking resetSkillState in the UI.
@@ -708,9 +719,4 @@ public class XpTrackerPlugin extends Plugin
pauseSkill(skill, pause);
}
}
private boolean hasOverlay(final Skill skill)
{
return overlayManager.anyMatch(o -> o instanceof XpInfoBoxOverlay && ((XpInfoBoxOverlay) o).getSkill() == skill);
}
}