10
10
import android .annotation .SuppressLint ;
11
11
import android .app .Activity ;
12
12
import android .content .Context ;
13
- import android .content .Intent ;
14
13
import android .graphics .Bitmap ;
15
14
import android .graphics .Color ;
16
15
import android .graphics .Typeface ;
33
32
import android .webkit .JavascriptInterface ;
34
33
import android .webkit .WebSettings ;
35
34
import android .webkit .WebView ;
36
- import android .widget .FrameLayout ;
37
35
import android .widget .HorizontalScrollView ;
38
36
import android .widget .SearchView ;
39
37
import android .widget .Toast ;
@@ -93,11 +91,11 @@ public static DocumentEditAndViewFragment newInstance(final @NonNull Document do
93
91
private HighlightingEditor _hlEditor ;
94
92
private WebView _webView ;
95
93
private MarkorWebViewClient _webViewClient ;
96
- private FrameLayout _editorHolder ;
94
+ private ViewGroup _editorHolder ;
97
95
private ViewGroup _textActionsBar ;
98
96
99
- private DraggableScrollbarScrollView _primaryScrollView ;
100
- private HorizontalScrollView _hsView ;
97
+ private DraggableScrollbarScrollView _verticalScrollView ;
98
+ private HorizontalScrollView _horizontalScrollView ;
101
99
private LineNumbersTextView _lineNumbersView ;
102
100
private SearchView _menuSearchViewForViewMode ;
103
101
private Document _document ;
@@ -108,7 +106,6 @@ public static DocumentEditAndViewFragment newInstance(final @NonNull Document do
108
106
private boolean _isPreviewVisible ;
109
107
private boolean _nextConvertToPrintMode = false ;
110
108
111
-
112
109
public DocumentEditAndViewFragment () {
113
110
super ();
114
111
}
@@ -139,10 +136,10 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
139
136
_editorHolder = view .findViewById (R .id .document__fragment__edit__editor_holder );
140
137
_textActionsBar = view .findViewById (R .id .document__fragment__edit__text_actions_bar );
141
138
_webView = view .findViewById (R .id .document__fragment_view_webview );
142
- _primaryScrollView = view .findViewById (R .id .document__fragment__edit__content_editor__scrolling_parent );
139
+ _verticalScrollView = view .findViewById (R .id .document__fragment__edit__content_editor__scrolling_parent );
143
140
_lineNumbersView = view .findViewById (R .id .document__fragment__edit__line_numbers_view );
144
-
145
141
_cu = new MarkorContextUtils (activity );
142
+ _editTextUndoRedoHelper = new TextViewUndoRedo ();
146
143
147
144
// Using `if (_document != null)` everywhere is dangerous
148
145
// It may cause reads or writes to _silently fail_
@@ -203,7 +200,7 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
203
200
setViewModeVisibility (startInPreview , false );
204
201
}
205
202
206
- // Configure the editor. Doing so after load helps prevent some errors
203
+ // Configure the editor
207
204
// ---------------------------------------------------------
208
205
_hlEditor .setLineSpacing (0 , _appSettings .getEditorLineSpacing ());
209
206
_hlEditor .setTextSize (TypedValue .COMPLEX_UNIT_SP , _appSettings .getDocumentFontSize (_document .path ));
@@ -213,23 +210,19 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
213
210
_hlEditor .setGravity (_appSettings .isEditorStartEditingInCenter () ? Gravity .CENTER : Gravity .NO_GRAVITY );
214
211
_hlEditor .setHighlightingEnabled (_appSettings .getDocumentHighlightState (_document .path , _hlEditor .getText ()));
215
212
_hlEditor .setAutoFormatEnabled (_appSettings .getDocumentAutoFormatEnabled (_document .path ));
213
+ _hlEditor .setSaveInstanceState (false ); // We will reload from disk
214
+ _hlEditor .setOverScrollMode (View .OVER_SCROLL_ALWAYS );
216
215
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
217
216
// Do not need to send contents to accessibility
218
217
_hlEditor .setImportantForAccessibility (View .IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS );
219
218
}
220
219
221
220
// Various settings
222
- setHorizontalScrollMode (isDisplayedAtMainActivity () || _appSettings .getDocumentWrapState (_document .path ));
221
+ setWrapState (isDisplayedAtMainActivity () || _appSettings .getDocumentWrapState (_document .path ));
223
222
updateMenuToggleStates (0 );
224
- // ---------------------------------------------------------
225
223
226
- _hlEditor .setSaveInstanceState (false ); // We will reload from disk
227
- _document .resetChangeTracking (); // Force next reload
228
- loadDocument ();
229
- // If not set the undo-redo helper by loadDocument, set it here
230
- if (_editTextUndoRedoHelper == null ) {
231
- _editTextUndoRedoHelper = new TextViewUndoRedo (_hlEditor );
232
- }
224
+ // ---------------------------------------------------------
225
+ _document .resetChangeTracking (); // Force next reload in onResume
233
226
234
227
final Runnable debounced = TextViewUtils .makeDebounced (500 , () -> {
235
228
checkTextChangeState ();
@@ -263,27 +256,30 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
263
256
@ Override
264
257
protected void onFragmentFirstTimeVisible () {
265
258
final Bundle args = getArguments ();
266
-
267
259
int startPos = _appSettings .getLastEditPosition (_document .path , _hlEditor .length ());
268
260
if (args != null && args .containsKey (Document .EXTRA_FILE_LINE_NUMBER )) {
269
261
final int lno = args .getInt (Document .EXTRA_FILE_LINE_NUMBER );
270
262
if (lno >= 0 ) {
271
263
startPos = TextViewUtils .getIndexFromLineOffset (_hlEditor .getText (), lno , 0 );
272
- } else if ( lno == Document . EXTRA_FILE_LINE_NUMBER_LAST ) {
264
+ } else {
273
265
startPos = _hlEditor .length ();
274
266
}
275
267
}
276
268
277
- _primaryScrollView .invalidate ();
278
- // Can affect layout so run before setting scroll position
279
- _hlEditor .recomputeHighlighting ();
269
+ _hlEditor .recomputeHighlighting (); // Run before setting scroll position
280
270
TextViewUtils .setSelectionAndShow (_hlEditor , startPos );
271
+
272
+ _editorHolder .invalidate ();
273
+ _editorHolder .post (() -> _hlEditor .setMinHeight (_editorHolder .getHeight ()));
281
274
}
282
275
283
276
@ Override
284
277
public void onResume () {
285
- loadDocument ();
286
278
_webView .onResume ();
279
+ loadDocument ();
280
+ if (_editTextUndoRedoHelper != null && _editTextUndoRedoHelper .getTextView () != _hlEditor ) {
281
+ _editTextUndoRedoHelper .setTextView (_hlEditor );
282
+ }
287
283
super .onResume ();
288
284
}
289
285
@@ -438,31 +434,15 @@ public boolean loadDocument() {
438
434
}
439
435
440
436
if (!_document .isContentSame (_hlEditor .getText ())) {
441
-
442
- final int [] sel = TextViewUtils .getSelection (_hlEditor );
443
- sel [0 ] = Math .min (sel [0 ], content .length ());
444
- sel [1 ] = Math .min (sel [1 ], content .length ());
445
-
446
- if (_editTextUndoRedoHelper != null ) {
447
- _editTextUndoRedoHelper .disconnect ();
448
- _editTextUndoRedoHelper .clearHistory ();
449
- }
450
-
451
- _hlEditor .withAutoFormatDisabled (() -> _hlEditor .setText (content ));
452
-
453
- if (_editTextUndoRedoHelper == null ) {
454
- _editTextUndoRedoHelper = new TextViewUndoRedo (_hlEditor );
455
- } else {
456
- _editTextUndoRedoHelper .setTextView (_hlEditor );
457
- }
458
-
459
- TextViewUtils .setSelectionAndShow (_hlEditor , sel );
437
+ _hlEditor .withAutoFormatDisabled (() -> _hlEditor .setTextKeepState (content ));
460
438
}
439
+
461
440
checkTextChangeState ();
462
441
463
442
if (_isPreviewVisible ) {
464
443
updateViewModeText ();
465
444
}
445
+
466
446
return true ;
467
447
}
468
448
return false ;
@@ -558,12 +538,12 @@ public boolean onOptionsItemSelected(@NonNull final MenuItem item) {
558
538
setViewModeVisibility (true );
559
539
Toast .makeText (activity , R .string .please_wait , Toast .LENGTH_LONG ).show ();
560
540
_webView .postDelayed (() -> {
561
- if (item . getItemId () == R .id .action_share_pdf ) {
541
+ if (itemId == R .id .action_share_pdf ) {
562
542
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
563
543
_cu .printOrCreatePdfFromWebview (_webView , _document , getTextString ().contains ("beamer\n " ));
564
544
}
565
545
} else {
566
- Bitmap bmp = _cu .getBitmapFromWebView (_webView , item . getItemId () == R .id .action_share_image );
546
+ Bitmap bmp = _cu .getBitmapFromWebView (_webView , itemId == R .id .action_share_image );
567
547
_cu .shareImage (getContext (), bmp , null );
568
548
}
569
549
}, 7000 );
@@ -619,7 +599,7 @@ public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
619
599
case R .id .action_wrap_words : {
620
600
final boolean newState = !isWrapped ();
621
601
_appSettings .setDocumentWrapState (_document .path , newState );
622
- setHorizontalScrollMode (newState );
602
+ setWrapState (newState );
623
603
updateMenuToggleStates (0 );
624
604
return true ;
625
605
}
@@ -659,10 +639,7 @@ public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
659
639
}
660
640
case R .id .action_show_file_browser : {
661
641
// Delay because I want menu to close before we open the file browser
662
- _hlEditor .postDelayed (() -> {
663
- final Intent intent = new Intent (activity , MainActivity .class ).putExtra (Document .EXTRA_FILE , _document .file );
664
- GsContextUtils .instance .animateToActivity (activity , intent , false , null );
665
- }, 250 );
642
+ _hlEditor .postDelayed (() -> MainActivity .launch (activity , _document .file , false ), 250 );
666
643
return true ;
667
644
}
668
645
case R .id .action_toggle_case :
@@ -765,34 +742,39 @@ private void updateMenuToggleStates(final int selectedFormatActionId) {
765
742
}
766
743
767
744
private boolean isWrapped () {
768
- return _hsView == null || (_hlEditor .getParent () == _editorHolder );
745
+ return _horizontalScrollView == null || (_hlEditor .getParent () != _horizontalScrollView );
769
746
}
770
747
771
- private void setHorizontalScrollMode (final boolean wrap ) {
748
+ private void makeHorizontalScrollView () {
749
+ if (_horizontalScrollView != null ) {
750
+ return ;
751
+ }
752
+
753
+ _horizontalScrollView = new HorizontalScrollView (getContext ());
754
+ _horizontalScrollView .setLayoutParams (new ViewGroup .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT ));
755
+ _horizontalScrollView .setFillViewport (true );
756
+ }
757
+
758
+ private void setWrapState (final boolean wrap ) {
772
759
final Context context = getContext ();
773
760
if (context != null && _hlEditor != null && isWrapped () != wrap ) {
774
761
final int [] sel = TextViewUtils .getSelection (_hlEditor );
775
- final boolean hlEnabled = _hlEditor .getHighlightingEnabled ();
776
- _hlEditor .setHighlightingEnabled (false );
762
+ final boolean hlEnabled = _hlEditor .setHighlightingEnabled (false );
777
763
778
- _editorHolder .removeAllViews ();
779
- if (_hsView != null ) {
780
- _hsView .removeAllViews ();
781
- }
782
- if (!wrap ) {
783
- if (_hsView == null ) {
784
- _hsView = new HorizontalScrollView (context );
785
- _hsView .setFillViewport (true );
786
- }
787
- _hsView .addView (_hlEditor );
788
- _editorHolder .addView (_hsView );
764
+ makeHorizontalScrollView ();
765
+
766
+ if (wrap ) {
767
+ _horizontalScrollView .removeView (_hlEditor );
768
+ _editorHolder .removeView (_horizontalScrollView );
769
+ _editorHolder .addView (_hlEditor , 1 );
789
770
} else {
790
- _editorHolder .addView (_hlEditor );
771
+ _editorHolder .removeView (_hlEditor );
772
+ _horizontalScrollView .addView (_hlEditor );
773
+ _editorHolder .addView (_horizontalScrollView , 1 );
791
774
}
792
775
793
776
_hlEditor .setHighlightingEnabled (hlEnabled );
794
- // Run after layout() of immediate parent completes
795
- (wrap ? _editorHolder : _hsView ).post (() -> TextViewUtils .setSelectionAndShow (_hlEditor , sel ));
777
+ _editorHolder .post (() -> TextViewUtils .setSelectionAndShow (_hlEditor , sel ));
796
778
}
797
779
}
798
780
@@ -890,10 +872,10 @@ public void setViewModeVisibility(boolean show, final boolean animate) {
890
872
_cu .showSoftKeyboard (activity , false , _hlEditor );
891
873
_hlEditor .clearFocus ();
892
874
_hlEditor .postDelayed (() -> _cu .showSoftKeyboard (activity , false , _hlEditor ), 300 );
893
- GsContextUtils .fadeInOut (_webView , _primaryScrollView , animate );
875
+ GsContextUtils .fadeInOut (_webView , _verticalScrollView , animate );
894
876
} else {
895
877
_webViewClient .setRestoreScrollY (_webView .getScrollY ());
896
- GsContextUtils .fadeInOut (_primaryScrollView , _webView , animate );
878
+ GsContextUtils .fadeInOut (_verticalScrollView , _webView , animate );
897
879
}
898
880
899
881
_nextConvertToPrintMode = false ;
0 commit comments