diff --git a/examples/example1/src/example1.d b/examples/example1/src/example1.d index cb08fdb0..a30e373e 100644 --- a/examples/example1/src/example1.d +++ b/examples/example1/src/example1.d @@ -567,6 +567,11 @@ extern (C) int UIAppMain(string[] args) { buttons10.addChild(new TextWidget(null, "ImageTextButton widgets: "d)); buttons10.addChild(new ImageTextButton("btn5", "text-plain", "Enabled"d)); buttons10.addChild((new ImageTextButton("btn6", "folder", "Disabled"d)).enabled(false)); + buttons10.addChild(new TextWidget(null, "SwitchWidget widgets: "d)); + buttons10.addChild((new SwitchWidget("SW1")).checked(true)); + buttons10.addChild((new SwitchWidget("SW2")).checked(false)); + buttons10.addChild((new SwitchWidget("SW3")).checked(true).enabled(false)); + buttons10.addChild((new SwitchWidget("SW4")).checked(false).enabled(false)); layout3.addChild(buttons10); WidgetGroup buttons11 = new HorizontalLayout(); diff --git a/src/dlangui/widgets/controls.d b/src/dlangui/widgets/controls.d index 4cace9a1..c20144e2 100644 --- a/src/dlangui/widgets/controls.d +++ b/src/dlangui/widgets/controls.d @@ -159,6 +159,49 @@ class MultilineTextWidget : TextWidget { } } +/// Switch (on/off) widget +class SwitchWidget : Widget { + this(string ID = null) { + super(ID); + styleId = STYLE_SWITCH; + clickable = true; + focusable = true; + trackHover = true; + } + // called to process click and notify listeners + override protected bool handleClick() { + checked = !checked; + return super.handleClick(); + } + override void measure(int parentWidth, int parentHeight) { + DrawableRef img = backgroundDrawable; + int w = 0; + int h = 0; + if (!img.isNull) { + w = img.width; + h = img.height; + } + measuredContent(parentWidth, parentHeight, w, h); + } + + override void onDraw(DrawBuf buf) { + if (visibility != Visibility.Visible) + return; + Rect rc = _pos; + applyMargins(rc); + auto saver = ClipRectSaver(buf, rc, alpha); + DrawableRef img = backgroundDrawable; + if (!img.isNull) { + Point sz; + sz.x = img.width; + sz.y = img.height; + applyAlign(rc, sz); + uint st = state; + img.drawTo(buf, rc, st); + } + } +} + /// static image widget class ImageWidget : Widget { diff --git a/src/dlangui/widgets/styles.d b/src/dlangui/widgets/styles.d index d0aefc11..b84a572b 100644 --- a/src/dlangui/widgets/styles.d +++ b/src/dlangui/widgets/styles.d @@ -49,6 +49,8 @@ immutable string STYLE_BUTTON_IMAGE = "BUTTON_IMAGE"; immutable string STYLE_BUTTON_TRANSPARENT = "BUTTON_TRANSPARENT"; /// style id for Button w/o margins immutable string STYLE_BUTTON_NOMARGINS = "BUTTON_NOMARGINS"; +/// standard style id for Switch +immutable string STYLE_SWITCH = "SWITCH"; /// standard style id for CheckBox immutable string STYLE_CHECKBOX = "CHECKBOX"; /// standard style id for CheckBox image diff --git a/views/res/mdpi/switch_off.png b/views/res/mdpi/switch_off.png new file mode 100644 index 00000000..c46f26ae Binary files /dev/null and b/views/res/mdpi/switch_off.png differ diff --git a/views/res/mdpi/switch_off_disabled.png b/views/res/mdpi/switch_off_disabled.png new file mode 100644 index 00000000..4928fe1a Binary files /dev/null and b/views/res/mdpi/switch_off_disabled.png differ diff --git a/views/res/mdpi/switch_on.png b/views/res/mdpi/switch_on.png new file mode 100644 index 00000000..31d53274 Binary files /dev/null and b/views/res/mdpi/switch_on.png differ diff --git a/views/res/mdpi/switch_on_disabled.png b/views/res/mdpi/switch_on_disabled.png new file mode 100644 index 00000000..e5dce452 Binary files /dev/null and b/views/res/mdpi/switch_on_disabled.png differ diff --git a/views/res/switch.xml b/views/res/switch.xml new file mode 100644 index 00000000..19997211 --- /dev/null +++ b/views/res/switch.xml @@ -0,0 +1,10 @@ + + + + + + + + diff --git a/views/res/theme_default.xml b/views/res/theme_default.xml index ceffedeb..3327410f 100644 --- a/views/res/theme_default.xml +++ b/views/res/theme_default.xml @@ -66,6 +66,11 @@ margins="2,2,2,2" align="Center" /> +