Program Listing for File Slider.h

Program Listing for File Slider.h#

Return to documentation for file (include/Karana/WebUI/Slider.h)

/*
 * Copyright (c) 2024-2026 Karana Dynamics Pty Ltd. All rights reserved.
 *
 * NOTICE TO USER:
 *
 * This source code and/or documentation (the "Licensed Materials") is
 * the confidential and proprietary information of Karana Dynamics Inc.
 * Use of these Licensed Materials is governed by the terms and conditions
 * of a separate software license agreement between Karana Dynamics and the
 * Licensee ("License Agreement"). Unless expressly permitted under that
 * agreement, any reproduction, modification, distribution, or disclosure
 * of the Licensed Materials, in whole or in part, to any third party
 * without the prior written consent of Karana Dynamics is strictly prohibited.
 *
 * THE LICENSED MATERIALS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
 * KARANA DYNAMICS DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-INFRINGEMENT, AND
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * IN NO EVENT SHALL KARANA DYNAMICS BE LIABLE FOR ANY DAMAGES WHATSOEVER,
 * INCLUDING BUT NOT LIMITED TO LOSS OF PROFITS, DATA, OR USE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES, WHETHER IN CONTRACT, TORT,
 * OR OTHERWISE ARISING OUT OF OR IN CONNECTION WITH THE LICENSED MATERIALS.
 *
 * U.S. Government End Users: The Licensed Materials are a "commercial item"
 * as defined at 48 C.F.R. 2.101, and are provided to the U.S. Government
 * only as a commercial end item under the terms of this license.
 *
 * Any use of the Licensed Materials in individual or commercial software must
 * include, in the user documentation and internal source code comments,
 * this Notice, Disclaimer, and U.S. Government Use Provision.
 */

#pragma once

#include "Karana/WebUI/Router.h"
#include "Karana/WebUI/Widget.h"
#include <functional>

namespace Karana::WebUI {
    /// Widget displaying a Slider triggering a server-side callback
    struct SliderOptions {
      public:
        /// Lower bound on possible values
        double min = 0;
        /// Upper bound on possible values
        double max = 100;
        /// Step size of values on slider
        double step = 1;
        // TODO @kelly fix this
        /// Whether to use log scale (currently unsupported)
        bool log_scale = false;
        /// Tooltip to display on hover (if any)
        std::string_view tooltip = "";
    };

    /**
     * @brief Widget displaying a slider + editable text box composed of two HTML `range` objects.
     *
     * Uses a single State object to store the value, and supports customization via a constructor
     * parameter.
     *
     */
    class Slider : public Widget {
      public:
        /**
         * @brief Slider constructor
         * @param router The connection router to use
         * @param text The text displayed in the Slider
         * @param on_change Called when the Slider is modified
         * @param opts Options for the slider (e.g. min, max, step)
         * @param value_state State for synchronizing the slider's value
         */
        Slider(Router &router,
               std::string_view text,
               std::function<void(double)> on_change = {},
               const SliderOptions &opts = {},
               const Karana::Core::ks_ptr<State> &value_state = nullptr);

        /**
         * @brief Slider destructor
         */
        virtual ~Slider();

        /**
         * @brief Set the current value of the slider
         * @param value The value of the slider
         */
        void setValue(double value);

        /**
         * @brief Set the min value of the slider
         * @param min The lower bound on possible values
         */
        void setMin(double min);

        /**
         * @brief Set the max value of the slider
         * @param max The upper bound on possible values
         */
        void setMax(double max);

        /**
         * @brief Set the step size of the slider
         * @param step The step size of values on the slider
         */
        void setStep(double step);

      protected:
        /**
         * @brief First setup on initial connection
         *
         * @param client_id The ID of the client connecting.
         */
        virtual void _onConnect(int client_id) override;

      private:
        double _min = 0.0;
        double _max = 100.0;
        double _step = 1.0;
        Karana::Core::id_t _state_callback_id;
    };
} // namespace Karana::WebUI