Skip to content

bindings/cpp/desktop_gui.hpp

C++98 API source. Preserve the bindings/cpp/ layout when downloading multiple files.

Source files · Download desktop_gui.hpp

#ifndef GASPARDOS_DESKTOP_GUI_HPP
#define GASPARDOS_DESKTOP_GUI_HPP
#include "desktop_native.hpp"

namespace gaspardos
{
    namespace desktop
    {
        class TextInput
        {
        public:
            std::string value;
            std::size_t cursor;

            explicit TextInput(const std::string& text = "") : value(text), cursor(text.size())
            {
            }

            void        set(const std::string& text);
            void        key(uint32_t key);
            std::string visible(unsigned columns, bool focused) const;
        };

        class PaintQueue
        {
            struct Command
            {
                bool        text;
                int         x, y, w, h;
                uint32_t    color;
                std::string label;

                Command(
                    bool t, int px, int py, int pw, int ph, uint32_t c, const std::string& s = "")
                    : text(t), x(px), y(py), w(pw), h(ph), color(c), label(s)
                {
                }
            };

            std::vector<Command> commands_;
            std::size_t          position_;

        public:
            PaintQueue() : position_(0)
            {
            }

            void clear();
            void fill(int x, int y, int width, int height, uint32_t color);
            void text(int                x,
                      int                y,
                      const std::string& value,
                      uint32_t           color   = 0x202020,
                      unsigned           columns = 110);
            void button(int x, int y, int width, const std::string& text, bool selected = false);
            void input(int x, int y, int width, const TextInput& input, bool focused);

            bool finished() const
            {
                return position_ == commands_.size();
            }

            int64_t paint(gui::Painter& painter);
        };

        // Embedded modal dialog: forward events and tick/paint from the parent loop.
        // Parent network jobs continue running; no nested blocking event loop.
        class FileDialog
        {
            FileExplorer explorer_;
            PaintQueue   queue_;
            TextInput    address_, name_;
            std::string  lastDirectory_;
            std::size_t  scroll_, volumeScroll_;
            int          focus_, pressed_, lastClicked_;
            uint64_t     lastClick_;
            bool         active_, dirty_;
            void         snapshot();
            void         synchronize();
            int          hit(int x, int y) const;
            void         action(int id);

        public:
            explicit FileDialog(FileAccess& access);
            void    begin(FileExplorer::Mode mode,
                          const std::string& initial,
                          const std::string& suggested      = "",
                          bool               allowOverwrite = false);
            void    event(const GO64_Event& event, uint64_t ticks);
            void    tick();
            int64_t paint(gui::Painter& painter);

            bool active() const
            {
                return active_;
            }

            bool accepted() const
            {
                return !active_ && explorer_.accepted();
            }

            const std::string& result() const
            {
                return explorer_.result();
            }
        };
    }  // namespace desktop
}  // namespace gaspardos
#endif