Skip to content

[Lib Manager] Avoid updating the UI at every keystroke #8537

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

Merged
merged 2 commits into from
Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 6 additions & 18 deletions app/src/cc/arduino/contributions/ui/FilterJTextField.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

Expand Down Expand Up @@ -66,35 +68,21 @@ public void focusGained(FocusEvent focusEvent) {
}
});

getDocument().addDocumentListener(new DocumentListener() {
public void removeUpdate(DocumentEvent e) {
applyFilter();
}

public void insertUpdate(DocumentEvent e) {
applyFilter();
}

public void changedUpdate(DocumentEvent e) {
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
applyFilter();
}
});
}

private String lastFilter = "";

private void applyFilter() {
public void applyFilter() {
String filter = showingHint ? "" : getText();
filter = filter.toLowerCase();

// Replace anything but 0-9, a-z, or : with a space
filter = filter.replaceAll("[^\\x30-\\x39^\\x61-\\x7a^\\x3a]", " ");

// Fire event only if the filter is changed
if (filter.equals(lastFilter))
return;

lastFilter = filter;
onFilter(filter.split(" "));
}

Expand Down
1 change: 1 addition & 0 deletions app/src/cc/arduino/contributions/ui/InstallerJDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ public void setFilterText(String filterText) {
listener.focusGained(new FocusEvent(filterField, FocusEvent.FOCUS_GAINED));
}
filterField.setText(filterText);
filterField.applyFilter();
}

public void selectDropdownItemByClassName(String dropdownItem) {
Expand Down