Skip to content

Commit

Permalink
Don't switch to selected tab on value change (#771)
Browse files Browse the repository at this point in the history
  • Loading branch information
Starlight220 authored Mar 27, 2023
1 parent 1c2afe2 commit 69b2dc2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
@Description(
group = "edu.wpi.first.shuffleboard",
name = "NetworkTables",
version = "2.3.1",
version = "2.3.2",
summary = "Provides sources and widgets for NetworkTables"
)
public class NetworkTablesPlugin extends Plugin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import edu.wpi.first.networktables.NetworkTableType;
import edu.wpi.first.networktables.NetworkTableValue;
import edu.wpi.first.networktables.StringArraySubscriber;
import edu.wpi.first.networktables.StringSubscriber;
import edu.wpi.first.networktables.PubSubOption;
import edu.wpi.first.networktables.Topic;

import java.util.EnumSet;
Expand Down Expand Up @@ -55,6 +57,8 @@ final class TabGenerator {
private final NetworkTableInstance inst;
private StringArraySubscriber tabsSubscriber;
private int tabsListener;
private StringSubscriber tabSelectionSubscriber;
private int tabSelectionListener;
private int metadataListener;
private int dataListener;
private final Components componentRegistry;
Expand All @@ -79,6 +83,21 @@ public void start() {
tabs.dirty();
});

tabSelectionSubscriber = rootMetaTable.getStringTopic(SELECTED_ENTRY_NAME)
.subscribe("", PubSubOption.keepDuplicates(true));
tabSelectionListener = inst.addListener(
tabSelectionSubscriber,
EnumSet.of(NetworkTableEvent.Kind.kValueAll, NetworkTableEvent.Kind.kImmediate),
event -> {
// If the value can be parsed as an int, assume it's the tab index, otherwise assume tab title.
String str = event.valueData.value.getString();
try {
tabs.setSelectedTab(Integer.parseInt(str));
} catch (NumberFormatException e) {
tabs.setSelectedTab(str);
}
});

metadataListener = inst.addListener(
new String[] {METADATA_TABLE_NAME + "/"},
EnumSet.of(NetworkTableEvent.Kind.kValueAll, NetworkTableEvent.Kind.kImmediate),
Expand All @@ -95,6 +114,8 @@ public void start() {
public void stop() {
tabsSubscriber.close();
inst.removeListener(tabsListener);
tabSelectionSubscriber.close();
inst.removeListener(tabSelectionListener);
inst.removeListener(metadataListener);
inst.removeListener(dataListener);
}
Expand All @@ -109,18 +130,6 @@ public TabStructure getStructure() {
private void metadataChanged(NetworkTableEvent event) {
String name = event.valueData.getTopic().getName();

// Special case for global metadata, not tab or widget data
if (name.equals("/Shuffleboard/.metadata/Selected")) {
// If the value can be parsed as an int, assume it's the tab index, otherwise assume tab title.
String str = event.valueData.value.getString();
try {
tabs.setSelectedTab(Integer.parseInt(str));
} catch (NumberFormatException e) {
tabs.setSelectedTab(str);
}
return;
}

List<String> metaHierarchy = NetworkTable.getHierarchy(name);
if (metaHierarchy.size() < 5) {
// Not metadata for a component or a tab, bail
Expand Down Expand Up @@ -206,16 +215,12 @@ private void dataChanged(NetworkTableEvent event) {
.takeWhile(s -> !s.contains("/."))
.collect(Collectors.toList());
if (tables.size() >= 3) {
updateFrom(tables);
updateStructure(tables);
tabs.dirty();
}
return;
}
updateFrom(hierarchy);
}

private void updateFrom(List<String> tables) {
updateStructure(tables);
tabs.dirty();
updateStructure(hierarchy);
}

private void updateStructure(List<String> hierarchy) {
Expand Down

0 comments on commit 69b2dc2

Please sign in to comment.