Search in sources :

Example 1 with CustomToolbarButton

use of com.ichi2.anki.noteeditor.CustomToolbarButton in project AnkiChinaAndroid by ankichinateam.

the class NoteEditor method updateToolbar.

private void updateToolbar() {
    if (mToolbar == null) {
        return;
    }
    if (shouldHideToolbar()) {
        mToolbar.setVisibility(View.GONE);
        return;
    } else {
        mToolbar.setVisibility(View.VISIBLE);
    }
    mToolbar.clearCustomItems();
    View clozeIcon = mToolbar.getClozeIcon();
    if (Models.isCloze(mEditorNote.model())) {
        Toolbar.TextFormatter clozeFormatter = s -> {
            Toolbar.TextWrapper.StringFormat stringFormat = new Toolbar.TextWrapper.StringFormat();
            String prefix = "{{c" + getNextClozeIndex() + "::";
            stringFormat.result = prefix + s + "}}";
            if (s.length() == 0) {
                stringFormat.start = prefix.length();
                stringFormat.end = prefix.length();
            } else {
                stringFormat.start = 0;
                stringFormat.end = stringFormat.result.length();
            }
            return stringFormat;
        };
        clozeIcon.setOnClickListener(l -> mToolbar.onFormat(clozeFormatter));
        clozeIcon.setVisibility(View.VISIBLE);
    } else {
        clozeIcon.setVisibility(View.GONE);
    }
    ArrayList<CustomToolbarButton> buttons = getToolbarButtons();
    for (CustomToolbarButton b : buttons) {
        // 0th button shows as '1' and is Ctrl + 1
        int visualIndex = b.getIndex() + 1;
        String text = Integer.toString(visualIndex);
        Drawable bmp = mToolbar.createDrawableForString(text);
        View v = mToolbar.insertItem(0, bmp, b.toFormatter());
        // Allow Ctrl + 1...Ctrl + 0 for item 10.
        v.setTag(Integer.toString(visualIndex % 10));
        v.setOnLongClickListener(discard -> {
            suggestRemoveButton(b);
            return true;
        });
    }
    // Let the user add more buttons (always at the end).
    mToolbar.insertItem(0, R.drawable.ic_add_toolbar_icon, this::displayAddToolbarDialog);
}
Also used : Arrays(java.util.Arrays) Bundle(android.os.Bundle) TagsDialog(com.ichi2.anki.dialogs.TagsDialog) Deck(com.ichi2.libanki.Deck) NonNull(androidx.annotation.NonNull) Uri(android.net.Uri) Drawable(android.graphics.drawable.Drawable) AudioRecordingField(com.ichi2.anki.multimediacard.fields.AudioRecordingField) Map(java.util.Map) ClipboardManager(android.content.ClipboardManager) ContextCompat(androidx.core.content.ContextCompat) TargetApi(android.annotation.TargetApi) FieldChangeType(com.ichi2.anki.noteeditor.FieldState.FieldChangeType) IntentFilter(android.content.IntentFilter) IField(com.ichi2.anki.multimediacard.fields.IField) Set(java.util.Set) JSONObject(com.ichi2.utils.JSONObject) PopupMenuWithIcons(com.ichi2.anki.widgets.PopupMenuWithIcons) StringRes(androidx.annotation.StringRes) Nullable(androidx.annotation.Nullable) Consts(com.ichi2.libanki.Consts) CompatHelper(com.ichi2.compat.CompatHelper) DiscardChangesDialog(com.ichi2.anki.dialogs.DiscardChangesDialog) Models(com.ichi2.libanki.Models) TextWatcher(android.text.TextWatcher) DialogFragment(androidx.fragment.app.DialogFragment) ClozeUtils(com.ichi2.libanki.Note.ClozeUtils) KeyUtils(com.ichi2.utils.KeyUtils) Direction(com.ichi2.anim.ActivityTransitionAnimation.Direction) IMultimediaEditableNote(com.ichi2.anki.multimediacard.IMultimediaEditableNote) SdCardReceiver(com.ichi2.anki.receiver.SdCardReceiver) Editable(android.text.Editable) ImageField(com.ichi2.anki.multimediacard.fields.ImageField) ArrayList(java.util.ArrayList) NOT_FOUND_NOTE_TYPE(com.ichi2.libanki.Models.NOT_FOUND_NOTE_TYPE) MenuInflater(android.view.MenuInflater) Menu(android.view.Menu) IntegerDialog(com.ichi2.anki.dialogs.IntegerDialog) CheckResult(androidx.annotation.CheckResult) ContentResolverUtil(com.ichi2.utils.ContentResolverUtil) TextUtils(android.text.TextUtils) IOException(java.io.IOException) Toolbar(com.ichi2.anki.noteeditor.Toolbar) CustomToolbarButton(com.ichi2.anki.noteeditor.CustomToolbarButton) File(java.io.File) TextViewUtil(com.ichi2.utils.TextViewUtil) ArrayAdapter(android.widget.ArrayAdapter) SharedPreferences(android.content.SharedPreferences) FileUtil(com.ichi2.utils.FileUtil) ActivityTransitionAnimation(com.ichi2.anim.ActivityTransitionAnimation) EditText(android.widget.EditText) RequiresApi(androidx.annotation.RequiresApi) ImageButton(android.widget.ImageButton) ActionMode(android.view.ActionMode) LinearLayout(android.widget.LinearLayout) MultimediaEditFieldActivity(com.ichi2.anki.multimediacard.activity.MultimediaEditFieldActivity) WindowManager(android.view.WindowManager) ConfirmationDialog(com.ichi2.anki.dialogs.ConfirmationDialog) Locale(java.util.Locale) EFieldType(com.ichi2.anki.multimediacard.fields.EFieldType) View(android.view.View) TaskData(com.ichi2.async.TaskData) AdapterView(android.widget.AdapterView) MultimediaEditableNote(com.ichi2.anki.multimediacard.impl.MultimediaEditableNote) LocaleSelectionDialog(com.ichi2.anki.dialogs.LocaleSelectionDialog) EXTRA_PROCESS_TEXT(com.ichi2.compat.Compat.EXTRA_PROCESS_TEXT) BroadcastReceiver(android.content.BroadcastReceiver) FieldState(com.ichi2.anki.noteeditor.FieldState) Timber(timber.log.Timber) TextField(com.ichi2.anki.multimediacard.fields.TextField) List(java.util.List) TextView(android.widget.TextView) OnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener) TaskListenerWithContext(com.ichi2.async.TaskListenerWithContext) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Typeface(android.graphics.Typeface) Note(com.ichi2.libanki.Note) Context(android.content.Context) KeyEvent(android.view.KeyEvent) AudioClipField(com.ichi2.anki.multimediacard.fields.AudioClipField) Pair(android.util.Pair) NamedJSONComparator(com.ichi2.utils.NamedJSONComparator) Intent(android.content.Intent) HashMap(java.util.HashMap) Collection(com.ichi2.libanki.Collection) StyledProgressDialog(com.ichi2.themes.StyledProgressDialog) MenuItem(android.view.MenuItem) InputMethodManager(android.view.inputmethod.InputMethodManager) MapUtil(com.ichi2.utils.MapUtil) HashSet(java.util.HashSet) SuppressLint(android.annotation.SuppressLint) JSONArray(com.ichi2.utils.JSONArray) WidgetStatus(com.ichi2.widget.WidgetStatus) Model(com.ichi2.libanki.Model) Build(android.os.Build) LinkedList(java.util.LinkedList) DeckComparator(com.ichi2.utils.DeckComparator) Utils(com.ichi2.libanki.Utils) ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) NoteService(com.ichi2.anki.servicelayer.NoteService) Consumer(com.ichi2.utils.FunctionalInterfaces.Consumer) CollectionTask(com.ichi2.async.CollectionTask) Themes(com.ichi2.themes.Themes) Spinner(android.widget.Spinner) PopupMenu(androidx.appcompat.widget.PopupMenu) ACTION_PROCESS_TEXT(com.ichi2.compat.Compat.ACTION_PROCESS_TEXT) ADD_NOTE(com.ichi2.async.CollectionTask.TASK_TYPE.ADD_NOTE) AdaptionUtil(com.ichi2.utils.AdaptionUtil) Card(com.ichi2.libanki.Card) VisibleForTesting(androidx.annotation.VisibleForTesting) Collections(java.util.Collections) NoteFieldDecorator(com.ichi2.utils.NoteFieldDecorator) Resources(android.content.res.Resources) InputStream(java.io.InputStream) CustomToolbarButton(com.ichi2.anki.noteeditor.CustomToolbarButton) Drawable(android.graphics.drawable.Drawable) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) SuppressLint(android.annotation.SuppressLint) Toolbar(com.ichi2.anki.noteeditor.Toolbar)

