Skip to content

Commit

Permalink
Note editor toolbar new item string as icon
Browse files Browse the repository at this point in the history
  • Loading branch information
krmanik committed Jan 30, 2022
1 parent ae0911d commit 47b8340
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
12 changes: 6 additions & 6 deletions AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -1913,8 +1913,7 @@ private void updateToolbar() {

// 0th button shows as '1' and is Ctrl + 1
int visualIndex = b.getIndex() + 1;
String text = Integer.toString(visualIndex);
Drawable bmp = mToolbar.createDrawableForString(text);
Drawable bmp = mToolbar.createDrawableForString(b.getIcon());

View v = mToolbar.insertItem(0, bmp, b.toFormatter());

Expand Down Expand Up @@ -1946,14 +1945,14 @@ private void saveToolbarButtons(ArrayList<CustomToolbarButton> buttons) {
.apply();
}

private void addToolbarButton(String prefix, String suffix) {
if (TextUtils.isEmpty(prefix) && TextUtils.isEmpty(suffix)) {
private void addToolbarButton(String icon, String prefix, String suffix) {
if (TextUtils.isEmpty(icon) && TextUtils.isEmpty(prefix) && TextUtils.isEmpty(suffix)) {
return;
}

ArrayList<CustomToolbarButton> toolbarButtons = getToolbarButtons();

toolbarButtons.add(new CustomToolbarButton(toolbarButtons.size(), prefix, suffix));
toolbarButtons.add(new CustomToolbarButton(toolbarButtons.size(), icon, prefix, suffix));
saveToolbarButtons(toolbarButtons);

updateToolbar();
Expand Down Expand Up @@ -1988,10 +1987,11 @@ private void displayAddToolbarDialog() {
.onNeutral((m, v) -> openUrl(Uri.parse(getString(R.string.link_manual_note_format_toolbar))))
.onPositive((m, v) -> {
View view = m.getView();
EditText etIcon = view.findViewById(R.id.note_editor_toolbar_item_icon);
EditText et = view.findViewById(R.id.note_editor_toolbar_before);
EditText et2 = view.findViewById(R.id.note_editor_toolbar_after);

addToolbarButton(et.getText().toString(), et2.getText().toString());
addToolbarButton(etIcon.getText().toString(), et.getText().toString(), et2.getText().toString());
})
.show();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import com.ichi2.utils.HashUtil.HashSetInit
import timber.log.Timber
import java.util.*

class CustomToolbarButton(var index: Int, private val prefix: String, private val suffix: String) {
class CustomToolbarButton(var index: Int, var icon: String, private val prefix: String, private val suffix: String) {
fun toFormatter(): Toolbar.TextFormatter {
return TextWrapper(prefix, suffix)
}
Expand All @@ -34,7 +34,7 @@ class CustomToolbarButton(var index: Int, private val prefix: String, private va
return null
}
val fields = s.split(Consts.FIELD_SEPARATOR.toRegex(), KEEP_EMPTY_ENTRIES.coerceAtLeast(0)).toTypedArray()
if (fields.size != 3) {
if (fields.size != 4) {
return null
}
val index: Int = try {
Expand All @@ -43,7 +43,7 @@ class CustomToolbarButton(var index: Int, private val prefix: String, private va
Timber.w(e)
return null
}
return CustomToolbarButton(index, fields[1], fields[2])
return CustomToolbarButton(index, fields[1], fields[2], fields[3])
}

@JvmStatic
Expand All @@ -66,7 +66,7 @@ class CustomToolbarButton(var index: Int, private val prefix: String, private va
fun toStringSet(buttons: ArrayList<CustomToolbarButton>): Set<String> {
val ret = HashSetInit<String>(buttons.size)
for (b in buttons) {
val values = arrayOf(b.index.toString(), b.prefix, b.suffix)
val values = arrayOf(b.index.toString(), b.icon, b.prefix, b.suffix)
for (i in values.indices) {
values[i] = values[i].replace(Consts.FIELD_SEPARATOR, "")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@
android:text="@string/toolbar_item_explain_remove"
android:paddingBottom="15dp"/>

<com.google.android.material.textfield.TextInputLayout

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/note_editor_toolbar_icon">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/note_editor_toolbar_item_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/content_vertical_padding"/>
</com.google.android.material.textfield.TextInputLayout>


<com.google.android.material.textfield.TextInputLayout

android:layout_width="match_parent"
Expand Down
1 change: 1 addition & 0 deletions AnkiDroid/src/main/res/values/02-strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@
<string name="insert_mathjax">Insert MathJax Equation</string>
<string name="insert_cloze">Insert Cloze Deletion</string>

<string name="note_editor_toolbar_icon">Character As Icon</string>
<string name="before_text">HTML Before Selection</string>
<string name="after_text">HTML After Selection</string>
<string name="add_toolbar_item">Create Toolbar Item</string>
Expand Down

0 comments on commit 47b8340

Please sign in to comment.