Move Theme code to its own class

This commit is contained in:
Jules Aguillon 2021-12-26 23:55:18 +01:00
parent 86655d4959
commit dd24ad323b
2 changed files with 78 additions and 50 deletions

View File

@ -1,11 +1,9 @@
package juloo.keyboard2; package juloo.keyboard2;
import android.content.Context; import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.RectF; import android.graphics.RectF;
import android.graphics.Typeface;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.os.Vibrator; import android.os.Vibrator;
@ -37,18 +35,7 @@ public class Keyboard2View extends View
private float _keyWidth; private float _keyWidth;
private Paint _keyBgPaint = new Paint(); private Theme _theme;
private Paint _keyDownBgPaint = new Paint();
private Paint _keyLabelPaint;
private Paint _keySubLabelPaint;
private Paint _specialKeyLabelPaint;
private Paint _specialKeySubLabelPaint;
private int _lockedColor;
private int _activatedColor;
private int _labelColor;
private int _subLabelColor;
private float _labelTextSize;
private float _sublabelTextSize;
private static RectF _tmpRect = new RectF(); private static RectF _tmpRect = new RectF();
@ -57,6 +44,7 @@ public class Keyboard2View extends View
super(context, attrs); super(context, attrs);
_vibratorService = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE); _vibratorService = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
_handler = new Handler(this); _handler = new Handler(this);
_theme = new Theme(getContext(), attrs);
refreshConfig(((Keyboard2)context).getConfig(), null); refreshConfig(((Keyboard2)context).getConfig(), null);
setOnTouchListener(this); setOnTouchListener(this);
} }
@ -64,34 +52,11 @@ public class Keyboard2View extends View
/* Internally calls [reset()]. */ /* Internally calls [reset()]. */
public void refreshConfig(Config config, KeyboardData kw) public void refreshConfig(Config config, KeyboardData kw)
{ {
Resources res = getResources();
_config = config; _config = config;
_lockedColor = res.getColor(R.color.key_label_locked);
_activatedColor = res.getColor(R.color.key_label_activated);
_labelColor = res.getColor(R.color.key_label);
_subLabelColor = res.getColor(R.color.key_sub_label);
_labelTextSize = res.getDimension(R.dimen.label_text_size) * config.characterSize;
_sublabelTextSize = res.getDimension(R.dimen.sublabel_text_size) * config.characterSize;
_keyBgPaint.setColor(res.getColor(R.color.key_bg));
_keyDownBgPaint.setColor(res.getColor(R.color.key_down_bg));
_keyLabelPaint = initLabelPaint(Paint.Align.CENTER, null);
_keySubLabelPaint = initLabelPaint(Paint.Align.LEFT, null);
Typeface specialKeysFont = ((Keyboard2)getContext()).getSpecialKeyFont();
_specialKeyLabelPaint = initLabelPaint(Paint.Align.CENTER, specialKeysFont);
_specialKeySubLabelPaint = initLabelPaint(Paint.Align.LEFT, specialKeysFont);
if (kw != null) if (kw != null)
setKeyboard(kw); // handle layout options then calls reset(). setKeyboard(kw); // handle layout options then calls reset().
} }
private Paint initLabelPaint(Paint.Align align, Typeface font)
{
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setTextAlign(align);
if (font != null)
paint.setTypeface(font);
return (paint);
}
public void setKeyboard(KeyboardData kw) public void setKeyboard(KeyboardData kw)
{ {
if (!_config.shouldOfferSwitchingToNextInputMethod) if (!_config.shouldOfferSwitchingToNextInputMethod)
@ -388,11 +353,11 @@ public class Keyboard2View extends View
KeyDown keyDown = getKeyDown(k); KeyDown keyDown = getKeyDown(k);
_tmpRect.set(x, y, x + keyW, y + keyH); _tmpRect.set(x, y, x + keyW, y + keyH);
if (keyDown != null) if (keyDown != null)
canvas.drawRect(_tmpRect, _keyDownBgPaint); canvas.drawRect(_tmpRect, _theme.keyDownBgPaint);
else else
canvas.drawRoundRect(_tmpRect, _config.keyRound, _config.keyRound, _keyBgPaint); canvas.drawRoundRect(_tmpRect, _config.keyRound, _config.keyRound, _theme.keyBgPaint);
if (k.key0 != null) if (k.key0 != null)
drawLabel(canvas, k.key0, keyW / 2f + x, (keyH + _labelTextSize) / 2f + y, keyDown); drawLabel(canvas, k.key0, keyW / 2f + x, (keyH + _theme.labelTextSize) / 2f + y, keyDown);
float subPadding = _config.keyPadding; float subPadding = _config.keyPadding;
if (k.key1 != null) if (k.key1 != null)
drawSubLabel(canvas, k.key1, x + subPadding, y + subPadding, false, true, keyDown); drawSubLabel(canvas, k.key1, x + subPadding, y + subPadding, false, true, keyDown);
@ -422,9 +387,9 @@ public class Keyboard2View extends View
if (kd != null) if (kd != null)
{ {
if ((kd.flags & KeyValue.FLAG_LOCKED) != 0) if ((kd.flags & KeyValue.FLAG_LOCKED) != 0)
return _lockedColor; return _theme.lockedColor;
if (kd.pointerId == -1) if (kd.pointerId == -1)
return _activatedColor; return _theme.activatedColor;
} }
} }
return defaultColor; return defaultColor;
@ -433,26 +398,25 @@ public class Keyboard2View extends View
private void drawLabel(Canvas canvas, KeyValue k, float x, float y, KeyDown keyDown) private void drawLabel(Canvas canvas, KeyValue k, float x, float y, KeyDown keyDown)
{ {
k = KeyModifier.handleFlags(k, _flags); k = KeyModifier.handleFlags(k, _flags);
Paint p = ((k.flags & KeyValue.FLAG_KEY_FONT) != 0) ? _specialKeyLabelPaint : _keyLabelPaint; Paint p = _theme.labelPaint(((k.flags & KeyValue.FLAG_KEY_FONT) != 0));
p.setColor(labelColor(k, keyDown, _labelColor)); p.setColor(labelColor(k, keyDown, _theme.labelColor));
p.setTextSize(_labelTextSize * scaleTextSize(k)); p.setTextSize(_theme.labelTextSize * scaleTextSize(k));
canvas.drawText(k.symbol, x, y, p); canvas.drawText(k.symbol, x, y, p);
} }
private void drawSubLabel(Canvas canvas, KeyValue k, float x, float y, boolean right, boolean up, KeyDown keyDown) private void drawSubLabel(Canvas canvas, KeyValue k, float x, float y, boolean right, boolean up, KeyDown keyDown)
{ {
k = KeyModifier.handleFlags(k, _flags); k = KeyModifier.handleFlags(k, _flags);
Paint p = ((k.flags & KeyValue.FLAG_KEY_FONT) != 0) ? _specialKeySubLabelPaint : _keySubLabelPaint; Paint p = _theme.subLabelPaint(((k.flags & KeyValue.FLAG_KEY_FONT) != 0), right);
p.setColor(labelColor(k, keyDown, _subLabelColor)); p.setColor(labelColor(k, keyDown, _theme.subLabelColor));
p.setTextAlign(right ? Paint.Align.RIGHT : Paint.Align.LEFT); p.setTextSize(_theme.sublabelTextSize * scaleTextSize(k));
p.setTextSize(_sublabelTextSize * scaleTextSize(k));
y -= up ? p.ascent() : p.descent(); y -= up ? p.ascent() : p.descent();
canvas.drawText(k.symbol, x, y, p); canvas.drawText(k.symbol, x, y, p);
} }
private float scaleTextSize(KeyValue k) private float scaleTextSize(KeyValue k)
{ {
return (k.symbol.length() < 2) ? 1.f : 0.8f; return ((k.symbol.length() < 2) ? 1.f : 0.8f) * _config.characterSize;
} }
private static class KeyDown private static class KeyDown

