Skip to content

Commit

Permalink
fix(android): set correct title after titleAttributes update (#13957)
Browse files Browse the repository at this point in the history
* fix(android): update title text for NavigationWindow child windows

* update

* update

* rename and remove

* use constructor
  • Loading branch information
m1ga committed Jan 12, 2024
1 parent be053b0 commit 53738a1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Expand Up @@ -81,6 +81,7 @@ public class WindowProxy extends TiWindowProxy implements TiActivityWindow
protected static final int MSG_LAST_ID = MSG_FIRST_ID + 999;

private static int id_toolbar;
private int barColor = -1;

private WeakReference<TiBaseActivity> windowActivity;

Expand Down Expand Up @@ -349,6 +350,7 @@ private void changeTitleColor(ActionBar actionBar, int colorInt)
ssb = new SpannableStringBuilder(abTitle);
}

barColor = colorInt;
ssb.setSpan(new ForegroundColorSpan(colorInt),
0, ssb.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
actionBar.setTitle(ssb);
Expand Down Expand Up @@ -526,6 +528,22 @@ public boolean handleMessage(Message msg)
Activity activity = getWindowActivity();
if (activity != null) {
activity.setTitle(TiConvert.toString((Object) (msg.obj), ""));

if (windowActivity != null && windowActivity.get() != null
&& windowActivity.get().getSupportActionBar() != null) {
ActionBar actionBar = windowActivity.get().getSupportActionBar();
if (actionBar.getTitle() instanceof SpannableStringBuilder) {
SpannableStringBuilder stringBuilder =
new SpannableStringBuilder(TiConvert.toString((Object) (msg.obj), ""));
if (barColor != -1) {
stringBuilder.setSpan(new ForegroundColorSpan(barColor),
0, stringBuilder.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
}
actionBar.setTitle(stringBuilder);
} else {
actionBar.setTitle(TiConvert.toString((Object) (msg.obj), ""));
}
}
}
return true;
}
Expand Down
Expand Up @@ -34,6 +34,7 @@
import org.appcelerator.titanium.util.TiActivityResultHandler;
import org.appcelerator.titanium.util.TiActivitySupport;
import org.appcelerator.titanium.util.TiActivitySupportHelper;
import org.appcelerator.titanium.util.TiColorHelper;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiLocaleManager;
import org.appcelerator.titanium.util.TiMenuSupport;
Expand All @@ -46,6 +47,8 @@
import org.appcelerator.titanium.view.TiInsetsProvider;

import android.app.Activity;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Dialog;
import android.content.Context;
Expand All @@ -67,6 +70,9 @@
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;

import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
Expand Down Expand Up @@ -338,7 +344,8 @@ protected void updateTitle()
if (window.hasProperty(TiC.PROPERTY_TITLE)) {
String oldTitle = (String) getTitle();
String newTitle = TiConvert.toString(window.getProperty(TiC.PROPERTY_TITLE));

int colorInt = -1;

if (oldTitle == null) {
oldTitle = "";
}
Expand All @@ -347,12 +354,32 @@ protected void updateTitle()
newTitle = "";
}

if (window.hasProperty(TiC.PROPERTY_TITLE_ATTRIBUTES)) {
KrollDict innerAttributes = window.getProperties().getKrollDict(TiC.PROPERTY_TITLE_ATTRIBUTES);
colorInt = TiColorHelper.parseColor(
TiConvert.toString(innerAttributes.getString(TiC.PROPERTY_COLOR)), this);
}

if (!newTitle.equals(oldTitle)) {
final String fnewTitle = newTitle;
final int finalColorInt = colorInt;
runOnUiThread(new Runnable() {
public void run()
{
setTitle(fnewTitle);

ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
if (finalColorInt != -1) {
SpannableStringBuilder ssb;
ssb = new SpannableStringBuilder(fnewTitle);
ssb.setSpan(new ForegroundColorSpan(finalColorInt),
0, ssb.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
actionBar.setTitle(ssb);
} else {
actionBar.setTitle(fnewTitle);
}
}
}
});
}
Expand Down

0 comments on commit 53738a1

Please sign in to comment.