support overriding of drawables in theme using customDrawable

This commit is contained in:
Vadim Lopatin 2015-03-06 10:24:14 +03:00
parent 4a4152bd67
commit c646095cb8
2 changed files with 13 additions and 18 deletions

View File

@ -148,7 +148,7 @@ class ImageWidget : Widget {
if (!_drawable.isNull) if (!_drawable.isNull)
return _drawable; return _drawable;
if (_drawableId !is null) if (_drawableId !is null)
_drawable = drawableCache.get(_drawableId); _drawable = drawableCache.get(overrideCustomDrawableId(_drawableId));
return _drawable; return _drawable;
} }
/// set custom drawable (not one from resources) /// set custom drawable (not one from resources)
@ -167,6 +167,12 @@ class ImageWidget : Widget {
return this; return this;
} }
/// handle theme change: e.g. reload some themed resources
override void onThemeChanged() {
if (_drawableId !is null)
_drawable.clear(); // remove cached drawable
}
override void measure(int parentWidth, int parentHeight) { override void measure(int parentWidth, int parentHeight) {
DrawableRef img = drawable; DrawableRef img = drawable;
int w = 0; int w = 0;
@ -310,13 +316,13 @@ class UrlImageTextButton : ImageTextButton {
/// checkbox /// checkbox
class CheckBox : ImageTextButton { class CheckBox : ImageTextButton {
this(string ID = null, string textResourceId = null) { this(string ID = null, string textResourceId = null) {
super(ID, getCustomDrawableId("btn_check"), textResourceId); super(ID, "btn_check", textResourceId);
} }
this(string ID, dstring labelText) { this(string ID, dstring labelText) {
super(ID, getCustomDrawableId("btn_check"), labelText); super(ID, "btn_check", labelText);
} }
this(string ID, UIString label) { this(string ID, UIString label) {
super(ID, getCustomDrawableId("btn_check"), label); super(ID, "btn_check", label);
} }
override protected void init(string drawableId, UIString caption) { override protected void init(string drawableId, UIString caption) {
super.init(drawableId, caption); super.init(drawableId, caption);
@ -332,20 +338,15 @@ class CheckBox : ImageTextButton {
checked = !checked; checked = !checked;
return super.handleClick(); return super.handleClick();
} }
/// handle theme change: e.g. reload some themed resources
override void onThemeChanged() {
if (currentTheme)
_icon.drawableId = getCustomDrawableId("btn_check");
}
} }
/// radio button /// radio button
class RadioButton : ImageTextButton { class RadioButton : ImageTextButton {
this(string ID = null, string textResourceId = null) { this(string ID = null, string textResourceId = null) {
super(ID,getCustomDrawableId("btn_radio"), textResourceId); super(ID, "btn_radio", textResourceId);
} }
this(string ID, dstring labelText) { this(string ID, dstring labelText) {
super(ID, getCustomDrawableId("btn_radio"), labelText); super(ID, "btn_radio", labelText);
} }
override protected void init(string drawableId, UIString caption) { override protected void init(string drawableId, UIString caption) {
super.init(drawableId, caption); super.init(drawableId, caption);
@ -357,12 +358,6 @@ class RadioButton : ImageTextButton {
checkable = true; checkable = true;
} }
/// handle theme change: e.g. reload some themed resources
override void onThemeChanged() {
if (currentTheme)
_icon.drawableId = getCustomDrawableId("btn_radio");
}
void uncheckSiblings() { void uncheckSiblings() {
Widget p = parent; Widget p = parent;
if (!p) if (!p)

View File

@ -1372,7 +1372,7 @@ class DrawableAttribute {
} }
/// returns custom drawable replacement id for specified id from current theme, or returns passed value if not found or no current theme /// returns custom drawable replacement id for specified id from current theme, or returns passed value if not found or no current theme
string getCustomDrawableId(string id) { string overrideCustomDrawableId(string id) {
string res = currentTheme ? currentTheme.customDrawableId(id) : id; string res = currentTheme ? currentTheme.customDrawableId(id) : id;
return !res ? id : res; return !res ? id : res;
} }