-
Notifications
You must be signed in to change notification settings - Fork 42
Feat/colorpicker #705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Feat/colorpicker #705
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| // AUI Framework - Declarative UI toolkit for modern C++20 | ||
| // Copyright (C) 2020-2025 Alex2772 and Contributors | ||
| // | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
| // | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| // file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <AUI/View/AGroupBox.h> | ||
| #include <AUI/View/ASlider.h> | ||
|
|
||
| namespace declarative::color_picker { | ||
|
|
||
| /** | ||
| * @brief View that displays a single color. | ||
| * @ingroup views_indication | ||
| * @details | ||
| * The `ColorView` simply renders a solid rectangle matching the provided color contract. It is intended to be used as a | ||
| * visual preview of a color value. | ||
| * | ||
| * <!-- aui:snippet examples/ui/color_picker1/src/main.cpp colorpicker_example --> | ||
| */ | ||
| struct ColorView { | ||
| /** | ||
| * @brief The color to display. | ||
| */ | ||
| contract::In<AColor> color; | ||
|
|
||
| _<AView> operator()() { | ||
| auto color = this->color; | ||
| return Centered { | ||
| _new<AView>() AUI_LET { | ||
| AObject::connect( | ||
| AUI_REACT(ass::PropertyListRecursive { | ||
| BackgroundSolid { | ||
| color.value(), | ||
| }, | ||
| Expanding {}, | ||
| }), | ||
| AUI_SLOT(it)::setCustomStyle); | ||
| }, | ||
| }; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * @brief RGB sliders for editing a color. | ||
| * @ingroup views_input | ||
| * @details | ||
| * `SlidersRGB` exposes three sliders, one for each channel (red, green, | ||
| * blue). The sliders are bound to a shared `AColor` contract and notify a | ||
| * callback when the color changes. | ||
| * | ||
| * <!-- aui:snippet examples/ui/color_picker1/src/main.cpp colorpicker_example --> | ||
| */ | ||
| struct SlidersRGB { | ||
| /** | ||
| * @brief Pointer to a color channel. | ||
| */ | ||
| using Channel = float(AColor::*); | ||
|
|
||
| /** | ||
| * @brief The color contract to edit. | ||
| */ | ||
| contract::In<AColor> color; | ||
|
|
||
| /** | ||
| * @brief Callback invoked when the color changes. | ||
| */ | ||
| std::function<void(AColor)> onColorChanged; | ||
|
|
||
| /** | ||
| * @brief Factory used to create individual sliders. | ||
| */ | ||
| std::function<_<AView>(contract::In<AColor> color, std::function<void(AColor)> onColorChanged, Channel channel)> | ||
| sliderFactory = defaultSlider; | ||
|
|
||
| /** | ||
| * @brief Creates a slider for a single channel. | ||
| * @param color The color contract. | ||
| * @param onColorChanged Callback when the color changes. | ||
| * @param channel Pointer to the channel member. | ||
| * @return A view containing the slider. | ||
| */ | ||
| static _<AView> | ||
| defaultSlider(contract::In<AColor> color, std::function<void(AColor)> onColorChanged, Channel channel) { | ||
| return Slider { | ||
| .value = AUI_REACT(std::invoke(channel, color)), | ||
| .onValueChanged = | ||
| [color, onColorChanged = std::move(onColorChanged), channel](aui::float_within_0_1 v) { | ||
| auto copy = *color; | ||
| std::invoke(channel, copy) = v; | ||
| onColorChanged(copy); | ||
| }, | ||
| .track = | ||
| _new<AView>() AUI_LET { | ||
| AObject::connect( | ||
| AUI_REACT(ass::PropertyListRecursive { | ||
| Expanding {}, | ||
| BackgroundGradient { | ||
| adjustChannel(color.value(), channel, 0.0f), | ||
| adjustChannel(color.value(), channel, 1.0f), | ||
| 90_deg, | ||
| }, | ||
| MinSize { 20_dp }, | ||
| }), | ||
| AUI_SLOT(it)::setCustomStyle); | ||
| }, | ||
| .handle = | ||
| _new<AView>() AUI_OVERRIDE_STYLE { | ||
| FixedSize { 3_px, {} }, | ||
| Expanding {}, | ||
| BackgroundSolid { AColor::WHITE }, | ||
| Border { 1_px, AColor::BLACK }, | ||
| Margin { 4_px, 0 }, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| _<AView> operator()() { | ||
| return Vertical { | ||
| sliderFactory(color, onColorChanged, &AColor::r), | ||
| sliderFactory(color, onColorChanged, &AColor::g), | ||
| sliderFactory(color, onColorChanged, &AColor::b), | ||
| } AUI_OVERRIDE_STYLE { LayoutSpacing { 4_dp } }; | ||
| } | ||
|
|
||
| private: | ||
| /** | ||
| * @brief Adjusts a single channel of a color. | ||
| * @param src The source color. | ||
| * @param channel Pointer to the channel member. | ||
| * @param newValue The new channel value. | ||
| * @return The color with the channel updated. | ||
| */ | ||
| static AColor adjustChannel(AColor src, float(AColor::* channel), float newValue) { | ||
| std::invoke(channel, src) = newValue; | ||
| return src; | ||
| } | ||
| }; | ||
| } // namespace declarative::color_picker | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| cmake_minimum_required(VERSION 3.10) | ||
|
|
||
| get_filename_component(_t ${CMAKE_CURRENT_SOURCE_DIR} NAME) | ||
|
|
||
| aui_executable("aui.example.${_t}") | ||
| aui_link("aui.example.${_t}" PRIVATE aui::core aui::views) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * AUI Framework - Declarative UI toolkit for modern C++20 | ||
| * Copyright (C) 2020-2025 Alex2772 and Contributors | ||
| * | ||
| * SPDX-License-Identifier: MPL-2.0 | ||
| * | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| */ | ||
| /// [colorpicker_example] | ||
| #include <AUI/Platform/Entry.h> | ||
| #include <AUI/Platform/AWindow.h> | ||
| #include <AUI/View/AColorPicker.h> | ||
|
|
||
| using namespace ass; | ||
| using namespace declarative; | ||
|
|
||
|
|
||
| class MainWindow : public AWindow { | ||
| public: | ||
| MainWindow() : AWindow("Color picker", 600_dp, 300_dp) { | ||
| auto color = _new<AProperty<AColor>>(AColor::RED); | ||
| setContents(Vertical { | ||
| GroupBox { | ||
| Label { "View" }, | ||
| color_picker::ColorView { AUI_REACT(**color) } AUI_OVERRIDE_STYLE { | ||
| FixedSize(40_dp, 20_dp), | ||
| Border { 1_px, AColor::BLACK }, | ||
| Padding { 2_px }, | ||
| }, | ||
| }, | ||
| GroupBox { | ||
| Label { "Sliders RGB" }, | ||
| color_picker::SlidersRGB { | ||
| .color = AUI_REACT(**color), | ||
| .onColorChanged = [color](AColor c) { *color = c; }, | ||
| }, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| }; | ||
|
|
||
| AUI_ENTRY { | ||
| _new<MainWindow>()->show(); | ||
| return 0; | ||
| } | ||
| /// [colorpicker_example] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.