Skip to content

fix(action-bar-ios): navigation button support for font icons #7918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,11 @@ describe(`${suite}-${spec}-suite`, async function () {
if (driver.isIOS && imageName.includes("android")) {
this.skip();
}
if (driver.platformName === Platform.ANDROID
&& (sample.sample.toLowerCase() === "all" || sample.sample.toLowerCase() === "reset")) {
await driver.scroll(Direction.down, 400, 200, 300, 200);
await driver.scroll(Direction.down, 400, 200, 300, 200);
await driver.scroll(Direction.down, 400, 200, 300, 200);

let scenarioBtn = await driver.waitForElement(sample.sample);
if (!scenarioBtn) {
await driver.scroll(Direction.up, 400, 200, 300, 200);
scenarioBtn = await driver.waitForElement(sample.sample);
}
const scenarioBtn = await driver.waitForElement(sample.sample);
await scenarioBtn.click();
imageName = setImageName(suite, spec, imageName);
await driver.imageHelper.compareScreen({ imageName: imageName, timeOutSeconds: 5, tolerance: 0, toleranceType: ImageOptions.pixel });
Expand Down
2 changes: 1 addition & 1 deletion tns-core-modules/ui/action-bar/action-bar-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
CSSType,
traceWrite,
traceCategories,
traceMessageType, Color
traceMessageType
} from "../core/view";
import { ShorthandProperty, CssProperty, Style } from "../core/properties/properties";
import { Length } from "../core/view";
Expand Down
76 changes: 37 additions & 39 deletions tns-core-modules/ui/action-bar/action-bar.android.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AndroidActionBarSettings as AndroidActionBarSettingsDefinition, AndroidActionItemSettings } from ".";
import { AndroidActionItemSettings, AndroidActionBarSettings as AndroidActionBarSettingsDefinition, ActionItem as ActionItemDefinition } from ".";
import {
ActionItemBase, ActionBarBase, isVisible,
View, layout, colorProperty, flatProperty, Color,
traceMissingIcon, androidContentInsetLeftProperty, androidContentInsetRightProperty, Length
traceMissingIcon, androidContentInsetLeftProperty, androidContentInsetRightProperty
} from "./action-bar-common";
import { RESOURCE_PREFIX, isFontIconURI } from "../../utils/utils";
import { fromFileOrResource, fromFontIconCode } from "../../image-source";
Expand All @@ -22,6 +22,31 @@ function generateItemId(): number {
return actionItemIdGenerator;
}

function loadActionIconDrawableOrResourceId(item: ActionItemDefinition): any {
const itemIcon = item.icon;
const itemStyle = item.style;
let drawableOrId = null;

if (isFontIconURI(itemIcon)) {
const fontIconCode = itemIcon.split("//")[1];
const font = itemStyle.fontInternal;
const color = itemStyle.color;
const is = fromFontIconCode(fontIconCode, font, color);

if (is && is.android) {
drawableOrId = new android.graphics.drawable.BitmapDrawable(appResources, is.android);
}
} else {
drawableOrId = getDrawableOrResourceId(itemIcon, appResources);
}

if (!drawableOrId) {
traceMissingIcon(itemIcon);
}

return drawableOrId;
}

