mirror of
https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin.git
synced 2026-07-31 00:02:14 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
093b523dc6 | ||
|
|
b5adb0da91 | ||
|
|
52e4072ce6 | ||
|
|
11bcf9b14d | ||
|
|
55f9b41d87 | ||
|
|
0aeb2c4f95 | ||
|
|
dc6c537125 |
@@ -0,0 +1,19 @@
|
||||
[The MIT License (MIT)](http://www.opensource.org/licenses/mit-license.html)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
@@ -25,7 +25,6 @@ If you like this plugin and want to say thanks please send a PR or donation. Bot
|
||||
4. [Styling](#styling)
|
||||
5. [Credits](#5-credits)
|
||||
6. [Changelog](#6-changelog)
|
||||
7. [License](#7-license)
|
||||
|
||||
## 1. Description
|
||||
|
||||
@@ -230,6 +229,7 @@ Note that on WP this object is currently ignored.
|
||||
opacity: 0.75, // 0.0 (transparent) to 1.0 (opaque). Default 0.8
|
||||
backgroundColor: '#FF0000', // make sure you use #RRGGBB. Default #333333
|
||||
textColor: '#FFFF00', // Ditto. Default #FFFFFF
|
||||
textSize: 20.5, // Default is approx. 13.
|
||||
cornerRadius: 16, // minimum is 0 (square). iOS default 20, Android default 100
|
||||
horizontalPadding: 20, // iOS default 16, Android default 50
|
||||
verticalPadding: 16 // iOS default 12, Android default 30
|
||||
@@ -254,6 +254,7 @@ The Android code was entirely created by me.
|
||||
For iOS most credits go to this excellent [Toast for iOS project by Charles Scalesse] (https://github.com/scalessec/Toast).
|
||||
|
||||
## 6. CHANGELOG
|
||||
- 2.5.1: You can now specify the `textSize` used in the font for iOS and Android.
|
||||
- 2.5.0: By popular demand: Specify the duration of the Toast on iOS and Android. Pass in `short` (2000ms), `long` (4000ms), or any nr of milliseconds: `900`.
|
||||
- 2.4.2: You can now also set the Toast `opacity` for iOS.
|
||||
- 2.4.1: As an addition to 2.4.0, [Sino](https://github.com/SinoBoeckmann) added the option to change the text color!
|
||||
@@ -263,25 +264,3 @@ For iOS most credits go to this excellent [Toast for iOS project by Charles Scal
|
||||
- 2.0.1: iOS messages are hidden when another one is shown. [Thanks Richie Min!](https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin/pull/13)
|
||||
- 2.0: WP8 support
|
||||
- 1.0: initial version supporting Android and iOS
|
||||
|
||||
## 7. License
|
||||
|
||||
[The MIT License (MIT)](http://www.opensource.org/licenses/mit-license.html)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "cordova-plugin-x-toast",
|
||||
"version": "2.5.0",
|
||||
"version": "2.5.2",
|
||||
"description": "This plugin allows you to show a Toast. A Toast is a little non intrusive buttonless popup which automatically disappears.",
|
||||
"cordova": {
|
||||
"id": "cordova-plugin-x-toast",
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
id="cordova-plugin-x-toast"
|
||||
version="2.5.0">
|
||||
version="2.5.2">
|
||||
|
||||
<name>Toast</name>
|
||||
|
||||
|
||||
@@ -4,6 +4,10 @@ import android.graphics.Color;
|
||||
import android.graphics.drawable.GradientDrawable;
|
||||
import android.os.Build;
|
||||
import android.os.CountDownTimer;
|
||||
import android.text.Layout;
|
||||
import android.text.Spannable;
|
||||
import android.text.SpannableString;
|
||||
import android.text.style.AlignmentSpan;
|
||||
import android.view.Gravity;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
@@ -31,6 +35,8 @@ public class Toast extends CordovaPlugin {
|
||||
private android.widget.Toast mostRecentToast;
|
||||
private ViewGroup viewGroup;
|
||||
|
||||
private static final boolean IS_AT_LEAST_LOLLIPOP = Build.VERSION.SDK_INT >= 21;
|
||||
|
||||
// note that webView.isPaused() is not Xwalk compatible, so tracking it poor-man style
|
||||
private boolean isPaused;
|
||||
|
||||
@@ -49,7 +55,14 @@ public class Toast extends CordovaPlugin {
|
||||
}
|
||||
|
||||
final JSONObject options = args.getJSONObject(0);
|
||||
final String message = options.getString("message");
|
||||
final String msg = options.getString("message");
|
||||
final Spannable message = new SpannableString(msg);
|
||||
message.setSpan(
|
||||
new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER),
|
||||
0,
|
||||
msg.length() - 1,
|
||||
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
|
||||
|
||||
final String duration = options.getString("duration");
|
||||
final String position = options.getString("position");
|
||||
final int addPixelsY = options.has("addPixelsY") ? options.getInt("addPixelsY") : 0;
|
||||
@@ -68,7 +81,7 @@ public class Toast extends CordovaPlugin {
|
||||
hideAfterMs = Integer.parseInt(duration);
|
||||
}
|
||||
final android.widget.Toast toast = android.widget.Toast.makeText(
|
||||
Build.VERSION.SDK_INT >= 21 ? cordova.getActivity().getWindow().getContext() : cordova.getActivity().getApplicationContext(),
|
||||
IS_AT_LEAST_LOLLIPOP ? cordova.getActivity().getWindow().getContext() : cordova.getActivity().getApplicationContext(),
|
||||
message,
|
||||
android.widget.Toast.LENGTH_LONG // actually controlled by a timer further down
|
||||
);
|
||||
@@ -90,6 +103,7 @@ public class Toast extends CordovaPlugin {
|
||||
// the defaults mimic the default toast as close as possible
|
||||
final String backgroundColor = styling.optString("backgroundColor", "#333333");
|
||||
final String textColor = styling.optString("textColor", "#ffffff");
|
||||
final Double textSize = styling.optDouble("textSize", -1);
|
||||
final double opacity = styling.optDouble("opacity", 0.8);
|
||||
final int cornerRadius = styling.optInt("cornerRadius", 100);
|
||||
final int horizontalPadding = styling.optInt("horizontalPadding", 50);
|
||||
@@ -104,6 +118,9 @@ public class Toast extends CordovaPlugin {
|
||||
final TextView toastTextView;
|
||||
toastTextView = (TextView) toast.getView().findViewById(android.R.id.message);
|
||||
toastTextView.setTextColor(Color.parseColor(textColor));
|
||||
if (textSize > -1) {
|
||||
toastTextView.setTextSize(textSize.floatValue());
|
||||
}
|
||||
|
||||
toast.getView().setPadding(horizontalPadding, verticalPadding, horizontalPadding, verticalPadding);
|
||||
|
||||
@@ -113,51 +130,60 @@ public class Toast extends CordovaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
// On newer Android devices you can no longer rely on the 'toast.getView().setOnTouchListener',
|
||||
// On Android >= 5 you can no longer rely on the 'toast.getView().setOnTouchListener',
|
||||
// so created something funky that compares the Toast position to the tap coordinates.
|
||||
getViewGroup().setOnTouchListener(new View.OnTouchListener() {
|
||||
@Override
|
||||
public boolean onTouch(View view, MotionEvent motionEvent) {
|
||||
if (motionEvent.getAction() != MotionEvent.ACTION_DOWN) {
|
||||
return false;
|
||||
if (IS_AT_LEAST_LOLLIPOP) {
|
||||
getViewGroup().setOnTouchListener(new View.OnTouchListener() {
|
||||
@Override
|
||||
public boolean onTouch(View view, MotionEvent motionEvent) {
|
||||
if (motionEvent.getAction() != MotionEvent.ACTION_DOWN) {
|
||||
return false;
|
||||
}
|
||||
if (mostRecentToast == null || !mostRecentToast.getView().isShown()) {
|
||||
getViewGroup().setOnTouchListener(null);
|
||||
return false;
|
||||
}
|
||||
|
||||
float w = mostRecentToast.getView().getWidth();
|
||||
float startX = (view.getWidth() / 2) - (w / 2);
|
||||
float endX = (view.getWidth() / 2) + (w / 2);
|
||||
|
||||
float startY;
|
||||
float endY;
|
||||
|
||||
float g = mostRecentToast.getGravity();
|
||||
float y = mostRecentToast.getYOffset();
|
||||
float h = mostRecentToast.getView().getHeight();
|
||||
|
||||
if (g == GRAVITY_BOTTOM) {
|
||||
startY = view.getHeight() - y - h;
|
||||
endY = view.getHeight() - y;
|
||||
} else if (g == GRAVITY_CENTER) {
|
||||
startY = (view.getHeight() / 2) + y - (h / 2);
|
||||
endY = (view.getHeight() / 2) + y + (h / 2);
|
||||
} else {
|
||||
// top
|
||||
startY = y;
|
||||
endY = y + h;
|
||||
}
|
||||
|
||||
float tapX = motionEvent.getX();
|
||||
float tapY = motionEvent.getY();
|
||||
|
||||
final boolean tapped = tapX >= startX && tapX <= endX &&
|
||||
tapY >= startY && tapY <= endY;
|
||||
|
||||
return tapped && returnTapEvent(msg, data, callbackContext);
|
||||
}
|
||||
if (mostRecentToast == null || !mostRecentToast.getView().isShown()) {
|
||||
getViewGroup().setOnTouchListener(null);
|
||||
return false;
|
||||
});
|
||||
} else {
|
||||
toast.getView().setOnTouchListener(new View.OnTouchListener() {
|
||||
@Override
|
||||
public boolean onTouch(View view, MotionEvent motionEvent) {
|
||||
return motionEvent.getAction() == MotionEvent.ACTION_DOWN && returnTapEvent(msg, data, callbackContext);
|
||||
}
|
||||
|
||||
float w = mostRecentToast.getView().getWidth();
|
||||
float startX = (view.getWidth() / 2) - (w / 2);
|
||||
float endX = (view.getWidth() / 2) + (w / 2);
|
||||
|
||||
float startY;
|
||||
float endY;
|
||||
|
||||
float g = mostRecentToast.getGravity();
|
||||
float y = mostRecentToast.getYOffset();
|
||||
float h = mostRecentToast.getView().getHeight();
|
||||
|
||||
if (g == GRAVITY_BOTTOM) {
|
||||
startY = view.getHeight() - y - h;
|
||||
endY = view.getHeight() - y;
|
||||
} else if (g == GRAVITY_CENTER) {
|
||||
startY = (view.getHeight() / 2) + y - (h / 2);
|
||||
endY = (view.getHeight() / 2) + y + (h / 2);
|
||||
} else {
|
||||
// top
|
||||
startY = y;
|
||||
endY = y + h;
|
||||
}
|
||||
|
||||
float tapX = motionEvent.getX();
|
||||
float tapY = motionEvent.getY();
|
||||
|
||||
final boolean tapped = tapX >= startX && tapX <= endX &&
|
||||
tapY >= startY && tapY <= endY;
|
||||
|
||||
return tapped && returnTapEvent(message, data, callbackContext);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// trigger show every 2500 ms for as long as the requested duration
|
||||
_timer = new CountDownTimer(hideAfterMs, 2500) {
|
||||
|
||||
@@ -323,10 +323,14 @@ static id styling;
|
||||
UIColor *theColor = backgroundColor == nil ? [UIColor blackColor] : [self colorFromHexString:backgroundColor];
|
||||
|
||||
NSNumber * horizontalPadding = styling[@"horizontalPadding"];
|
||||
NSNumber * verticalPadding = styling[@"verticalPadding"];
|
||||
CGFloat theHorizontalPadding = horizontalPadding == nil ? CSToastHorizontalPadding : [horizontalPadding floatValue];
|
||||
|
||||
NSNumber * verticalPadding = styling[@"verticalPadding"];
|
||||
CGFloat theVerticalPadding = verticalPadding == nil ? CSToastVerticalPadding : [verticalPadding floatValue];
|
||||
|
||||
NSNumber * textSize = styling[@"textSize"];
|
||||
CGFloat theTextSize = textSize == nil ? CSToastFontSize : [textSize floatValue];
|
||||
|
||||
wrapperView.backgroundColor = theColor;
|
||||
|
||||
if(image != nil) {
|
||||
@@ -352,8 +356,8 @@ static id styling;
|
||||
|
||||
titleLabel = [[UILabel alloc] init];
|
||||
titleLabel.numberOfLines = CSToastMaxTitleLines;
|
||||
titleLabel.font = [UIFont boldSystemFontOfSize:CSToastFontSize];
|
||||
titleLabel.textAlignment = NSTextAlignmentLeft;
|
||||
titleLabel.font = [UIFont boldSystemFontOfSize:theTextSize];
|
||||
titleLabel.textAlignment = NSTextAlignmentCenter;
|
||||
titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
|
||||
titleLabel.textColor = theTitleLabelTextColor;
|
||||
titleLabel.backgroundColor = [UIColor clearColor];
|
||||
@@ -372,8 +376,9 @@ static id styling;
|
||||
|
||||
messageLabel = [[UILabel alloc] init];
|
||||
messageLabel.numberOfLines = CSToastMaxMessageLines;
|
||||
messageLabel.font = [UIFont systemFontOfSize:CSToastFontSize];
|
||||
messageLabel.font = [UIFont systemFontOfSize:theTextSize];
|
||||
messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
|
||||
messageLabel.textAlignment = NSTextAlignmentCenter;
|
||||
messageLabel.textColor = theMessageLabelTextColor;
|
||||
messageLabel.backgroundColor = [UIColor clearColor];
|
||||
messageLabel.alpha = 1.0;
|
||||
|
||||
+2
-2
@@ -36,9 +36,9 @@ Toast.prototype.optionsBuilder = function () {
|
||||
duration: duration,
|
||||
position: position,
|
||||
addPixelsY: addPixelsY
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user