Checkbox to enable/disable autoindentation

This commit is contained in:
TrianguloY 2019-04-09 23:27:47 +02:00
parent 5b715fc6b4
commit 96c9b3ffef
4 changed files with 31 additions and 4 deletions

View file

@ -101,6 +101,7 @@ public class ScriptEditor extends ResourceWrapperActivity implements View.OnClic
private static final String PREF_LAST_SCRIPT_ID = "se_lsi"; private static final String PREF_LAST_SCRIPT_ID = "se_lsi";
private static final String PREF_LAST_SCRIPT_LINE = "se_lsl"; private static final String PREF_LAST_SCRIPT_LINE = "se_lsl";
private static final String PREF_WORDWRAP = "se_w"; private static final String PREF_WORDWRAP = "se_w";
private static final String PREF_AUTOINDENT = "se_ind";
private static final String PREF_FONT_SIZE = "se_fs"; private static final String PREF_FONT_SIZE = "se_fs";
private static final String PREF_DIRECTORY = "se_d"; private static final String PREF_DIRECTORY = "se_d";
private static final String PREF_SUB_DIRS = "se_sd"; private static final String PREF_SUB_DIRS = "se_sd";
@ -382,7 +383,6 @@ public class ScriptEditor extends ResourceWrapperActivity implements View.OnClic
} }
}); });
mIndentation = new Indentation(); mIndentation = new Indentation();
mIndentation.register(mScriptText); // TODO: add setting to register/unregister
mSearch = new Search(this, mScriptText); mSearch = new Search(this, mScriptText);
((TextView)findViewById(R.id.sc_ma)).setText(R.string.sc_ma); ((TextView)findViewById(R.id.sc_ma)).setText(R.string.sc_ma);
@ -434,6 +434,8 @@ public class ScriptEditor extends ResourceWrapperActivity implements View.OnClic
boolean wordwrap = mSharedPrefs.getBoolean(PREF_WORDWRAP, true); boolean wordwrap = mSharedPrefs.getBoolean(PREF_WORDWRAP, true);
mScriptText.setWordWrap(wordwrap); mScriptText.setWordWrap(wordwrap);
boolean autoindent = mSharedPrefs.getBoolean(PREF_AUTOINDENT, true);
if(autoindent) mIndentation.register(mScriptText);
float text_size = mSharedPrefs.getFloat(PREF_FONT_SIZE, mScriptText.getTextSize() / mScaledDensity); float text_size = mSharedPrefs.getFloat(PREF_FONT_SIZE, mScriptText.getTextSize() / mScaledDensity);
mScriptText.setTextSize(text_size); mScriptText.setTextSize(text_size);
((TextView)findViewById(R.id.sc_o)).setText(R.string.sc_o); ((TextView)findViewById(R.id.sc_o)).setText(R.string.sc_o);
@ -441,7 +443,12 @@ public class ScriptEditor extends ResourceWrapperActivity implements View.OnClic
wordwrap_checkbox.setText(R.string.sc_w); wordwrap_checkbox.setText(R.string.sc_w);
wordwrap_checkbox.setChecked(wordwrap); wordwrap_checkbox.setChecked(wordwrap);
wordwrap_checkbox.setOnCheckedChangeListener(this); wordwrap_checkbox.setOnCheckedChangeListener(this);
CheckBox autoindent_checkbox = findViewById(R.id.sc_ind);
autoindent_checkbox.setText(R.string.sc_ind);
autoindent_checkbox.setChecked(autoindent);
autoindent_checkbox.setOnCheckedChangeListener(this);
TextPosition position = null; TextPosition position = null;
int sel_start = 0, sel_end = 0; int sel_start = 0, sel_end = 0;
Intent intent = getIntent(); Intent intent = getIntent();
@ -849,6 +856,15 @@ public class ScriptEditor extends ResourceWrapperActivity implements View.OnClic
mScriptText.setWordWrap(isChecked); mScriptText.setWordWrap(isChecked);
mSharedPrefs.edit().putBoolean(PREF_WORDWRAP, isChecked).commit(); mSharedPrefs.edit().putBoolean(PREF_WORDWRAP, isChecked).commit();
break; break;
case R.id.sc_ind:
// toggled autoindentation
if(isChecked){
mIndentation.register(mScriptText);
} else {
mIndentation.unregister(mScriptText);
}
mSharedPrefs.edit().putBoolean(PREF_AUTOINDENT, isChecked).commit();
break;
} }
} }

View file

@ -37,8 +37,11 @@ public class Indentation {
* Unregisters the AutoIndentation on the provided editText (if it was registered before) * Unregisters the AutoIndentation on the provided editText (if it was registered before)
*/ */
public void unregister(EditText editText){ public void unregister(EditText editText){
if(mRegistered.containsKey(editText)) if(!mRegistered.containsKey(editText))
editText.removeTextChangedListener(mRegistered.get(editText)); return;
editText.removeTextChangedListener(mRegistered.get(editText));
mRegistered.remove(editText);
} }
/** /**

View file

@ -180,6 +180,13 @@
android:checked="true" android:checked="true"
android:text="@string/sc_w"/> android:text="@string/sc_w"/>
<CheckBox
android:id="@+id/sc_ind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/sc_ind" />
<TextView <TextView
android:id="@+id/sc_a" android:id="@+id/sc_a"
android:layout_width="wrap_content" android:layout_width="wrap_content"

View file

@ -1448,6 +1448,7 @@
<string name="srch_back">Backwards</string> <string name="srch_back">Backwards</string>
<string name="srch_regexp">Regexp</string> <string name="srch_regexp">Regexp</string>
<string name="srch_case">Match case</string> <string name="srch_case">Match case</string>
<string name="sc_ind">Autoindentation</string>