View File

@ -0,0 +1,64 @@
package juloo.keyboard2;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
public class Theme
{
public final Paint keyBgPaint = new Paint();
public final Paint keyDownBgPaint = new Paint();
public final int lockedColor;
public final int activatedColor;
public final int labelColor;
public final int subLabelColor;
public final float labelTextSize;
public final float sublabelTextSize;
private final Paint _keyLabelPaint;
private final Paint _specialKeyLabelPaint;
private final Paint _keySubLabelPaint;
private final Paint _specialKeySubLabelPaint;
public Theme(Context context, AttributeSet attrs)
{
Resources res = context.getResources();
lockedColor = res.getColor(R.color.key_label_locked);
activatedColor = res.getColor(R.color.key_label_activated);
labelColor = res.getColor(R.color.key_label);
subLabelColor = res.getColor(R.color.key_sub_label);
labelTextSize = res.getDimension(R.dimen.label_text_size);
sublabelTextSize = res.getDimension(R.dimen.sublabel_text_size);
keyBgPaint.setColor(res.getColor(R.color.key_bg));
keyDownBgPaint.setColor(res.getColor(R.color.key_down_bg));
_keyLabelPaint = initLabelPaint(Paint.Align.CENTER, null);
_keySubLabelPaint = initLabelPaint(Paint.Align.LEFT, null);
Typeface specialKeysFont = ((Keyboard2)context).getSpecialKeyFont();
_specialKeyLabelPaint = initLabelPaint(Paint.Align.CENTER, specialKeysFont);
_specialKeySubLabelPaint = initLabelPaint(Paint.Align.LEFT, specialKeysFont);
}
public Paint labelPaint(boolean special_font)
{
Paint p = special_font ? _specialKeyLabelPaint : _keyLabelPaint;
return p;
}
public Paint subLabelPaint(boolean special_font, boolean align_right)
{
Paint p = special_font ? _specialKeySubLabelPaint : _keySubLabelPaint;
p.setTextAlign(align_right ? Paint.Align.RIGHT : Paint.Align.LEFT);
return p;
}
private Paint initLabelPaint(Paint.Align align, Typeface font)
{
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setTextAlign(align);
if (font != null)
paint.setTypeface(font);
return (paint);
}
}