Files
glassMxrph/quickshell/wallpaper/WallpaperPicker.qml
T
mxandClaude Opus 5.5 ebace10767 Show wallpapers as a justified gallery in the picker drawer
Back to the drawer picker, now with justified rows: each thumbnail keeps
its aspect ratio (read from the file header by the scan) and every row
fills the width. Arrows move in two dimensions, click or Enter applies,
✦ marks outline wallpapers. Drops the full-screen preview picker.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 22:57:08 +01:00

243 lines
13 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.Controls
import QtQuick.Effects
import QtQuick.Layouts
import Quickshell
import Quickshell.Wayland
import "../components"
import "../services"
import "../core"
import "../theme"
// qmllint disable uncreatable-type
PanelWindow {
id:root
screen:ScreenService.current(ShellState.wallpaperScreen)
// Hangs from the middle of the bar as a drawer; the layer starts at the
// screen top so the bar join is inside its box.
anchors {top:true}
readonly property real screenWidth:screen ? screen.width : 1920
readonly property real screenHeight:screen ? screen.height : 1080
implicitWidth:Math.round(surface.width)
implicitHeight:Math.round(BarDrawers.bottom+surface.contentHeight)
visible:ShellState.wallpaperPickerOpen || surface.shown
color:Theme.transparent
exclusionMode:ExclusionMode.Ignore
WlrLayershell.namespace:"glassMxrph-wallpaper-picker"
WlrLayershell.layer:WlrLayer.Overlay
WlrLayershell.keyboardFocus:ShellState.wallpaperPickerOpen ? WlrKeyboardFocus.Exclusive : WlrKeyboardFocus.None
Component.onCompleted:ShellState.wallpaperWindow=root
Component.onDestruction:ShellState.wallpaperWindow=null
property string filter:"all"
readonly property var results:WallpaperService.images.filter(i=>(filter!=="history" || ConfigService.data.wallpaper.history.includes(i.path)) && (!search.text || i.name.toLowerCase().includes(search.text.toLowerCase())))
// Justified rows, as in a photo gallery: every thumbnail keeps its own
// aspect ratio (from the scan's header sizes) and each row is scaled so it
// fills the width exactly. The last row keeps the target height.
readonly property real rowTarget:170
readonly property real gap:10
readonly property var rows:layoutRows(results,view.width)
function aspectOf(image) {
const a=image.width>0 && image.height>0 ? image.width/image.height : 16/9;
return Math.max(0.5,Math.min(3,a)); // keep panoramas and slivers usable
}
function layoutRows(images,width) {
const out=[];
if(width<=0)return out;
let row=[],sum=0;
const close=height=>{
let x=0;
out.push({height:height,tiles:row.map(t=>{const w=t.aspect*height;const tile={index:t.index,x:x,width:w};x+=w+root.gap;return tile;})});
row=[];sum=0;
};
images.forEach((image,index)=>{
const aspect=aspectOf(image);
row.push({index:index,aspect:aspect});sum+=aspect;
if(sum*rowTarget+gap*(row.length-1)>=width)close((width-gap*(row.length-1))/sum);
});
if(row.length)close(rowTarget);
return out;
}
// Where each result sits: its row, and its horizontal centre for up/down.
readonly property var places:{
const p=[];
rows.forEach((row,r)=>row.tiles.forEach(t=>p[t.index]={row:r,center:t.x+t.width/2}));
return p;
}
property int current:0
onResultsChanged:if(current>=results.length)current=Math.max(0,results.length-1)
function select(index) {
if(index<0 || index>=results.length)return;
current=index;
if(places[index])view.positionViewAtIndex(places[index].row,ListView.Contain);
}
// Up/down: the tile in the next row whose centre is closest.
function vertical(direction) {
const here=places[current];
if(!here)return select(0);
const row=rows[here.row+direction];
if(!row)return;
let best=row.tiles[0];
for(const t of row.tiles)if(Math.abs(t.x+t.width/2-here.center)<Math.abs(best.x+best.width/2-here.center))best=t;
select(best.index);
}
function applyCurrent() {if(root.results[current])WallpaperService.apply(root.results[current].path);}
// For the test driver: the thumbnail item of a result, scrolled into view.
function tileAt(index) {
const place=places[index];
if(!place)return null;
view.positionViewAtIndex(place.row,ListView.Contain);
const row=view.itemAtIndex(place.row);
return row ? row.tileAt(index) : null;
}
Connections {
target:ShellState
function onWallpaperPickerOpenChanged() {
if(ShellState.wallpaperPickerOpen) {
WallpaperService.refresh();WallpaperService.scan();
const at=root.results.findIndex(i=>i.path===WallpaperService.currentPath);
if(at>=0)root.select(at);
Qt.callLater(()=>view.forceActiveFocus());
}
}
}
// Exclusive focus routes outside clicks here (see ControlCenter).
MouseArea {x:-20000;y:-20000;width:40000;height:40000;onPressed:ShellState.wallpaperPickerOpen=false}
BarDrawer {
id:surface
objectName:"wallpaperPickerSurface"
y:BarDrawers.bottom
open:ShellState.wallpaperPickerOpen
contentWidth:Math.min(1240,root.screenWidth-2*(BarDrawers.margin+BarDrawers.radius+ear))
contentHeight:Math.min(640,root.screenHeight-BarDrawers.bottom-40)
drawerId:"wallpaper"
screenName:root.screen ? root.screen.name : ""
screenX:(root.screenWidth-width)/2
MouseArea {anchors.fill:parent}
ColumnLayout {
anchors.fill:parent;anchors.margins:22;spacing:16
RowLayout {
Layout.fillWidth:true
Text {text:"Wallpapers";color:Theme.text;font.pixelSize:24;Layout.fillWidth:true}
GlassComboBox {
Layout.preferredWidth:160
model:["all","primary"].concat(Quickshell.screens.map(s=>s.name))
currentIndex:Math.max(0,model.indexOf(WallpaperService.selectedMonitor))
onActivated:WallpaperService.selectedMonitor=currentText
}
GlassIconButton {text:"⚙";onClicked:ShellState.openSettings("wallpaper");Accessible.name:"Wallpaper settings"}
GlassIconButton {text:"×";onClicked:ShellState.wallpaperPickerOpen=false;Accessible.name:"Close picker"}
}
RowLayout {
GlassButton {text:"All";accented:root.filter==="all";onClicked:root.filter="all"}
GlassButton {text:"History";accented:root.filter==="history";onClicked:root.filter="history"}
GlassTextField {id:search;objectName:"wallpaperSearch";Layout.fillWidth:true;placeholderText:"Search wallpapers…";onTextChanged:root.select(0);Keys.onEscapePressed:ShellState.wallpaperPickerOpen=false;Keys.onDownPressed:view.forceActiveFocus()}
GlassIconButton {text:"↻";onClicked:WallpaperService.scan();Accessible.name:"Refresh wallpapers"}
}
ListView {
id:view
objectName:"wallpaperGallery"
Layout.fillWidth:true;Layout.fillHeight:true
spacing:root.gap
clip:true
model:root.rows
boundsBehavior:Flickable.StopAtBounds
ScrollBar.vertical:ScrollBar {}
Keys.onLeftPressed:root.select(root.current-1)
Keys.onRightPressed:root.select(root.current+1)
Keys.onUpPressed:root.vertical(-1)
Keys.onDownPressed:root.vertical(1)
Keys.onReturnPressed:root.applyCurrent()
Keys.onEnterPressed:root.applyCurrent()
Keys.onEscapePressed:ShellState.wallpaperPickerOpen=false
delegate:Item {
id:rowItem
required property var modelData
width:view.width
height:modelData.height
function tileAt(index) {
for(let i=0;i<tiles.count;i++)if(tiles.itemAt(i).modelData.index===index)return tiles.itemAt(i);
return null;
}
Repeater {
id:tiles
model:rowItem.modelData.tiles
delegate:Item {
id:tile
required property var modelData
readonly property var image:root.results[modelData.index]
readonly property bool selected:root.current===modelData.index
x:modelData.x;width:modelData.width;height:rowItem.height
Item {
anchors.fill:parent
scale:hover.containsMouse && !tile.selected ? 1.02 : 1
Behavior on scale {NumberAnimation {duration:Theme.duration;easing.type:Easing.OutCubic}}
Image {
anchors.fill:parent
source:tile.image ? tile.image.url : ""
sourceSize:Qt.size(Math.round(tile.width*1.5),Math.round(tile.height*1.5))
asynchronous:true
fillMode:Image.PreserveAspectCrop
opacity:status===Image.Ready ? 1 : 0
Behavior on opacity {NumberAnimation {duration:Theme.duration}}
layer.enabled:true
layer.effect:MultiEffect {maskEnabled:true;maskSource:mask;maskThresholdMin:0.5;maskSpreadAtMin:1}
}
Item {
id:mask
anchors.fill:parent
visible:false
layer.enabled:true
Rectangle {anchors.fill:parent;radius:12}
}
Rectangle {
anchors.fill:parent;radius:12;color:"transparent"
border.width:tile.selected ? 3 : 1
border.color:tile.selected ? Theme.accent : Theme.border
}
// Name on the selected or hovered thumbnail only.
Rectangle {
anchors.left:parent.left;anchors.right:parent.right;anchors.bottom:parent.bottom;anchors.margins:3
height:30;radius:9
color:Qt.alpha(Theme.surface,0.78)
opacity:tile.selected || hover.containsMouse ? 1 : 0
Behavior on opacity {NumberAnimation {duration:Theme.duration}}
Text {
anchors.fill:parent;anchors.leftMargin:10;anchors.rightMargin:10
verticalAlignment:Text.AlignVCenter
text:tile.image ? tile.image.name : ""
color:Theme.text;font.pixelSize:12;elide:Text.ElideMiddle
}
}
// ✦ the subject's outline is drawn first · ✓ current wallpaper
Row {
anchors.top:parent.top;anchors.right:parent.right;anchors.margins:7;spacing:5
Repeater {
model:tile.image ? [WallpaperService.outlineStatus[tile.image.path]==="outline" ? "✦" : "",
tile.image.path===WallpaperService.currentPath ? "✓" : ""].filter(s=>s) : []
delegate:Rectangle {
required property string modelData
width:22;height:22;radius:11
color:Qt.alpha("black",0.55)
Text {anchors.centerIn:parent;text:parent.modelData;color:"white";font.pixelSize:12}
}
}
}
}
MouseArea {
id:hover
anchors.fill:parent
hoverEnabled:true
onClicked:{root.current=tile.modelData.index;view.forceActiveFocus();root.applyCurrent();}
}
}
}
}
}
Text {visible:!root.results.length;text:WallpaperService.scanning ? "Loading wallpapers…" : "No wallpapers here. Choose a folder in Wallpaper settings.";color:Theme.textSecondary;Layout.fillWidth:true;wrapMode:Text.Wrap}
Text {text:WallpaperService.error || (WallpaperService.busy?"Applying wallpaper…":"Click or arrows and Enter to apply · ✦ outline transition · Esc Close");color:WallpaperService.error?Theme.urgent:Theme.textSecondary;Layout.fillWidth:true;wrapMode:Text.Wrap}
}
}
}