3636import com .compdfkit .tools .common .pdf .config .AnnotationsConfig ;
3737import com .compdfkit .tools .common .utils .CListUtil ;
3838import com .compdfkit .tools .common .utils .CLog ;
39+ import com .compdfkit .tools .common .utils .annotation .CPDFAnnotationManager ;
3940import com .compdfkit .tools .common .utils .customevent .CPDFCustomEventCallbackHelper ;
4041import com .compdfkit .tools .common .utils .customevent .CPDFCustomEventField ;
4142import com .compdfkit .tools .common .utils .customevent .CPDFCustomEventType ;
5253import com .compdfkit .tools .common .views .pdfview .CPDFViewCtrl ;
5354import com .compdfkit .ui .proxy .attach .IInkDrawCallback ;
5455import com .compdfkit .ui .proxy .attach .IInkDrawCallback .Mode ;
56+ import com .compdfkit .ui .proxy .CPDFBaseAnnotImpl ;
57+ import com .compdfkit .ui .reader .CPDFPageView ;
5558import com .compdfkit .ui .reader .CPDFReaderView ;
5659import com .compdfkit .ui .reader .CPDFReaderView .ViewMode ;
60+ import com .compdfkit .ui .reader .CPDFSelectAnnotCallback ;
61+ import com .compdfkit .ui .reader .OnViewModeChangedListener ;
5762
5863import java .util .ArrayList ;
5964import java .util .Arrays ;
@@ -86,6 +91,20 @@ public class CAnnotationToolbar extends FrameLayout {
8691
8792 private LinearLayout llAnnotTools ;
8893
94+ @ Nullable
95+ private CPDFBaseAnnotImpl <CPDFAnnotation > selectedAnnotImpl ;
96+
97+ @ Nullable
98+ private CPDFPageView selectedAnnotPageView ;
99+
100+ @ Nullable
101+ private CPDFSelectAnnotCallback selectAnnotCallback ;
102+
103+ @ Nullable
104+ private OnViewModeChangedListener viewModeChangedListener ;
105+
106+ private boolean listeningSelectAnnot = false ;
107+
89108 public enum UndoManagerType {
90109 AnnotUndo ,
91110
@@ -131,15 +150,15 @@ private void initListener() {
131150 public void initWithPDFView (CPDFViewCtrl pdfView ) {
132151 this .pdfView = pdfView ;
133152 toolListAdapter .setList (CAnnotationToolDatas .getAnnotationList (pdfView ));
153+ setupSelectedAnnotationCallbacks ();
134154 this .pdfView .addOnPDFFocusedTypeChangeListener (type -> {
135155 if (type == CPDFAnnotation .Type .UNKNOWN ) {
156+ clearSelectedAnnotationStyleTarget ();
136157 if (toolListAdapter .hasSelectAnnotType ()) {
137158 if (toolListAdapter .getCurrentAnnotType () != CAnnotationType .INK_ERASER ){
138159 toolListAdapter .selectByType (CAnnotationType .UNKNOWN );
139160 }
140- if (ivSetting != null ) {
141- ivSetting .setEnabled (toolListAdapter .annotEnableSetting ());
142- }
161+ updateSettingButtonState ();
143162 }
144163 if (toolListAdapter .getCurrentAnnotType () != CAnnotationType .INK_ERASER ) {
145164 setUndoManagerType (UndoManagerType .AnnotUndo );
@@ -152,6 +171,108 @@ public void initWithPDFView(CPDFViewCtrl pdfView) {
152171 setUndoManagerType (UndoManagerType .AnnotUndo );
153172 }
154173 });
174+ syncSelectAnnotListenerWithViewMode (pdfView .getCPdfReaderView ().getViewMode ());
175+ }
176+
177+ private void setupSelectedAnnotationCallbacks () {
178+ if (selectAnnotCallback == null ) {
179+ selectAnnotCallback = new CPDFSelectAnnotCallback () {
180+ @ Override
181+ public void onAnnotationSelected (CPDFPageView pageView ,
182+ CPDFBaseAnnotImpl <CPDFAnnotation > annotImpl ) {
183+ if (canShowSelectedAnnotationStyleDialog (annotImpl )) {
184+ selectedAnnotPageView = pageView ;
185+ selectedAnnotImpl = annotImpl ;
186+ } else {
187+ clearSelectedAnnotationStyleTarget ();
188+ }
189+ updateSettingButtonState ();
190+ }
191+
192+ @ Override
193+ public void onAnnotationDeselected (CPDFPageView pageView ,
194+ CPDFBaseAnnotImpl <CPDFAnnotation > annotImpl ) {
195+ if (selectedAnnotImpl == annotImpl
196+ || (selectedAnnotImpl != null && annotImpl != null
197+ && selectedAnnotImpl .getId () == annotImpl .getId ())) {
198+ clearSelectedAnnotationStyleTarget ();
199+ updateSettingButtonState ();
200+ }
201+ }
202+ };
203+ }
204+ if (viewModeChangedListener == null ) {
205+ viewModeChangedListener = this ::syncSelectAnnotListenerWithViewMode ;
206+ pdfView .addOnPDFViewModeChangeListener (viewModeChangedListener );
207+ }
208+ }
209+
210+ private void syncSelectAnnotListenerWithViewMode (ViewMode viewMode ) {
211+ if (viewMode == ViewMode .ANNOT ) {
212+ startListenSelectAnnot ();
213+ } else {
214+ stopListenSelectAnnot ();
215+ clearSelectedAnnotationStyleTarget ();
216+ updateSettingButtonState ();
217+ }
218+ }
219+
220+ private void startListenSelectAnnot () {
221+ if (!listeningSelectAnnot && pdfView != null && selectAnnotCallback != null ) {
222+ pdfView .addOnPDFSelectAnnotChangeListener (selectAnnotCallback );
223+ listeningSelectAnnot = true ;
224+ }
225+ }
226+
227+ private void stopListenSelectAnnot () {
228+ if (listeningSelectAnnot && pdfView != null && selectAnnotCallback != null ) {
229+ pdfView .removeOnPDFSelectAnnotChangeListener (selectAnnotCallback );
230+ listeningSelectAnnot = false ;
231+ }
232+ }
233+
234+ private void clearSelectedAnnotationStyleTarget () {
235+ selectedAnnotImpl = null ;
236+ selectedAnnotPageView = null ;
237+ }
238+
239+ private boolean hasSelectedAnnotationStyleTarget () {
240+ return isAnnotationMode ()
241+ && selectedAnnotImpl != null
242+ && selectedAnnotPageView != null
243+ && canShowSelectedAnnotationStyleDialog (selectedAnnotImpl );
244+ }
245+
246+ private boolean isAnnotationMode () {
247+ return pdfView != null && pdfView .getCPdfReaderView ().getViewMode () == ViewMode .ANNOT ;
248+ }
249+
250+ private boolean canShowSelectedAnnotationStyleDialog (@ Nullable CPDFBaseAnnotImpl <CPDFAnnotation > annotImpl ) {
251+ if (annotImpl == null ) {
252+ return false ;
253+ }
254+ switch (annotImpl .getAnnotType ()) {
255+ case TEXT :
256+ case HIGHLIGHT :
257+ case UNDERLINE :
258+ case SQUIGGLY :
259+ case STRIKEOUT :
260+ case INK :
261+ case SQUARE :
262+ case CIRCLE :
263+ case LINE :
264+ case FREETEXT :
265+ return true ;
266+ default :
267+ return false ;
268+ }
269+ }
270+
271+ private void updateSettingButtonState () {
272+ if (ivSetting != null ) {
273+ ivSetting .setEnabled (hasSelectedAnnotationStyleTarget ()
274+ || toolListAdapter .annotEnableSetting ());
275+ }
155276 }
156277 private void setUndoManagerType (UndoManagerType undoManagerType ) {
157278 this .undoManagerType = undoManagerType ;
@@ -176,10 +297,26 @@ private void setUndoManagerType(UndoManagerType undoManagerType) {
176297 }
177298
178299 private void showAnnotStyleDialog () {
300+ if (hasSelectedAnnotationStyleTarget ()) {
301+ showSelectedAnnotStyleDialog ();
302+ return ;
303+ }
179304 CStyleType styleType = toolListAdapter .getCurrentAnnotType ().getStyleType ();
180305 showAnnotStyleDialog (styleType );
181306 }
182307
308+ private void showSelectedAnnotStyleDialog () {
309+ saveInk ();
310+ CViewUtils .hideKeyboard (this );
311+ FragmentActivity fragmentActivity = CViewUtils .getFragmentActivity (getContext ());
312+ CPDFBaseAnnotImpl <CPDFAnnotation > annotImpl = selectedAnnotImpl ;
313+ CPDFPageView pageView = selectedAnnotPageView ;
314+ if (fragmentActivity != null && annotImpl != null && pageView != null ) {
315+ CPDFAnnotationManager .showPropertiesDialog (
316+ fragmentActivity .getSupportFragmentManager (), annotImpl , pageView );
317+ }
318+ }
319+
183320 public void showAnnotStyleDialog (CStyleType styleType ) {
184321 saveInk ();
185322 CViewUtils .hideKeyboard (this );
@@ -230,9 +367,8 @@ public void switchAnnotationUnknown(){
230367 setUndoManagerType (UndoManagerType .AnnotUndo );
231368 }
232369 toolListAdapter .selectByType (CAnnotationType .UNKNOWN );
233- if (ivSetting != null ) {
234- ivSetting .setEnabled (toolListAdapter .annotEnableSetting ());
235- }
370+ clearSelectedAnnotationStyleTarget ();
371+ updateSettingButtonState ();
236372 pdfView .resetAnnotationType ();
237373 pdfView .getCPdfReaderView ().getInkDrawHelper ().onSave ();
238374 pdfView .getCPdfReaderView ().getInkDrawHelper ().setMode (Mode .DRAW );
@@ -245,9 +381,8 @@ public void switchAnnotationType(CAnnotationType type) {
245381 return ;
246382 }
247383 toolListAdapter .selectByType (type );
248- if (ivSetting != null ) {
249- ivSetting .setEnabled (toolListAdapter .annotEnableSetting ());
250- }
384+ clearSelectedAnnotationStyleTarget ();
385+ updateSettingButtonState ();
251386 AnnotationsConfig annotationsConfig = pdfView .getCPDFConfiguration ().annotationsConfig ;
252387 pdfView .getCPdfReaderView ().getInkDrawHelper ().onSave ();
253388 pdfView .getCPdfReaderView ().removeAllAnnotFocus ();
@@ -453,6 +588,7 @@ public void setTools(List<AnnotationsConfig.AnnotationTools> tools) {
453588 showAnnotStyleDialog ();
454589 });
455590 ivSetting = toolView ;
591+ updateSettingButtonState ();
456592 break ;
457593 case Undo :
458594 toolView .setImageResource (R .drawable .tools_ic_annotation_undo );
@@ -560,6 +696,8 @@ public void setAnnotationList(CAnnotationType... types) {
560696
561697 public void reset () {
562698 toolListAdapter .selectByType (CAnnotationType .UNKNOWN );
699+ clearSelectedAnnotationStyleTarget ();
700+ updateSettingButtonState ();
563701 rvAnnotationList .scrollToPosition (0 );
564702 redoUndoManager ();
565703 }
@@ -572,5 +710,15 @@ public void addAnnotationCreatePreparedListener(COnAnnotationCreatePreparedListe
572710 annotationCreatePreparedListeners .add (listener );
573711 }
574712
713+ public void release () {
714+ stopListenSelectAnnot ();
715+ if (pdfView != null && viewModeChangedListener != null ) {
716+ pdfView .removeOnPDFViewModeChangeListener (viewModeChangedListener );
717+ }
718+ viewModeChangedListener = null ;
719+ clearSelectedAnnotationStyleTarget ();
720+ updateSettingButtonState ();
721+ }
722+
575723
576724}
0 commit comments