From ffb6c9a7a7ff7f004f7facdf2d79d6b2945050e1 Mon Sep 17 00:00:00 2001 From: Levi Bard Date: Mon, 24 Jun 2019 10:33:05 +0200 Subject: [PATCH] Add @ and # helper buttons to compose activity (#1277) Addresses #810 --- .../keylesspalace/tusky/ComposeActivity.java | 27 ++++++++++++++++++- app/src/main/res/layout/activity_compose.xml | 26 ++++++++++++++++++ app/src/main/res/values/donottranslate.xml | 2 ++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java index be8d67a6..28cea2e5 100644 --- a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java @@ -54,6 +54,7 @@ import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.webkit.MimeTypeMap; +import android.widget.Button; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ImageView; @@ -212,6 +213,8 @@ public final class ComposeActivity private ImageButton contentWarningButton; private ImageButton emojiButton; private ImageButton hideMediaToggle; + private Button atButton; + private Button hashButton; private ComposeOptionsView composeOptionsView; private BottomSheetBehavior composeOptionsBehavior; @@ -267,6 +270,8 @@ public final class ComposeActivity hideMediaToggle = findViewById(R.id.composeHideMediaButton); emojiView = findViewById(R.id.emojiView); emojiList = Collections.emptyList(); + atButton = findViewById(R.id.atButton); + hashButton = findViewById(R.id.hashButton); saveTootHelper = new SaveTootHelper(database.tootDao(), this); @@ -371,6 +376,8 @@ public final class ComposeActivity contentWarningButton.setOnClickListener(v -> onContentWarningChanged()); emojiButton.setOnClickListener(v -> showEmojis()); hideMediaToggle.setOnClickListener(v -> toggleHideMedia()); + atButton.setOnClickListener(v -> atButtonClicked()); + hashButton.setOnClickListener(v -> hashButtonClicked()); TextView actionPhotoTake = findViewById(R.id.action_photo_take); TextView actionPhotoPick = findViewById(R.id.action_photo_pick); @@ -693,6 +700,24 @@ public final class ComposeActivity textEditor.requestFocus(); } + private void replaceTextAtCaret(CharSequence text) { + // If you select "backward" in an editable, you get SelectionStart > SelectionEnd + int start = Math.min(textEditor.getSelectionStart(), textEditor.getSelectionEnd()); + int end = Math.max(textEditor.getSelectionStart(), textEditor.getSelectionEnd()); + textEditor.getText().replace(start, end, text); + + // Set the cursor after the inserted text + textEditor.setSelection(start + text.length()); + } + + private void atButtonClicked() { + replaceTextAtCaret("@"); + } + + private void hashButtonClicked() { + replaceTextAtCaret("#"); + } + @Override protected void onSaveInstanceState(Bundle outState) { ArrayList savedMediaQueued = new ArrayList<>(); @@ -1785,7 +1810,7 @@ public final class ComposeActivity @Override public void onEmojiSelected(@NotNull String shortcode) { - textEditor.getText().insert(textEditor.getSelectionStart(), ":" + shortcode + ": "); + replaceTextAtCaret(":" + shortcode + ": "); } private void loadCachedInstanceMetadata(@NotNull AccountEntity activeAccount) { diff --git a/app/src/main/res/layout/activity_compose.xml b/app/src/main/res/layout/activity_compose.xml index b61c8dfc..33a5b35f 100644 --- a/app/src/main/res/layout/activity_compose.xml +++ b/app/src/main/res/layout/activity_compose.xml @@ -21,6 +21,32 @@ android:padding="8dp" tools:ignore="ContentDescription" /> + + + + com.keylesspalace.tusky.PREFERENCES <b>%1$s</b><br>%2$s + \@ + # public