jmpessoa / lazandroidmodulewizard

LAMW: Lazarus Android Module Wizard: Form Designer and Components development model!
251 stars 83 forks source link

Could you add longclick for jToggleButton #398

Closed a6p closed 3 years ago

a6p commented 3 years ago
--- lazandroidmodulewizard/android_bridges/togglebutton.pas     2020-10-22 01:18:54.795146667 +1000
+++ new/lazandroidmodulewizard/android_bridges/togglebutton.pas  2020-09-26 01:36:11.714976286 +1000
@@ -14,14 +14,16 @@

 {jVisualControl template}

   jToggleButton = class(jVisualControl)
   private
     FTextOff: string;
     FTextOn: string;
     FToggleState: TToggleState;
     FOnToggle: TOnClickToggleButton;
+    FOnLongClick: TOnNotify;
     procedure SetColor(Value: TARGBColorBridge); //background
-    
   public
     constructor Create(AOwner: TComponent); override;
     destructor  Destroy; override;
@@ -29,6 +31,7 @@
     procedure Refresh;
     procedure UpdateLayout; override;
     procedure GenEvent_OnClickToggleButton(Obj: TObject; state: boolean);
+    procedure GenEvent_OnLongClick(Obj: TObject);
     function jCreate(): jObject;
     procedure jFree();
     procedure SetViewParent(_viewgroup: jObject);  override;
@@ -58,6 +61,7 @@
     property State: TToggleState read FToggleState write SetToggleState;
     property GravityInParent: TLayoutGravity read FGravityInParent write SetLGravity;
     property OnToggle: TOnClickToggleButton read FOnToggle write FOnToggle;
+    property OnLongClick: TOnNotify read FOnLongClick write FOnLongClick;
   end;

 function jToggleButton_jCreate(env: PJNIEnv;_Self: int64; this: jObject): jObject;
@@ -198,6 +202,11 @@
     View_SetBackGroundColor(FjEnv, FjObject, GetARGB(FCustomColor, FColor));
 end;

+procedure jToggleButton.GenEvent_OnLOngClick(Obj: TObject);
+begin
+  if Assigned(FOnLongClick) then FOnLongClick(Obj);
+end;
+
 procedure jToggleButton.UpdateLayout;
 begin
   if not FInitialized then exit;
--- ./lazandroidmodulewizard/android_wizard/smartdesigner/java/jToggleButton.java       2020-10-22 01:18:54.871147274 +1000
+++ new/lazandroidmodulewizard/android_wizard/smartdesigner/java/jToggleButton.java      2020-09-26 02:55:05.715708270 +1000

@@ -22,7 +22,10 @@

    private Context context = null;
    private OnClickListener onClickListener;   // click event
-
+   private OnLongClickListener onLongClickListener;
+   private Boolean  mEnabled  = true; 
+    
    private Boolean enabled  = false;           // click-touch enabled!
    boolean mState = false;

@@ -46,6 +49,20 @@
          };
       };
       setOnClickListener(onClickListener);
+      
+      onLongClickListener = new OnLongClickListener() {
+
+                       @Override
+                       public boolean onLongClick(View arg0) {
+                                  if (mEnabled) {
+                           controls.pOnLongClick(pascalObj,Const.Click_Default);
+                      }
+                                  return false;  //true if the callback consumed the long click, false otherwise. 
+                       }
+                                                                                                                                                                                                
+        };                                                                                                                                                                                      
+        setOnLongClickListener(onLongClickListener);                                                                                                                                            
    } //end constructor                                                                                                                                                                          

    public void jFree() { 
--- lazandroidmodulewizard/android_bridges/Laz_And_Controls.pas 2020-10-22 01:18:54.747146285 +1000
+++ new/lazandroidmodulewizard/android_bridges/Laz_And_Controls.pas      2020-09-26 01:34:56.647030746 +1000
@@ -145,7 +145,7 @@
 uses
   SysUtils, Classes,
   And_bitmap_h, And_jni, And_jni_Bridge, PaintShader,
-  AndroidWidget, systryparent;
+  AndroidWidget, systryparent, togglebutton;

 type
@@ -3157,6 +3144,13 @@
     Exit;
   end;

+  if Obj is jToggleButton then
+  begin
+    jForm(jToggleButton(Obj).Owner).UpdateJNI(gApp);
+    jToggleButton(Obj).GenEvent_OnLongClick(Obj);
+    Exit;
+  end;
+
 end;
jmpessoa commented 3 years ago

Done!

Added new property "EnabledLongClick" (default=false) Added new event "OnLongClick"

Thank you!

a6p commented 3 years ago

Thank you!