// PolarisUI retained controls — custom SDK license, see LICENSE. #pragma once #include #include #include #include namespace polaris::ui { enum class ControlKind { none, Button, Label, CheckBox, RadioButton, Switch, TextField, TextArea, PasswordField, SearchField, Slider, ProgressBar, ScrollBar, ComboSelect, ComboBox, ListBox, Spinner, Tabs, MenuBar, DropDownMenu, RightClickMenu, ToolBar, StatusBar, Callout, Tooltip, Notification, Dialog, Modal, MessageBox, Panel, GroupBox, Accordion, SplitPane, ScrollPane, TreeView, Table, GridView, PropertyGrid, Paginator, Sidebar, NavBar, Breadcrumb, ColorPicker, DatePicker, TimePicker, FilePicker, DirectoryPicker, IconButton, ToggleButton, RadioGroup, ButtonGroup, ListItem, TreeNode, TableCell, TabPage, Card, Avatar, Badge, Separator, SpinnerIndicator, RingProgress, DragBar, RangeSlider, NumberField, KeyBindingField, AutoComplete, PopupMenu, ContextMenu, CommandPalette, count }; static_assert(static_cast(ControlKind::count) == 69, "Java control inventory changed"); #define POLARIS_CONTROL(Name) \ ControlKind kind() const noexcept override { return ControlKind::Name; } \ const char *class_name() const noexcept override { return #Name; } class Scene; class Panel; class Widget; class PopupMenu; class CommandPalette; class Notification; enum class EventType { down, move, up, wheel, key, text, focus, blur }; struct Event { EventType type = EventType::move; Vec2 position; Input input; int key = 0, button = 1, clicks = 1; float wheel = 0; std::string text; bool handled = false; }; struct PaintContext { Canvas &canvas; Scene &scene; const Theme &theme; double time; }; class Widget : public std::enable_shared_from_this { public: explicit Widget(std::string text = ""); virtual ~Widget(); Widget(const Widget &) = delete; Widget &operator=(const Widget &) = delete; Rect bounds{0, 0, 160, 36}; std::string id, text, tooltip; bool visible = true, enabled = true, focusable = false, clip_children = false, mouse_transparent = false; std::function on_click, on_change; std::shared_ptr context_menu; virtual ControlKind kind() const noexcept { return ControlKind::none; } virtual const char *class_name() const noexcept { return "Widget"; } virtual void event(Event &event); virtual void layout(Scene &scene); virtual void tick(Scene &scene); virtual void paint(PaintContext &context); virtual void paint_overlay(PaintContext &context); virtual bool text_input() const noexcept { return false; } virtual bool children_active() const noexcept { return true; } virtual Vec2 content_offset() const noexcept { return {}; } virtual Rect children_clip() const noexcept { return screen_; } virtual std::shared_ptr hit(Vec2 point); void add(std::shared_ptr child); void remove(const std::shared_ptr &child); void clear(); template std::shared_ptr emplace(Args &&...args) { auto child = std::make_shared(std::forward(args)...); add(child); return child; } const std::vector> &children() const noexcept { return children_; } Widget *parent() const noexcept { return parent_; } Scene *scene() const noexcept { return scene_; } Rect screen_bounds() const noexcept { return screen_; } Rect effective_clip() const noexcept { return clip_; } bool hovered() const noexcept { return hovered_; } bool pressed() const noexcept { return pressed_; } bool focused() const noexcept; void activate(); void changed(); bool descendant_of(const Widget *ancestor) const noexcept; protected: friend class Scene; std::vector> children_; Widget *parent_ = nullptr; Scene *scene_ = nullptr; Rect screen_, clip_; bool hovered_ = false, pressed_ = false; void surface(PaintContext &context, bool selected = false, float radius = -1) const; void centered(PaintContext &context, const std::string &label, Rect rect, Color color, float size = 0, int weight = 400) const; virtual float clip_radius(const Theme &theme) const noexcept; void place(Scene &scene, Vec2 origin, Rect clip); void draw(PaintContext &context); }; class Scene { public: Scene(); ~Scene(); Scene(const Scene &) = delete; Scene &operator=(const Scene &) = delete; Theme theme; Panel &root(); std::shared_ptr root_ptr() const { return root_; } void frame(Canvas &canvas, const Input &input = {}, double time = 0); void layout(float width, float height); void focus(std::shared_ptr widget); std::shared_ptr focused() const { return focus_; } std::shared_ptr find(const std::string &id) const; void show(std::shared_ptr overlay, bool modal = false, bool popup = false, Vec2 position = {-1, -1}, bool keep_focus = false); void close(const Widget *overlay); void close_top(); void notify(std::shared_ptr notification, double lifetime = 3); std::shared_ptr command_palette; std::function clipboard_write; bool wants_text_input() const; float width() const noexcept { return width_; } float height() const noexcept { return height_; } double time() const noexcept { return time_; } const Input &input() const noexcept { return input_; } std::size_t overlay_count() const noexcept { return overlays_.size(); } std::size_t notification_count() const noexcept { return notices_.size(); } const std::set &rendered_controls() const noexcept { return rendered_; } const std::set &rendered_classes() const noexcept { return rendered_names_; } Canvas *metrics() const noexcept { return canvas_; } void reset_render_coverage() { rendered_.clear(); rendered_names_.clear(); } private: friend class Widget; struct Overlay { std::shared_ptr widget, previous_focus; bool modal, popup, keep_focus; }; struct Notice { std::shared_ptr widget; double expires; }; std::shared_ptr root_; std::shared_ptr focus_, capture_, hover_; std::vector overlays_; std::vector notices_; std::set rendered_; std::set rendered_names_; Canvas *canvas_ = nullptr; Input input_; float width_ = 1, height_ = 1; double time_ = 0, last_click_time_ = -10, hover_since_ = 0; Vec2 last_click_position_; const Widget *last_click_widget_ = nullptr; bool swallow_release_ = false; std::vector> focus_order() const; std::shared_ptr pointer_target(Vec2 point) const; bool dispatch(std::shared_ptr target, Event event, bool bubble = false); void tick_tree(const std::shared_ptr &widget); void collect_focus(const std::shared_ptr &widget, std::vector> &result) const; }; struct ControlDescriptor { int java_index; ControlKind kind; const char *name; const char *category; std::shared_ptr (*create)(); }; const std::vector &control_registry(); const ControlDescriptor &describe(ControlKind kind); std::shared_ptr make_control(ControlKind kind); } // namespace polaris::ui