From 44025b17eaed97be84671e33330bb6cb5e345734 Mon Sep 17 00:00:00 2001 From: Uwe Bruckhaus Date: Mon, 19 Oct 2026 20:31:52 +0200 Subject: [PATCH 1/2] Let bar widgets live in drawers of their own A drawer is a bar entry with "type": "drawer" and an id of its own. Its widgets are a section of their own, drawer:, kept under bar.layout.drawers. and drawn inside the drawer the way the tray section is drawn inside the tray's drawer. A click on its button opens and closes it, or "reveal": "hover" opens it while the pointer is over it. Every drawer opens while a widget is dragged along the bar, and a drop anywhere over a drawer, its button too, goes into it. A panel open from a widget in a drawer keeps the drawer open. A drawer opens away from the edge its section keeps to. Drawer sections are ordinary sections in BarModel's layoutWidgets, moveEntry and dropRoom, so widgets keep their instance moving in and out. The tray and drawers cannot go into a drawer. omarchy bar takes --section drawer:, and get finds widgets in drawers. Co-Authored-By: Claude Opus 5.5 --- bin/omarchy-bar | 12 +- default/i18n/en/shell.json | 4 + docs/omarchy-shell.md | 15 +- manual/05-the-top-bar.md | 3 + shell/Commons/BarLayout.js | 132 ++++++++ shell/Commons/Util.qml | 15 + shell/plugins/bar/Bar.qml | 314 ++++++++++++++---- shell/plugins/bar/BarModel.js | 100 +++++- shell/plugins/bar/widgets/TrayModel.js | 9 +- shell/services/PluginRegistry.qml | 76 +++-- shell/shell.qml | 4 +- test/shell.d/bar-drag-test.sh | 2 +- test/shell.d/bar-test.sh | 107 +++++- test/shell.d/fixtures/bar-drag/shell.qml | 57 +++- .../fixtures/plugin-registry/shell.qml | 21 ++ 15 files changed, 732 insertions(+), 139 deletions(-) create mode 100644 shell/Commons/BarLayout.js diff --git a/bin/omarchy-bar b/bin/omarchy-bar index d4152f8a4f0b..a329a6ed60ce 100755 --- a/bin/omarchy-bar +++ b/bin/omarchy-bar @@ -32,8 +32,9 @@ Usage: omarchy bar [args...] Placement: --section
Target section: left, centerBegin, - center, centerEnd, tray, or right; - tray is the tray's drawer + center, centerEnd, tray, right, or + drawer:; tray is the tray's + drawer, drawer: a drawer's --index Target index --before Insert before a widget --after Insert after a widget @@ -68,8 +69,8 @@ bar_option_exists() { } validate_section() { - [[ $1 =~ ^(left|centerBegin|center|centerEnd|tray|right)$ ]] || - fail "section must be left, centerBegin, center, centerEnd, tray, or right" + [[ $1 =~ ^(left|centerBegin|center|centerEnd|tray|right|drawer:[A-Za-z0-9_.-]{1,64})$ ]] || + fail "section must be left, centerBegin, center, centerEnd, tray, right, or drawer:" } validate_index() { @@ -446,7 +447,8 @@ cmd_get() { (( $# == 2 )) || fail "get takes a widget id and a setting key" jq -e -r --arg id "$id" --arg key "$key" ' - first(.bar.layout[]?[]? | select(.id == $id) | .[$key] | select(. != null)) + first(.bar.layout | (.left, .centerBegin, .center, .centerEnd, .right, .tray, (.drawers // {} | .[]?)) | arrays | .[] + | select(type == "object" and .id == $id) | .[$key] | select(. != null)) | if type == "string" then . else tojson end ' "$(source_file)" 2>/dev/null || exit 1 } diff --git a/default/i18n/en/shell.json b/default/i18n/en/shell.json index 7a5daf27432d..bdaba4c936bd 100644 --- a/default/i18n/en/shell.json +++ b/default/i18n/en/shell.json @@ -126,6 +126,10 @@ "bar.allowNotifications": "Allow Notifications", "bar.dayLight": "Day Light", "bar.disablePasswordlessSudo": "Disable Passwordless Sudo", + "bar.drawerWidgets": { + "one": "%1 widget", + "other": "%1 widgets" + }, "bar.microphoneInUse": "Microphone in use", "bar.microphoneLive": "Microphone live", "bar.microphoneMuted": "Microphone muted", diff --git a/docs/omarchy-shell.md b/docs/omarchy-shell.md index d08afa81f1f1..26c0ff4bfef4 100644 --- a/docs/omarchy-shell.md +++ b/docs/omarchy-shell.md @@ -172,7 +172,9 @@ Rules: The sections are `left`, `centerBegin`, `center`, `centerEnd`, `tray` and `right`. `tray` is drawn inside the tray's drawer, after the app icons, and shows only while - `omarchy.tray` is on the bar; it cannot hold the tray itself. + `omarchy.tray` is on the bar; it cannot hold the tray itself. Each drawer on + the bar adds a section `drawer:`, kept under `bar.layout.drawers.` + (see *Drawers* below). 3. Settings are inline on the entry. No `config:` sub-object, no merge layers. 4. Built-in bar widget ids are namespaced (`omarchy.clock`, `omarchy.audio`, …). @@ -425,6 +427,17 @@ If a full plugin is overkill, declare a one-off module inline in Output is plain text or Waybar-style JSON (`{ "text": ..., "tooltip": ..., "class": ... }`). +A drawer holds widgets of its own, folded away behind one button, the way the tray's drawer holds the tray section: + +```json +"right": [ { "id": "home", "type": "drawer", "name": "Home", "icon": "󰋜", "color": "#f38d70" } ], +"drawers": { "home": [ { "id": "omarchy.bluetooth" }, { "id": "omarchy.audio" } ] } +``` + +The id names the drawer and its section, `drawer:home`; it is letters, digits, `.`, `_` and `-`. A drawer goes in `left`, `centerBegin`, `center`, `centerEnd` or `right`, never in the tray section or another drawer, and neither does the tray. `name` is the tooltip, `icon` the button's glyph and `color` (`#rrggbb`) tints the button and the line under the open drawer. It starts closed; `"collapsed": false` starts it open, and `"reveal": "hover"` opens it while the pointer is over it instead of on a click. + +While a widget is dragged along the bar every drawer opens, and a drop anywhere over a drawer, its button included, goes into it; a drop on the neighbouring widget lands beside it. A panel open from a widget in a drawer keeps the drawer open. A drawer opens away from the edge its section keeps to, as the tray does: toward the middle of the bar on the right, outward beside the center box. Widgets under `drawers` whose drawer is no longer on the bar are not drawn and count as off the bar. + For a custom QML widget: ```json diff --git a/manual/05-the-top-bar.md b/manual/05-the-top-bar.md index 1eeda96e9814..4b7981a07a72 100644 --- a/manual/05-the-top-bar.md +++ b/manual/05-the-top-bar.md @@ -88,6 +88,8 @@ The middle of the bar is a box: whatever is in it stays centered as one group, a A widget you want around but not always in view can go in the tray's drawer: drag it onto the tray, and the drawer opens to take it. It sits there after the app icons, shows up whenever the drawer does, and keeps its drawer open while its panel is up. Drag it back out onto the bar to put it back. +For more than one such place, add drawers of your own: a button on the bar that folds a group of widgets away, like Home for the lights and the heating. Each drawer is one entry in `~/.config/omarchy/shell.json` with `"type": "drawer"`, a name and an icon; while you drag a widget along the bar every drawer opens, so you can drop it into one. A click on the drawer's button opens and closes it. + If you'd rather pick from a menu, **Appearance → Menu Bar** has the position, and **Bar Background** picks how the bar is drawn: **Opaque Bar**, **Glassy Bar** (see-through, with what's behind it blurred), or **Clear Bar**. The same things have commands, which is what you want for a [dotfiles](31-dotfiles.md) setup: @@ -97,6 +99,7 @@ omarchy bar position bottom omarchy bar background glassy # opaque, glassy, none, or cycle omarchy bar move omarchy.weather --section centerBegin --index 0 omarchy bar move omarchy.bluetooth --section tray # into the tray's drawer +omarchy bar move omarchy.bluetooth --section drawer:home # into the drawer "home" omarchy bar set omarchy.clock format "HH:mm" omarchy bar defaults # back to the shipped layout ``` diff --git a/shell/Commons/BarLayout.js b/shell/Commons/BarLayout.js new file mode 100644 index 000000000000..0af9673288e0 --- /dev/null +++ b/shell/Commons/BarLayout.js @@ -0,0 +1,132 @@ +// Sections of the bar layout. +// +// left, centerBegin, center, centerEnd and right are the bar itself, as +// Util.barSections runs them. tray is drawn inside the tray's drawer. A +// drawer is a bar entry with "type": "drawer" and an id of its own; its +// widgets are a section too, named drawer: and kept under +// layout.drawers[]. Drawers sit on the bar itself, never inside the tray +// or another drawer, so only the bar's own sections can name one. + +var BAR_SECTIONS = ["left", "centerBegin", "center", "centerEnd", "right"] +var BASE_SECTIONS = ["left", "centerBegin", "center", "centerEnd", "right", "tray"] +var DRAWER_PREFIX = "drawer:" + +function isPlainObject(value) { + return value !== null && typeof value === "object" && !Array.isArray(value) +} + +function entryId(entry) { + if (typeof entry === "string") return entry + if (isPlainObject(entry) && entry.id !== undefined && entry.id !== null) return String(entry.id) + return "" +} + +function validDrawerId(id) { + return /^[A-Za-z0-9_.-]{1,64}$/.test(String(id || "")) +} + +function isDrawerEntry(entry) { + return isPlainObject(entry) && entry.type === "drawer" && validDrawerId(entryId(entry)) +} + +function drawerSection(id) { + return validDrawerId(id) ? DRAWER_PREFIX + String(id) : "" +} + +function drawerIdOf(section) { + var name = String(section || "") + if (name.indexOf(DRAWER_PREFIX) !== 0) return "" + var id = name.slice(DRAWER_PREFIX.length) + return validDrawerId(id) ? id : "" +} + +// The sections drawn inside another widget: the tray's and every drawer's. +function isHostedSection(section) { + return section === "tray" || drawerIdOf(section) !== "" +} + +function isSectionName(section) { + return BASE_SECTIONS.indexOf(String(section || "")) !== -1 || drawerIdOf(section) !== "" +} + +// The ids of the drawers placed on the bar, in bar order, each once. +function drawerIds(layout) { + var ids = [] + if (!isPlainObject(layout)) return ids + for (var s = 0; s < BAR_SECTIONS.length; s++) { + var entries = layout[BAR_SECTIONS[s]] + if (!Array.isArray(entries)) continue + for (var i = 0; i < entries.length; i++) { + if (!isDrawerEntry(entries[i])) continue + var id = entryId(entries[i]) + if (ids.indexOf(id) === -1) ids.push(id) + } + } + return ids +} + +// Every section a widget can be in: the fixed ones, then one per drawer +// on the bar. A drawers[] list whose drawer is gone is not a section: its +// widgets are not drawn and not counted as on the bar. +function sectionNames(layout) { + var names = BASE_SECTIONS.slice() + var ids = drawerIds(layout) + for (var i = 0; i < ids.length; i++) names.push(DRAWER_PREFIX + ids[i]) + return names +} + +// The entries of one section, or null when the layout has none. +function sectionEntries(layout, section) { + if (!isPlainObject(layout)) return null + var id = drawerIdOf(section) + if (id) { + var drawers = layout.drawers + return isPlainObject(drawers) && Array.isArray(drawers[id]) ? drawers[id] : null + } + if (BASE_SECTIONS.indexOf(String(section || "")) === -1) return null + return Array.isArray(layout[section]) ? layout[section] : null +} + +// The entries of one section in a layout being edited, created when missing. +// Returns null for a name that is no section. +function ensureSection(layout, section) { + if (!isPlainObject(layout)) return null + var id = drawerIdOf(section) + if (id) { + if (!isPlainObject(layout.drawers)) layout.drawers = {} + if (!Array.isArray(layout.drawers[id])) layout.drawers[id] = [] + return layout.drawers[id] + } + if (BASE_SECTIONS.indexOf(String(section || "")) === -1) return null + if (!Array.isArray(layout[section])) layout[section] = [] + return layout[section] +} + +// Every section's entries as one plain object keyed by section name. +function sections(layout) { + var out = {} + var names = sectionNames(layout) + for (var i = 0; i < names.length; i++) { + var entries = sectionEntries(layout, names[i]) + out[names[i]] = Array.isArray(entries) ? entries : [] + } + return out +} + +if (typeof module !== "undefined") { + module.exports = { + BAR_SECTIONS: BAR_SECTIONS, + BASE_SECTIONS: BASE_SECTIONS, + validDrawerId: validDrawerId, + isDrawerEntry: isDrawerEntry, + drawerSection: drawerSection, + drawerIdOf: drawerIdOf, + isHostedSection: isHostedSection, + isSectionName: isSectionName, + drawerIds: drawerIds, + sectionNames: sectionNames, + sectionEntries: sectionEntries, + ensureSection: ensureSection, + sections: sections + } +} diff --git a/shell/Commons/Util.qml b/shell/Commons/Util.qml index 688714e02d28..65e2f536d62f 100644 --- a/shell/Commons/Util.qml +++ b/shell/Commons/Util.qml @@ -1,6 +1,7 @@ pragma Singleton import Quickshell import QtQuick +import "BarLayout.js" as BarLayout // Shared utility helpers used across plugins. Pure functions only — no // state. Anything stateful belongs on Color, Style, or a service. @@ -183,6 +184,20 @@ QtObject { var out = {} for (var i = 0; i < barSections.length; i++) out[barSections[i]] = normalizeLayoutSection(src[barSections[i]]) + // Only drawers on the bar keep their widgets; see BarLayout.js. + out.drawers = {} + var ids = BarLayout.drawerIds(out) + for (var d = 0; d < ids.length; d++) + out.drawers[ids[d]] = normalizeLayoutSection(BarLayout.sectionEntries(src, BarLayout.drawerSection(ids[d]))) return out } + + // Bar layout sections, drawers included: see BarLayout.js. + function layoutSectionNames(layout) { return BarLayout.sectionNames(layout) } + function layoutSectionEntries(layout, section) { return BarLayout.sectionEntries(layout, section) } + function ensureLayoutSection(layout, section) { return BarLayout.ensureSection(layout, section) } + function layoutSections(layout) { return BarLayout.sections(layout) } + function isLayoutSection(section) { return BarLayout.isSectionName(section) } + function isHostedLayoutSection(section) { return BarLayout.isHostedSection(section) } + function drawerLayoutSection(id) { return BarLayout.drawerSection(id) } } diff --git a/shell/plugins/bar/Bar.qml b/shell/plugins/bar/Bar.qml index 2831ab535a63..ed55b343d8bf 100644 --- a/shell/plugins/bar/Bar.qml +++ b/shell/plugins/bar/Bar.qml @@ -9,6 +9,7 @@ import qs.Commons import qs.Commons as Commons import qs.Ui import "BarModel.js" as BarModel +import "widgets/TrayModel.js" as TrayModel Item { id: root @@ -98,24 +99,41 @@ Item { property bool tooltipShown: false property int tooltipRequest: 0 property var activePopout: null - // A widget is being dragged that could go into the tray section, so the - // tray's drawer opens to show where. - readonly property bool traySectionDragActive: !!barDragSource && !barDragSource.hostsTraySection - // An open panel from a widget in the tray section keeps the tray's drawer - // open, so the panel's widget stays on screen under it. - readonly property bool traySectionPopoutOpen: { + // A widget is being dragged that could go into the tray section or a + // drawer, so the tray's drawer and every drawer open to show where. + readonly property bool traySectionDragActive: !!barDragSource && !barDragSource.hostsSection + // The section of the widget whose panel is open. An open panel from a + // widget in the tray section or a drawer keeps that drawer open, so the + // panel's widget stays on screen under it. + readonly property string popoutSection: { var owner = activePopout - if (!owner) return false + if (!owner) return "" for (var i = 0; i < moduleSlots.length; i++) { var slot = moduleSlots[i] - if (slot && slot.activeItem === owner) return slot.region === "tray" + if (slot && slot.activeItem === owner) return slot.region } - return false + return "" + } + readonly property bool traySectionPopoutOpen: popoutSection === "tray" + // Which drawers are open, by drawer id (DrawerModule). Shared by every + // monitor's bar. + property var drawerOpenState: ({}) + + function drawerIsOpen(id, entry) { + if (id in drawerOpenState) return drawerOpenState[id] === true + return Util.isPlainObject(entry) && entry.collapsed === false + } + + function setDrawerOpen(id, open) { + var next = ({}) + for (var key in drawerOpenState) next[key] = drawerOpenState[key] + next[id] = !!open + drawerOpenState = next } property var barDragSource: null // Where the dragged widget would land: { slot, after } beside a widget, or // { region, after } at an end of a section (see BarModel.centerDropTarget - // and BarModel.traySectionDropTarget). + // and BarModel.sectionDropTarget). property var barDragTarget: null readonly property string barDragRegion: barDragTarget ? (barDragTarget.slot ? barDragTarget.slot.region : barDragTarget.region) : "" // Where room is open for the dragged widget (BarModel.dropRoom), and that @@ -768,7 +786,7 @@ Item { function layoutEntries(region) { var serial = barConfigSerial - var entries = layoutConfig ? layoutConfig[region] : null + var entries = layoutConfig ? BarModel.sectionEntries(layoutConfig, region) : null return Array.isArray(entries) ? entries : [] } @@ -996,9 +1014,9 @@ Item { return null } - // The tray cannot go into its own drawer. - var intoTraySection = !sourceSlot || !sourceSlot.hostsTraySection - var traySlot = null + // The tray and drawers cannot go into a drawer: the tray's or a drawer's. + var intoSections = !sourceSlot || !sourceSlot.hostsSection + var hostSlots = [] // Each widget's own room; spacers are not widgets. The dragged widget // counts too: over its own room it would land back there, and folded // away, it still answers at the point it left, which the section's layout @@ -1008,8 +1026,8 @@ Item { var slot = moduleSlots[i] if (!slot || !slot.visible || slot.widgetWidth <= 0 || slot.widgetHeight <= 0) continue if (sourceWindow && !root.sameWindow(root.slotWindow(slot), sourceWindow)) continue - if (slot.region === "tray" && !intoTraySection) continue - if (intoTraySection && slot.hostsTraySection) traySlot = slot + if (BarModel.isHostedSection(slot.region) && !intoSections) continue + if (intoSections && slot.hostsSection) hostSlots.push(slot) var slotPoint = { x: slot.x, y: slot.y } try { @@ -1027,29 +1045,35 @@ Item { }) } - // The tray's drawer section works as the center box does: over it, a - // drop lands among the widgets in it, or in it, and off it they are out - // of reach. - var drawer = BarModel.traySectionDropTarget(candidates, root.traySectionDropRect(traySlot, sourceWindow), scenePoint, root.vertical, sourceSlot) - if (drawer) return drawer - var onBar = candidates.filter(function(row) { return row.region !== "tray" }) + // The tray's drawer section and every drawer's work as the center box + // does: over one, a drop lands among the widgets in it, or in it, and off + // it they are out of reach. + for (var h = 0; h < hostSlots.length; h++) { + var drawer = BarModel.sectionDropTarget(candidates, root.hostedSectionDropRect(hostSlots[h], sourceWindow), scenePoint, root.vertical, sourceSlot, hostSlots[h].hostedSection) + if (drawer) return drawer + } + var onBar = candidates.filter(function(row) { return !BarModel.isHostedSection(row.region) }) var box = sourceWindow ? root.centerBoxFor(sourceWindow) : null return BarModel.centerDropTarget(onBar, root.centerBoxDropRect(box), scenePoint, root.vertical, sourceSlot) } - // Where a drop lands in the tray's drawer section on a bar surface, in - // scene coordinates: the section, any room a drag has opened there + // Where a drop lands in the section a slot hosts on a bar surface, in scene + // coordinates. The tray's: the section, any room a drag has opened there // included, reaching past it by half the bar's thickness either way, short - // of the tray's own ends (BarModel.sectionDropZone). - function traySectionDropRect(slot, window) { - var shown = slot ? slot.traySectionNow() : null + // of the tray's own ends (BarModel.sectionDropZone), so a drop on the + // tray's chevron lands beside the tray. A drawer's: the whole drawer, its + // button too, since the button is the drawer; beside it is the + // neighbouring widgets' place. + function hostedSectionDropRect(slot, window) { + var shown = slot ? slot.sectionNow() : null if (!shown || !window || !window.contentItem) return null try { var area = slot.widgetRoom - var origin = area.mapToItem(null, shown.x, shown.y) - var section = { x: origin.x, y: origin.y, width: shown.width, height: shown.height } var corner = area.mapToItem(null, 0, 0) var tray = { x: corner.x, y: corner.y, width: area.width, height: area.height } + if (BarModel.drawerIdOf(slot.hostedSection)) return tray + var origin = area.mapToItem(null, shown.x, shown.y) + var section = { x: origin.x, y: origin.y, width: shown.width, height: shown.height } var reach = vertical ? window.contentItem.width : window.contentItem.height return BarModel.sectionDropZone(section, reach, vertical, tray) } catch (e) { @@ -2204,15 +2228,13 @@ Item { } } - // The tray section's list, which the tray's slot draws inside the tray's - // drawer and hands its bar surface's pool and window. + // The list of a section drawn inside another widget: the tray section in + // the tray's drawer, a drawer's section in the drawer. The slot hosting it + // names the section and hands it its bar surface's pool and window. Component { - id: traySectionComponent + id: hostedSectionComponent - ModuleList { - keys: root.layoutWidgets.sections.tray - region: "tray" - } + ModuleList {} } // A bar surface's widgets: one for each entry in the layout, under the key @@ -2311,6 +2333,7 @@ Item { } readonly property bool qmlCustom: customType === "qml" readonly property bool commandCustom: customType === "command" + readonly property bool drawerCustom: customType === "drawer" readonly property bool registered: registryComponent !== null readonly property var activeItem: { if (registered) return registryLoader.item @@ -2360,7 +2383,8 @@ Item { Loader { id: componentLoader active: host.placed && !host.qmlCustom && !host.registered - sourceComponent: host.commandCustom ? customCommandModuleComponent : emptyModuleComponent + sourceComponent: host.commandCustom ? customCommandModuleComponent + : host.drawerCustom ? drawerModuleComponent : emptyModuleComponent width: host.widgetWidth height: host.widgetHeight opacity: host.dragSource ? 0.22 : 1.0 @@ -2416,6 +2440,11 @@ Item { id: customCommandModuleComponent CustomCommandModule { entry: host.entry } } + + Component { + id: drawerModuleComponent + DrawerModule { entry: host.entry } + } } // A widget's place in its section. It shows the widget its bar surface @@ -2463,32 +2492,38 @@ Item { var point = mark.mapToItem(widgetArea, mark.width / 2, mark.height / 2) return root.vertical ? point.y : point.x } - // The tray, or a clone of it, hosts the tray section in its drawer; one - // inside the drawer does not, so the section never holds itself. - readonly property bool hostsTraySection: region !== "tray" && !!activeItem && "sectionHost" in activeItem - // Its drawer section is outlined while a widget that could go in it is - // dragged on this bar, as the center box is. - readonly property bool traySectionShown: hostsTraySection && root.traySectionDragActive && root.sameWindow(root.barDragWindow, window) - // The part of the section the drawer shows, in the slot's widget area, + // The tray, or a clone of it, hosts the tray section in its drawer, and a + // drawer hosts its own section, drawer: (DrawerModule). One inside + // the tray's drawer or a drawer does not, so no section ever holds itself + // or another drawer. + readonly property string hostedSection: { + if (!activeItem || !("sectionHost" in activeItem) || BarModel.isHostedSection(region)) return "" + return "hostedSection" in activeItem ? String(activeItem.hostedSection || "") : "tray" + } + readonly property bool hostsSection: hostedSection !== "" + // Its section is outlined while a widget that could go in it is dragged + // on this bar, as the center box is. + readonly property bool sectionOutlined: hostsSection && root.traySectionDragActive && root.sameWindow(root.barDragWindow, window) + // The part of the section its drawer shows, in the slot's widget area, // or null while the drawer hides all of it (the tray's sectionShown). - readonly property var traySectionGeometry: { - var shown = hostsTraySection ? activeItem.sectionShown : null + readonly property var sectionGeometry: { + var shown = hostsSection ? activeItem.sectionShown : null if (!shown) return null return { x: holder.x + shown.x, y: holder.y + shown.y, width: shown.width, height: shown.height } } - // Where the section is now, for a drop: as traySectionGeometry, or for a + // Where the section is now, for a drop: as sectionGeometry, or for a // tray cloned before trays said how much of it shows, the whole section. - function traySectionNow() { - if (!hostsTraySection) return null - if ("sectionShown" in activeItem) return traySectionGeometry + function sectionNow() { + if (!hostsSection) return null + if ("sectionShown" in activeItem) return sectionGeometry var area = activeItem.sectionHost if (!area) return null var point = area.mapToItem(widgetArea, 0, 0) return { x: point.x, y: point.y, width: area.width, height: area.height } } - function traySectionContains(x, y) { - var area = hostsTraySection ? activeItem.sectionHost : null + function sectionContains(x, y) { + var area = hostsSection ? activeItem.sectionHost : null if (!area || !area.visible || area.width <= 0 || area.height <= 0) return false var point = slot.mapToItem(area, x, y) return point.x >= 0 && point.y >= 0 && point.x <= area.width && point.y <= area.height @@ -2551,16 +2586,16 @@ Item { height: slot.widgetHeight } - // The tray section's outline, as the center box's: around what it + // The hosted section's outline, as the center box's: around what it // holds, the room for the dragged widget included, or a line where it - // holds nothing. Drawn over the tray rather than in its drawer, whose - // clip would hide an empty section's line. + // holds nothing. Drawn over the tray or drawer rather than in its + // drawer, whose clip would hide an empty section's line. DropOutline { - readonly property var section: slot.traySectionGeometry || { x: 0, y: 0, width: 0, height: 0 } + readonly property var section: slot.sectionGeometry || { x: 0, y: 0, width: 0, height: 0 } - visible: slot.traySectionShown && !!slot.traySectionGeometry + visible: slot.sectionOutlined && !!slot.sectionGeometry length: root.vertical ? section.height : section.width - dropInto: root.barDragRegion === "tray" + dropInto: root.barDragRegion === slot.hostedSection x: root.vertical ? Math.round((parent.width - across) / 2) : section.x y: root.vertical ? section.y : Math.round((parent.height - across) / 2) z: 40 @@ -2596,22 +2631,24 @@ Item { } } - // The tray section's widgets, drawn inside the tray's drawer. They are - // ordinary module slots of their own region, so they open panels, drag - // and keep settings the way widgets on the bar do. + // The hosted section's widgets, drawn inside the tray's drawer or a + // drawer. They are ordinary module slots of their own region, so they + // open panels, drag and keep settings the way widgets on the bar do. // Built through a Component outside ModuleSlot: a module list holds // module slots, so one declared in here would make the types a cycle. Loader { - id: traySectionLoader - readonly property Item host: slot.hostsTraySection ? slot.activeItem.sectionHost : null + id: hostedSectionLoader + readonly property Item host: slot.hostsSection ? slot.activeItem.sectionHost : null parent: host || slot active: !!host - sourceComponent: traySectionComponent + sourceComponent: hostedSectionComponent } - Binding { target: traySectionLoader.item; property: "window"; value: slot.window } - Binding { target: traySectionLoader.item; property: "pool"; value: slot.pool } - // Its widgets grow from the edge the tray anchors the section by. - Binding { target: traySectionLoader.item; property: "fixedEdge"; value: slot.hostsTraySection && slot.activeItem.sectionEdge || "center" } + Binding { target: hostedSectionLoader.item; property: "region"; value: slot.hostedSection } + Binding { target: hostedSectionLoader.item; property: "keys"; value: root.layoutWidgets.sections[slot.hostedSection] || [] } + Binding { target: hostedSectionLoader.item; property: "window"; value: slot.window } + Binding { target: hostedSectionLoader.item; property: "pool"; value: slot.pool } + // Its widgets grow from the edge the tray or drawer anchors the section by. + Binding { target: hostedSectionLoader.item; property: "fixedEdge"; value: slot.hostsSection && slot.activeItem.sectionEdge || "center" } MouseArea { id: modulePointer @@ -2637,9 +2674,10 @@ Item { // follows the pointer instead. onPressed: function(mouse) { - // A press on a widget in the tray's drawer belongs to that widget's - // own slot, underneath this one, so it drags that widget, not the tray. - if (slot.traySectionContains(mouse.x, mouse.y)) { + // A press on a widget in the tray's drawer or a drawer belongs to that + // widget's own slot, underneath this one, so it drags that widget, not + // the tray or the drawer. + if (slot.sectionContains(mouse.x, mouse.y)) { mouse.accepted = false return } @@ -2773,4 +2811,138 @@ Item { onTriggered: root.runProcess(customProc) } } + + // A drawer: a bar entry with "type": "drawer" and an id of its own, holding + // widgets the way the tray's drawer holds the tray section. Its widgets are + // the section drawer: (BarModel.layoutSections), which the slot holding + // it draws into sectionHost, as it does the tray's. The button opens and + // closes it; with "reveal": "hover" it opens while the pointer is over it. + // A drawer starts closed unless its entry says "collapsed": false, and it + // stays open while a widget is dragged along the bar or a panel of one of + // its widgets is up. It opens away from the edge it keeps to, so on the + // right of the bar toward the middle, like the tray. It declares no bar, + // moduleName or settings: its host would hand a plugin facade to a bar + // property, and the drawer needs the bar itself. + component DrawerModule: Item { + id: drawer + + required property var entry + // Set by its host: the edge it keeps to as it grows. + property string fixedEdge: "center" + readonly property string drawerId: root.entryId(entry) + readonly property string hostedSection: BarModel.drawerIdOf("drawer:" + drawerId) ? "drawer:" + drawerId : "" + readonly property var drawerSettings: root.entrySettings(entry) + property Item sectionHost: null + readonly property int sectionCount: (root.layoutWidgets.sections[hostedSection] || []).length + readonly property bool hoverReveal: drawerSettings.reveal === "hover" + readonly property bool opensBackward: fixedEdge === "end" + // The section's widgets grow from the button's side. + readonly property string sectionEdge: opensBackward ? "end" : "start" + readonly property color tint: /^#([0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/.test(String(drawerSettings.color || "")) + ? String(drawerSettings.color) : root.barForeground + readonly property bool barDragging: root.traySectionDragActive + readonly property bool holdOpen: barDragging || (hostedSection !== "" && root.popoutSection === hostedSection) + readonly property bool open: root.drawerIsOpen(drawerId, entry) || holdOpen + readonly property real fullExtent: sectionHost ? (root.vertical ? sectionHost.implicitHeight : sectionHost.implicitWidth) : 0 + property real revealProgress: open ? 1 : 0 + readonly property real revealExtent: fullExtent * revealProgress + readonly property real buttonExtent: root.vertical ? toggle.implicitHeight : toggle.implicitWidth + // The part of the section the drawer shows, in the drawer, or null while + // it shows none of it, as the tray's sectionShown. + readonly property var sectionShown: { + var span = root.vertical + ? TrayModel.shownSpan(drawerClip.y, drawerClip.height, drawerClip.y + sectionArea.y, sectionArea.height) + : TrayModel.shownSpan(drawerClip.x, drawerClip.width, drawerClip.x + sectionArea.x, sectionArea.width) + if (!span) return null + return root.vertical + ? { x: 0, y: span.start, width: root.barSize, height: span.length } + : { x: span.start, y: 0, width: span.length, height: root.barSize } + } + + implicitWidth: root.vertical ? root.barSize : buttonExtent + revealExtent + implicitHeight: root.vertical ? buttonExtent + revealExtent : root.barSize + + Behavior on revealProgress { + NumberAnimation { duration: Style.duration(260); easing.type: Easing.OutCubic } + } + + function setOpen(value) { root.setDrawerOpen(drawerId, value) } + + HoverHandler { + id: drawerHover + enabled: drawer.hoverReveal + onHoveredChanged: { + if (hovered) { + collapseDelay.stop() + drawer.setOpen(true) + } else { + collapseDelay.restart() + } + } + } + + Timer { + id: collapseDelay + interval: 450 + onTriggered: if (drawer.hoverReveal && !drawerHover.hovered) drawer.setOpen(false) + } + + WidgetButton { + id: toggle + bar: root + x: root.vertical ? 0 : (drawer.opensBackward ? drawer.revealExtent : 0) + y: root.vertical ? (drawer.opensBackward ? drawer.revealExtent : 0) : 0 + text: { + var icon = String(drawer.drawerSettings.icon || "") || "\udb80\ude4b" + if (root.vertical) return icon + var pointsOut = drawer.open === drawer.opensBackward + var chevron = pointsOut ? "\uf054" : "\uf053" + return drawer.opensBackward ? chevron + " " + icon : icon + " " + chevron + } + foreground: drawer.tint + tooltipText: { + var name = String(drawer.drawerSettings.name || drawer.drawerId) + return name + " · " + Commons.I18n.tn("bar.drawerWidgets", drawer.sectionCount).arg(Commons.I18n.formatNumber(drawer.sectionCount)) + } + onPressed: function(button) { + if (button === Qt.LeftButton || button === Qt.MiddleButton) drawer.setOpen(!drawer.open) + } + } + + Item { + id: drawerClip + clip: true + x: root.vertical ? 0 : (drawer.opensBackward ? 0 : drawer.buttonExtent) + y: root.vertical ? (drawer.opensBackward ? 0 : drawer.buttonExtent) : 0 + width: root.vertical ? drawer.width : drawer.revealExtent + height: root.vertical ? drawer.revealExtent : drawer.height + + // The bar parents the section's list here. Its widgets stay loaded + // while the drawer is closed, so they keep their state; they only stop + // taking the pointer, since a closed drawer is narrower than they are. + // Empty, it takes no room. + Item { + id: sectionArea + enabled: drawer.revealProgress > 0 + x: root.vertical ? 0 : (drawer.opensBackward ? drawer.revealExtent - drawer.fullExtent : 0) + y: root.vertical ? (drawer.opensBackward ? drawer.revealExtent - drawer.fullExtent : 0) : 0 + implicitWidth: childrenRect.width + implicitHeight: childrenRect.height + + Component.onCompleted: drawer.sectionHost = sectionArea + Component.onDestruction: if (drawer.sectionHost === sectionArea) drawer.sectionHost = null + } + + // A thin line along the open drawer, in the drawer's colour. + Rectangle { + visible: !root.vertical && drawer.sectionCount > 0 + x: sectionArea.x + width: drawer.fullExtent + height: Style.space(1) + y: root.position === "top" ? drawerClip.height - height : 0 + color: drawer.tint + opacity: 0.35 * drawer.revealProgress + } + } + } } diff --git a/shell/plugins/bar/BarModel.js b/shell/plugins/bar/BarModel.js index d86e05d2e151..b9ed949ce133 100644 --- a/shell/plugins/bar/BarModel.js +++ b/shell/plugins/bar/BarModel.js @@ -5,6 +5,59 @@ var sections = ["left", "centerBegin", "center", "centerEnd", "right", "tray"] var centerSections = ["centerBegin", "center", "centerEnd"] +// A drawer is a bar entry with "type": "drawer" and an id of its own; its +// widgets are a section too, drawer:, kept under layout.drawers[], as +// Commons/BarLayout.js has them. Only a drawer on the bar itself, not in the +// tray's drawer or another drawer, makes one. +var drawerPrefix = "drawer:" + +function drawerIdOf(section) { + var name = String(section || "") + var id = name.indexOf(drawerPrefix) === 0 ? name.slice(drawerPrefix.length) : "" + return /^[A-Za-z0-9_.-]{1,64}$/.test(id) ? id : "" +} + +// The sections drawn inside another widget: the tray's and every drawer's. +function isHostedSection(section) { + return section === "tray" || drawerIdOf(section) !== "" +} + +function isSectionName(section) { + return sections.indexOf(String(section || "")) !== -1 || drawerIdOf(section) !== "" +} + +// Every section of a layout: the fixed ones, then one per drawer on the bar, +// in bar order, each once. +function layoutSections(layout) { + var names = sections.slice() + if (!isPlainObject(layout)) return names + for (var s = 0; s < sections.length; s++) { + var list = sections[s] === "tray" || !Array.isArray(layout[sections[s]]) ? [] : layout[sections[s]] + for (var i = 0; i < list.length; i++) { + var section = isPlainObject(list[i]) && list[i].type === "drawer" ? drawerPrefix + entryId(list[i]) : "" + if (drawerIdOf(section) && names.indexOf(section) === -1) names.push(section) + } + } + return names +} + +// One section's entries in a layout, or null when it has none. With +// `create`, a missing list is made, as a write needs. +function sectionEntries(layout, section, create) { + if (!isPlainObject(layout) || !isSectionName(section)) return null + var id = drawerIdOf(section) + var owner = layout + var key = String(section) + if (id) { + if (create && !isPlainObject(layout.drawers)) layout.drawers = {} + owner = isPlainObject(layout.drawers) ? layout.drawers : null + key = id + } + if (!owner) return null + if (create && !Array.isArray(owner[key])) owner[key] = [] + return Array.isArray(owner[key]) ? owner[key] : null +} + function isPlainObject(value) { return !!value && typeof value === "object" && !Array.isArray(value) } @@ -257,11 +310,13 @@ function sectionDropZone(section, reach, vertical, tray) { : { x: start, y: section.y, width: end - start, height: section.height } } -// A drop over the tray's drawer section, given as where a drop lands in it -// (sectionDropZone): among the widgets in the section, or in it when it holds -// none. Over the room the dragged widget (`source`) still holds, it goes -// back. Null off the section, where the widgets in it are out of reach. -function traySectionDropTarget(candidates, zone, point, vertical, source) { +// A drop over a section drawn inside another widget, the tray's drawer +// section or a drawer's (`region`, tray when not given), given as where a +// drop lands in it (sectionDropZone): among the widgets in the section, or in +// it when it holds none. Over the room the dragged widget (`source`) still +// holds, it goes back. Null off the section, where the widgets in it are out +// of reach. +function sectionDropTarget(candidates, zone, point, vertical, source, region) { if (!zone) return null var rows = Array.isArray(candidates) ? candidates : [] var axis = vertical ? Number(point && point.y) : Number(point && point.x) @@ -272,8 +327,9 @@ function traySectionDropTarget(candidates, zone, point, vertical, source) { var own = ownRoomTarget(rows, axis, vertical, source) if (own) return own - var inside = nearestEdge(rows.filter(function(row) { return row && row.region === "tray" }), axis, vertical) - return inside ? inside.target : { region: "tray", after: true } + var section = region || "tray" + var inside = nearestEdge(rows.filter(function(row) { return row && row.region === section }), axis, vertical) + return inside ? inside.target : { region: section, after: true } } // Where a drop opens room for the dragged widget (`source`, { key, region }): @@ -340,11 +396,12 @@ function layoutWidgets(layout, memo) { var source = isPlainObject(layout) ? layout : {} var state = memo || {} var previous = state.widgets || Object.create(null) + var names = layoutSections(source) var planned = [] - for (var s = 0; s < sections.length; s++) { - var list = Array.isArray(source[sections[s]]) ? source[sections[s]] : [] + for (var s = 0; s < names.length; s++) { + var list = sectionEntries(source, names[s]) || [] for (var i = 0; i < list.length; i++) - planned.push({ section: sections[s], index: i, id: entryId(list[i]), json: entryJson(list[i]), entry: list[i], custom: !!customModuleType(list[i]), key: "" }) + planned.push({ section: names[s], index: i, id: entryId(list[i]), json: entryJson(list[i]), entry: list[i], custom: !!customModuleType(list[i]), key: "" }) } var byId = Object.create(null) @@ -378,7 +435,7 @@ function layoutWidgets(layout, memo) { var next = Object.create(null) var bySection = {} var entries = Object.create(null) - for (var r = 0; r < sections.length; r++) bySection[sections[r]] = [] + for (var r = 0; r < names.length; r++) bySection[names[r]] = [] for (var gone in previous) entries[gone] = previous[gone].entry planned.forEach(function(plan) { if (!plan.key) plan.key = plan.id + "#" + (state.serial = (state.serial || 0) + 1) @@ -473,12 +530,10 @@ function roomLengths(extent, shares) { function moveEntry(config, move, normalizeEntry) { var normalize = typeof normalizeEntry === "function" ? normalizeEntry : function(entry) { return entryId(entry) ? entry : null } if (!isPlainObject(config) || !move || !move.from || !move.to) return false - if (sections.indexOf(move.from.region) === -1 || sections.indexOf(move.to.region) === -1) return false + if (!isSectionName(move.from.region) || !isSectionName(move.to.region)) return false if (!isPlainObject(config.bar)) config.bar = {} if (!isPlainObject(config.bar.layout)) config.bar.layout = {} var layout = config.bar.layout - if (!Array.isArray(layout[move.from.region])) layout[move.from.region] = [] - if (!Array.isArray(layout[move.to.region])) layout[move.to.region] = [] // Where the widget the bar draws at `index` sits among the raw entries, or // past them all for one past the last widget. @@ -492,8 +547,8 @@ function moveEntry(config, move, normalizeEntry) { return index === widgets ? entries.length : -1 } - var fromEntries = layout[move.from.region] - var toEntries = layout[move.to.region] + var fromEntries = sectionEntries(layout, move.from.region, true) + var toEntries = sectionEntries(layout, move.to.region, true) var fromIndex = rawIndex(fromEntries, Number(move.from.index)) if (fromIndex < 0 || fromIndex >= fromEntries.length || entryId(normalize(fromEntries[fromIndex])) !== String(move.from.id)) return false @@ -503,6 +558,12 @@ function moveEntry(config, move, normalizeEntry) { if (sameSection && fromIndex < toIndex) toIndex -= 1 if (sameSection && fromIndex === toIndex) return false + // The tray and drawers hold sections of their own, so neither goes into + // the tray's drawer or a drawer. + var moving = normalize(fromEntries[fromIndex]) + var hosts = entryId(moving) === "omarchy.tray" || (isPlainObject(moving) && moving.type === "drawer") + if (hosts && (move.to.region === "tray" || drawerIdOf(move.to.region))) return false + var entry = fromEntries.splice(fromIndex, 1)[0] toEntries.splice(toIndex, 0, entry) return true @@ -629,7 +690,7 @@ if (typeof module !== "undefined") { nearestDropTarget: nearestDropTarget, centerDropTarget: centerDropTarget, centerDropZone: centerDropZone, - traySectionDropTarget: traySectionDropTarget, + sectionDropTarget: sectionDropTarget, sectionDropZone: sectionDropZone, dropRoom: dropRoom, sectionRows: sectionRows, @@ -640,6 +701,11 @@ if (typeof module !== "undefined") { navigationRegions: navigationRegions, slotEdge: slotEdge, sections: sections, + drawerIdOf: drawerIdOf, + isHostedSection: isHostedSection, + isSectionName: isSectionName, + layoutSections: layoutSections, + sectionEntries: sectionEntries, normalizePosition: normalizePosition, entrySettings: entrySettings, entryId: entryId, diff --git a/shell/plugins/bar/widgets/TrayModel.js b/shell/plugins/bar/widgets/TrayModel.js index ea34a97c09ab..02e8891497d5 100644 --- a/shell/plugins/bar/widgets/TrayModel.js +++ b/shell/plugins/bar/widgets/TrayModel.js @@ -18,10 +18,15 @@ function entryId(entry) { return "" } +// Drawers count as on the bar too: their widgets are under layout.drawers +// (see Commons/BarLayout.js). function layoutHasWidget(layout, id) { - var sections = ["left", "centerBegin", "center", "centerEnd", "right", "tray"] + var sections = ["left", "centerBegin", "center", "centerEnd", "right", "tray"].map(function(name) { return layout && layout[name] }) + var drawers = layout && layout.drawers + if (drawers && typeof drawers === "object" && !Array.isArray(drawers)) + for (var key in drawers) sections.push(drawers[key]) for (var s = 0; s < sections.length; s++) { - var entries = layout && layout[sections[s]] + var entries = sections[s] if (!Array.isArray(entries)) continue for (var i = 0; i < entries.length; i++) { if (entryId(entries[i]) === id) return true diff --git a/shell/services/PluginRegistry.qml b/shell/services/PluginRegistry.qml index 19151f50e929..5316e1af1ce2 100644 --- a/shell/services/PluginRegistry.qml +++ b/shell/services/PluginRegistry.qml @@ -200,6 +200,13 @@ QtObject { return Util.barSections.indexOf(section) !== -1 && section !== "center" ? section : "centerEnd" } + // One section's entries in a shell config, drawers included; null when the + // config has none. + function layoutSection(config, section) { + if (!Util.isPlainObject(config) || !Util.isPlainObject(config.bar)) return null + return Util.layoutSectionEntries(config.bar.layout, section) + } + function barEntryId(entry) { return Util.canonicalWidgetId(String(Util.isPlainObject(entry) ? entry.id : entry || "")) } @@ -208,10 +215,10 @@ QtObject { if (!Util.isPlainObject(config) || !Util.isPlainObject(config.bar) || !Util.isPlainObject(config.bar.layout)) return { found: false } var key = Util.canonicalWidgetId(String(id)) - var sections = Util.barSections + var sections = Util.layoutSectionNames(config.bar.layout) for (var s = 0; s < sections.length; s++) { if (section && sections[s] !== section) continue - var entries = config.bar.layout[sections[s]] + var entries = layoutSection(config, sections[s]) if (!Array.isArray(entries)) continue for (var i = 0; i < entries.length; i++) { if (barEntryId(entries[i]) === key) @@ -257,39 +264,43 @@ QtObject { return id === "omarchy.tray" || (!!metadata && Util.canonicalWidgetId(String(metadata.clonedFrom || "")) === "omarchy.tray") } - // The tray section is drawn inside the tray, so the tray cannot go there. + function trayDrawerError(section) { + return { error: section === "tray" ? "the tray cannot go in its own drawer" : "the tray cannot go in a drawer" } + } + + // The tray section is drawn inside the tray and a drawer's section inside + // the drawer, so the tray cannot go into either. function barTarget(config, placement, fallbackSection, id) { var target = placement || {} - var section = Util.barSections.indexOf(String(target.section || "")) !== -1 + var section = Util.layoutSectionNames(config.bar.layout).indexOf(String(target.section || "")) !== -1 ? String(target.section) : fallbackSection var tray = isTray(id) - var trayError = { error: "the tray cannot go in its own drawer" } - if (tray && section === "tray") return trayError + if (tray && Util.isHostedLayoutSection(section)) return trayDrawerError(section) var relativeId = String(target.before || target.after || "") if (relativeId) { var relative = findRelativeBarLocation(config, relativeId, section && target.section ? section : "") if (!relative.found) return { error: "could not find target widget " + relativeId } - if (tray && relative.section === "tray") return trayError + if (tray && Util.isHostedLayoutSection(relative.section)) return trayDrawerError(relative.section) return { section: relative.section, index: relative.index + (target.after ? 1 : 0) } } - if (!Array.isArray(config.bar.layout[section])) config.bar.layout[section] = [] + if (!Util.ensureLayoutSection(config.bar.layout, section)) return { error: "unknown bar section " + section } if (target.index !== undefined && target.index !== null) { var requested = Math.max(0, Math.floor(Number(target.index))) - return { section: section, index: Math.min(requested, config.bar.layout[section].length) } + return { section: section, index: Math.min(requested, layoutSection(config, section).length) } } - // A section without an anchor (the tray's drawer, for one) takes a widget - // put there at its end. + // A section without an anchor (the tray's drawer or a drawer, for one) + // takes a widget put there at its end. var anchors = { left: "omarchy.workspaces", centerEnd: "omarchy.weather", right: "omarchy.tray" } - if (!anchors[section]) return { section: section, index: config.bar.layout[section].length } + if (!anchors[section]) return { section: section, index: layoutSection(config, section).length } var anchor = findRelativeBarLocation(config, anchors[section], section) return { section: section, - index: anchor.found ? anchor.index + 1 : config.bar.layout[section].length + index: anchor.found ? anchor.index + 1 : layoutSection(config, section).length } } @@ -299,7 +310,7 @@ QtObject { if (placement.fromIndex !== undefined && placement.fromIndex !== null) { var fromSection = String(placement.fromSection || "") if (!fromSection) return "from-index requires from-section" - var entries = config.bar.layout[fromSection] + var entries = layoutSection(config, fromSection) var fromIndex = Math.floor(Number(placement.fromIndex)) if (!Array.isArray(entries) || fromIndex < 0 || fromIndex >= entries.length) return "no widget at " + fromSection + "[" + fromIndex + "]" @@ -311,14 +322,14 @@ QtObject { if (!source.found) return "could not find widget " + key } - var entry = config.bar.layout[source.section][source.index] - config.bar.layout[source.section].splice(source.index, 1) + var entry = layoutSection(config, source.section)[source.index] + layoutSection(config, source.section).splice(source.index, 1) var target = barTarget(config, placement, source.section, key) if (target.error) { - config.bar.layout[source.section].splice(source.index, 0, entry) + layoutSection(config, source.section).splice(source.index, 0, entry) return target.error } - config.bar.layout[target.section].splice(target.index, 0, entry) + layoutSection(config, target.section).splice(target.index, 0, entry) return "" } @@ -371,7 +382,7 @@ QtObject { error = "index requires section" return } - var entries = config.bar.layout[section] + var entries = layoutSection(config, section) var numericIndex = Math.floor(Number(index)) if (!Array.isArray(entries) || numericIndex < 0 || numericIndex >= entries.length) { error = "no widget at " + section + "[" + numericIndex + "]" @@ -385,11 +396,11 @@ QtObject { error = "could not find widget " + id return } - if (barEntryId(config.bar.layout[location.section][location.index]) !== String(id)) { + if (barEntryId(layoutSection(config, location.section)[location.index]) !== String(id)) { error = "widget at " + location.section + "[" + location.index + "] is not " + id return } - var entry = config.bar.layout[location.section][location.index] + var entry = layoutSection(config, location.section)[location.index] if (!Util.isPlainObject(entry)) { error = "widget entry must be an object" return @@ -467,20 +478,21 @@ QtObject { } else { var cloneLocation = findEntryLocation(config, cloneId) if (cloneLocation.kind === "bar") { - var cloneEntry = config.bar.layout[cloneLocation.section][cloneLocation.index] - var sections = Util.barSections + var cloneEntry = layoutSection(config, cloneLocation.section)[cloneLocation.index] + var sections = Util.layoutSectionNames(config.bar.layout) for (var s = 0; s < sections.length; s++) { - if (!Array.isArray(config.bar.layout[sections[s]])) continue - for (var i = config.bar.layout[sections[s]].length - 1; i >= 0; i--) { - if (barEntryId(config.bar.layout[sections[s]][i]) === sourceId) - config.bar.layout[sections[s]].splice(i, 1) + var sectionEntries = layoutSection(config, sections[s]) + if (!sectionEntries) continue + for (var i = sectionEntries.length - 1; i >= 0; i--) { + if (barEntryId(sectionEntries[i]) === sourceId) + sectionEntries.splice(i, 1) } } cloneLocation = findBarLocation(config, cloneId, "") if (cloneLocation.found) { var restoredEntry = Util.isPlainObject(cloneEntry) ? Util.cloneJson(cloneEntry) : {} restoredEntry.id = sourceId - config.bar.layout[cloneLocation.section][cloneLocation.index] = restoredEntry + layoutSection(config, cloneLocation.section)[cloneLocation.index] = restoredEntry } } else if (cloneLocation.kind === "plugin") { config.plugins.splice(cloneLocation.index, 1) @@ -550,14 +562,14 @@ QtObject { if (!location.found && isBarWidget) { var sourceLocation = clonedFrom ? findEntryLocation(config, clonedFrom) : { found: false } if (sourceLocation.kind === "bar") { - var sourceEntry = config.bar.layout[sourceLocation.section][sourceLocation.index] + var sourceEntry = layoutSection(config, sourceLocation.section)[sourceLocation.index] var replacement = Util.isPlainObject(sourceEntry) ? Util.cloneJson(sourceEntry) : entry replacement.id = key - config.bar.layout[sourceLocation.section][sourceLocation.index] = replacement + layoutSection(config, sourceLocation.section)[sourceLocation.index] = replacement } else { var section = defaultBarWidgetSection(manifest) var target = barTarget(config, placement || {}, section, key) - config.bar.layout[target.section].splice(target.index, 0, entry) + layoutSection(config, target.section).splice(target.index, 0, entry) insertedWithPlacement = true } } else if (!location.found && !isFirstParty) { @@ -575,7 +587,7 @@ QtObject { } if (clonedFrom) restoreCloneSource(config, key, clonedFrom) - else if (location.kind === "bar") config.bar.layout[location.section].splice(location.index, 1) + else if (location.kind === "bar") layoutSection(config, location.section).splice(location.index, 1) else if (location.kind === "plugin") config.plugins.splice(location.index, 1) // Dropping the layout entry is the whole story for a widget. Anything diff --git a/shell/shell.qml b/shell/shell.qml index 78e38d0d9dab..42429a14ca85 100644 --- a/shell/shell.qml +++ b/shell/shell.qml @@ -1104,11 +1104,11 @@ ShellRoot { if (!Util.isPlainObject(copy.bar.layout)) copy.bar.layout = {} if (!Array.isArray(copy.plugins)) copy.plugins = [] - var sections = Util.barSections + var sections = Util.layoutSectionNames(copy.bar.layout) var foundInLayout = false var dirty = false for (var s = 0; s < sections.length; s++) { - var arr = copy.bar.layout[sections[s]] || [] + var arr = Util.layoutSectionEntries(copy.bar.layout, sections[s]) || [] for (var i = 0; i < arr.length; i++) { if (arr[i] && Util.canonicalWidgetId(arr[i].id) === stripped) { var next = { id: stripped } diff --git a/test/shell.d/bar-drag-test.sh b/test/shell.d/bar-drag-test.sh index a52d06f93d34..26370bc0bbf4 100755 --- a/test/shell.d/bar-drag-test.sh +++ b/test/shell.d/bar-drag-test.sh @@ -49,7 +49,7 @@ PATH="$TMPDIR/bin:$ROOT/bin:$PATH" \ quickshell -p "$config_dir" --no-color >"$log" 2>&1 & QS_PID=$! -for _ in {1..100}; do +for _ in {1..200}; do [[ -s $result ]] && break if ! kill -0 "$QS_PID" 2>/dev/null; then sed -n '1,160p' "$log" >&2 diff --git a/test/shell.d/bar-test.sh b/test/shell.d/bar-test.sh index de20b56e097c..b9619ba4e02c 100644 --- a/test/shell.d/bar-test.sh +++ b/test/shell.d/bar-test.sh @@ -89,7 +89,7 @@ for (const edge of ['top', 'bottom', 'left', 'right']) { const moduleSlot = barSource.slice(barSource.indexOf('component ModuleSlot'), barSource.indexOf('component CustomCommandModule')) const dragSpacer = barSource.slice(barSource.indexOf('component DragSpacer'), barSource.indexOf('component CenterGestureArea')) assert( - /if \(slot\.traySectionContains\(mouse\.x, mouse\.y\)\) \{\s*mouse\.accepted = false/.test(moduleSlot), + /if \(slot\.sectionContains\(mouse\.x, mouse\.y\)\) \{\s*mouse\.accepted = false/.test(moduleSlot), 'a press in the tray\'s drawer goes to the widget in it, not the tray' ) // A third-party widget's api lives as long as the widget does, wherever it @@ -532,12 +532,12 @@ const grownRight = bar.sectionDropZone({ x: 700, y: 0, width: 30, height: 26 }, for (const grown of [grownLeft, grownRight]) assert(grown.x <= sectionZone.x && grown.x + grown.width >= sectionZone.x + sectionZone.width, 'room opening in the section, from either end, keeps the stretch it reached before') const trayRows = [dropRow(weatherSlot, 'tray', 660, 40), dropRow(menuSlot, 'right', 740, 30)] -assertEqual(bar.traySectionDropTarget(trayRows, null, { x: 680 }, false), null, 'no tray section, no drop in it') -assertEqual(bar.traySectionDropTarget(trayRows, bar.sectionDropZone({ x: 660, y: 0, width: 40, height: 26 }, 26, false), { x: 720 }, false), null, 'off the section, its widgets are out of reach') -assertDeepEqual(bar.traySectionDropTarget(trayRows, bar.sectionDropZone({ x: 660, y: 0, width: 40, height: 26 }, 26, false), { x: 690 }, false), { slot: weatherSlot, after: true }, 'over the section, a drop lands among its widgets') -assertDeepEqual(bar.traySectionDropTarget([trayRows[1]], sectionZone, { x: 708 }, false), { region: 'tray', after: true }, 'over an empty section, a drop lands in it') +assertEqual(bar.sectionDropTarget(trayRows, null, { x: 680 }, false), null, 'no tray section, no drop in it') +assertEqual(bar.sectionDropTarget(trayRows, bar.sectionDropZone({ x: 660, y: 0, width: 40, height: 26 }, 26, false), { x: 720 }, false), null, 'off the section, its widgets are out of reach') +assertDeepEqual(bar.sectionDropTarget(trayRows, bar.sectionDropZone({ x: 660, y: 0, width: 40, height: 26 }, 26, false), { x: 690 }, false), { slot: weatherSlot, after: true }, 'over the section, a drop lands among its widgets') +assertDeepEqual(bar.sectionDropTarget([trayRows[1]], sectionZone, { x: 708 }, false), { region: 'tray', after: true }, 'over an empty section, a drop lands in it') const besideTray = { id: 'beside' } -assertDeepEqual(bar.traySectionDropTarget([trayRows[1], dropRow(besideTray, 'right', 705, 30)], sectionZone, { x: 708 }, false, besideTray), { slot: besideTray, after: false }, 'over the dragged widget\'s own room, it goes back') +assertDeepEqual(bar.sectionDropTarget([trayRows[1], dropRow(besideTray, 'right', 705, 30)], sectionZone, { x: 708 }, false, besideTray), { slot: besideTray, after: false }, 'over the dragged widget\'s own room, it goes back') // The tray's own ends stay where a drop lands beside the tray, even where its // section reaches them with no pinned icons between: here the tray alone in // centerEnd, hanging off the center box, with its section first. @@ -547,7 +547,7 @@ const inSection = { id: 'indicators' } const boxRow = dropRow(elsewhen, 'center', 300, 100) const trayRow = dropRow(trayAtBox, 'centerEnd', 400, 120) const trayAtBoxZone = bar.sectionDropZone({ x: 400, y: 0, width: 80, height: 26 }, 26, false, { x: 400, y: 0, width: 120, height: 26 }) -const dropAt = x => bar.traySectionDropTarget([boxRow, trayRow, dropRow(inSection, 'tray', 400, 80)], trayAtBoxZone, { x }, false) || +const dropAt = x => bar.sectionDropTarget([boxRow, trayRow, dropRow(inSection, 'tray', 400, 80)], trayAtBoxZone, { x }, false) || bar.centerDropTarget([boxRow, trayRow], { x: 300, y: 0, width: 100, height: 26 }, { x }, false) assertDeepEqual(dropAt(405), { slot: trayAtBox, after: false }, 'just past the center box, a drop lands before a tray whose section comes first') assertDeepEqual(dropAt(395), { slot: elsewhen, after: true }, 'over the end of the box, a drop still lands in the box') @@ -683,6 +683,99 @@ assertEqual( ) JS +# Drawers: a bar entry with type drawer holds widgets of its own, the section +# drawer: kept under layout.drawers, the way the tray's drawer holds the +# tray section. +run_node_test <<'JS' +const fs = require('fs') +const layout = requireFromRoot('shell/Commons/BarLayout.js') +const bar = requireFromRoot('shell/plugins/bar/BarModel.js') +const tray = requireFromRoot('shell/plugins/bar/widgets/TrayModel.js') +const barSource = fs.readFileSync(root + '/shell/plugins/bar/Bar.qml', 'utf8') + +const drawerLayout = { + left: [{ id: 'omarchy.workspaces' }], + center: [{ id: 'omarchy.clock' }], + centerEnd: [{ id: 'tools', type: 'drawer' }], + right: [{ id: 'home', type: 'drawer', name: 'Home' }, { id: 'omarchy.tray' }, { id: 'home', type: 'drawer' }], + tray: [{ id: 'omarchy.dropbox' }], + drawers: { home: [{ id: 'omarchy.bluetooth' }], gone: [{ id: 'omarchy.audio' }] } +} +const fixed = ['left', 'centerBegin', 'center', 'centerEnd', 'right', 'tray'] + +assertDeepEqual(layout.BASE_SECTIONS, bar.sections, 'BarLayout and BarModel list the same fixed sections') +assertDeepEqual(layout.drawerIds(drawerLayout), ['tools', 'home'], 'drawers are the drawer entries on the bar, in bar order, each once') +assertDeepEqual(layout.sectionNames(drawerLayout), fixed.concat(['drawer:tools', 'drawer:home']), 'every drawer on the bar is a section after the fixed ones') +assertDeepEqual(bar.layoutSections(drawerLayout), layout.sectionNames(drawerLayout), 'BarModel finds the same drawer sections as BarLayout') +assertDeepEqual(layout.sectionEntries(drawerLayout, 'drawer:home'), [{ id: 'omarchy.bluetooth' }], 'a drawer section reads its widgets from layout.drawers') +assertDeepEqual(bar.sectionEntries(drawerLayout, 'drawer:home'), [{ id: 'omarchy.bluetooth' }], 'so does BarModel') +assertEqual(layout.sectionEntries(drawerLayout, 'right').length, 3, 'a fixed section reads its own list') +assertEqual(layout.sectionEntries(drawerLayout, 'drawer:nope'), null, 'a drawer with no widgets list has no entries') +assertEqual(bar.sectionEntries(drawerLayout, 'drawer:nope'), null, 'nor does it in BarModel') +assertEqual(layout.sectionEntries(drawerLayout, 'middle'), null, 'a name that is no section has no entries') +assertDeepEqual(Object.keys(layout.sections(drawerLayout)), fixed.concat(['drawer:tools', 'drawer:home']), 'a drawers list whose drawer left the bar is no section') + +for (const nested of [{ right: [], tray: [{ id: 'nested', type: 'drawer' }], drawers: { x: [{ id: 'inner', type: 'drawer' }] } }]) { + assertDeepEqual(layout.drawerIds(nested), [], 'drawers inside the tray section or another drawer are not drawers') + assertDeepEqual(bar.layoutSections(nested), fixed, 'nor are they to BarModel') +} +assertDeepEqual(layout.drawerIds({ left: [{ id: '../x', type: 'drawer' }, { id: 'a b', type: 'drawer' }, { id: 'ok_1.2-3', type: 'drawer' }] }), ['ok_1.2-3'], 'drawer ids are plain names') +assertEqual(bar.drawerIdOf('drawer:../x'), '', 'BarModel takes plain drawer ids only') + +const edited = { right: [] } +assertDeepEqual(layout.ensureSection(edited, 'drawer:home'), [], 'a drawer section is created when missing') +assertDeepEqual(edited.drawers, { home: [] }, 'a created drawer section lives under layout.drawers') +assertEqual(layout.ensureSection(edited, 'drawer:../x'), null, 'no section is created for a bad drawer id') +assertEqual(layout.ensureSection(edited, 'middle'), null, 'no section is created for a name that is no section') + +for (const helper of [layout, bar]) { + assert(helper.isHostedSection('tray') && helper.isHostedSection('drawer:home'), 'the tray section and drawers are drawn inside another widget') + assert(!helper.isHostedSection('right') && !helper.isHostedSection('centerEnd') && !helper.isHostedSection('drawer:'), 'bar sections and an empty drawer id are not') +} + +// Each widget in a drawer keeps its key as any other does, so a drag in or +// out hands it over rather than building it again. +const memo = {} +const first = bar.layoutWidgets(drawerLayout, memo) +assertDeepEqual(Object.keys(first.sections), fixed.concat(['drawer:tools', 'drawer:home']), 'every drawer on the bar gets a list of keys') +assertEqual(first.sections['drawer:home'].length, 1, 'a drawer lists its widgets') +assert(!JSON.stringify(first.sections).includes('omarchy.audio'), 'widgets of a drawer gone from the bar are not drawn') +const bluetooth = first.sections['drawer:home'][0] +const outOfDrawer = JSON.parse(JSON.stringify(drawerLayout)) +outOfDrawer.drawers.home = [] +outOfDrawer.center.push({ id: 'omarchy.bluetooth' }) +const second = bar.layoutWidgets(outOfDrawer, memo) +assertDeepEqual(second.sections.center, [first.sections.center[0], bluetooth], 'a widget moved out of a drawer keeps its key') + +// The bar's own move writes into and out of drawers. +const config = { bar: { layout: JSON.parse(JSON.stringify(drawerLayout)) } } +const keep = entry => bar.entryId(entry) ? entry : null +assert(bar.moveEntry(config, { from: { region: 'center', index: 0, id: 'omarchy.clock' }, to: { region: 'drawer:home', index: 0 } }, keep), 'a widget moves into a drawer') +assertDeepEqual(config.bar.layout.drawers.home.map(bar.entryId), ['omarchy.clock', 'omarchy.bluetooth'], 'it lands where it was dropped in the drawer') +assert(bar.moveEntry(config, { from: { region: 'drawer:home', index: 1, id: 'omarchy.bluetooth' }, to: { region: 'left', index: -1 } }, keep), 'a widget moves out of a drawer') +assertDeepEqual(config.bar.layout.left.map(bar.entryId), ['omarchy.workspaces', 'omarchy.bluetooth'], 'it lands on the bar') +assert(bar.moveEntry(config, { from: { region: 'centerEnd', index: 0, id: 'tools' }, to: { region: 'drawer:tools', index: -1 } }, keep) === false, 'a drawer never goes into a drawer') +assert(bar.moveEntry(config, { from: { region: 'right', index: 1, id: 'omarchy.tray' }, to: { region: 'drawer:home', index: -1 } }, keep) === false, 'nor does the tray') +assert(bar.moveEntry(config, { from: { region: 'left', index: 0, id: 'omarchy.workspaces' }, to: { region: 'drawer:x', index: -1 } }, keep), 'a move to a drawer section with no list yet makes one') +assertDeepEqual(config.bar.layout.drawers.x.map(bar.entryId), ['omarchy.workspaces'], 'the made list holds the widget') + +// Dropping over a drawer's section lands in it, as over the tray's. +const zone = bar.sectionDropZone({ x: 700, y: 0, width: 0, height: 26 }, 26, false) +assertDeepEqual(bar.sectionDropTarget([], zone, { x: 705 }, false, null, 'drawer:home'), { region: 'drawer:home', after: true }, 'over an empty drawer, a drop lands in it') + +assert(tray.layoutHasWidget({ right: [], drawers: { home: [{ id: 'omarchy.dropbox' }] } }, 'omarchy.dropbox'), 'a widget in a drawer counts as on the bar for the tray') +assert(/drawerCustom: customType === "drawer"/.test(barSource) && /component DrawerModule/.test(barSource), 'the bar draws drawer entries with its drawer module') +assert(/BarModel\.isHostedSection\(slot\.region\) && !intoSections/.test(barSource), 'the tray and drawers are never dropped into a drawer') +// Anywhere over a drawer, its button too, a drop goes into it; the tray's +// chevron still drops beside the tray. +assert(/if \(BarModel\.drawerIdOf\(slot\.hostedSection\)\) return tray/.test(barSource), 'a drop anywhere over a drawer goes into it') +const drawerRows = [dropRowFor('drawer:home', 300, 30)] +function dropRowFor(region, x, width) { return { slot: { region, widgetKey: region + x }, region, x, y: 0, width, height: 26 } } +const wholeDrawer = { x: 280, y: 0, width: 60, height: 26 } +assertDeepEqual(bar.sectionDropTarget(drawerRows, wholeDrawer, { x: 285 }, false, null, 'drawer:home'), { slot: drawerRows[0].slot, after: false }, 'over the drawer button, a drop lands beside the widget nearest it, inside the drawer') +assertEqual(bar.sectionDropTarget(drawerRows, wholeDrawer, { x: 345 }, false, null, 'drawer:home'), null, 'past the drawer, the drop is beside it on the bar') +JS + put_tmp=$(mktemp -d) trap 'rm -rf "$put_tmp"' EXIT mkdir -p "$put_tmp/bin" diff --git a/test/shell.d/fixtures/bar-drag/shell.qml b/test/shell.d/fixtures/bar-drag/shell.qml index 19bee92f5590..3d31e1ed8f4f 100644 --- a/test/shell.d/fixtures/bar-drag/shell.qml +++ b/test/shell.d/fixtures/bar-drag/shell.qml @@ -42,7 +42,9 @@ ShellRoot { } function ids(region) { - return (host.config.bar.layout[region] || []).map(function(entry) { return entry.id }) + var layout = host.config.bar.layout + var list = region.indexOf("drawer:") === 0 ? (layout.drawers || {})[region.slice(7)] : layout[region] + return (list || []).map(function(entry) { return entry.id }) } // A widget's slots on the surface, along the bar. @@ -132,6 +134,7 @@ ShellRoot { "test.a": { component: testWidget }, "test.b": { component: testWidget }, "test.c": { component: testWidget }, + "test.d": { component: testWidget }, "test.clock": { component: testWidget }, "test.twin": { component: testWidget }, "test.inner": { component: testWidget }, @@ -254,6 +257,58 @@ ShellRoot { [function() { var inner = root.slotsOf("test.inner")[0] root.check(inner && inner.activeItem.fixedEdge === "start", "moved to the left section, the tray has them grow from its start: " + (inner ? inner.activeItem.fixedEdge : "none shown")) + + // A drawer holds widgets of its own, the section drawer:, drawn + // inside it as the tray section is inside the tray. + host.mutateShellConfig(function(config) { + config.bar.layout.right.push({ id: "box", type: "drawer", collapsed: false }) + config.bar.layout.drawers = { box: [{ id: "test.d" }] } + }) + }, 600], + [function() { + var box = root.slotOf("box", "right") + var inside = root.slotOf("test.d", "drawer:box") + root.check(box && box.hostsSection && box.hostedSection === "drawer:box", "a drawer on the bar hosts its own section") + root.check(!!inside, "the drawer shows its widget") + root.check(inside && inside.activeItem.fixedEdge === "end", "a drawer in the right section opens toward the middle, its widgets growing from its end: " + (inside ? inside.activeItem.fixedEdge : "none shown")) + if (!inside) return + root.shown = inside.activeItem + root.counts = root.countsNow() + root.lift(inside) + }, 300], + [function() { root.pointAt(root.slotOf("test.a", "left"), 0.8) }, 300], + [function() { root.letGo(root.slotOf("test.a", "left"), 0.8) }, 700], + [function() { + root.check(JSON.stringify(root.ids("left")) === JSON.stringify(["test.a", "test.d", "test.tray"]), "a widget dragged out of a drawer lands on the bar: " + JSON.stringify(root.ids("left"))) + root.check(JSON.stringify(root.ids("drawer:box")) === "[]", "and leaves the drawer: " + JSON.stringify(root.ids("drawer:box"))) + var moved = root.slotOf("test.d", "left") + root.check(moved && moved.activeItem === root.shown, "it arrives as the very instance dragged") + root.check(root.countsNow() === root.counts, "nothing is built again: " + root.countsNow()) + if (moved) root.lift(moved) + }, 300], + // While a widget is dragged the drawer opens, empty, and a drop over it + // goes in. + [function() { root.pointAt(root.slotOf("box", "right"), 0.5) }, 400], + [function() { + var box = root.slotOf("box", "right") + root.check(root.bar.barDragRegion === "drawer:box", "over an empty drawer, the drop would go into it: " + root.bar.barDragRegion) + root.letGo(box, 0.5) + }, 700], + [function() { + root.check(JSON.stringify(root.ids("drawer:box")) === JSON.stringify(["test.d"]), "a widget dropped over a drawer lands in it: " + JSON.stringify(root.ids("drawer:box"))) + var back = root.slotOf("test.d", "drawer:box") + root.check(back && back.activeItem === root.shown, "it is still the instance dragged") + root.check(root.countsNow() === root.counts, "still nothing is built again: " + root.countsNow()) + // The drawer itself never goes into a drawer. + root.lift(root.slotOf("box", "right")) + }, 300], + [function() { root.pointAt(root.slotOf("box", "right"), 0.5) }, 400], + [function() { + root.check(root.bar.barDragRegion !== "drawer:box", "a drawer is never dropped into itself: " + root.bar.barDragRegion) + root.letGo(root.slotOf("box", "right"), 0.5) + }, 700], + [function() { + root.check(JSON.stringify(root.ids("drawer:box")) === JSON.stringify(["test.d"]), "the drawer keeps its widget: " + JSON.stringify(root.ids("drawer:box"))) }, 100] ] stepper.start() diff --git a/test/shell.d/fixtures/plugin-registry/shell.qml b/test/shell.d/fixtures/plugin-registry/shell.qml index 988d380478fe..57bf9007e890 100644 --- a/test/shell.d/fixtures/plugin-registry/shell.qml +++ b/test/shell.d/fixtures/plugin-registry/shell.qml @@ -257,6 +257,27 @@ ShellRoot { root.assertEqual(registry.moveBarWidget("third.widget", { section: "right" }), "", "registry moves widgets out of the tray section") root.assertDeepEqual(root.config.bar.layout.tray, [], "a widget moved out of the tray section leaves it") + // A drawer is a bar entry with type drawer; its widgets are the section + // drawer:, kept under layout.drawers. + root.config = { version: 1, bar: { layout: { left: [], center: [], right: [{ id: "home", type: "drawer" }, { id: "third.widget", size: 7 }], tray: [] } }, plugins: [] } + root.assertEqual(registry.moveBarWidget("third.widget", { section: "drawer:home" }), "", "registry moves widgets into a drawer") + root.assertDeepEqual(root.config.bar.layout.right, [{ id: "home", type: "drawer" }], "a widget moved into a drawer leaves its section") + root.assertDeepEqual(root.config.bar.layout.drawers, { home: [{ id: "third.widget", size: 7 }] }, "a drawer keeps the widget and its settings") + root.assertEqual(registry.setBarWidget("third.widget", "size", 9, {}), "", "registry finds widgets in a drawer") + root.assertEqual(root.config.bar.layout.drawers.home[0].size, 9, "registry sets options on widgets in a drawer") + registry.setEnabled("third.widget", true) + root.assertDeepEqual(root.config.bar.layout.right, [{ id: "home", type: "drawer" }], "enabling a widget in a drawer does not add it to the bar again") + root.assertEqual(registry.moveBarWidget("third.widget", { section: "drawer:gone" }), "", "a section naming no drawer on the bar falls back to the widget's own section") + root.assertDeepEqual(root.config.bar.layout.drawers.home, [{ id: "third.widget", size: 9 }], "a widget stays in its drawer when the target drawer does not exist") + root.assertEqual(registry.moveBarWidget("third.widget", { section: "left" }), "", "registry moves widgets out of a drawer") + root.assertDeepEqual(root.config.bar.layout.drawers.home, [], "a widget moved out of a drawer leaves it") + root.config = { version: 1, bar: { layout: { left: [], center: [], right: [{ id: "home", type: "drawer" }, { id: "omarchy.tray" }], tray: [], drawers: { home: [{ id: "third.widget" }] } } }, plugins: [] } + root.assertEqual(registry.moveBarWidget("omarchy.tray", { section: "drawer:home" }), "the tray cannot go in a drawer", "registry keeps the tray out of drawers") + root.assertEqual(registry.moveBarWidget("omarchy.tray", { after: "third.widget" }), "the tray cannot go in a drawer", "registry keeps the tray out of drawers when placed beside a widget in one") + root.config = { version: 1, bar: { layout: { left: [], center: [], right: [], tray: [], drawers: { gone: [{ id: "third.widget" }] } } }, plugins: [] } + registry.setEnabled("third.widget", true) + root.assertEqual(root.config.bar.layout.left.concat(root.config.bar.layout.center, root.config.bar.layout.right).filter(function(e) { return e.id === "third.widget" }).length, 1, "a widget in a drawer that is no longer on the bar counts as off the bar") + root.config = { version: 1, bar: { layout: { left: [], center: [], right: [{ id: "omarchy.tray" }], tray: [{ id: "third.widget" }] } }, plugins: [] } root.assertEqual(registry.moveBarWidget("omarchy.tray", { section: "tray" }), "the tray cannot go in its own drawer", "registry keeps the tray out of its own drawer") root.assertEqual(registry.moveBarWidget("omarchy.tray", { after: "third.widget" }), "the tray cannot go in its own drawer", "registry keeps the tray out of its own drawer when placed beside a widget in it") From ae9b7dff2354dd2960d5b473d62fab187f8f6ecc Mon Sep 17 00:00:00 2001 From: Uwe Bruckhaus Date: Mon, 19 Oct 2026 21:08:50 +0200 Subject: [PATCH 2/2] Keep drawers out of drawers from the command line too omarchy bar move took a drawer into another drawer or the tray's drawer, where it has no section of its own and its widgets are not drawn. The registry now refuses it as it does the tray, beside a widget in a drawer too, knowing the drawer by the entry being moved. omarchy bar get reads only drawers on the bar, as the shell does, so it no longer finds settings of widgets that set cannot reach. Co-Authored-By: Claude Opus 5.5 --- bin/omarchy-bar | 5 +++- shell/services/PluginRegistry.qml | 26 +++++++++++++++---- test/shell.d/bar-test.sh | 16 ++++++++++++ .../fixtures/plugin-registry/shell.qml | 9 +++++++ 4 files changed, 50 insertions(+), 6 deletions(-) diff --git a/bin/omarchy-bar b/bin/omarchy-bar index a329a6ed60ce..8d3ee5104d43 100755 --- a/bin/omarchy-bar +++ b/bin/omarchy-bar @@ -447,7 +447,10 @@ cmd_get() { (( $# == 2 )) || fail "get takes a widget id and a setting key" jq -e -r --arg id "$id" --arg key "$key" ' - first(.bar.layout | (.left, .centerBegin, .center, .centerEnd, .right, .tray, (.drawers // {} | .[]?)) | arrays | .[] + first(.bar.layout + # Drawers count only while their entry is on the bar itself, as in the shell. + | [(.left, .centerBegin, .center, .centerEnd, .right) | arrays | .[] | select(type == "object" and .type == "drawer") | .id] as $drawers + | (.left, .centerBegin, .center, .centerEnd, .right, .tray, ($drawers[] as $drawer | .drawers[$drawer]?)) | arrays | .[] | select(type == "object" and .id == $id) | .[$key] | select(. != null)) | if type == "string" then . else tojson end ' "$(source_file)" 2>/dev/null || exit 1 diff --git a/shell/services/PluginRegistry.qml b/shell/services/PluginRegistry.qml index 5316e1af1ce2..158c63217b7e 100644 --- a/shell/services/PluginRegistry.qml +++ b/shell/services/PluginRegistry.qml @@ -268,19 +268,35 @@ QtObject { return { error: section === "tray" ? "the tray cannot go in its own drawer" : "the tray cannot go in a drawer" } } + // Why the tray or a drawer cannot go into a section drawn inside another + // widget (the tray's drawer or a drawer), or null when it can go there. + function hostedSectionError(section, tray, drawer) { + if (!Util.isHostedLayoutSection(section)) return null + if (tray) return trayDrawerError(section) + if (drawer) return { error: section === "tray" ? "a drawer cannot go in the tray's drawer" : "a drawer cannot go in a drawer" } + return null + } + // The tray section is drawn inside the tray and a drawer's section inside - // the drawer, so the tray cannot go into either. - function barTarget(config, placement, fallbackSection, id) { + // the drawer, so neither the tray nor a drawer can go into either: a + // drawer inside one would have no section of its own, and its widgets + // would not be drawn. `entry` is the entry being moved, when there is one: + // a drawer is known by its entry, which a move has already taken out of + // the layout by now. + function barTarget(config, placement, fallbackSection, id, entry) { var target = placement || {} var section = Util.layoutSectionNames(config.bar.layout).indexOf(String(target.section || "")) !== -1 ? String(target.section) : fallbackSection var tray = isTray(id) - if (tray && Util.isHostedLayoutSection(section)) return trayDrawerError(section) + var drawer = Util.isPlainObject(entry) && entry.type === "drawer" + var refused = hostedSectionError(section, tray, drawer) + if (refused) return refused var relativeId = String(target.before || target.after || "") if (relativeId) { var relative = findRelativeBarLocation(config, relativeId, section && target.section ? section : "") if (!relative.found) return { error: "could not find target widget " + relativeId } - if (tray && Util.isHostedLayoutSection(relative.section)) return trayDrawerError(relative.section) + refused = hostedSectionError(relative.section, tray, drawer) + if (refused) return refused return { section: relative.section, index: relative.index + (target.after ? 1 : 0) @@ -324,7 +340,7 @@ QtObject { var entry = layoutSection(config, source.section)[source.index] layoutSection(config, source.section).splice(source.index, 1) - var target = barTarget(config, placement, source.section, key) + var target = barTarget(config, placement, source.section, key, entry) if (target.error) { layoutSection(config, source.section).splice(source.index, 0, entry) return target.error diff --git a/test/shell.d/bar-test.sh b/test/shell.d/bar-test.sh index b9619ba4e02c..2406f3c9aafd 100644 --- a/test/shell.d/bar-test.sh +++ b/test/shell.d/bar-test.sh @@ -905,3 +905,19 @@ put_output=$(PATH="$put_tmp/bin:$ROOT/bin:$PATH" \ fail "put places a widget through a ready shell" "$put_output" [[ $put_output == "omarchy.keyboard-layout is on the bar" ]] || fail "put reports the placed widget" "$put_output" pass "put places a widget through a ready shell" + +# get reads widgets in drawers on the bar, never in a drawer gone from it, +# whose widgets are off the bar and which set cannot reach either. +get_home=$(mktemp -d) +mkdir -p "$get_home/.config/omarchy" +cat >"$get_home/.config/omarchy/shell.json" <<'JSON' +{"bar":{"layout":{"left":[],"right":[{"id":"home","type":"drawer"}],"tray":[],"drawers":{"home":[{"id":"w.in","size":1}],"gone":[{"id":"w.gone","size":2}]}}}} +JSON +get_output=$(HOME="$get_home" "$ROOT/bin/omarchy-bar" get w.in size) || fail "get finds a widget in a drawer on the bar" +[[ $get_output == "1" ]] || fail "get reads a setting of a widget in a drawer" "$get_output" +pass "get reads a setting of a widget in a drawer" +if HOME="$get_home" "$ROOT/bin/omarchy-bar" get w.gone size >/dev/null 2>&1; then + fail "get leaves widgets of a drawer gone from the bar alone" +fi +pass "get leaves widgets of a drawer gone from the bar alone" +rm -rf "$get_home" diff --git a/test/shell.d/fixtures/plugin-registry/shell.qml b/test/shell.d/fixtures/plugin-registry/shell.qml index 57bf9007e890..ebaa8dac1ba2 100644 --- a/test/shell.d/fixtures/plugin-registry/shell.qml +++ b/test/shell.d/fixtures/plugin-registry/shell.qml @@ -274,6 +274,15 @@ ShellRoot { root.config = { version: 1, bar: { layout: { left: [], center: [], right: [{ id: "home", type: "drawer" }, { id: "omarchy.tray" }], tray: [], drawers: { home: [{ id: "third.widget" }] } } }, plugins: [] } root.assertEqual(registry.moveBarWidget("omarchy.tray", { section: "drawer:home" }), "the tray cannot go in a drawer", "registry keeps the tray out of drawers") root.assertEqual(registry.moveBarWidget("omarchy.tray", { after: "third.widget" }), "the tray cannot go in a drawer", "registry keeps the tray out of drawers when placed beside a widget in one") + // A drawer inside another would have no section, and its widgets would + // not be drawn: drawers stay out of drawers and the tray's drawer. + root.config = { version: 1, bar: { layout: { left: [{ id: "tools", type: "drawer" }], center: [], right: [{ id: "home", type: "drawer" }, { id: "omarchy.tray" }], tray: [], drawers: { home: [{ id: "third.widget" }], tools: [] } } }, plugins: [] } + root.assertEqual(registry.moveBarWidget("home", { section: "drawer:tools" }), "a drawer cannot go in a drawer", "registry keeps a drawer out of another drawer") + root.assertEqual(registry.moveBarWidget("tools", { after: "third.widget" }), "a drawer cannot go in a drawer", "registry keeps a drawer out of a drawer when placed beside a widget in one") + root.assertEqual(registry.moveBarWidget("home", { section: "tray" }), "a drawer cannot go in the tray's drawer", "registry keeps a drawer out of the tray's drawer") + root.assertDeepEqual(root.config.bar.layout.left, [{ id: "tools", type: "drawer" }], "a refused drawer move changes nothing") + root.assertEqual(registry.moveBarWidget("home", { section: "left" }), "", "a drawer still moves along the bar") + root.assertDeepEqual(root.config.bar.layout.left.map(function(entry) { return entry.id }), ["tools", "home"], "the moved drawer lands in the other section") root.config = { version: 1, bar: { layout: { left: [], center: [], right: [], tray: [], drawers: { gone: [{ id: "third.widget" }] } } }, plugins: [] } registry.setEnabled("third.widget", true) root.assertEqual(root.config.bar.layout.left.concat(root.config.bar.layout.center, root.config.bar.layout.right).filter(function(e) { return e.id === "third.widget" }).length, 1, "a widget in a drawer that is no longer on the bar counts as off the bar")