nix/files/sddm-i3-theme/Main.qml
2024-08-01 23:14:45 +02:00

362 lines
8.3 KiB
QML

import QtQuick 2.15
import QtQuick.Controls 2.0
import SddmComponents 2.0
import "components"
Rectangle {
id : gRoot
property color cTextNormal : "#ffffff"
property color cTextFailure : "#f43841"
property color cTextSuccess : "#00ff00"
property color cBackgroundRoot : "#000000"
property color cBackgroundNormal : "#333333"
property color cBackgroundActive : "#285577"
property color cBorderNormal : "#222222"
property color cBorderActive : "#4c7899"
property string fontFamily : "monospace"
property int fontSize : 16
property string defaultUser : ""
property int panelSize : 420
property int offset : 96
color: cBackgroundRoot
property int activeInput : initInputs()
property int currentSessionsIndex : sessionModel.lastIndex
property string currentSession: sessionModel.data(sessionModel.index(currentSessionsIndex, 0), Qt.UserRole + 4)
function xCenter(sz) {
return gRoot.width / 2 - sz / 2;
}
function yCenter(sz) {
return gRoot.height / 2 - sz / 2;
}
function initInputs() {
if(defaultUser != "") {
gPasswordInput.forceActiveFocus();
return 1;
} else {
gLoginInput.forceActiveFocus();
return 0;
}
}
function initInfoText() {
var text = "";
if(sddm.canPowerOff) {
text += "f1 shutdown"
}
if(sddm.canReboot) {
if(text != "") {
text += " "
}
text += "f2 reboot"
}
return text;
}
function sessionsCycleSelectPrev() {
if(timer.running) {
return;
}
if (currentSessionsIndex - 1 < 0) {
currentSessionsIndex = sessionModel.rowCount() - 1;
} else {
currentSessionsIndex--;
}
}
function sessionsCycleSelectNext() {
if(timer.running) {
return;
}
if (currentSessionsIndex >= sessionModel.rowCount() - 1) {
currentSessionsIndex = 0;
} else {
currentSessionsIndex++;
}
}
function focusNext() {
if(timer.running) {
return;
}
if(gRoot.activeInput == 0) {
gPasswordInput.forceActiveFocus();
gRoot.activeInput = 1;
} else if(gRoot.activeInput == -1) {
gLoginInput.forceActiveFocus();
gRoot.activeInput = 0;
}
}
function focusPrev() {
if(timer.running) {
return;
}
if(gRoot.activeInput == 1) {
gLoginInput.forceActiveFocus();
gRoot.activeInput = 0;
} else if(gRoot.activeInput == 0) {
gSessionBox.forceActiveFocus();
gRoot.activeInput = -1;
}
}
function setInputLock(lock) {
gLoginInput.readOnly = lock;
gPasswordInput.readOnly = lock;
gLoginInput.enabled = !lock;
gPasswordInput.enabled = !lock;
gSessionBox.enabled = !lock;
}
Timer {
id: timer
interval: 750
repeat: true
running: false
onTriggered: {
gInfoLabel.text += ".";
}
}
function tryLogin() {
gInfoLabel.color = cTextNormal;
gInfoLabel.text = ".";
timer.start();
setInputLock(true);
sddm.login(gLoginInput.text, gPasswordInput.text, currentSessionsIndex);
}
Connections {
target: sddm
function onLoginSucceeded() {
timer.stop();
gInfoLabel.color = cTextSuccess
gInfoLabel.text = "success"
}
function onLoginFailed() {
timer.stop();
gInfoLabel.color = cTextFailure
gInfoLabel.text = "auth failure"
setInputLock(false);
}
}
Shortcut {
sequence: "Down"
onActivated: focusNext();
}
Shortcut {
sequence: "Up"
onActivated: focusPrev();
}
Shortcut {
sequence: "F1"
onActivated: {
sddm.powerOff();
}
}
Shortcut {
sequence: "F2"
onActivated: {
sddm.reboot();
}
}
Text {
x: xCenter(panelSize)
y: yCenter(panelSize)
width: panelSize
height: 2 * fontSize
horizontalAlignment: Qt.AlignHCenter
verticalAlignment: Qt.AlignVCenter
font.pixelSize: fontSize
font.family: fontFamily
color: cTextNormal
text: sddm.hostName;
}
Text {
x: 0
y: gRoot.height - 2 * fontSize
width: gRoot.width
height: 2 * fontSize
horizontalAlignment: Qt.AlignHCenter
verticalAlignment: Qt.AlignVCenter
font.pixelSize: fontSize
font.family: fontFamily
color: cTextNormal
text: initInfoText();
}
Rectangle {
x: xCenter(panelSize)
y: yCenter(panelSize) + 3 * fontSize
Text {
width: panelSize
height: 2 * fontSize
horizontalAlignment: Qt.AlignHLeft
verticalAlignment: Qt.AlignVCenter
font.pixelSize: fontSize
font.family: fontFamily
color: cTextNormal
text: "session:"
}
SessionSelect {
id: gSessionBox
x: offset
width: panelSize - offset
height: fontSize * 2
text: currentSession
onPrevClicked: sessionsCycleSelectPrev();
onNextClicked: sessionsCycleSelectNext();
KeyNavigation.tab : gLoginInput
Keys.onPressed: function (event) {
if(event.key == Qt.Key_Return || event.key == Qt.Key_Enter) {
focusNext();
event.accepted = true;
}
}
}
}
Rectangle {
x: xCenter(panelSize)
y: yCenter(panelSize) + 2 * 3 * fontSize
Text {
width: panelSize
height: 2 * fontSize
horizontalAlignment: Qt.AlignHLeft
verticalAlignment: Qt.AlignVCenter
font.pixelSize: fontSize
font.family: fontFamily
color: cTextNormal
text: "login:"
}
TextBoxCustom {
id: gLoginInput
x: offset
width: panelSize - offset
height: 2 * fontSize
font.pixelSize: fontSize
font.family: fontFamily
cText: cTextNormal
// TODO set the remaining colors
text: defaultUser
KeyNavigation.tab : gPasswordInput
KeyNavigation.backtab : gSessionBox
Keys.onPressed: function (event) {
if(event.key == Qt.Key_Return || event.key == Qt.Key_Enter) {
focusNext();
event.accepted = true;
}
}
}
}
Rectangle {
x: xCenter(panelSize)
y: yCenter(panelSize) + 3 * 3 * fontSize
Text {
width: panelSize
height: 2 * fontSize
horizontalAlignment: Qt.AlignHLeft
verticalAlignment: Qt.AlignVCenter
font.pixelSize: fontSize
font.family: fontFamily
color: cTextNormal
text: "password:"
}
TextBoxCustom {
id: gPasswordInput
x: offset
width: panelSize - offset
height: 2 * fontSize
font.pixelSize: fontSize
font.family: fontFamily
cText: cTextNormal
// todo set the remaining colors here
echoMode: TextInput.Password
Keys.onPressed: function(event) {
if(event.key == Qt.Key_Return || event.key == Qt.Key_Enter) {
tryLogin();
event.accepted = true;
}
}
}
}
Text {
id: gInfoLabel
text: ""
x: xCenter(panelSize)
y: yCenter(panelSize) + 4 * 3 * fontSize
width: panelSize
height: 2 * fontSize
horizontalAlignment: Qt.AlignHCenter
verticalAlignment: Qt.AlignVCenter
color: cTextNormal
}
}