From 188ee7598677406b9f8aac6ea6862c973ab67897 Mon Sep 17 00:00:00 2001 From: mkpaz Date: Tue, 14 Mar 2023 21:24:33 +0400 Subject: [PATCH] Add toggle switch pseudo-classes support --- CHANGELOG.md | 1 + docs/docs/reference/controls.md | 6 ++++ .../page/components/ToggleSwitchPage.java | 30 +++++++++++++++++-- styles/src/components/_toggle-switch.scss | 20 +++++++++++++ 4 files changed, 54 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d5a608..c5be3b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ - (Base) Improved Javadoc. See full API reference in [docs](https://mkpaz.github.io/atlantafx/apidocs/atlantafx.base/module-summary.html). - (Base) `ToggleSwitch` label position support (left or right). +- (CSS) `ToggleSwitch` pseudo-classes (`success`, `danger`) support. - (CSS) `Button` shadow support (`-color-button-shadow`). Only for themes compiled with the `button.$use-shadow` flag enabled. - (CSS) Looked-up color variables support: `Separator`. - (CSS) Added border radius/shadow to popup menu for `ComboBox` (and all `ComboBox`-based) controls. diff --git a/docs/docs/reference/controls.md b/docs/docs/reference/controls.md index 657fc35..d564b8d 100644 --- a/docs/docs/reference/controls.md +++ b/docs/docs/reference/controls.md @@ -280,3 +280,9 @@ Color variables: * `-color-button-bg-selected` * `-color-button-fg-selected` + +## Toggle Switch + +Pseudo-classes: + +* `:success`, `:danger` \ No newline at end of file diff --git a/sampler/src/main/java/atlantafx/sampler/page/components/ToggleSwitchPage.java b/sampler/src/main/java/atlantafx/sampler/page/components/ToggleSwitchPage.java index 561f553..1e1439c 100644 --- a/sampler/src/main/java/atlantafx/sampler/page/components/ToggleSwitchPage.java +++ b/sampler/src/main/java/atlantafx/sampler/page/components/ToggleSwitchPage.java @@ -3,6 +3,7 @@ package atlantafx.sampler.page.components; import atlantafx.base.controls.ToggleSwitch; +import atlantafx.base.theme.Styles; import atlantafx.sampler.page.AbstractPage; import atlantafx.sampler.page.Page; import atlantafx.sampler.page.SampleBlock; @@ -23,18 +24,19 @@ public class ToggleSwitchPage extends AbstractPage { super(); setUserContent(new FlowPane( Page.PAGE_HGAP, Page.PAGE_VGAP, - basicSample() + basicSample(), + stateSample() )); } private SampleBlock basicSample() { - var leftToggle = new ToggleSwitch("Enable"); + var leftToggle = new ToggleSwitch("Enabled"); leftToggle.selectedProperty().addListener( (obs, old, val) -> leftToggle.setText(val ? "Enabled" : "Disabled") ); leftToggle.setSelected(true); - var rightToggle = new ToggleSwitch("Disable"); + var rightToggle = new ToggleSwitch("Disabled"); rightToggle.selectedProperty().addListener( (obs, old, val) -> rightToggle.setText(val ? "Enabled" : "Disabled") ); @@ -43,4 +45,26 @@ public class ToggleSwitchPage extends AbstractPage { return new SampleBlock("Basic", new VBox(SampleBlock.BLOCK_VGAP, leftToggle, rightToggle)); } + + private SampleBlock stateSample() { + var successToggle = new ToggleSwitch("Enabled"); + successToggle.selectedProperty().addListener((obs, old, val) -> { + successToggle.setText(val ? "Enabled" : "Disabled"); + successToggle.pseudoClassStateChanged(Styles.STATE_SUCCESS, val); + } + ); + successToggle.setSelected(true); + successToggle.pseudoClassStateChanged(Styles.STATE_SUCCESS, true); + + var dangerToggle = new ToggleSwitch("Disabled"); + dangerToggle.selectedProperty().addListener((obs, old, val) -> { + dangerToggle.setText(val ? "Enabled" : "Disabled"); + dangerToggle.pseudoClassStateChanged(Styles.STATE_DANGER, val); + } + ); + dangerToggle.setLabelPosition(HorizontalDirection.RIGHT); + dangerToggle.setSelected(false); + + return new SampleBlock("State", new VBox(SampleBlock.BLOCK_VGAP, successToggle, dangerToggle)); + } } diff --git a/styles/src/components/_toggle-switch.scss b/styles/src/components/_toggle-switch.scss index 25ee268..64c295b 100644 --- a/styles/src/components/_toggle-switch.scss +++ b/styles/src/components/_toggle-switch.scss @@ -60,6 +60,26 @@ $thumb-area-padding: 0.85em 1.4em 0.85em 1.4em !default; >.thumb-area { -fx-background-color: $color-thumb-area-border-selected, $color-thumb-area-selected; } + + &:success { + >.thumb { + -fx-background-color: -color-success-emphasis, $color-thumb-selected; + } + + >.thumb-area { + -fx-background-color: -color-success-emphasis, -color-success-emphasis; + } + } + + &:danger { + >.thumb { + -fx-background-color: -color-danger-emphasis, $color-thumb-selected; + } + + >.thumb-area { + -fx-background-color: -color-danger-emphasis, -color-danger-emphasis; + } + } } &:right {