Add toast notification when copying from clipboard (#752)

and use the toast for showing dictionary on double-long-press word: switch from one ugly workaround (popup menu) to another (hiding more suggestions panel)

Co-authored-by: codokie <@>
Co-authored-by: Helium314 <helium314@mailbox.org>
This commit is contained in:
codokie 2024-05-31 18:01:50 +03:00 committed by GitHub
parent df6af1f07e
commit e357f84572
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 101 additions and 7 deletions

View file

@ -10,14 +10,19 @@ import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import helium314.keyboard.latin.utils.Log;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.ContextThemeWrapper;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodSubtype;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
@ -36,6 +41,7 @@ import helium314.keyboard.latin.settings.Settings;
import helium314.keyboard.latin.settings.SettingsValues;
import helium314.keyboard.latin.utils.CapsModeUtils;
import helium314.keyboard.latin.utils.LanguageOnSpacebarUtils;
import helium314.keyboard.latin.utils.Log;
import helium314.keyboard.latin.utils.RecapitalizeStatus;
import helium314.keyboard.latin.utils.ResourceUtils;
import helium314.keyboard.latin.utils.ScriptUtils;
@ -53,6 +59,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
private HorizontalScrollView mClipboardStripScrollView;
private View mSuggestionStripView;
private ClipboardHistoryView mClipboardHistoryView;
private TextView mFakeToastView;
private LatinIME mLatinIME;
private RichInputMethodManager mRichImm;
private boolean mIsHardwareAcceleratedDrawingEnabled;
@ -467,6 +474,46 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
Settings.getInstance().writeOneHandedModeGravity(mKeyboardViewWrapper.getOneHandedGravity());
}
/**
* Displays a toast message.
*
* @param text The text to display in the toast message.
* @param briefToast If true, the toast duration will be short; otherwise, it will last longer.
*/
public void showToast(final String text, final boolean briefToast){
// In API 32 and below, toasts can be shown without a notification permission.
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.S_V2) {
final int toastLength = briefToast ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG;
final Toast toast = Toast.makeText(mLatinIME, text, toastLength);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
} else {
final int toastLength = briefToast ? 2000 : 3500;
showFakeToast(text, toastLength);
}
}
// Displays a toast-like message with the provided text for a specified duration.
public void showFakeToast(final String text, final int timeMillis) {
if (mFakeToastView.getVisibility() == View.VISIBLE) return;
final Drawable appIcon = mFakeToastView.getCompoundDrawables()[0];
if (appIcon != null) {
final int bound = mFakeToastView.getLineHeight();
appIcon.setBounds(0, 0, bound, bound);
mFakeToastView.setCompoundDrawables(appIcon, null, null, null);
}
mFakeToastView.setText(text);
mFakeToastView.setVisibility(View.VISIBLE);
mFakeToastView.bringToFront();
mFakeToastView.startAnimation(AnimationUtils.loadAnimation(mLatinIME, R.anim.fade_in));
mFakeToastView.postDelayed(() -> {
mFakeToastView.startAnimation(AnimationUtils.loadAnimation(mLatinIME, R.anim.fade_out));
mFakeToastView.setVisibility(View.GONE);
}, timeMillis);
}
// Implements {@link KeyboardState.SwitchActions}.
@Override
public boolean isInDoubleTapShiftKeyTimeout() {
@ -562,6 +609,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
mMainKeyboardFrame = mCurrentInputView.findViewById(R.id.main_keyboard_frame);
mEmojiPalettesView = mCurrentInputView.findViewById(R.id.emoji_palettes_view);
mClipboardHistoryView = mCurrentInputView.findViewById(R.id.clipboard_history_view);
mFakeToastView = mCurrentInputView.findViewById(R.id.fakeToast);
mKeyboardViewWrapper = mCurrentInputView.findViewById(R.id.keyboard_view_wrapper);
mKeyboardViewWrapper.setKeyboardActionListener(mLatinIME.mKeyboardActionListener);

View file

@ -10,11 +10,14 @@ import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.inputmethodservice.InputMethodService;
import android.os.Build;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.CharacterStyle;
import helium314.keyboard.keyboard.KeyboardSwitcher;
import helium314.keyboard.latin.utils.Log;
import android.view.KeyEvent;
import android.view.inputmethod.CompletionInfo;
@ -671,6 +674,9 @@ public final class RichInputConnection implements PrivateCommandPerformer {
if (text == null || text.length() == 0) return;
final ClipboardManager cm = (ClipboardManager) mParent.getSystemService(Context.CLIPBOARD_SERVICE);
cm.setPrimaryClip(ClipData.newPlainText("copied text", text));
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.S_V2) {
KeyboardSwitcher.getInstance().showToast(mParent.getString(R.string.toast_msg_clipboard_copy), true);
}
}
public void commitCorrection(final CorrectionInfo correctionInfo) {

View file

@ -41,6 +41,7 @@ import android.widget.TextView;
import helium314.keyboard.accessibility.AccessibilityUtils;
import helium314.keyboard.keyboard.Keyboard;
import helium314.keyboard.keyboard.KeyboardSwitcher;
import helium314.keyboard.keyboard.MainKeyboardView;
import helium314.keyboard.keyboard.PopupKeysPanel;
import helium314.keyboard.keyboard.internal.keyboard_parser.floris.KeyCode;
@ -445,12 +446,10 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
final SuggestedWordInfo info = mSuggestedWords.getInfo(index);
if (!info.getWord().equals(word)) return;
final String text = info.mSourceDict.mDictType + ":" + info.mSourceDict.mLocale;
// apparently toast is not working on some Android versions, probably
// Android 13 with the notification permission
// Toast.makeText(getContext(), text, Toast.LENGTH_LONG).show();
final PopupMenu uglyWorkaround = new PopupMenu(DialogUtilsKt.getPlatformDialogThemeContext(getContext()), wordView);
uglyWorkaround.getMenu().add(Menu.NONE, 1, Menu.NONE, text);
uglyWorkaround.show();
if (isShowingMoreSuggestionPanel()) {
mMoreSuggestionsView.dismissPopupKeysPanel();
}
KeyboardSwitcher.getInstance().showToast(text, true);
}
private void removeSuggestion(TextView wordView) {

View file

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="150" />

View file

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="150" />

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/toast_background" />
<corners android:radius="24dp" />
</shape>

View file

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
SPDX-License-Identifier: GPL-3.0-only
-->
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginHorizontal="16dp"
android:paddingHorizontal="16dp"
android:paddingVertical="8dp"
android:drawablePadding="12dp"
android:drawableStart="@mipmap/ic_launcher_round"
android:background="@drawable/toast_background"
android:textColor="@android:color/white"
android:textAppearance="?android:textAppearanceMedium"
android:visibility="gone" />

View file

@ -10,6 +10,9 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?attr/inputViewStyle">
<include
android:id="@+id/fakeToast"
layout="@layout/fake_toast" />
<include
android:id="@+id/main_keyboard_frame"
layout="@layout/main_keyboard_frame" />

View file

@ -6,6 +6,7 @@
<color name="foreground">#FFF</color>
<color name="foreground_weak">#BBB</color>
<color name="almost_background">#666</color>
<color name="toast_background">#444444</color>
<color name="keyboard_background">@color/keyboard_background_dark</color>

View file

@ -68,6 +68,7 @@
<color name="foreground">#000</color>
<color name="foreground_weak">#555</color>
<color name="almost_background">#AAA</color>
<color name="toast_background">#666666</color>
<color name="highlight_color_lxx_light">#1A73E8</color> <!-- todo: remove / replace with accent? -->
<color name="keyboard_background">@color/keyboard_background_light</color>

View file

@ -830,4 +830,6 @@ New dictionary:
<string name="var_toolbar_direction">Variable toolbar direction</string>
<!-- Description of the variable toolbar direction setting -->
<string name="var_toolbar_direction_summary">Reverse direction when a right-to-left keyboard subtype is selected</string>
<!-- Toast message shown when content is copied to the clipboard -->
<string name="toast_msg_clipboard_copy">Content copied</string>
</resources>