fix(style): Styles are not applied to dialogs (#5612)

This commit is contained in:
Alexander Vakrilov
2018-04-03 17:06:21 +02:00
committed by GitHub
parent 70f01123df
commit 38e6f6688e
3 changed files with 56 additions and 65 deletions

View File

@ -1,4 +1,4 @@
// Deifinitions.
// Types.
import { View } from "../core/view";
import { Color } from "../../color";
import { Page } from "../page";
@ -46,71 +46,62 @@ export function getCurrentPage(): Page {
return undefined;
}
function applySelectors(view: View) {
function applySelectors<T extends View>(view: T, callback: (view: T) => void) {
let currentPage = getCurrentPage();
if (currentPage) {
let styleScope = currentPage._styleScope;
if (styleScope) {
styleScope.matchSelectors(view);
view._inheritStyleScope(styleScope);
view.onLoaded();
callback(view);
view.onUnloaded();
}
}
}
let button: View;
let label: View;
let textField: View;
export function getButtonColors(): { color: Color, backgroundColor: Color } {
if (!button) {
const Button = require("ui/button").Button;
button = new Button;
}
let buttonColor: Color;
let buttonBackgroundColor: Color;
function getButtonColors(): void {
const Button = require("ui/button").Button;
const btn = new Button();
applySelectors(btn);
applySelectors(button, (btn) => {
buttonColor = btn.color;
buttonBackgroundColor = btn.backgroundColor;
btn.onUnloaded();
buttonBackgroundColor = <Color>btn.backgroundColor;
});
return { color: buttonColor, backgroundColor: buttonBackgroundColor };
}
// NOTE: This will fail if app.css is changed.
export function getButtonColor(): Color {
if (!buttonColor) {
getButtonColors();
}
return buttonColor;
}
// NOTE: This will fail if app.css is changed.
export function getButtonBackgroundColor(): Color {
if (!buttonBackgroundColor) {
getButtonColors();
}
return buttonBackgroundColor;
}
let textFieldColor: Color;
export function getTextFieldColor(): Color {
if (!textFieldColor) {
const TextField = require("ui/text-field").TextField;
const tf = new TextField();
applySelectors(tf);
textFieldColor = tf.color;
tf.onUnloaded();
}
return textFieldColor;
export function getLabelColor(): Color {
if (!label) {
const Label = require("ui/label").Label;
label = new Label;
}
let labelColor: Color;
// NOTE: This will fail if app.css is changed.
export function getLabelColor(): Color {
if (!labelColor) {
const Label = require("ui/label").Label;
let lbl = new Label();
applySelectors(lbl);
applySelectors(label, (lbl) => {
labelColor = lbl.color;
lbl.onUnloaded();
});
return labelColor;
}
return labelColor;
export function getTextFieldColor(): Color {
if (!textField) {
const TextField = require("ui/text-field").TextField;
textField = new TextField();
}
let textFieldColor: Color;
applySelectors(textField, (tf) => {
textFieldColor = tf.color;
});
return textFieldColor;
}
export function isDialogOptions(arg): boolean {

View File

@ -2,7 +2,7 @@
* Android specific dialogs functions implementation.
*/
import { DialogOptions, ConfirmOptions, PromptOptions, PromptResult, LoginOptions, LoginResult, ActionOptions } from ".";
import { getLabelColor, getButtonColor, getButtonBackgroundColor, isDialogOptions, inputType, ALERT, OK, CONFIRM, CANCEL, PROMPT, LOGIN } from "./dialogs-common";
import { getLabelColor, getButtonColors, isDialogOptions, inputType, ALERT, OK, CONFIRM, CANCEL, PROMPT, LOGIN } from "./dialogs-common";
import { android as androidApp } from "../../application";
export * from "./dialogs-common";
@ -43,9 +43,9 @@ function showDialog(builder: android.app.AlertDialog.Builder) {
}
}
let buttonColor = getButtonColor();
let buttonBackgroundColor = getButtonBackgroundColor();
if (buttonColor) {
let { color, backgroundColor } = getButtonColors();
if (color) {
let buttons: android.widget.Button[] = [];
for (let i = 0; i < 3; i++) {
let id = dlg.getContext().getResources().getIdentifier("android:id/button" + i, null, null);
@ -54,11 +54,11 @@ function showDialog(builder: android.app.AlertDialog.Builder) {
buttons.forEach(button => {
if (button) {
if (buttonColor) {
button.setTextColor(buttonColor.android);
if (color) {
button.setTextColor(color.android);
}
if (buttonBackgroundColor) {
button.setBackgroundColor(buttonBackgroundColor.android);
if (backgroundColor) {
button.setBackgroundColor(backgroundColor.android);
}
}
});

View File

@ -3,7 +3,7 @@
*/
import { ConfirmOptions, PromptOptions, PromptResult, LoginOptions, LoginResult, ActionOptions } from ".";
import { getCurrentPage, getLabelColor, getButtonColor, getTextFieldColor, isDialogOptions, inputType, ALERT, OK, CONFIRM, CANCEL, PROMPT, LOGIN } from "./dialogs-common";
import { getCurrentPage, getLabelColor, getButtonColors, getTextFieldColor, isDialogOptions, inputType, ALERT, OK, CONFIRM, CANCEL, PROMPT, LOGIN } from "./dialogs-common";
import { isString, isDefined, isFunction } from "../../utils/types";
export * from "./dialogs-common";
@ -162,13 +162,14 @@ export function login(arg: any): Promise<LoginResult> {
let passwordTextField: UITextField;
let alertController = UIAlertController.alertControllerWithTitleMessagePreferredStyle(options.title, options.message, UIAlertControllerStyle.Alert);
let textFieldColor = getTextFieldColor();
alertController.addTextFieldWithConfigurationHandler((arg: UITextField) => {
arg.placeholder = "Login";
arg.text = isString(options.userName) ? options.userName : "";
let color = getTextFieldColor();
if (color) {
arg.textColor = arg.tintColor = color.ios;
if (textFieldColor) {
arg.textColor = arg.tintColor = textFieldColor.ios;
}
});
@ -177,9 +178,8 @@ export function login(arg: any): Promise<LoginResult> {
arg.secureTextEntry = true;
arg.text = isString(options.password) ? options.password : "";
let color = getTextFieldColor();
if (color) {
arg.textColor = arg.tintColor = color.ios;
if (textFieldColor) {
arg.textColor = arg.tintColor = textFieldColor.ios;
}
});
@ -214,7 +214,7 @@ function showUIAlertController(alertController: UIAlertController) {
alertController.popoverPresentationController.permittedArrowDirections = 0;
}
let color = getButtonColor();
let color = getButtonColors().color;
if (color) {
alertController.view.tintColor = color.ios;
}