Skip to content
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

fix(android): set correct title after titleAttributes update #13957

Merged
merged 7 commits into from
Jan 12, 2024
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 @@ -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), ""));
hansemannn marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
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
Loading