diff options
| author | shedaniel <daniel@shedaniel.me> | 2022-12-03 22:17:27 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2022-12-12 21:01:07 +0800 |
| commit | 47ae69cd83f36aa5acf4bfedf9bbccc0eec34b67 (patch) | |
| tree | b111b75620e263fa5a027241b03ebbe122f90669 /api/src | |
| parent | 2caa595aa94d46ddaefc5ecdb79f7ec834f7eae8 (diff) | |
| download | RoughlyEnoughItems-47ae69cd83f36aa5acf4bfedf9bbccc0eec34b67.tar.gz RoughlyEnoughItems-47ae69cd83f36aa5acf4bfedf9bbccc0eec34b67.tar.bz2 RoughlyEnoughItems-47ae69cd83f36aa5acf4bfedf9bbccc0eec34b67.zip | |
Add init progress to IME
Diffstat (limited to 'api/src')
| -rw-r--r-- | api/src/main/java/me/shedaniel/rei/api/client/search/method/CharacterUnpackingInputMethod.java | 5 | ||||
| -rw-r--r-- | api/src/main/java/me/shedaniel/rei/api/client/search/method/InputMethod.java | 97 |
2 files changed, 99 insertions, 3 deletions
diff --git a/api/src/main/java/me/shedaniel/rei/api/client/search/method/CharacterUnpackingInputMethod.java b/api/src/main/java/me/shedaniel/rei/api/client/search/method/CharacterUnpackingInputMethod.java index f14e1b334..f7f1779f8 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/search/method/CharacterUnpackingInputMethod.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/search/method/CharacterUnpackingInputMethod.java @@ -45,9 +45,8 @@ public interface CharacterUnpackingInputMethod extends InputMethod<IntList> { default String suggestInputString(String str) { return str.codePoints().mapToObj(c -> { List<ExpendedChar> chars = expendSourceChar(c); - String result; - if (chars.isEmpty()) return result = ((char) c) + ""; - result = chars.get(0).phonemes().stream() + if (chars.isEmpty()) return ((char) c) + ""; + String result = chars.get(0).phonemes().stream() .flatMap(integers -> integers.intStream().mapToObj(value -> ((char) value) + "")) .collect(Collectors.joining()); if (result.codePointCount(0, result.length()) == 1 && result.codePointAt(0) == c) { diff --git a/api/src/main/java/me/shedaniel/rei/api/client/search/method/InputMethod.java b/api/src/main/java/me/shedaniel/rei/api/client/search/method/InputMethod.java index 90e6ba092..fc81acd33 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/search/method/InputMethod.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/search/method/InputMethod.java @@ -34,35 +34,132 @@ import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; +/** + * An input method to match a search filter to another source input. + * + * @param <T> the type of expansion from the search filter + * @see me.shedaniel.rei.impl.client.search.method.DefaultInputMethod + */ @ApiStatus.Experimental public interface InputMethod<T> { + /** + * Returns the active input method. + * + * @return the active input method + */ static InputMethod<?> active() { return InputMethodRegistry.getInstance().getOrDefault(ConfigObject.getInstance().getInputMethodId()); } + /** + * Returns the list of all Minecraft supported locales. + * + * @return the list of all Minecraft supported locales + */ static List<Locale> getAllLocales() { return CollectionUtils.map(Minecraft.getInstance().getLanguageManager().getLanguages(), info -> new Locale(info.getCode(), Component.literal(info.getName()))); } + /** + * Returns the list of locales that are supported by this input method. + * You should use {@link #getAllLocales()} to get the list of all Minecraft supported locales. + * + * @return the list of locales that are supported by this input method + */ List<Locale> getMatchingLocales(); + /** + * Returns all the possible expansions from the search filter. + * + * @param filter the search filter + * @return all the possible expansions from the search filter + */ Iterable<T> expendFilter(String filter); + /** + * Returns whether the search filter matches the input. + * + * @param str the input + * @param substr the expanded search filter + * @return whether the search filter matches the input + */ boolean contains(String str, T substr); + /** + * Returns a suggested expansion from the search filter. + * + * @param str the input + * @return a suggested expansion from the search filter, or {@code null} if no suggestion is available + */ @Nullable default String suggestInputString(String str) { return null; } + /** + * Prepares the input method for activation. + * + * @param executor the executor to run the preparation on + * @return a future that completes when the preparation is done + */ CompletableFuture<Void> prepare(Executor executor); + /** + * Prepares the input method for activation. + * + * @param executor the executor to run the preparation on + * @param progressCallback the callback to call when the progress is updated + * @return a future that completes when the preparation is done + */ + default CompletableFuture<Void> prepare(Executor executor, ProgressCallback progressCallback) { + progressCallback.onProgress(0.0); + return prepare(executor).whenComplete((aVoid, throwable) -> progressCallback.onProgress(1.0)); + } + + /** + * Disposes the input method. + * + * @param executor the executor to run the disposal on + * @return a future that completes when the disposal is done + */ CompletableFuture<Void> dispose(Executor executor); + /** + * Disposes the input method. + * + * @param executor the executor to run the disposal on + * @param progressCallback the callback to call when the progress is updated + * @return a future that completes when the disposal is done + */ + default CompletableFuture<Void> dispose(Executor executor, ProgressCallback progressCallback) { + progressCallback.onProgress(0.0); + return dispose(executor).whenComplete((aVoid, throwable) -> progressCallback.onProgress(1.0)); + } + + /** + * Returns the name of this input method. + * + * @return the name of this input method + */ Component getName(); + /** + * Returns the description of this input method. + * + * @return the description of this input method + */ Component getDescription(); record Locale(String code, Component name) {} + + @FunctionalInterface + interface ProgressCallback { + /** + * Called when the progress of the preparation is updated. + * + * @param progress the progress of the preparation, between 0.0 and 1.0 + */ + void onProgress(double progress); + } } |
