Files
glassMxrph/quickshell/settings/controls/BarEditor.qml
T

274 lines
18 KiB
QML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
pragma ComponentBehavior: Bound
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import "../../components"
import "../../core"
import "../../services"
import "../../theme"
import "../../services/SettingsLogic.js" as Logic
Item {
id: root
objectName:"barEditor"
implicitHeight: lanes.implicitHeight
property var laneItems: []
property string dragging: ""
property string targetLane: ""
property string beforeId: ""
// Screen whose layout is edited; "" or a screen without its own layout edits the shared one.
property string screenName: ""
// Card whose options panel (cog) is open.
property string expandedId: ""
// Option fields for a module: built-in choice lists, or a plugin's
// manifest schema (boolean / integer / number / string / choices).
function fieldsFor(id) {
if (!ModuleRegistry.isPlugin(id))
return (ModuleRegistry.optionSchemas[id] || []).map(f => Object.assign({type:"choice"}, f)).concat(ModuleRegistry.sharedOptions[id] || []);
return PluginService.schema(String(id).slice(7)).filter(f => f && f.key).map(f => {
const choices = Array.isArray(f.choices) ? f.choices : Array.isArray(f.options) ? f.options.map(o => typeof o === "object" ? o.value : o) : null;
return Object.assign({}, f, {label: f.label || f.key, type: choices ? "choice" : (f.type || "string"), choices: choices || []});
});
}
function valueOf(entry, field) {
if (field.setting)
return ConfigService.value(field.setting);
if (field.module)
return root.modules.some(m => m.id === field.module && m.enabled);
if (!ModuleRegistry.isPlugin(entry.id))
return ModuleRegistry.option(entry, field.key);
return PluginService.widgetSettings(String(entry.id).slice(7), entry.options)[field.key];
}
function setOption(entry, key, value) {
const field = root.fieldsFor(entry.id).find(f => f.key === key) || {};
if (field.setting)
return ConfigService.setValue(field.setting, value);
if (field.module) {
// Toggle the companion module, adding it right after this one the first time.
if (root.modules.some(m => m.id === field.module))
return root.save(Logic.editModule(root.modules, field.module, "enabled", value));
if (!value)
return;
const list = root.modules.slice();
list.splice(list.findIndex(m => m.id === entry.id) + 1, 0, {id: field.module, position: entry.position, enabled: true});
return root.save(list);
}
root.save(Logic.editModule(root.modules, entry.id, "options", Object.assign({}, entry.options || {}, {[key]: value})));
}
readonly property var modules: BarLayout.modulesFor(screenName)
function save(list) {
BarLayout.save(screenName, list);
}
function updateDrag(source,x,y) {
const p=source.mapToItem(root,x,y);
ghost.x=p.x-70;ghost.y=p.y-22;
targetLane="";beforeId="";
for(let i=0;i<laneItems.length;i++) {
const lane=laneItems[i],local=root.mapToItem(lane,p.x,p.y);
if(local.x<0 || local.x>lane.width || local.y<0 || local.y>lane.height)continue;
targetLane=lane.modelData;
for(let j=0;j<lane.cards.count;j++) {
const card=lane.cards.itemAt(j),q=root.mapToItem(card,p.x,p.y);
if(q.y<card.height/2) {beforeId=card.modelData.id;break;}
}
}
}
function finish() {
const id=dragging,lane=targetLane,before=beforeId;
dragging="";targetLane="";beforeId="";
if(id && lane)Qt.callLater(()=>root.save(Logic.dropModule(root.modules,id,lane,before)));
}
RowLayout {
id: lanes
anchors.left:parent.left;anchors.right:parent.right
spacing:12
Repeater {
id: laneRepeater
model:["left","center","right"]
GlassPanel {
id: lane
Component.onCompleted: root.laneItems=root.laneItems.concat([lane])
Component.onDestruction: root.laneItems=root.laneItems.filter(l=>l!==lane)
required property string modelData
property alias cards: cards
objectName:"bar-lane-"+modelData
Layout.fillWidth:true
Layout.minimumWidth:130
Layout.fillHeight:true
implicitHeight:column.implicitHeight+32
border.color:root.targetLane===modelData ? Theme.accent : Theme.border
ColumnLayout {
id:column
anchors {left:parent.left;right:parent.right;top:parent.top;margins:10}
spacing:8
Text {text:lane.modelData.toUpperCase();color:Theme.textSecondary;font.pixelSize:11}
Repeater {
id:cards
model:root.modules.filter(m=>m.position===lane.modelData)
GlassPanel {
id:card
required property var modelData
readonly property var schema:root.fieldsFor(modelData.id)
readonly property var link:ModuleRegistry.settingsLinks[modelData.id] || null
readonly property bool expanded:root.expandedId===modelData.id && schema.length>0
Layout.fillWidth:true
implicitHeight:52+(expanded ? optionsColumn.implicitHeight+4 : 0)
Behavior on implicitHeight {NumberAnimation {duration:Theme.duration;easing.type:Easing.OutCubic}}
clip:true
radius:12
HoverHandler {id:cardHover}
opacity:root.dragging===modelData.id ? 0.35 : 1
Rectangle {anchors.top:parent.top;width:parent.width;height:3;radius:2;color:Theme.accent;visible:root.targetLane===lane.modelData && root.beforeId===card.modelData.id}
RowLayout {
anchors {left:parent.left;right:parent.right;top:parent.top;margins:7}
height:38;spacing:5
Item {
Layout.preferredWidth:24;Layout.fillHeight:true
activeFocusOnTab:true
Accessible.name:"Move "+card.modelData.id
Accessible.role:Accessible.Button
Text {anchors.centerIn:parent;text:"⠿";color:parent.activeFocus ? Theme.accent : Theme.textSecondary;font.pixelSize:24}
MouseArea {
id:grip
objectName:"bar-drag-"+card.modelData.id
anchors.fill:parent
preventStealing:true
cursorShape:pressed ? Qt.ClosedHandCursor : Qt.OpenHandCursor
property point start
onPressed:event=>{start=Qt.point(event.x,event.y);parent.forceActiveFocus();}
onPositionChanged:event=>{
if(!pressed)return;
if(!root.dragging && Math.hypot(event.x-start.x,event.y-start.y)<8)return;
root.dragging=card.modelData.id;root.updateDrag(grip,event.x,event.y);
}
onReleased:root.finish()
onCanceled:{root.dragging="";root.targetLane="";}
}
Keys.onPressed:event=>{
if(event.key===Qt.Key_Escape) {root.dragging="";root.targetLane="";event.accepted=true;}
else if(event.modifiers & Qt.ControlModifier && [Qt.Key_Up,Qt.Key_Down].includes(event.key)) {
root.save(Logic.moveModule(root.modules,card.modelData.id,event.key===Qt.Key_Up?-1:1));event.accepted=true;
} else if(event.modifiers & Qt.AltModifier && [Qt.Key_Left,Qt.Key_Right].includes(event.key)) {
const index=["left","center","right"].indexOf(lane.modelData)+(event.key===Qt.Key_Left?-1:1);
if(index>=0 && index<3)root.save(Logic.dropModule(root.modules,card.modelData.id,["left","center","right"][index],""));event.accepted=true;
}
}
}
Text {text:ModuleRegistry.title(card.modelData.id);color:Theme.text;Layout.fillWidth:true;elide:Text.ElideRight;font.pixelSize:12}
// Cog: module options inline, or the page that holds them.
Rectangle {
objectName:"bar-options-"+card.modelData.id
visible:card.schema.length>0 || !!card.link
implicitWidth:26;implicitHeight:26;radius:13
color:card.expanded ? Qt.alpha(Theme.accent,0.3) : cogArea.containsMouse ? Theme.hover : Qt.alpha(Theme.text,0.06)
Accessible.name:"Options for "+card.modelData.id
Accessible.role:Accessible.Button
Text {anchors.centerIn:parent;text:Icons.available ? "󰒓" : "⚙";font.family:Icons.available ? Icons.family : "";color:card.expanded || cogArea.containsMouse ? Theme.text : Theme.textSecondary;font.pixelSize:13}
MouseArea {
id:cogArea
anchors.fill:parent
hoverEnabled:true
cursorShape:Qt.PointingHandCursor
onClicked:{
if(card.schema.length>0)root.expandedId=card.expanded ? "" : card.modelData.id;
else if(card.link)ShellState.revealSetting(card.link.page,card.link.setting);
}
}
}
GlassToggle {objectName:"bar-enable-"+card.modelData.id;checked:card.modelData.enabled;onToggled:root.save(Logic.editModule(root.modules,card.modelData.id,"enabled",checked))}
// Removed modules return to the "+" list below the lanes.
Rectangle {
objectName:"bar-remove-"+card.modelData.id
// Shown on hover so narrow lanes keep room for the name.
visible:cardHover.hovered || card.activeFocus
implicitWidth:26;implicitHeight:26;radius:13
color:removeArea.containsMouse ? Qt.alpha(Theme.urgent,0.25) : Qt.alpha(Theme.text,0.06)
Accessible.name:"Remove "+card.modelData.id
Accessible.role:Accessible.Button
Text {anchors.centerIn:parent;text:"×";color:removeArea.containsMouse ? Theme.text : Theme.textSecondary;font.pixelSize:16}
MouseArea {
id:removeArea
anchors.fill:parent
hoverEnabled:true
cursorShape:Qt.PointingHandCursor
onClicked:root.save(Logic.removeModule(root.modules,card.modelData.id))
}
}
}
ColumnLayout {
id:optionsColumn
visible:card.expanded
anchors {left:parent.left;right:parent.right;top:parent.top;topMargin:50;leftMargin:12;rightMargin:10}
spacing:6
Repeater {
model:card.schema
RowLayout {
id:optionRow
required property var modelData
readonly property var value:root.valueOf(card.modelData,modelData)
Layout.fillWidth:true
spacing:8
Text {text:optionRow.modelData.label;color:Theme.textSecondary;font.pixelSize:11;Layout.fillWidth:true;elide:Text.ElideRight
HoverHandler {id:labelHover}
ToolTip.visible:labelHover.hovered && !!optionRow.modelData.description
ToolTip.text:optionRow.modelData.description || ""
ToolTip.delay:600}
GlassComboBox {
objectName:"bar-option-"+card.modelData.id+"-"+optionRow.modelData.key
visible:optionRow.modelData.type==="choice"
Layout.preferredWidth:110
implicitHeight:30
model:optionRow.modelData.choices.map(c=>({text:String(c).charAt(0).toUpperCase()+String(c).slice(1),value:c}))
textRole:"text"
valueRole:"value"
currentIndex:optionRow.modelData.choices.indexOf(optionRow.value)
onActivated:root.setOption(card.modelData,optionRow.modelData.key,currentValue)
}
GlassToggle {
visible:optionRow.modelData.type==="boolean"
checked:optionRow.value===true
onToggled:root.setOption(card.modelData,optionRow.modelData.key,checked)
}
Text {
visible:optionRow.modelData.type==="integer" || optionRow.modelData.type==="number"
text:optionRow.value===undefined ? "" : String(optionRow.value)
color:Theme.text;font.pixelSize:11
Layout.preferredWidth:26;horizontalAlignment:Text.AlignRight
}
GlassSlider {
visible:optionRow.modelData.type==="integer" || optionRow.modelData.type==="number"
Layout.preferredWidth:90
implicitHeight:26
from:Number(optionRow.modelData.min !== undefined ? optionRow.modelData.min : 0)
to:Number(optionRow.modelData.max !== undefined ? optionRow.modelData.max : 100)
stepSize:Number(optionRow.modelData.step || (optionRow.modelData.type==="integer" ? 1 : 0.1))
value:Number(optionRow.value || 0)
onMoved:root.setOption(card.modelData,optionRow.modelData.key,optionRow.modelData.type==="integer" ? Math.round(value) : value)
}
GlassTextField {
visible:optionRow.modelData.type==="string"
Layout.preferredWidth:110
implicitHeight:30
text:optionRow.value===undefined ? "" : String(optionRow.value)
onEditingFinished:root.setOption(card.modelData,optionRow.modelData.key,text)
}
}
}
}
}
}
Rectangle {Layout.fillWidth:true;implicitHeight:3;radius:2;color:root.targetLane===lane.modelData && !root.beforeId ? Theme.accent : Theme.transparent}
Text {text:cards.count ? "Drop to append" : "Drop a module here";color:Theme.textSecondary;font.pixelSize:11;Layout.fillWidth:true;wrapMode:Text.Wrap}
}
}
}
}
GlassPill {
id:ghost
z:100
visible:!!root.dragging
width:140;height:44
color:Theme.surfaceVariant;border.color:Theme.accent
Text {anchors.centerIn:parent;text:root.dragging;color:Theme.text}
}
}