feat(controls): add popup_window & use it in bar widget settings

This commit is contained in:
Ly-sec
2026-05-07 12:11:44 +02:00
parent c9ecc49618
commit 730bea8d53
11 changed files with 803 additions and 28 deletions
+2
View File
@@ -475,6 +475,7 @@ _noctalia_sources = files(
'src/shell/settings/settings_registry.cpp',
'src/shell/settings/settings_sidebar.cpp',
'src/shell/settings/widget_settings_registry.cpp',
'src/shell/settings/widget_add_popup.cpp',
'src/shell/settings/settings_window.cpp',
'src/shell/test/test_panel.cpp',
'src/shell/tray/tray_drawer_panel.cpp',
@@ -550,6 +551,7 @@ _noctalia_sources = files(
'src/ui/controls/label.cpp',
'src/ui/controls/list_editor.cpp',
'src/ui/controls/progress_bar.cpp',
'src/ui/controls/popup_window.cpp',
'src/ui/controls/radio_button.cpp',
'src/ui/controls/scroll_view.cpp',
'src/ui/controls/search_picker.cpp',
+13 -1
View File
@@ -1921,6 +1921,7 @@ namespace settings {
if (!inherited) {
const bool pickerOpenForLane = ctx.openWidgetPickerPath == pickerKey;
auto addBtn = std::make_unique<Button>();
auto* addBtnPtr = addBtn.get();
addBtn->setText(i18n::tr("settings.entities.widget.add"));
addBtn->setGlyph("add");
addBtn->setVariant(pickerOpenForLane ? ButtonVariant::Default : ButtonVariant::Ghost);
@@ -1933,7 +1934,18 @@ namespace settings {
&editingWidgetName = ctx.editingWidgetName, &renamingWidgetName = ctx.renamingWidgetName,
&pendingDeleteWidgetName = ctx.pendingDeleteWidgetName, pickerKey,
&pendingDeleteWidgetSettingPath = ctx.pendingDeleteWidgetSettingPath,
&creatingWidgetType = ctx.creatingWidgetType, requestRebuild = ctx.requestRebuild]() {
&creatingWidgetType = ctx.creatingWidgetType, requestRebuild = ctx.requestRebuild,
openWidgetAddPopup = ctx.openWidgetAddPopup, lanePath, addBtnPtr]() {
if (openWidgetAddPopup) {
openWidgetPickerPath.clear();
editingWidgetName.clear();
renamingWidgetName.clear();
pendingDeleteWidgetName.clear();
pendingDeleteWidgetSettingPath.clear();
creatingWidgetType.clear();
openWidgetAddPopup(lanePath, addBtnPtr);
return;
}
openWidgetPickerPath = openWidgetPickerPath == pickerKey ? std::string{} : pickerKey;
editingWidgetName.clear();
renamingWidgetName.clear();
+1
View File
@@ -31,6 +31,7 @@ namespace settings {
std::function<void()> requestRebuild;
std::function<void()> resetContentScroll;
std::function<void(InputArea*)> focusArea;
std::function<void(const std::vector<std::string>&, Button*)> openWidgetAddPopup;
std::function<void(std::vector<std::string>, ConfigOverrideValue)> setOverride;
std::function<void(std::vector<std::pair<std::vector<std::string>, ConfigOverrideValue>>)> setOverrides;
std::function<void(std::vector<std::string>)> clearOverride;
+1
View File
@@ -1233,6 +1233,7 @@ namespace settings {
.requestRebuild = ctx.requestRebuild,
.resetContentScroll = ctx.resetContentScroll,
.focusArea = ctx.focusArea,
.openWidgetAddPopup = ctx.openBarWidgetAddPopup,
.setOverride = ctx.setOverride,
.setOverrides = ctx.setOverrides,
.clearOverride = ctx.clearOverride,
+2
View File
@@ -12,6 +12,7 @@
class Flex;
class InputArea;
class Button;
namespace settings {
@@ -38,6 +39,7 @@ namespace settings {
std::function<void()> requestContentRebuild;
std::function<void()> resetContentScroll;
std::function<void(InputArea*)> focusArea;
std::function<void(const std::vector<std::string>&, Button*)> openBarWidgetAddPopup;
std::function<void(std::vector<std::string>, ConfigOverrideValue)> setOverride;
std::function<void(std::vector<std::pair<std::vector<std::string>, ConfigOverrideValue>>)> setOverrides;
std::function<void(std::vector<std::string>)> clearOverride;
+174 -27
View File
@@ -49,6 +49,7 @@ namespace {
constexpr Logger kLog("settings");
constexpr std::int32_t kActionSupportReport = 1;
constexpr std::int32_t kActionFlattenedConfig = 2;
constexpr std::string_view kCreateInstancePrefix = "create-instance:";
constexpr float kWindowWidth = 1080.0f;
constexpr float kWindowHeight = 600.0f;
@@ -111,6 +112,73 @@ namespace {
return key;
}
bool isCreateInstanceValue(std::string_view value) { return value.starts_with(kCreateInstancePrefix); }
std::string createInstanceTypeFromValue(std::string_view value) {
if (!isCreateInstanceValue(value)) {
return {};
}
value.remove_prefix(kCreateInstancePrefix.size());
return std::string(value);
}
std::string pathKey(const std::vector<std::string>& path) {
std::string out;
for (const auto& part : path) {
if (!out.empty()) {
out.push_back('.');
}
out += part;
}
return out;
}
bool isBarWidgetListPath(const std::vector<std::string>& path) {
if (path.size() < 3 || path.front() != "bar") {
return false;
}
const auto& key = path.back();
return key == "start" || key == "center" || key == "end";
}
std::vector<std::string> barWidgetItemsForPath(const Config& cfg, const std::vector<std::string>& path) {
if (!isBarWidgetListPath(path) || path.size() < 3) {
return {};
}
const auto* bar = settings::findBar(cfg, path[1]);
if (bar == nullptr) {
return {};
}
const auto& lane = path.back();
if (path.size() >= 5 && path[2] == "monitor") {
const auto* ovr = settings::findMonitorOverride(*bar, path[3]);
if (ovr != nullptr) {
if (lane == "start") {
return ovr->startWidgets.value_or(bar->startWidgets);
}
if (lane == "center") {
return ovr->centerWidgets.value_or(bar->centerWidgets);
}
if (lane == "end") {
return ovr->endWidgets.value_or(bar->endWidgets);
}
}
}
if (lane == "start") {
return bar->startWidgets;
}
if (lane == "center") {
return bar->centerWidgets;
}
if (lane == "end") {
return bar->endWidgets;
}
return {};
}
} // namespace
SettingsWindow::~SettingsWindow() = default;
@@ -217,6 +285,10 @@ void SettingsWindow::destroyWindow() {
m_actionsMenuPopup->close();
m_actionsMenuPopup.reset();
}
if (m_widgetAddPopup != nullptr) {
m_widgetAddPopup->close();
m_widgetAddPopup.reset();
}
m_sceneRoot.reset();
m_surface.reset();
m_pointerInside = false;
@@ -340,6 +412,9 @@ void SettingsWindow::clearTransientSettingsState() {
m_pendingDeleteMonitorOverrideBarName.clear();
m_pendingDeleteMonitorOverrideMatch.clear();
m_pendingResetPageScope.clear();
if (m_widgetAddPopup != nullptr && m_widgetAddPopup->isOpen()) {
m_widgetAddPopup->close();
}
}
void SettingsWindow::openActionsMenu() {
@@ -401,6 +476,50 @@ void SettingsWindow::openActionsMenu() {
static_cast<std::int32_t>(m_actionsMenuButton->height()), m_surface->xdgSurface(), output);
}
void SettingsWindow::openBarWidgetAddPopup(const std::vector<std::string>& lanePath, Button* anchorButton) {
if (m_wayland == nullptr || m_renderContext == nullptr || m_surface == nullptr || anchorButton == nullptr ||
m_surface->xdgSurface() == nullptr || m_config == nullptr) {
return;
}
if (m_widgetAddPopup == nullptr) {
m_widgetAddPopup = std::make_unique<settings::WidgetAddPopup>(*m_wayland, *m_renderContext);
m_widgetAddPopup->setOnSelect([this](const std::vector<std::string>& selectedLanePath, const std::string& value) {
if (value.empty() || m_config == nullptr) {
return;
}
const Config& activeConfig = m_config->config();
auto laneItems = barWidgetItemsForPath(activeConfig, selectedLanePath);
m_pendingDeleteWidgetName.clear();
m_pendingDeleteWidgetSettingPath.clear();
m_renamingWidgetName.clear();
m_editingWidgetName.clear();
if (const auto type = createInstanceTypeFromValue(value); !type.empty()) {
m_creatingWidgetType = type;
m_openWidgetPickerPath = pathKey(selectedLanePath);
requestSceneRebuild();
return;
}
m_creatingWidgetType.clear();
m_openWidgetPickerPath.clear();
laneItems.push_back(value);
setSettingOverride(selectedLanePath, laneItems);
});
}
wl_output* output = m_wayland->lastPointerOutput();
if (output == nullptr) {
output = m_output;
}
m_widgetAddPopup->open(m_surface->xdgSurface(), output, m_wayland->lastInputSerial(), anchorButton, lanePath,
m_config->config(), uiScale(), PopupWindow::AnchorMode::CenterOnAnchor);
}
void SettingsWindow::saveSupportReport() {
if (m_config == nullptr) {
return;
@@ -898,33 +1017,36 @@ void SettingsWindow::rebuildSettingsContent() {
},
});
settings::addSettingsContentSections(*m_contentContainer, m_settingsRegistry,
settings::SettingsContentContext{
.config = cfg,
.configService = m_config,
.scale = scale,
.searchQuery = m_searchQuery,
.selectedSection = m_selectedSection,
.selectedBar = selectedBar,
.selectedMonitorOverride = selectedMonitorOverride,
.showAdvanced = m_showAdvanced,
.showOverriddenOnly = m_showOverriddenOnly,
.openWidgetPickerPath = m_openWidgetPickerPath,
.openSearchPickerPath = m_openSearchPickerPath,
.editingWidgetName = m_editingWidgetName,
.pendingDeleteWidgetName = m_pendingDeleteWidgetName,
.pendingDeleteWidgetSettingPath = m_pendingDeleteWidgetSettingPath,
.renamingWidgetName = m_renamingWidgetName,
.creatingWidgetType = m_creatingWidgetType,
.requestRebuild = requestRebuild,
.requestContentRebuild = requestContent,
.resetContentScroll = [this]() { m_contentScrollState.offset = 0.0f; },
.focusArea = [this](InputArea* area) { m_inputDispatcher.setFocus(area); },
.setOverride = setOverride,
.setOverrides = setOverrides,
.clearOverride = clearOverride,
.renameWidgetInstance = renameWidget,
});
settings::addSettingsContentSections(
*m_contentContainer, m_settingsRegistry,
settings::SettingsContentContext{
.config = cfg,
.configService = m_config,
.scale = scale,
.searchQuery = m_searchQuery,
.selectedSection = m_selectedSection,
.selectedBar = selectedBar,
.selectedMonitorOverride = selectedMonitorOverride,
.showAdvanced = m_showAdvanced,
.showOverriddenOnly = m_showOverriddenOnly,
.openWidgetPickerPath = m_openWidgetPickerPath,
.openSearchPickerPath = m_openSearchPickerPath,
.editingWidgetName = m_editingWidgetName,
.pendingDeleteWidgetName = m_pendingDeleteWidgetName,
.pendingDeleteWidgetSettingPath = m_pendingDeleteWidgetSettingPath,
.renamingWidgetName = m_renamingWidgetName,
.creatingWidgetType = m_creatingWidgetType,
.requestRebuild = requestRebuild,
.requestContentRebuild = requestContent,
.resetContentScroll = [this]() { m_contentScrollState.offset = 0.0f; },
.focusArea = [this](InputArea* area) { m_inputDispatcher.setFocus(area); },
.openBarWidgetAddPopup = [this](const std::vector<std::string>& lanePath,
Button* anchorButton) { openBarWidgetAddPopup(lanePath, anchorButton); },
.setOverride = setOverride,
.setOverrides = setOverrides,
.clearOverride = clearOverride,
.renameWidgetInstance = renameWidget,
});
}
void SettingsWindow::buildScene(std::uint32_t width, std::uint32_t height) {
@@ -1315,6 +1437,15 @@ bool SettingsWindow::onPointerEvent(const PointerEvent& event) {
return false;
}
if (m_widgetAddPopup != nullptr && m_widgetAddPopup->onPointerEvent(event)) {
return true;
}
if (m_widgetAddPopup != nullptr && m_widgetAddPopup->isOpen() && event.type == PointerEvent::Type::Button &&
event.state == 1) {
m_widgetAddPopup->close();
return true;
}
if (m_actionsMenuPopup != nullptr && m_actionsMenuPopup->onPointerEvent(event)) {
return true;
}
@@ -1390,6 +1521,16 @@ void SettingsWindow::onKeyboardEvent(const KeyboardEvent& event) {
if (!isOpen() || m_config == nullptr) {
return;
}
if (m_widgetAddPopup != nullptr && m_widgetAddPopup->isOpen()) {
if (event.pressed && m_config->matchesKeybind(KeybindAction::Cancel, event.sym, event.modifiers)) {
m_widgetAddPopup->close();
return;
}
m_widgetAddPopup->onKeyboardEvent(event);
return;
}
const auto requestRebuild = [this]() {
if (m_surface != nullptr) {
m_rebuildRequested = true;
@@ -1443,12 +1584,18 @@ void SettingsWindow::onKeyboardEvent(const KeyboardEvent& event) {
void SettingsWindow::onThemeChanged() {
if (isOpen()) {
if (m_widgetAddPopup != nullptr && m_widgetAddPopup->isOpen()) {
m_widgetAddPopup->requestRedraw();
}
m_surface->requestRedraw();
}
}
void SettingsWindow::onFontChanged() {
if (isOpen()) {
if (m_widgetAddPopup != nullptr && m_widgetAddPopup->isOpen()) {
m_widgetAddPopup->requestLayout();
}
m_surface->requestLayout();
}
}
+3
View File
@@ -4,6 +4,7 @@
#include "render/scene/input_dispatcher.h"
#include "render/scene/node.h"
#include "shell/settings/settings_registry.h"
#include "shell/settings/widget_add_popup.h"
#include "ui/controls/context_menu_popup.h"
#include "ui/controls/scroll_view.h"
#include "wayland/toplevel_surface.h"
@@ -58,6 +59,7 @@ private:
void clearStatusMessage();
void clearTransientSettingsState();
void openActionsMenu();
void openBarWidgetAddPopup(const std::vector<std::string>& lanePath, Button* anchorButton);
void saveSupportReport();
void saveFlattenedConfig();
void setSettingOverride(std::vector<std::string> path, ConfigOverrideValue value);
@@ -87,6 +89,7 @@ private:
Button* m_actionsMenuButton = nullptr;
Flex* m_contentContainer = nullptr;
std::unique_ptr<ContextMenuPopup> m_actionsMenuPopup;
std::unique_ptr<settings::WidgetAddPopup> m_widgetAddPopup;
InputDispatcher m_inputDispatcher;
AnimationManager m_animations;
bool m_pointerInside = false;
+236
View File
@@ -0,0 +1,236 @@
#include "shell/settings/widget_add_popup.h"
#include "config/config_service.h"
#include "i18n/i18n.h"
#include "render/scene/node.h"
#include "shell/settings/widget_settings_registry.h"
#include "ui/controls/button.h"
#include "ui/controls/flex.h"
#include "ui/controls/label.h"
#include "ui/palette.h"
#include "ui/style.h"
#include "wayland/wayland_connection.h"
#include "wayland/wayland_seat.h"
#include <algorithm>
#include <string>
#include <string_view>
#include <utility>
namespace settings {
namespace {
constexpr std::string_view kCreateInstancePrefix = "create-instance:";
std::string laneLabel(std::string_view lane) {
if (lane == "start") {
return i18n::tr("settings.entities.widget.lanes.start");
}
if (lane == "center") {
return i18n::tr("settings.entities.widget.lanes.center");
}
if (lane == "end") {
return i18n::tr("settings.entities.widget.lanes.end");
}
return std::string(lane);
}
std::unique_ptr<Label> makeLabel(std::string_view text, float fontSize, const ColorSpec& color, bool bold = false) {
auto label = std::make_unique<Label>();
label->setText(text);
label->setFontSize(fontSize);
label->setColor(color);
label->setBold(bold);
return label;
}
} // namespace
WidgetAddPopup::WidgetAddPopup(WaylandConnection& wayland, RenderContext& renderContext)
: m_wayland(wayland), m_renderContext(renderContext),
m_popup(std::make_unique<PopupWindow>(wayland, renderContext)) {
m_popup->setOnDismissed([this]() {
m_lanePath.clear();
m_searchPicker = nullptr;
m_focusSearchOnOpen = false;
if (m_onDismissed) {
m_onDismissed();
}
});
}
WidgetAddPopup::~WidgetAddPopup() = default;
void WidgetAddPopup::setOnSelect(SelectCallback callback) { m_onSelect = std::move(callback); }
void WidgetAddPopup::setOnDismissed(std::function<void()> callback) { m_onDismissed = std::move(callback); }
void WidgetAddPopup::open(xdg_surface* parentXdgSurface, wl_output* output, std::uint32_t serial,
Button* anchorButton, const std::vector<std::string>& lanePath, const Config& config,
float scale, PopupWindow::AnchorMode anchorMode) {
if (m_popup == nullptr || parentXdgSurface == nullptr || anchorButton == nullptr) {
return;
}
const auto pickerEntries = widgetPickerEntries(config);
std::vector<SearchPickerOption> options;
options.reserve(pickerEntries.size() * 2);
for (const auto& entry : pickerEntries) {
options.push_back(SearchPickerOption{.value = entry.value,
.label = entry.label,
.description = entry.description,
.category = entry.category,
.enabled = true});
if (entry.kind != WidgetReferenceKind::BuiltIn) {
continue;
}
for (const auto& spec : widgetTypeSpecs()) {
if (spec.type != entry.value || !spec.supportsMultipleInstances) {
continue;
}
options.push_back(SearchPickerOption{
.value = std::string(kCreateInstancePrefix) + entry.value,
.label = i18n::tr("settings.entities.widget.picker.create-label", "label", entry.label),
.description = i18n::tr("settings.entities.widget.picker.create-description", "type", entry.value),
.category = i18n::tr("settings.entities.widget.kinds.new-instance"),
.enabled = true,
});
break;
}
}
if (options.empty()) {
return;
}
m_lanePath = lanePath;
m_searchPicker = nullptr;
const float panelPadding = Style::spaceSm * scale;
const float panelGap = Style::spaceSm * scale;
const float panelWidth = 520.0f * scale;
const float panelHeight = 420.0f * scale;
m_popup->setContentBuilder([this, options, panelPadding, panelGap, scale](float width, float height) {
auto root = std::make_unique<Flex>();
root->setDirection(FlexDirection::Vertical);
root->setAlign(FlexAlign::Stretch);
root->setGap(panelGap);
root->setPadding(panelPadding);
root->setFill(colorSpecFromRole(ColorRole::Surface));
root->setBorder(colorSpecFromRole(ColorRole::Outline, 0.35f), Style::borderWidth);
root->setRadius(Style::radiusLg);
root->setSize(width, height);
auto header = std::make_unique<Flex>();
header->setDirection(FlexDirection::Horizontal);
header->setAlign(FlexAlign::Center);
header->setGap(Style::spaceSm * scale);
header->addChild(makeLabel(i18n::tr("settings.entities.widget.inspector.add-title", "lane",
laneLabel(m_lanePath.empty() ? "" : m_lanePath.back())),
Style::fontSizeBody * scale, colorSpecFromRole(ColorRole::OnSurface), true));
auto spacer = std::make_unique<Flex>();
spacer->setFlexGrow(1.0f);
header->addChild(std::move(spacer));
auto closeBtn = std::make_unique<Button>();
closeBtn->setGlyph("close");
closeBtn->setVariant(ButtonVariant::Ghost);
closeBtn->setGlyphSize(Style::fontSizeBody * scale);
closeBtn->setMinWidth(Style::controlHeightSm * scale);
closeBtn->setMinHeight(Style::controlHeightSm * scale);
closeBtn->setPadding(Style::spaceXs * scale);
closeBtn->setRadius(Style::radiusSm * scale);
closeBtn->setOnClick([this]() {
if (m_popup != nullptr) {
m_popup->close();
}
});
header->addChild(std::move(closeBtn));
root->addChild(std::move(header));
auto picker = std::make_unique<SearchPicker>();
picker->setPlaceholder(i18n::tr("settings.entities.widget.picker.placeholder"));
picker->setEmptyText(i18n::tr("settings.entities.widget.picker.empty"));
picker->setOptions(options);
picker->setSize(std::max(1.0f, width - panelPadding * 2.0f),
std::max(1.0f, height - panelPadding * 2.0f - Style::controlHeightSm * scale));
picker->setOnActivated([this](const SearchPickerOption& option) {
if (option.value.empty()) {
return;
}
if (m_onSelect) {
m_onSelect(m_lanePath, option.value);
}
if (m_popup != nullptr) {
m_popup->close();
}
});
picker->setOnCancel([this]() {
if (m_popup != nullptr) {
m_popup->close();
}
});
m_searchPicker = picker.get();
root->addChild(std::move(picker));
return root;
});
m_popup->setSceneReadyCallback([this](InputDispatcher& dispatcher) {
if (!m_focusSearchOnOpen || m_searchPicker == nullptr) {
return;
}
auto* area = m_searchPicker->filterInputArea();
if (area != nullptr) {
dispatcher.setFocus(area);
m_focusSearchOnOpen = false;
}
});
float anchorAbsX = 0.0f;
float anchorAbsY = 0.0f;
Node::absolutePosition(anchorButton, anchorAbsX, anchorAbsY);
auto cfg = PopupWindow::makeConfig(static_cast<std::int32_t>(anchorAbsX), static_cast<std::int32_t>(anchorAbsY),
std::max(1, static_cast<std::int32_t>(anchorButton->width())),
std::max(1, static_cast<std::int32_t>(anchorButton->height())),
static_cast<std::uint32_t>(std::max(1.0f, panelWidth)),
static_cast<std::uint32_t>(std::max(1.0f, panelHeight)), serial, anchorMode, 0,
static_cast<std::int32_t>(Style::spaceXs * scale), true);
m_focusSearchOnOpen = true;
m_popup->openAsChild(cfg, parentXdgSurface, output);
}
void WidgetAddPopup::close() {
if (m_popup != nullptr) {
m_popup->close();
}
}
bool WidgetAddPopup::isOpen() const noexcept { return m_popup != nullptr && m_popup->isOpen(); }
bool WidgetAddPopup::onPointerEvent(const PointerEvent& event) {
return m_popup != nullptr && m_popup->onPointerEvent(event);
}
void WidgetAddPopup::onKeyboardEvent(const KeyboardEvent& event) {
if (m_popup != nullptr) {
m_popup->onKeyboardEvent(event);
}
}
void WidgetAddPopup::requestLayout() {
if (m_popup != nullptr) {
m_popup->requestLayout();
}
}
void WidgetAddPopup::requestRedraw() {
if (m_popup != nullptr) {
m_popup->requestRedraw();
}
}
} // namespace settings
+55
View File
@@ -0,0 +1,55 @@
#pragma once
#include "ui/controls/popup_window.h"
#include "ui/controls/search_picker.h"
#include <functional>
#include <memory>
#include <string>
#include <vector>
class Button;
class InputDispatcher;
class RenderContext;
class WaylandConnection;
struct Config;
struct KeyboardEvent;
struct PointerEvent;
struct wl_output;
struct xdg_surface;
namespace settings {
class WidgetAddPopup {
public:
using SelectCallback = std::function<void(const std::vector<std::string>& lanePath, const std::string& value)>;
WidgetAddPopup(WaylandConnection& wayland, RenderContext& renderContext);
~WidgetAddPopup();
void setOnSelect(SelectCallback callback);
void setOnDismissed(std::function<void()> callback);
void open(xdg_surface* parentXdgSurface, wl_output* output, std::uint32_t serial, Button* anchorButton,
const std::vector<std::string>& lanePath, const Config& config, float scale,
PopupWindow::AnchorMode anchorMode = PopupWindow::AnchorMode::CenterOnAnchor);
void close();
[[nodiscard]] bool isOpen() const noexcept;
[[nodiscard]] bool onPointerEvent(const PointerEvent& event);
void onKeyboardEvent(const KeyboardEvent& event);
void requestLayout();
void requestRedraw();
private:
WaylandConnection& m_wayland;
RenderContext& m_renderContext;
std::unique_ptr<PopupWindow> m_popup;
std::vector<std::string> m_lanePath;
SearchPicker* m_searchPicker = nullptr;
bool m_focusSearchOnOpen = false;
SelectCallback m_onSelect;
std::function<void()> m_onDismissed;
};
} // namespace settings
+244
View File
@@ -0,0 +1,244 @@
#include "ui/controls/popup_window.h"
#include "core/deferred_call.h"
#include "core/log.h"
#include "core/ui_phase.h"
#include "render/render_context.h"
#include "render/scene/node.h"
#include "wayland/wayland_connection.h"
#include "wayland/wayland_seat.h"
#include "xdg-shell-client-protocol.h"
#include <algorithm>
#include <cmath>
namespace {
constexpr Logger kLog("popup-window");
} // namespace
PopupWindow::PopupWindow(WaylandConnection& wayland, RenderContext& renderContext)
: m_wayland(wayland), m_renderContext(renderContext) {}
PopupWindow::~PopupWindow() { close(); }
void PopupWindow::setContentBuilder(ContentBuilder builder) { m_contentBuilder = std::move(builder); }
void PopupWindow::setSceneReadyCallback(SceneReadyCallback callback) { m_sceneReadyCallback = std::move(callback); }
void PopupWindow::setOnDismissed(std::function<void()> callback) { m_onDismissed = std::move(callback); }
void PopupWindow::open(PopupSurfaceConfig config, zwlr_layer_surface_v1* parentLayerSurface, wl_output* output) {
openCommon(config, parentLayerSurface, nullptr, output);
}
void PopupWindow::openAsChild(PopupSurfaceConfig config, xdg_surface* parentXdgSurface, wl_output* output) {
openCommon(config, nullptr, parentXdgSurface, output);
}
void PopupWindow::openCommon(PopupSurfaceConfig config, zwlr_layer_surface_v1* parentLayerSurface,
xdg_surface* parentXdgSurface, wl_output* output) {
close();
m_surface = std::make_unique<PopupSurface>(m_wayland);
m_surface->setRenderContext(&m_renderContext);
auto* self = this;
m_surface->setConfigureCallback(
[self](std::uint32_t /*w*/, std::uint32_t /*h*/) { self->m_surface->requestLayout(); });
m_surface->setPrepareFrameCallback([self](bool /*needsUpdate*/, bool needsLayout) {
if (self->m_surface == nullptr) {
return;
}
const auto width = self->m_surface->width();
const auto height = self->m_surface->height();
if (width == 0 || height == 0) {
return;
}
self->m_renderContext.makeCurrent(self->m_surface->renderTarget());
const bool needsSceneBuild = self->m_sceneRoot == nullptr ||
static_cast<std::uint32_t>(std::round(self->m_sceneRoot->width())) != width ||
static_cast<std::uint32_t>(std::round(self->m_sceneRoot->height())) != height;
if (needsSceneBuild || needsLayout) {
UiPhaseScope layoutPhase(UiPhase::Layout);
self->buildScene(width, height);
}
});
m_surface->setDismissedCallback([self]() { DeferredCall::callLater([self]() { self->close(); }); });
const bool initialized = parentXdgSurface != nullptr ? m_surface->initializeAsChild(parentXdgSurface, output, config)
: m_surface->initialize(parentLayerSurface, output, config);
if (!initialized) {
kLog.warn("failed to create popup window");
m_surface.reset();
return;
}
m_wlSurface = m_surface->wlSurface();
}
void PopupWindow::buildScene(std::uint32_t width, std::uint32_t height) {
const float fw = static_cast<float>(width);
const float fh = static_cast<float>(height);
if (m_contentBuilder) {
m_sceneRoot = m_contentBuilder(fw, fh);
}
if (m_sceneRoot == nullptr) {
m_sceneRoot = std::make_unique<Node>();
}
m_sceneRoot->setSize(fw, fh);
m_sceneRoot->layout(m_renderContext);
m_inputDispatcher.setSceneRoot(m_sceneRoot.get());
m_inputDispatcher.setCursorShapeCallback(
[this](std::uint32_t serial, std::uint32_t shape) { m_wayland.setCursorShape(serial, shape); });
m_surface->setSceneRoot(m_sceneRoot.get());
if (m_sceneReadyCallback) {
m_sceneReadyCallback(m_inputDispatcher);
}
}
void PopupWindow::close() {
const bool wasOpen = m_surface != nullptr;
m_sceneRoot.reset();
m_surface.reset();
m_inputDispatcher.setSceneRoot(nullptr);
m_wlSurface = nullptr;
m_pointerInside = false;
if (wasOpen && m_onDismissed) {
m_onDismissed();
}
}
bool PopupWindow::isOpen() const noexcept { return m_surface != nullptr; }
wl_surface* PopupWindow::wlSurface() const noexcept { return m_wlSurface; }
bool PopupWindow::onPointerEvent(const PointerEvent& event) {
if (!isOpen()) {
return false;
}
const bool onPopup = (event.surface != nullptr && event.surface == m_wlSurface);
switch (event.type) {
case PointerEvent::Type::Enter:
if (onPopup) {
m_pointerInside = true;
m_inputDispatcher.pointerEnter(static_cast<float>(event.sx), static_cast<float>(event.sy), event.serial);
}
break;
case PointerEvent::Type::Leave:
if (onPopup) {
m_pointerInside = false;
m_inputDispatcher.pointerLeave();
}
break;
case PointerEvent::Type::Motion:
if (onPopup || m_pointerInside) {
if (onPopup) {
m_pointerInside = true;
}
m_inputDispatcher.pointerMotion(static_cast<float>(event.sx), static_cast<float>(event.sy), 0);
return true;
}
break;
case PointerEvent::Type::Button:
if (onPopup || m_pointerInside) {
if (onPopup) {
m_pointerInside = true;
}
const bool pressed = (event.state == 1);
(void)m_inputDispatcher.pointerButton(static_cast<float>(event.sx), static_cast<float>(event.sy), event.button,
pressed);
return true;
}
break;
case PointerEvent::Type::Axis:
if (onPopup || m_pointerInside) {
return m_inputDispatcher.pointerAxis(static_cast<float>(event.sx), static_cast<float>(event.sy), event.axis,
event.axisSource, event.axisValue, event.axisDiscrete, event.axisValue120,
event.axisLines);
}
break;
}
if (m_surface != nullptr && m_sceneRoot != nullptr && m_surface->isRunning()) {
if (m_sceneRoot->layoutDirty()) {
m_surface->requestLayout();
} else if (m_sceneRoot->paintDirty()) {
m_surface->requestRedraw();
}
}
return onPopup;
}
void PopupWindow::onKeyboardEvent(const KeyboardEvent& event) {
if (!isOpen()) {
return;
}
m_inputDispatcher.keyEvent(event.sym, event.utf32, event.modifiers, event.pressed, event.preedit);
if (m_surface != nullptr && m_sceneRoot != nullptr && m_surface->isRunning()) {
if (m_sceneRoot->layoutDirty()) {
m_surface->requestLayout();
} else if (m_sceneRoot->paintDirty()) {
m_surface->requestRedraw();
}
}
}
void PopupWindow::requestLayout() {
if (m_surface != nullptr) {
m_surface->requestLayout();
}
}
void PopupWindow::requestRedraw() {
if (m_surface != nullptr) {
m_surface->requestRedraw();
}
}
PopupSurfaceConfig PopupWindow::makeConfig(std::int32_t anchorX, std::int32_t anchorY, std::int32_t anchorWidth,
std::int32_t anchorHeight, std::uint32_t width, std::uint32_t height,
std::uint32_t serial, AnchorMode mode, std::int32_t offsetX,
std::int32_t offsetY, bool grab) {
PopupSurfaceConfig cfg{
.anchorX = anchorX,
.anchorY = anchorY,
.anchorWidth = std::max(1, anchorWidth),
.anchorHeight = std::max(1, anchorHeight),
.width = std::max<std::uint32_t>(1, width),
.height = std::max<std::uint32_t>(1, height),
.anchor = XDG_POSITIONER_ANCHOR_BOTTOM,
.gravity = XDG_POSITIONER_GRAVITY_BOTTOM,
.constraintAdjustment = XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_X |
XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_Y |
XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_X | XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_Y,
.offsetX = offsetX,
.offsetY = offsetY,
.serial = serial,
.grab = grab,
};
if (mode == AnchorMode::CenterOnAnchor) {
cfg.anchor = XDG_POSITIONER_ANCHOR_BOTTOM;
cfg.gravity = XDG_POSITIONER_GRAVITY_BOTTOM;
cfg.anchorX += cfg.anchorWidth / 2;
cfg.anchorY += cfg.anchorHeight / 2;
cfg.anchorWidth = 1;
cfg.anchorHeight = 1;
}
return cfg;
}
+72
View File
@@ -0,0 +1,72 @@
#pragma once
#include "render/scene/input_dispatcher.h"
#include "wayland/popup_surface.h"
#include <functional>
#include <memory>
class Node;
class PopupSurface;
class RenderContext;
class WaylandConnection;
struct KeyboardEvent;
struct PointerEvent;
struct wl_output;
struct wl_surface;
struct xdg_surface;
struct zwlr_layer_surface_v1;
class PopupWindow {
public:
enum class AnchorMode : std::uint8_t {
BelowAnchor,
CenterOnAnchor,
};
using ContentBuilder = std::function<std::unique_ptr<Node>(float width, float height)>;
using SceneReadyCallback = std::function<void(InputDispatcher&)>;
PopupWindow(WaylandConnection& wayland, RenderContext& renderContext);
~PopupWindow();
void setContentBuilder(ContentBuilder builder);
void setSceneReadyCallback(SceneReadyCallback callback);
void setOnDismissed(std::function<void()> callback);
void open(PopupSurfaceConfig config, zwlr_layer_surface_v1* parentLayerSurface, wl_output* output);
void openAsChild(PopupSurfaceConfig config, xdg_surface* parentXdgSurface, wl_output* output);
void close();
[[nodiscard]] bool isOpen() const noexcept;
[[nodiscard]] wl_surface* wlSurface() const noexcept;
[[nodiscard]] bool onPointerEvent(const PointerEvent& event);
void onKeyboardEvent(const KeyboardEvent& event);
void requestLayout();
void requestRedraw();
[[nodiscard]] static PopupSurfaceConfig makeConfig(std::int32_t anchorX, std::int32_t anchorY,
std::int32_t anchorWidth, std::int32_t anchorHeight,
std::uint32_t width, std::uint32_t height, std::uint32_t serial,
AnchorMode mode, std::int32_t offsetX = 0,
std::int32_t offsetY = 0, bool grab = true);
private:
void openCommon(PopupSurfaceConfig config, zwlr_layer_surface_v1* parentLayerSurface, xdg_surface* parentXdgSurface,
wl_output* output);
void buildScene(std::uint32_t width, std::uint32_t height);
WaylandConnection& m_wayland;
RenderContext& m_renderContext;
std::unique_ptr<PopupSurface> m_surface;
std::unique_ptr<Node> m_sceneRoot;
InputDispatcher m_inputDispatcher;
wl_surface* m_wlSurface = nullptr;
bool m_pointerInside = false;
ContentBuilder m_contentBuilder;
SceneReadyCallback m_sceneReadyCallback;
std::function<void()> m_onDismissed;
};