|
| 1 | +package org.jackhuang.hmcl.ui.versions; |
| 2 | + |
| 3 | +import com.jfoenix.controls.JFXButton; |
| 4 | +import com.jfoenix.controls.JFXListView; |
| 5 | +import javafx.beans.binding.Bindings; |
| 6 | +import javafx.geometry.Insets; |
| 7 | +import javafx.geometry.Pos; |
| 8 | +import javafx.scene.control.Skin; |
| 9 | +import javafx.scene.control.SkinBase; |
| 10 | +import javafx.scene.image.Image; |
| 11 | +import javafx.scene.image.ImageView; |
| 12 | +import javafx.scene.layout.BorderPane; |
| 13 | +import javafx.scene.layout.HBox; |
| 14 | +import javafx.scene.layout.Priority; |
| 15 | +import javafx.scene.layout.StackPane; |
| 16 | +import javafx.stage.FileChooser; |
| 17 | +import org.jackhuang.hmcl.mod.LocalModFile; |
| 18 | +import org.jackhuang.hmcl.resourcepack.ResourcepackFile; |
| 19 | +import org.jackhuang.hmcl.setting.Profile; |
| 20 | +import org.jackhuang.hmcl.task.Schedulers; |
| 21 | +import org.jackhuang.hmcl.task.Task; |
| 22 | +import org.jackhuang.hmcl.ui.Controllers; |
| 23 | +import org.jackhuang.hmcl.ui.FXUtils; |
| 24 | +import org.jackhuang.hmcl.ui.ListPageBase; |
| 25 | +import org.jackhuang.hmcl.ui.SVG; |
| 26 | +import org.jackhuang.hmcl.ui.construct.*; |
| 27 | +import org.jackhuang.hmcl.util.Holder; |
| 28 | +import org.jackhuang.hmcl.util.io.FileUtils; |
| 29 | + |
| 30 | +import java.io.ByteArrayInputStream; |
| 31 | +import java.io.IOException; |
| 32 | +import java.lang.ref.WeakReference; |
| 33 | +import java.nio.file.Files; |
| 34 | +import java.nio.file.Path; |
| 35 | +import java.util.Comparator; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Objects; |
| 38 | +import java.util.stream.Stream; |
| 39 | + |
| 40 | +import static org.jackhuang.hmcl.ui.ToolbarListPageSkin.createToolbarButton2; |
| 41 | +import static org.jackhuang.hmcl.util.i18n.I18n.i18n; |
| 42 | +import static org.jackhuang.hmcl.util.logging.Logger.LOG; |
| 43 | + |
| 44 | +public final class ResourcepackListPage extends ListPageBase<ResourcepackListPage.ResourcepackInfoObject> implements VersionPage.VersionLoadable { |
| 45 | + private Path resourcepackDirectory; |
| 46 | + |
| 47 | + public ResourcepackListPage() { |
| 48 | + FXUtils.applyDragListener(this, file -> file.getFileName().toString().endsWith(".zip"), this::addFiles); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + protected Skin<?> createDefaultSkin() { |
| 53 | + return new ResourcepackListPageSkin(this); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void loadVersion(Profile profile, String version) { |
| 58 | + this.resourcepackDirectory = profile.getRepository().getResourcepacksDirectory(version); |
| 59 | + |
| 60 | + try { |
| 61 | + if (!Files.exists(resourcepackDirectory)) { |
| 62 | + Files.createDirectories(resourcepackDirectory); |
| 63 | + } |
| 64 | + } catch (IOException e) { |
| 65 | + LOG.warning("Failed to create resourcepack directory" + resourcepackDirectory, e); |
| 66 | + } |
| 67 | + refresh(); |
| 68 | + } |
| 69 | + |
| 70 | + public void refresh() { |
| 71 | + if (resourcepackDirectory == null || !Files.isDirectory(resourcepackDirectory)) return; |
| 72 | + setLoading(true); |
| 73 | + Task.supplyAsync(Schedulers.io(), () -> { |
| 74 | + try (Stream<Path> stream = Files.list(resourcepackDirectory)) { |
| 75 | + return stream.sorted(Comparator.comparing(FileUtils::getName)) |
| 76 | + .flatMap(item -> { |
| 77 | + try { |
| 78 | + return Stream.of(ResourcepackFile.parse(item)).filter(Objects::nonNull).map(ResourcepackInfoObject::new); |
| 79 | + } catch (IOException e) { |
| 80 | + LOG.warning("Failed to load resourcepack " + item, e); |
| 81 | + return Stream.empty(); |
| 82 | + } |
| 83 | + }) |
| 84 | + .toList(); |
| 85 | + } |
| 86 | + }).whenComplete(Schedulers.javafx(), ((result, exception) -> { |
| 87 | + if (exception == null) { |
| 88 | + getItems().setAll(result); |
| 89 | + } else { |
| 90 | + LOG.warning("Failed to load resourcepacks", exception); |
| 91 | + getItems().clear(); |
| 92 | + } |
| 93 | + setLoading(false); |
| 94 | + })).start(); |
| 95 | + } |
| 96 | + |
| 97 | + public void addFiles(List<Path> files) { |
| 98 | + if (resourcepackDirectory == null) return; |
| 99 | + |
| 100 | + try { |
| 101 | + for (Path file : files) { |
| 102 | + Path target = resourcepackDirectory.resolve(file.getFileName()); |
| 103 | + if (!Files.exists(target)) { |
| 104 | + Files.copy(file, target); |
| 105 | + } |
| 106 | + } |
| 107 | + } catch (IOException e) { |
| 108 | + LOG.warning("Failed to add resourcepacks", e); |
| 109 | + Controllers.dialog(i18n("resourcepack.add.failed"), i18n("message.error"), MessageDialogPane.MessageType.ERROR); |
| 110 | + } |
| 111 | + |
| 112 | + refresh(); |
| 113 | + } |
| 114 | + |
| 115 | + public void onAddFiles() { |
| 116 | + FileChooser fileChooser = new FileChooser(); |
| 117 | + fileChooser.setTitle(i18n("resourcepack.add")); |
| 118 | + fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter(i18n("resourcepack"), "*.zip")); |
| 119 | + List<Path> files = FileUtils.toPaths(fileChooser.showOpenMultipleDialog(Controllers.getStage())); |
| 120 | + if (files != null && !files.isEmpty()) { |
| 121 | + addFiles(files); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + private void onDownload() { |
| 126 | + Controllers.getDownloadPage().showResourcepackDownloads(); |
| 127 | + Controllers.navigate(Controllers.getDownloadPage()); |
| 128 | + } |
| 129 | + |
| 130 | + private static final class ResourcepackListPageSkin extends SkinBase<ResourcepackListPage> { |
| 131 | + private final JFXListView<ResourcepackInfoObject> listView; |
| 132 | + |
| 133 | + private ResourcepackListPageSkin(ResourcepackListPage control) { |
| 134 | + super(control); |
| 135 | + |
| 136 | + StackPane pane = new StackPane(); |
| 137 | + pane.setPadding(new Insets(10)); |
| 138 | + pane.getStyleClass().addAll("notice-pane"); |
| 139 | + |
| 140 | + ComponentList root = new ComponentList(); |
| 141 | + root.getStyleClass().add("no-padding"); |
| 142 | + listView = new JFXListView<>(); |
| 143 | + |
| 144 | + HBox toolbar = new HBox(); |
| 145 | + toolbar.setAlignment(Pos.CENTER_LEFT); |
| 146 | + toolbar.setPickOnBounds(false); |
| 147 | + toolbar.getChildren().setAll( |
| 148 | + createToolbarButton2(i18n("button.refresh"), SVG.REFRESH, control::refresh), |
| 149 | + createToolbarButton2(i18n("resourcepack.add"), SVG.ADD, control::onAddFiles), |
| 150 | + createToolbarButton2(i18n("resourcepack.download"), SVG.DOWNLOAD, control::onDownload) |
| 151 | + ); |
| 152 | + root.getContent().add(toolbar); |
| 153 | + |
| 154 | + SpinnerPane center = new SpinnerPane(); |
| 155 | + ComponentList.setVgrow(center, Priority.ALWAYS); |
| 156 | + center.getStyleClass().add("large-spinner-pane"); |
| 157 | + center.loadingProperty().bind(control.loadingProperty()); |
| 158 | + |
| 159 | + Holder<Object> lastCell = new Holder<>(); |
| 160 | + listView.setCellFactory(x -> new ResourcepackListCell(listView, lastCell, control)); |
| 161 | + Bindings.bindContent(listView.getItems(), control.getItems()); |
| 162 | + |
| 163 | + center.setContent(listView); |
| 164 | + root.getContent().add(center); |
| 165 | + |
| 166 | + pane.getChildren().setAll(root); |
| 167 | + getChildren().setAll(pane); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + public static class ResourcepackInfoObject { |
| 172 | + private final ResourcepackFile file; |
| 173 | + private WeakReference<Image> iconCache; |
| 174 | + |
| 175 | + public ResourcepackInfoObject(ResourcepackFile file) { |
| 176 | + this.file = file; |
| 177 | + } |
| 178 | + |
| 179 | + public ResourcepackFile getFile() { |
| 180 | + return file; |
| 181 | + } |
| 182 | + |
| 183 | + Image getIcon() { |
| 184 | + Image image = null; |
| 185 | + if (iconCache != null && (image = iconCache.get()) != null) { |
| 186 | + return image; |
| 187 | + } |
| 188 | + byte[] iconData = file.getIcon(); |
| 189 | + if (iconData != null) { |
| 190 | + try (ByteArrayInputStream inputStream = new ByteArrayInputStream(iconData)) { |
| 191 | + image = new Image(inputStream, 64, 64, true, true); |
| 192 | + } catch (Exception e) { |
| 193 | + LOG.warning("Failed to load resourcepack icon " + file.getPath(), e); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + if (image == null || image.isError() || image.getWidth() <= 0 || image.getHeight() <= 0 || |
| 198 | + (Math.abs(image.getWidth() - image.getHeight()) >= 1)) { |
| 199 | + image = FXUtils.newBuiltinImage("/assets/img/unknown_pack.png"); |
| 200 | + } |
| 201 | + iconCache = new WeakReference<>(image); |
| 202 | + return image; |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + private static final class ResourcepackListCell extends MDListCell<ResourcepackInfoObject> { |
| 207 | + private final ImageView imageView = new ImageView(); |
| 208 | + private final TwoLineListItem content = new TwoLineListItem(); |
| 209 | + private final JFXButton btnReveal = new JFXButton(); |
| 210 | + private final JFXButton btnDelete = new JFXButton(); |
| 211 | + private final ResourcepackListPage page; |
| 212 | + |
| 213 | + public ResourcepackListCell(JFXListView<ResourcepackInfoObject> listView, Holder<Object> lastCell, ResourcepackListPage page) { |
| 214 | + super(listView, lastCell); |
| 215 | + |
| 216 | + this.page = page; |
| 217 | + |
| 218 | + BorderPane root = new BorderPane(); |
| 219 | + root.getStyleClass().add("md-list-cell"); |
| 220 | + root.setPadding(new Insets(8)); |
| 221 | + |
| 222 | + HBox left = new HBox(8); |
| 223 | + left.setAlignment(Pos.CENTER); |
| 224 | + FXUtils.limitSize(imageView, 32, 32); |
| 225 | + left.getChildren().add(imageView); |
| 226 | + left.setPadding(new Insets(0, 8, 0, 0)); |
| 227 | + FXUtils.setLimitWidth(left, 48); |
| 228 | + root.setLeft(left); |
| 229 | + |
| 230 | + HBox.setHgrow(content, Priority.ALWAYS); |
| 231 | + root.setCenter(content); |
| 232 | + |
| 233 | + btnReveal.getStyleClass().add("toggle-icon4"); |
| 234 | + btnReveal.setGraphic(SVG.FOLDER_OPEN.createIcon()); |
| 235 | + |
| 236 | + btnDelete.getStyleClass().add("toggle-icon4"); |
| 237 | + btnDelete.setGraphic(SVG.DELETE_FOREVER.createIcon()); |
| 238 | + |
| 239 | + HBox right = new HBox(8); |
| 240 | + right.setAlignment(Pos.CENTER_RIGHT); |
| 241 | + right.getChildren().setAll(btnReveal, btnDelete); |
| 242 | + root.setRight(right); |
| 243 | + |
| 244 | + getContainer().getChildren().add(new RipplerContainer(root)); |
| 245 | + } |
| 246 | + |
| 247 | + @Override |
| 248 | + protected void updateControl(ResourcepackListPage.ResourcepackInfoObject item, boolean empty) { |
| 249 | + if (empty || item == null) { |
| 250 | + return; |
| 251 | + } |
| 252 | + |
| 253 | + ResourcepackFile file = item.getFile(); |
| 254 | + imageView.setImage(item.getIcon()); |
| 255 | + |
| 256 | + content.setTitle(file.getName()); |
| 257 | + LocalModFile.Description description = file.getDescription(); |
| 258 | + content.setSubtitle(description != null ? description.toString() : ""); |
| 259 | + |
| 260 | + FXUtils.installFastTooltip(btnReveal, i18n("reveal.in_file_manager")); |
| 261 | + btnReveal.setOnAction(event -> FXUtils.showFileInExplorer(file.getPath())); |
| 262 | + |
| 263 | + btnDelete.setOnAction(event -> |
| 264 | + Controllers.confirm(i18n("button.remove.confirm"), i18n("button.remove"), |
| 265 | + () -> onDelete(file), null)); |
| 266 | + } |
| 267 | + |
| 268 | + private void onDelete(ResourcepackFile file) { |
| 269 | + try { |
| 270 | + if (Files.isDirectory(file.getPath())) { |
| 271 | + FileUtils.deleteDirectory(file.getPath()); |
| 272 | + } else { |
| 273 | + Files.delete(file.getPath()); |
| 274 | + } |
| 275 | + page.refresh(); |
| 276 | + } catch (IOException e) { |
| 277 | + Controllers.dialog(i18n("resourcepack.delete.failed", e.getMessage()), i18n("message.error"), MessageDialogPane.MessageType.ERROR); |
| 278 | + LOG.warning("Failed to delete resourcepack", e); |
| 279 | + } |
| 280 | + } |
| 281 | + } |
| 282 | +} |
0 commit comments