interface MenuItemClickListener {
new(owner: ActionBar): androidx.appcompat.widget.Toolbar.OnMenuItemClickListener;
}
Expand Down Expand Up @@ -231,21 +256,9 @@ export class ActionBar extends ActionBarBase {
}
}
else if (navButton.icon) {
if (isFontIconURI(navButton.icon)) {
const fontIconCode = navButton.icon.split("//")[1];
const font = navButton.style.fontInternal;
const color = navButton.style.color;
const is = fromFontIconCode(fontIconCode, font, color);

if (is && is.android) {
const drawable = new android.graphics.drawable.BitmapDrawable(appResources, is.android);
this.nativeViewProtected.setNavigationIcon(drawable);
}
} else {
let drawableOrId = getDrawableOrResourceId(navButton.icon, appResources);
if (drawableOrId) {
this.nativeViewProtected.setNavigationIcon(drawableOrId);
}
const drawableOrId = loadActionIconDrawableOrResourceId(navButton);
if (drawableOrId) {
this.nativeViewProtected.setNavigationIcon(drawableOrId);
}
}

Expand Down Expand Up @@ -275,6 +288,8 @@ export class ActionBar extends ActionBarBase {
let drawableOrId = getDrawableOrResourceId(icon, appResources);
if (drawableOrId) {
this.nativeViewProtected.setLogo(drawableOrId);
} else {
traceMissingIcon(icon);
}
}
else {
Expand Down Expand Up @@ -327,21 +342,9 @@ export class ActionBar extends ActionBarBase {
}
}
else if (item.icon) {
if (isFontIconURI(item.icon)) {
const fontIconCode = item.icon.split("//")[1];
const font = item.style.fontInternal;
const color = item.style.color;
const is = fromFontIconCode(fontIconCode, font, color);

if (is && is.android) {
const drawable = new android.graphics.drawable.BitmapDrawable(appResources, is.android);
menuItem.setIcon(drawable);
}
} else {
let drawableOrId = getDrawableOrResourceId(item.icon, appResources);
if (drawableOrId) {
menuItem.setIcon(drawableOrId);
}
const drawableOrId = loadActionIconDrawableOrResourceId(item);
if (drawableOrId) {
menuItem.setIcon(drawableOrId);
}
}

Expand Down Expand Up @@ -468,10 +471,10 @@ let defaultTitleTextColor: number;

function getDrawableOrResourceId(icon: string, resources: android.content.res.Resources): any {
if (typeof icon !== "string") {
return undefined;
return null;
}

let result = undefined;
let result = null;
if (icon.indexOf(RESOURCE_PREFIX) === 0) {
let resourceId: number = resources.getIdentifier(icon.substr(RESOURCE_PREFIX.length), "drawable", application.android.packageName);
if (resourceId > 0) {
Expand All @@ -480,7 +483,6 @@ function getDrawableOrResourceId(icon: string, resources: android.content.res.Re
}
else {
let drawable: android.graphics.drawable.BitmapDrawable;

let is = fromFileOrResource(icon);
if (is) {
drawable = new android.graphics.drawable.BitmapDrawable(appResources, is.android);
Expand All @@ -489,10 +491,6 @@ function getDrawableOrResourceId(icon: string, resources: android.content.res.Re
result = drawable;
}

if (!result) {
traceMissingIcon(icon);
}

return result;
}

Expand Down
49 changes: 24 additions & 25 deletions tns-core-modules/ui/action-bar/action-bar.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,28 @@ export * from "./action-bar-common";
const majorVersion = iosUtils.MajorVersion;
const UNSPECIFIED = layout.makeMeasureSpec(0, layout.UNSPECIFIED);

function loadActionIconFromFileOrResource(icon: string): UIImage {
const img = fromFileOrResource(icon);
if (img && img.ios) {
return img.ios;
function loadActionIcon(item: ActionItemDefinition): any /* UIImage */ {
let is = null;
let img = null;

const itemIcon = item.icon;
const itemStyle = item.style;
if (isFontIconURI(itemIcon)) {
const fontIconCode = itemIcon.split("//")[1];
const font = itemStyle.fontInternal;
const color = itemStyle.color;
is = fromFontIconCode(fontIconCode, font, color);
} else {
traceMissingIcon(icon);
is = fromFileOrResource(itemIcon);
}

return null;
if (is && is.ios) {
img = is.ios;
} else {
traceMissingIcon(itemIcon);
}

return img;
}

class TapBarItemHandlerImpl extends NSObject {
Expand Down Expand Up @@ -183,7 +196,7 @@ export class ActionBar extends ActionBarBase {
// Set back button image
let img: UIImage;
if (this.navigationButton && isVisible(this.navigationButton) && this.navigationButton.icon) {
img = loadActionIconFromFileOrResource(this.navigationButton.icon);
img = loadActionIcon(this.navigationButton);
}

// TODO: This could cause issue when canceling BackEdge gesture - we will change the backIndicator to
Expand Down Expand Up @@ -256,25 +269,11 @@ export class ActionBar extends ActionBarBase {

barButtonItem = UIBarButtonItem.alloc().initWithBarButtonSystemItemTargetAction(id, tapHandler, "tap");
} else if (item.icon) {
let img = null;

if (isFontIconURI(item.icon)) {
const fontIconCode = item.icon.split("//")[1];
const font = item.style.fontInternal;
const color = item.style.color;
const is = fromFontIconCode(fontIconCode, font, color);

if (is && is.ios) {
img = is.ios;
} else {
traceMissingIcon(item.icon);
}
} else {
img = loadActionIconFromFileOrResource(item.icon);
const img = loadActionIcon(item);
if (img) {
const image = img.imageWithRenderingMode(this._getIconRenderingMode());
barButtonItem = UIBarButtonItem.alloc().initWithImageStyleTargetAction(image, UIBarButtonItemStyle.Plain, tapHandler, "tap");
}

const image = img.imageWithRenderingMode(this._getIconRenderingMode());
barButtonItem = UIBarButtonItem.alloc().initWithImageStyleTargetAction(image, UIBarButtonItemStyle.Plain, tapHandler, "tap");
} else {
barButtonItem = UIBarButtonItem.alloc().initWithTitleStyleTargetAction(item.text + "", UIBarButtonItemStyle.Plain, tapHandler, "tap");
}
Expand Down