Example 2 with CustomToolbarButton

use of com.ichi2.anki.noteeditor.CustomToolbarButton in project Anki-Android by ankidroid.

the class NoteEditor method updateToolbar.

private void updateToolbar() {
    if (mToolbar == null) {
        return;
    }
    View editorLayout = findViewById(R.id.note_editor_layout);
    int bottomMargin = shouldHideToolbar() ? 0 : (int) getResources().getDimension(R.dimen.note_editor_toolbar_height);
    MarginLayoutParams params = (MarginLayoutParams) editorLayout.getLayoutParams();
    params.bottomMargin = bottomMargin;
    editorLayout.setLayoutParams(params);
    if (shouldHideToolbar()) {
        mToolbar.setVisibility(View.GONE);
        return;
    } else {
        mToolbar.setVisibility(View.VISIBLE);
    }
    mToolbar.clearCustomItems();
    View clozeIcon = mToolbar.getClozeIcon();
    if (mEditorNote.model().isCloze()) {
        Toolbar.TextFormatter clozeFormatter = s -> {
            Toolbar.TextWrapper.StringFormat stringFormat = new Toolbar.TextWrapper.StringFormat();
            String prefix = "{{c" + getNextClozeIndex() + "::";
            stringFormat.result = prefix + s + "}}";
            if (s.length() == 0) {
                stringFormat.start = prefix.length();
                stringFormat.end = prefix.length();
            } else {
                stringFormat.start = 0;
                stringFormat.end = stringFormat.result.length();
            }
            return stringFormat;
        };
        clozeIcon.setOnClickListener(l -> mToolbar.onFormat(clozeFormatter));
        clozeIcon.setVisibility(View.VISIBLE);
    } else {
        clozeIcon.setVisibility(View.GONE);
    }
    ArrayList<CustomToolbarButton> buttons = getToolbarButtons();
    for (CustomToolbarButton b : buttons) {
        // 0th button shows as '1' and is Ctrl + 1
        int visualIndex = b.getIndex() + 1;
        String text = Integer.toString(visualIndex);
        if (!b.getButtonText().isEmpty()) {
            text = b.getButtonText();
        }
        Drawable bmp = mToolbar.createDrawableForString(text);
        View v = mToolbar.insertItem(0, bmp, b.toFormatter());
        // Allow Ctrl + 1...Ctrl + 0 for item 10.
        v.setTag(Integer.toString(visualIndex % 10));
        v.setOnLongClickListener(discard -> {
            suggestRemoveButton(b);
            return true;
        });
    }
    // Let the user add more buttons (always at the end).
    // Sets the add custom tag icon color.
    final Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_add_toolbar_icon, null);
    drawable.setTint(Themes.getColorFromAttr(NoteEditor.this, R.attr.toolbarIconColor));
    mToolbar.insertItem(0, drawable, this::displayAddToolbarDialog);
}
Also used : Arrays(java.util.Arrays) Bundle(android.os.Bundle) Deck(com.ichi2.libanki.Deck) NonNull(androidx.annotation.NonNull) Uri(android.net.Uri) Drawable(android.graphics.drawable.Drawable) AudioRecordingField(com.ichi2.anki.multimediacard.fields.AudioRecordingField) MarginLayoutParams(android.view.ViewGroup.MarginLayoutParams) Map(java.util.Map) ClipboardManager(android.content.ClipboardManager) ContextCompat(androidx.core.content.ContextCompat) TargetApi(android.annotation.TargetApi) FieldChangeType(com.ichi2.anki.noteeditor.FieldState.FieldChangeType) IntentFilter(android.content.IntentFilter) IField(com.ichi2.anki.multimediacard.fields.IField) Set(java.util.Set) JSONObject(com.ichi2.utils.JSONObject) PopupMenuWithIcons(com.ichi2.anki.widgets.PopupMenuWithIcons) StringRes(androidx.annotation.StringRes) Nullable(androidx.annotation.Nullable) TagsDialogListener(com.ichi2.anki.dialogs.tags.TagsDialogListener) Consts(com.ichi2.libanki.Consts) CompatHelper(com.ichi2.compat.CompatHelper) TagsDialogFactory(com.ichi2.anki.dialogs.tags.TagsDialogFactory) DiscardChangesDialog(com.ichi2.anki.dialogs.DiscardChangesDialog) Models(com.ichi2.libanki.Models) TextWatcher(android.text.TextWatcher) ClozeUtils(com.ichi2.libanki.Note.ClozeUtils) KeyUtils(com.ichi2.utils.KeyUtils) TagsDialog(com.ichi2.anki.dialogs.tags.TagsDialog) Direction(com.ichi2.anim.ActivityTransitionAnimation.Direction) IMultimediaEditableNote(com.ichi2.anki.multimediacard.IMultimediaEditableNote) SdCardReceiver(com.ichi2.anki.receiver.SdCardReceiver) Editable(android.text.Editable) ImageField(com.ichi2.anki.multimediacard.fields.ImageField) ArrayList(java.util.ArrayList) NOT_FOUND_NOTE_TYPE(com.ichi2.libanki.Models.NOT_FOUND_NOTE_TYPE) MenuInflater(android.view.MenuInflater) Menu(android.view.Menu) IntegerDialog(com.ichi2.anki.dialogs.IntegerDialog) CURRENT_DECK(com.ichi2.libanki.Decks.CURRENT_DECK) CheckResult(androidx.annotation.CheckResult) TextUtils(android.text.TextUtils) Toolbar(com.ichi2.anki.noteeditor.Toolbar) CustomToolbarButton(com.ichi2.anki.noteeditor.CustomToolbarButton) TextViewUtil(com.ichi2.utils.TextViewUtil) SharedPreferences(android.content.SharedPreferences) Configuration(android.content.res.Configuration) EditText(android.widget.EditText) RequiresApi(androidx.annotation.RequiresApi) ImageButton(android.widget.ImageButton) ActionMode(android.view.ActionMode) LinearLayout(android.widget.LinearLayout) MultimediaEditFieldActivity(com.ichi2.anki.multimediacard.activity.MultimediaEditFieldActivity) TaskManager(com.ichi2.async.TaskManager) WindowManager(android.view.WindowManager) ConfirmationDialog(com.ichi2.anki.dialogs.ConfirmationDialog) Locale(java.util.Locale) EFieldType(com.ichi2.anki.multimediacard.fields.EFieldType) View(android.view.View) AdapterView(android.widget.AdapterView) MultimediaEditableNote(com.ichi2.anki.multimediacard.impl.MultimediaEditableNote) HashUtil(com.ichi2.utils.HashUtil) HtmlCompat(androidx.core.text.HtmlCompat) EXTRA_PROCESS_TEXT(com.ichi2.compat.Compat.EXTRA_PROCESS_TEXT) BroadcastReceiver(android.content.BroadcastReceiver) AppCompatButton(androidx.appcompat.widget.AppCompatButton) FieldState(com.ichi2.anki.noteeditor.FieldState) Timber(timber.log.Timber) TextField(com.ichi2.anki.multimediacard.fields.TextField) List(java.util.List) TextView(android.widget.TextView) OnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener) TaskListenerWithContext(com.ichi2.async.TaskListenerWithContext) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Typeface(android.graphics.Typeface) Note(com.ichi2.libanki.Note) Context(android.content.Context) KeyEvent(android.view.KeyEvent) MediaClipField(com.ichi2.anki.multimediacard.fields.MediaClipField) ResourcesCompat(androidx.core.content.res.ResourcesCompat) Pair(android.util.Pair) Intent(android.content.Intent) HashMap(java.util.HashMap) NoteTypeSpinnerUtils(com.ichi2.anki.ui.NoteTypeSpinnerUtils) Collection(com.ichi2.libanki.Collection) StyledProgressDialog(com.ichi2.themes.StyledProgressDialog) MenuItem(android.view.MenuItem) MapUtil(com.ichi2.utils.MapUtil) DeckDropDownAdapter(com.ichi2.anki.widgets.DeckDropDownAdapter) SuppressLint(android.annotation.SuppressLint) JSONArray(com.ichi2.utils.JSONArray) WidgetStatus(com.ichi2.widget.WidgetStatus) Model(com.ichi2.libanki.Model) Build(android.os.Build) LinkedList(java.util.LinkedList) Utils(com.ichi2.libanki.Utils) ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) NoteService(com.ichi2.anki.servicelayer.NoteService) CollectionTask(com.ichi2.async.CollectionTask) Themes(com.ichi2.themes.Themes) Spinner(android.widget.Spinner) PopupMenu(androidx.appcompat.widget.PopupMenu) LanguageHintService(com.ichi2.anki.servicelayer.LanguageHintService) Consumer(java.util.function.Consumer) ACTION_PROCESS_TEXT(com.ichi2.compat.Compat.ACTION_PROCESS_TEXT) AdaptionUtil(com.ichi2.utils.AdaptionUtil) Card(com.ichi2.libanki.Card) VisibleForTesting(androidx.annotation.VisibleForTesting) NoteFieldDecorator(com.ichi2.utils.NoteFieldDecorator) Resources(android.content.res.Resources) DeckSelectionDialog(com.ichi2.anki.dialogs.DeckSelectionDialog) CustomToolbarButton(com.ichi2.anki.noteeditor.CustomToolbarButton) Drawable(android.graphics.drawable.Drawable) MarginLayoutParams(android.view.ViewGroup.MarginLayoutParams) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) SuppressLint(android.annotation.SuppressLint) Toolbar(com.ichi2.anki.noteeditor.Toolbar)

