/* * widgets.cpp * Widgets tab for the embedded desktop settings app * Copyright (c) 2026 Daniel Hammer */ // Lists every top bar widget the desktop registers (desktop/widgets.cpp). // Pinned ones come first, in the order they appear on the bar; the tab only // edits DesktopSettings, and the panel picks the change up on its next frame. #include "settings_internal.hpp" #include "../../widgets.hpp" namespace settings_app { namespace { constexpr int ROW_H = 46; constexpr int MOVE_BTN_W = 52; constexpr int MOVE_BTN_H = 24; constexpr int MOVE_BTN_GAP = 6; constexpr int UNIT_BTN_W = 96; constexpr int UNIT_BTN_H = 28; constexpr int MAX_ROWS = 16; struct WidgetRow { const PanelWidgetInfo* info; int pinned_index; // position on the bar, -1 when not pinned }; // One geometry shared by drawing and hit testing, so they cannot drift. struct WidgetsLayout { WidgetRow rows[MAX_ROWS]; int row_count; int pinned_count; int x; int width; int list_y; int unit_y; Rect row_rect(int i) const { return {x, list_y + i * ROW_H, width, ROW_H}; } Rect check_rect(int i) const { return {x, list_y + i * ROW_H + 4, width - 2 * MOVE_BTN_W - MOVE_BTN_GAP - 12, 20}; } Rect down_rect(int i) const { return {x + width - MOVE_BTN_W, list_y + i * ROW_H + (ROW_H - MOVE_BTN_H) / 2 - 2, MOVE_BTN_W, MOVE_BTN_H}; } Rect up_rect(int i) const { Rect d = down_rect(i); return {d.x - MOVE_BTN_W - MOVE_BTN_GAP, d.y, MOVE_BTN_W, MOVE_BTN_H}; } Rect celsius_rect() const { return {x + 180, unit_y, UNIT_BTN_W, UNIT_BTN_H}; } Rect fahrenheit_rect() const { return {x + 180 + UNIT_BTN_W + 8, unit_y, UNIT_BTN_W, UNIT_BTN_H}; } }; WidgetsLayout widgets_layout(const SettingsState* st, int content_w) { const DesktopState* ds = st->desktop; WidgetsLayout l; l.row_count = 0; l.x = 16; l.width = content_w - 2 * l.x; int total = desktop_widget_count(); // Pinned widgets first, in bar order... for (int p = 0; p < ds->settings.panel_widget_count && l.row_count < MAX_ROWS; p++) { for (int i = 0; i < total; i++) { const PanelWidgetInfo* info = desktop_widget_info(i); if (montauk::streq(info->id, ds->settings.panel_widgets[p])) { l.rows[l.row_count++] = {info, p}; break; } } } l.pinned_count = l.row_count; // ...then the rest, in registry order. for (int i = 0; i < total && l.row_count < MAX_ROWS; i++) { const PanelWidgetInfo* info = desktop_widget_info(i); if (desktop_widget_pinned_index(ds, info->id) < 0) l.rows[l.row_count++] = {info, -1}; } int sfh = system_font_height(); l.list_y = 12 + (sfh + 6) + (sfh + 12); l.unit_y = l.list_y + l.row_count * ROW_H + 8 + 14; return l; } } // namespace void settings_draw_widgets(Canvas& c, SettingsState* st) { DesktopState* ds = st->desktop; mtk::Theme theme = settings_theme(st); WidgetsLayout l = widgets_layout(st, c.w); int sfh = system_font_height(); c.text(l.x, 12, "Top Bar Widgets", theme.text); c.text(l.x, 12 + sfh + 6, "Pinned widgets show in the top bar in this order.", theme.text_muted); for (int i = 0; i < l.row_count; i++) { const WidgetRow& row = l.rows[i]; bool pinned = row.pinned_index >= 0; Rect check = l.check_rect(i); mtk::draw_checkbox(c, check, row.info->name, mtk::check_state(pinned), theme); c.text(check.x + 24, check.y + check.h + 1, row.info->description, theme.text_muted); if (pinned) { bool can_up = row.pinned_index > 0; bool can_down = row.pinned_index < l.pinned_count - 1; mtk::draw_button(c, l.up_rect(i), "Up", mtk::BUTTON_SECONDARY, mtk::widget_state(false, false, can_up), theme); mtk::draw_button(c, l.down_rect(i), "Down", mtk::BUTTON_SECONDARY, mtk::widget_state(false, false, can_down), theme); } } mtk::draw_separator(c, l.x, l.unit_y - 14, l.width, theme); bool fahrenheit = ds->settings.weather_fahrenheit; c.text(l.x, l.unit_y + 6, "Weather Unit", theme.text); mtk::draw_button(c, l.celsius_rect(), "Celsius", mtk::BUTTON_SECONDARY, mtk::widget_state(!fahrenheit), theme); mtk::draw_button(c, l.fahrenheit_rect(), "Fahrenheit", mtk::BUTTON_SECONDARY, mtk::widget_state(fahrenheit), theme); } bool settings_handle_widgets_click(Window* win, SettingsState* st, int mx, int cy) { DesktopState* ds = st->desktop; WidgetsLayout l = widgets_layout(st, win->content_w); for (int i = 0; i < l.row_count; i++) { const WidgetRow& row = l.rows[i]; bool pinned = row.pinned_index >= 0; if (pinned && l.up_rect(i).contains(mx, cy)) { if (desktop_widget_move(ds, row.info->id, -1)) settings_persist(st); return true; } if (pinned && l.down_rect(i).contains(mx, cy)) { if (desktop_widget_move(ds, row.info->id, +1)) settings_persist(st); return true; } if (l.check_rect(i).contains(mx, cy)) { desktop_widget_set_pinned(ds, row.info->id, !pinned); settings_persist(st); return true; } } bool want_f = l.fahrenheit_rect().contains(mx, cy); if (want_f || l.celsius_rect().contains(mx, cy)) { ds->settings.weather_fahrenheit = want_f; settings_persist(st); return true; } return false; } } // namespace settings_app