TextView: implement more ways to obtain textColor, textSize; misc cleanup

also added a try/catch block to avoid regression
This commit is contained in:
Mis012
2024-02-28 01:25:14 +01:00
parent 1a97841579
commit c430344bd7
2 changed files with 43 additions and 20 deletions

View File

@@ -35,18 +35,24 @@ JNIEXPORT void JNICALL Java_android_widget_TextView_native_1setText(JNIEnv *env,
gtk_label_set_text(GTK_LABEL(_PTR(_GET_LONG_FIELD(this, "widget"))), _CSTRING(charseq)); gtk_label_set_text(GTK_LABEL(_PTR(_GET_LONG_FIELD(this, "widget"))), _CSTRING(charseq));
} }
// FIXME: this will probably behave unfortunately if called multiple times
JNIEXPORT void JNICALL Java_android_widget_TextView_native_1setTextColor(JNIEnv *env, jobject this, jint color) JNIEXPORT void JNICALL Java_android_widget_TextView_native_1setTextColor(JNIEnv *env, jobject this, jint color)
{ {
GtkWidget *widget = GTK_WIDGET(_PTR(_GET_LONG_FIELD(this, "widget"))); GtkWidget *widget = GTK_WIDGET(_PTR(_GET_LONG_FIELD(this, "widget")));
GtkStyleContext *style_context = gtk_widget_get_style_context(widget);
GtkCssProvider *old_provider = g_object_get_data(G_OBJECT(widget), "color_style_provider");
if(old_provider)
gtk_style_context_remove_provider(style_context, old_provider);
GtkCssProvider *css_provider = gtk_css_provider_new(); GtkCssProvider *css_provider = gtk_css_provider_new();
char *css_string = g_markup_printf_escaped("* { color: #%06x; }", color & 0xFFFFFF); char *css_string = g_markup_printf_escaped("* { color: #%06x%02x; }", color & 0xFFFFFF, (color >> 24) & 0xFF);
gtk_css_provider_load_from_string(css_provider, css_string); gtk_css_provider_load_from_string(css_provider, css_string);
g_free(css_string); g_free(css_string);
gtk_style_context_add_provider(gtk_widget_get_style_context(widget), GTK_STYLE_PROVIDER(css_provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); gtk_style_context_add_provider(style_context, GTK_STYLE_PROVIDER(css_provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
g_object_set_data(G_OBJECT(widget), "color_style_provider", css_provider);
} }
JNIEXPORT void JNICALL Java_android_widget_TextView_setTextSize(JNIEnv *env, jobject this, jfloat size) JNIEXPORT void JNICALL Java_android_widget_TextView_setTextSize(JNIEnv *env, jobject this, jfloat size)
@@ -71,8 +77,5 @@ JNIEXPORT void JNICALL Java_android_widget_TextView_native_1set_1markup(JNIEnv *
{ {
GtkLabel *label = GTK_LABEL(_PTR(_GET_LONG_FIELD(this, "widget"))); GtkLabel *label = GTK_LABEL(_PTR(_GET_LONG_FIELD(this, "widget")));
printf("weeeheee!\n");
gtk_label_set_use_markup(label, value); gtk_label_set_use_markup(label, value);
printf("gtk_label_get_use_markup: %d, >%s<\n", gtk_label_get_use_markup(label), gtk_label_get_text(label));
} }

View File

@@ -16,12 +16,14 @@ import android.text.method.MovementMethod;
import android.text.method.TransformationMethod; import android.text.method.TransformationMethod;
import android.text.style.URLSpan; import android.text.style.URLSpan;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity; import android.view.Gravity;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.View; import android.view.View;
public class TextView extends View { public class TextView extends View {
public String text; public String text;
private ColorStateList colors = new ColorStateList(new int[][] {new int[0]}, new int[1]);
public TextView(Context context, AttributeSet attrs) { public TextView(Context context, AttributeSet attrs) {
this(context, attrs, 0); this(context, attrs, 0);
@@ -35,18 +37,29 @@ public class TextView extends View {
super(context, attrs, defStyleAttr); super(context, attrs, defStyleAttr);
TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.TextView, defStyleAttr, 0); TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.TextView, defStyleAttr, 0);
if (a.hasValue(com.android.internal.R.styleable.TextView_text)) { try {
setText(a.getText(com.android.internal.R.styleable.TextView_text)); if (a.hasValue(com.android.internal.R.styleable.TextView_text)) {
} setText(a.getText(com.android.internal.R.styleable.TextView_text));
int ap = a.getResourceId(com.android.internal.R.styleable.TextView_textAppearance, -1);
a.recycle();
if (ap != -1) {
a = context.obtainStyledAttributes(ap, com.android.internal.R.styleable.TextAppearance);
if (a.hasValue(com.android.internal.R.styleable.TextAppearance_textSize)) {
setTextSize(a.getDimensionPixelSize(com.android.internal.R.styleable.TextAppearance_textSize, 10));
} }
a.recycle(); int ap = a.getResourceId(com.android.internal.R.styleable.TextView_textAppearance, -1);
} if (ap != -1) {
TypedArray aa = context.obtainStyledAttributes(ap, com.android.internal.R.styleable.TextAppearance);
if (aa.hasValue(com.android.internal.R.styleable.TextAppearance_textColor)) {
setTextColor(aa.getColorStateList(com.android.internal.R.styleable.TextAppearance_textColor));
}
if (aa.hasValue(com.android.internal.R.styleable.TextAppearance_textSize)) {
setTextSize(aa.getDimensionPixelSize(com.android.internal.R.styleable.TextAppearance_textSize, 10));
}
aa.recycle();
}
if (a.hasValue(com.android.internal.R.styleable.TextView_textColor)) {
setTextColor(a.getColorStateList(com.android.internal.R.styleable.TextView_textColor));
}
if (a.hasValue(com.android.internal.R.styleable.TextView_textSize)) {
setTextSize(a.getDimensionPixelSize(com.android.internal.R.styleable.TextView_textSize, 10));
}
} catch(java.lang.Exception e) { System.out.println("exception while inflating TextView:"); e.printStackTrace(); }
a.recycle();
haveComplexMeasure = true; haveComplexMeasure = true;
} }
@@ -73,6 +86,12 @@ public class TextView extends View {
private native final void native_set_markup(int bool); private native final void native_set_markup(int bool);
public native final void native_setText(String text); public native final void native_setText(String text);
public void setTextSize(int unit, float size) {
if(unit != TypedValue.COMPLEX_UNIT_SP)
System.out.println("setTextSize called with non-SP unit ("+unit+"), we don't currently handle that");
setTextSize(size);
}
public native void setTextSize(float size); public native void setTextSize(float size);
public native final void native_setTextColor(int color); public native final void native_setTextColor(int color);
@@ -80,10 +99,11 @@ public class TextView extends View {
native_setTextColor(color); native_setTextColor(color);
} }
public void setTextColor(ColorStateList colors) { public void setTextColor(ColorStateList colors) {
if (colors != null) if (colors != null) {
this.colors = colors;
setTextColor(colors.getDefaultColor()); // TODO: do this properly setTextColor(colors.getDefaultColor()); // TODO: do this properly
}
} }
public void setTextSize(int unit, float size) {}
public void setTypeface(Typeface tf, int style) {} public void setTypeface(Typeface tf, int style) {}
public void setTypeface(Typeface tf) {} public void setTypeface(Typeface tf) {}
public void setLineSpacing(float add, float mult) {} public void setLineSpacing(float add, float mult) {}
@@ -94,7 +114,7 @@ public class TextView extends View {
public void setCursorVisible(boolean visible) {} public void setCursorVisible(boolean visible) {}
public void setImeOptions(int imeOptions) {} public void setImeOptions(int imeOptions) {}
public final ColorStateList getTextColors() { return new ColorStateList(new int[][] {new int[0]}, new int[1]); } public final ColorStateList getTextColors() { return colors; }
public static ColorStateList getTextColors(Context context, TypedArray attrs) { return new ColorStateList(new int[][] {new int[0]}, new int[1]); } public static ColorStateList getTextColors(Context context, TypedArray attrs) { return new ColorStateList(new int[][] {new int[0]}, new int[1]); }
public TextPaint getPaint() { public TextPaint getPaint() {