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

Evict unused objects from skin only #281

Merged
merged 1 commit into from
Aug 1, 2023
Merged
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
22 changes: 8 additions & 14 deletions rta/src/main/java/com/gluonhq/richtextarea/ParagraphTile.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Gluon
* Copyright (c) 2022, 2023, Gluon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -288,8 +288,8 @@ void mouseDraggedListener(MouseEvent e) {
});
}

void evictUnusedObjects() {
layers.forEach(Layer::evictUnusedObjects);
void evictUnusedObjects(Set<Font> usedFonts, Set<Image> usedImages) {
layers.forEach(layer -> layer.evictUnusedObjects(usedFonts, usedImages));
}

void updateLayout() {
Expand Down Expand Up @@ -461,26 +461,20 @@ void mouseDraggedListener(MouseEvent e) {
e.consume();
}

void evictUnusedObjects() {
Set<Font> usedFonts = textFlow.getChildren()
void evictUnusedObjects(Set<Font> usedFonts, Set<Image> usedImages) {
usedFonts.addAll(textFlow.getChildren()
.stream()
.filter(Text.class::isInstance)
.map(t -> ((Text) t).getFont())
.filter(Objects::nonNull)
.collect(Collectors.toSet());
List<Font> cachedFonts = new ArrayList<>(richTextAreaSkin.getFontCache().values());
cachedFonts.removeAll(usedFonts);
richTextAreaSkin.getFontCache().values().removeAll(cachedFonts);
.collect(Collectors.toSet()));

Set<Image> usedImages = textFlow.getChildren()
usedImages.addAll(textFlow.getChildren()
.stream()
.filter(ImageView.class::isInstance)
.map(t -> ((ImageView) t).getImage())
.filter(Objects::nonNull)
.collect(Collectors.toSet());
List<Image> cachedImages = new ArrayList<>(richTextAreaSkin.getImageCache().values());
cachedImages.removeAll(usedImages);
richTextAreaSkin.getImageCache().values().removeAll(cachedImages);
.collect(Collectors.toSet()));
}

double getCaretY() {
Expand Down
5 changes: 3 additions & 2 deletions rta/src/main/java/com/gluonhq/richtextarea/RichListCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import java.util.ArrayList;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Function;
Expand Down Expand Up @@ -315,8 +316,8 @@ private ImageView buildImage(ImageDecoration imageDecoration) {
return imageView;
}

void evictUnusedObjects() {
getParagraphTile().ifPresent(ParagraphTile::evictUnusedObjects);
void evictUnusedObjects(Set<Font> usedFonts, Set<Image> usedImages) {
getParagraphTile().ifPresent(tile -> tile.evictUnusedObjects(usedFonts, usedImages));
}

public void forwardDragEvent(MouseEvent e) {
Expand Down
20 changes: 19 additions & 1 deletion rta/src/main/java/com/gluonhq/richtextarea/RichTextAreaSkin.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,20 @@
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.gluonhq.richtextarea.viewmodel.RichTextAreaViewModel.Direction;
import static java.util.Map.entry;
Expand Down Expand Up @@ -447,10 +451,24 @@ protected VirtualFlow<ListCell<Paragraph>> createVirtualFlow() {
}

void evictUnusedObjects() {
Set<Font> usedFonts = new HashSet<>();
Set<Image> usedImages = new HashSet<>();
getSheet().getChildren().stream()
.filter(RichListCell.class::isInstance)
.map(RichListCell.class::cast)
.forEach(RichListCell::evictUnusedObjects);
.forEach(cell -> cell.evictUnusedObjects(usedFonts, usedImages));

List<Font> cachedFonts = new ArrayList<>(getFontCache().values());
cachedFonts.removeAll(usedFonts);
if (!cachedFonts.isEmpty()) {
getFontCache().values().removeAll(cachedFonts);
}

List<Image> cachedImages = new ArrayList<>(getImageCache().values());
cachedImages.removeAll(usedImages);
if (!cachedImages.isEmpty()) {
getImageCache().values().removeAll(cachedImages);
}
}

int getNextRowPosition(double x, boolean down) {
Expand Down