Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 147 additions & 49 deletions src/share/sleex/modules/settings/Bluetooth.qml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import Quickshell.Bluetooth
import qs.services
import qs.modules.common
import qs.modules.common.widgets
import Sleex.Services

ContentPage {
forceWidth: true


function getDeviceIcon(modelData) {
const icon = modelData?.icon ?? "";

Expand All @@ -32,6 +32,29 @@ ContentPage {
}
}

Timer {
id: refreshTimer
interval: 8000
onTriggered: BluetoothService.refreshPairedDevices()
}
Comment on lines +35 to +39
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain the idea of using a timer plz ?


function unpairDevice(address) {
BluetoothService.unpairDevice(address);
refreshTimer.start();
}

function connectDevice(device) {
if (device.paired) {
BluetoothService.connectDevice(device.address);
} else {
BluetoothService.pairDevice(device.address);
}
}

function disconnectDevice(address) {
BluetoothService.disconnectDevice(address);
}


ContentSection {
title: "Bluetooth settings"
Expand All @@ -43,17 +66,16 @@ ContentPage {

ConfigSwitch {
text: "Enabled"
checked: Config.options.bar.showTitle
checked: BluetoothService.bluetoothEnabled
onClicked: checked = !checked;
onCheckedChanged: {
if (Bluetooth.defaultAdapter)
Bluetooth.defaultAdapter.enabled = checked;
BluetoothService.bluetoothEnabled = checked;
}
}

ConfigSwitch {
text: "Discoverable"
checked: Bluetooth.defaultAdapter.discoverable
checked: Bluetooth.defaultAdapter?.discoverable ?? false
onClicked: checked = !checked;
onCheckedChanged: {
if (Bluetooth.defaultAdapter)
Expand All @@ -68,7 +90,7 @@ ContentPage {

StyledText {
text: {
const devices = Bluetooth.devices.values;
const devices = BluetoothService.devices;
let available = qsTr("%1 device%2 available").arg(devices.length).arg(devices.length === 1 ? "" : "s");
const connected = devices.filter(d => d.connected).length;
if (connected > 0)
Expand All @@ -79,36 +101,70 @@ ContentPage {
font.pixelSize: Appearance.font.pixelSize.huge
}

RippleButton {
id: discoverBtn
RowLayout {
spacing: 10

RippleButton {
id: discoverBtn

visible: Bluetooth.adapters.values.length > 0
visible: Bluetooth.adapters.values.length > 0

contentItem: Rectangle {
id: discoverBtnBody
radius: Appearance.rounding.full
color: Bluetooth.defaultAdapter?.discovering ? Appearance.m3colors.m3primary : Appearance.colors.colLayer2
implicitWidth: height
contentItem: Rectangle {
id: discoverBtnBody
radius: Appearance.rounding.full
color: BluetoothService.discovering ? Appearance.m3colors.m3primary : Appearance.colors.colLayer2
implicitWidth: height

MaterialSymbol {
id: scanIcon
MaterialSymbol {
id: scanIcon

anchors.centerIn: parent
text: "bluetooth_searching"
color: Bluetooth.defaultAdapter?.discovering ? Appearance.m3colors.m3onSecondary : Appearance.m3colors.m3onSecondaryContainer
fill: Bluetooth.defaultAdapter?.discovering ? 1 : 0
anchors.centerIn: parent
text: "bluetooth_searching"
color: BluetoothService.discovering ? Appearance.m3colors.m3onSecondary : Appearance.m3colors.m3onSecondaryContainer
fill: BluetoothService.discovering ? 1 : 0
}
}

MouseArea {
id: discoverArea
anchors.fill: parent
hoverEnabled: true
onClicked: {
BluetoothService.discovering = !BluetoothService.discovering;
}

StyledToolTip {
extraVisibleCondition: discoverArea.containsMouse
text: "Discover new devices"
}
}
}

MouseArea {
id: discoverArea
anchors.fill: parent
hoverEnabled: true
onClicked: Bluetooth.defaultAdapter.discovering = !Bluetooth.defaultAdapter.discovering
RippleButton {
id: refreshBtn
visible: Bluetooth.adapters.values.length > 0
width: 40
height: 40

StyledToolTip {
extraVisibleCondition: discoverArea.containsMouse
text: "Discover new devices"
contentItem: MaterialSymbol {
anchors.centerIn: parent
text: "refresh"
color: Appearance.colors.colOnLayer2
}

MouseArea {
id: refreshArea
anchors.fill: parent
hoverEnabled: true
onClicked: {
BluetoothService.refreshPairedDevices();
refreshTimer.restart();
}

StyledToolTip {
extraVisibleCondition: refreshArea.containsMouse
text: "Refresh device list"
}
}
}
}
Expand All @@ -135,7 +191,12 @@ ContentPage {
Repeater {
model: ScriptModel {
values: {
let devices = [...Bluetooth.devices.values].sort((a, b) => (b.connected - a.connected) || (b.bonded - a.bonded));
// Only show devices if Bluetooth is enabled
if (!BluetoothService.bluetoothEnabled) {
return [];
}
let devices = [...BluetoothService.devices].sort((a, b) => (b.connected - a.connected) || (b.paired - a.paired));

if (deviceSearch.text.trim() !== "") {
devices = devices.filter(d => d.name.toLowerCase().includes(deviceSearch.text.toLowerCase()) || d.address.toLowerCase().includes(deviceSearch.text.toLowerCase()));
}
Expand All @@ -146,8 +207,8 @@ ContentPage {
RowLayout {
id: device

required property BluetoothDevice modelData
readonly property bool loading: modelData.state === BluetoothDeviceState.Connecting || modelData.state === BluetoothDeviceState.Disconnecting
required property var modelData
readonly property bool loading: false // Placeholder if needed

Layout.fillWidth: true
spacing: 10
Expand All @@ -169,7 +230,7 @@ ContentPage {

MaterialSymbol {
anchors.centerIn: parent
text: getDeviceIcon(device.modelData)
text: device.modelData.icon
font.pixelSize: Appearance.font.pixelSize.title
color: Appearance.colors.colOnSecondaryContainer
}
Expand All @@ -185,32 +246,69 @@ ContentPage {
color: Appearance.colors.colOnSecondaryContainer
}
StyledText {
text: device.modelData.address + (device.modelData.connected ? qsTr(" (Connected)") : device.modelData.bonded ? qsTr(" (Paired)") : "")
text: device.modelData.address + (device.modelData.connected ? qsTr(" (Connected)") : (device.modelData.paired ? qsTr(" (Paired)") : qsTr(" (Available)")))
font.pixelSize: Appearance.font.pixelSize.small
color: Appearance.colors.colSubtext
}
}

RippleButton {
id: forgetButton
visible: device.modelData.paired

width: 40
height: 40
buttonRadius: Appearance.rounding.normal
colBackground: "transparent"
colBackgroundHover: Appearance.colors.colSurfaceContainerHigh
hoverEnabled: true

property bool processing: false

contentItem: MaterialSymbol {
anchors.centerIn: parent
text: forgetButton.processing ? "hourglass_empty" : "link_off"
font.pixelSize: Appearance.font.pixelSize.normal
color: forgetButton.hovered ? Appearance.colors.colOnSurface : Appearance.colors.colOnSurfaceVariant
}

onClicked: {
if (forgetButton.processing) return;

forgetButton.processing = true;
unpairDevice(device.modelData.address);

// Reset processing state after a delay
Qt.callLater(() => {
processingTimer.start();
});
}

Timer {
id: processingTimer
interval: 5000
onTriggered: forgetButton.processing = false
}

StyledToolTip {
extraVisibleCondition: forgetButton.hovered
text: forgetButton.processing ? "Removing device..." : "Forget device"
}
}

StyledSwitch {
scale: 0.80
Layout.fillWidth: false
checked: device.modelData.connected
onClicked: device.modelData.connected = !device.modelData.connected
}
}
}


Loader {
asynchronous: true
active: device.modelData.bonded
sourceComponent: Item {
implicitWidth: connectBtn.implicitWidth
implicitHeight: connectBtn.implicitHeight

MaterialSymbol {
anchors.centerIn: parent
text: "delete"
onClicked: {
if (checked) {
connectDevice(device.modelData);
} else {
disconnectDevice(device.modelData.address);
}
// Restore binding to ensure switch reflects actual state
checked = Qt.binding(function() { return device.modelData.connected; });
}
}
}
}
Expand Down
Loading