Example 3 with CustomToolbarButton

use of com.ichi2.anki.noteeditor.CustomToolbarButton in project Anki-Android by ankidroid.

the class NoteEditor method addToolbarButton.

private void addToolbarButton(String buttonText, String prefix, String suffix) {
    if (TextUtils.isEmpty(prefix) && TextUtils.isEmpty(suffix)) {
        return;
    }
    ArrayList<CustomToolbarButton> toolbarButtons = getToolbarButtons();
    toolbarButtons.add(new CustomToolbarButton(toolbarButtons.size(), buttonText, prefix, suffix));
    saveToolbarButtons(toolbarButtons);
    updateToolbar();
}
Also used : CustomToolbarButton(com.ichi2.anki.noteeditor.CustomToolbarButton)

Example 4 with CustomToolbarButton

use of com.ichi2.anki.noteeditor.CustomToolbarButton in project AnkiChinaAndroid by ankichinateam.

the class NoteEditor method addToolbarButton.

private void addToolbarButton(String prefix, String suffix) {
    if (TextUtils.isEmpty(prefix) && TextUtils.isEmpty(suffix)) {
        return;
    }
    ArrayList<CustomToolbarButton> toolbarButtons = getToolbarButtons();
    toolbarButtons.add(new CustomToolbarButton(toolbarButtons.size(), prefix, suffix));
    saveToolbarButtons(toolbarButtons);
    updateToolbar();
}
Also used : CustomToolbarButton(com.ichi2.anki.noteeditor.CustomToolbarButton)

Aggregations

CustomToolbarButton (com.ichi2.anki.noteeditor.CustomToolbarButton)4 SuppressLint (android.annotation.SuppressLint)2 TargetApi (android.annotation.TargetApi)2 BroadcastReceiver (android.content.BroadcastReceiver)2 ClipboardManager (android.content.ClipboardManager)2 Context (android.content.Context)2 Intent (android.content.Intent)2 IntentFilter (android.content.IntentFilter)2 SharedPreferences (android.content.SharedPreferences)2 Resources (android.content.res.Resources)2 Typeface (android.graphics.Typeface)2 Drawable (android.graphics.drawable.Drawable)2 Uri (android.net.Uri)2 Build (android.os.Build)2 Bundle (android.os.Bundle)2 Editable (android.text.Editable)2 TextUtils (android.text.TextUtils)2 TextWatcher (android.text.TextWatcher)2 Pair (android.util.Pair)2 ActionMode (android.view.ActionMode)2