Skip to content

Commit

Permalink
feat(android): window titleAttributes parity (#13954)
Browse files Browse the repository at this point in the history
  • Loading branch information
m1ga committed Dec 17, 2023
1 parent 3c0794f commit eb19d17
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
Expand Up @@ -45,6 +45,10 @@
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.transition.ChangeBounds;
import android.transition.ChangeClipBounds;
import android.transition.ChangeImageTransform;
Expand Down Expand Up @@ -314,12 +318,42 @@ public void windowCreated(TiBaseActivity activity, Bundle savedInstanceState)
Log.w(TAG, "Trying to set a barColor on a Window with ActionBar disabled. Property will be ignored.");
}
}

// Handle titleAttributes property.
if (hasProperty(TiC.PROPERTY_TITLE_ATTRIBUTES)) {
KrollDict innerAttributes = getProperties().getKrollDict(TiC.PROPERTY_TITLE_ATTRIBUTES);
int colorInt = TiColorHelper.parseColor(
TiConvert.toString(innerAttributes.getString(TiC.PROPERTY_COLOR)), activity);
ActionBar actionBar = activity.getSupportActionBar();

if (actionBar != null) {
changeTitleColor(actionBar, colorInt);
} else {
Log.w(TAG,
"Trying to set a titleAttributes on a Window with ActionBar disabled. Property will be ignored.");
}
}
activity.getActivityProxy().getDecorView().add(this);

// Need to handle the cached activity proxy properties and url window in the JS side.
callPropertySync(PROPERTY_POST_WINDOW_CREATED, null);
}

private void changeTitleColor(ActionBar actionBar, int colorInt)
{
SpannableStringBuilder ssb;
if (actionBar.getTitle() instanceof SpannableStringBuilder) {
ssb = (SpannableStringBuilder) actionBar.getTitle();
} else {
String abTitle = TiConvert.toString(actionBar.getTitle());
ssb = new SpannableStringBuilder(abTitle);
}

ssb.setSpan(new ForegroundColorSpan(colorInt),
0, ssb.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
actionBar.setTitle(ssb);
}

@Override
public void onWindowActivityCreated()
{
Expand Down Expand Up @@ -409,6 +443,23 @@ public void onPropertyChanged(String name, Object value)
}
}
}

if (name.equals(TiC.PROPERTY_TITLE_ATTRIBUTES)) {
if (windowActivity != null && windowActivity.get() != null) {
// Get a reference to the ActionBar.
AppCompatActivity activity = windowActivity.get();
ActionBar actionBar = activity.getSupportActionBar();
// Check if it is available ( app is using a theme with one or a Toolbar is used as one ).
if (actionBar != null) {
HashMap innerAttributes = (HashMap) value;
changeTitleColor(actionBar,
TiConvert.toColor(innerAttributes.get(TiC.PROPERTY_COLOR), activity));
} else {
// Log a warning if there is no ActionBar available.
Log.w(TAG, "There is no ActionBar available for this Window.");
}
}
}
super.onPropertyChanged(name, value);
}

Expand Down
Expand Up @@ -773,6 +773,7 @@ public class TiC
public static final String PROPERTY_TINT = "tint";
public static final String PROPERTY_TINT_COLOR = "tintColor";
public static final String PROPERTY_TITLE = "title";
public static final String PROPERTY_TITLE_ATTRIBUTES = "titleAttributes";
public static final String PROPERTY_TITLE_COLOR = "titleColor";
public static final String PROPERTY_TITLE_CONDENSED = "titleCondensed";
public static final String PROPERTY_TITLEID = "titleid";
Expand Down
12 changes: 8 additions & 4 deletions apidoc/Titanium/UI/Window.yml
Expand Up @@ -1409,8 +1409,8 @@ properties:
summary: Title text attributes of the window.
description: |
Use this property to specify the color, font and shadow attributes of the title.
platforms: [iphone, ipad, macos]
since: {iphone: "3.2.0", ipad: "3.2.0", macos: "9.2.0"}
platforms: [android, iphone, ipad, macos]
since: {android: "12.3.0", iphone: "3.2.0", ipad: "3.2.0", macos: "9.2.0"}
type: titleAttributesParams

- name: titleControl
Expand Down Expand Up @@ -1993,8 +1993,8 @@ properties:
---
name: titleAttributesParams
summary: Dictionary of options for the <Titanium.UI.Window.titleAttributes> property.
platforms: [iphone, ipad, macos]
since: {iphone: "3.2.0", ipad: "3.2.0", macos: "9.2.0"}
platforms: [android, iphone, ipad, macos]
since: {android: "12.3.0", iphone: "3.2.0", ipad: "3.2.0", macos: "9.2.0"}
examples:
- title: Simple Example
example: |
Expand All @@ -2017,14 +2017,18 @@ properties:
description: |
For information about color values, see the "Colors" section of <Titanium.UI>.
type: [String, Titanium.UI.Color]
platforms: [android, iphone, ipad, macos]
since: {android: "12.3.0", iphone: "3.2.0", ipad: "3.2.0", macos: "9.2.0"}

- name: font
summary: Font to use for the window title.
type: Font
platforms: [iphone, ipad, macos]

- name: shadow
summary: Shadow color and offset for the window title.
type: shadowDict
platforms: [iphone, ipad, macos]

---
name: shadowDict
Expand Down

0 comments on commit eb19d17

Please sign in to comment.