victronenergy / gui-v2

Other
24 stars 9 forks source link

Problems with showAccessLevel and writeAccessLevel #1404

Closed mr-manuel closed 1 month ago

mr-manuel commented 1 month ago

Change the access level to User.

ListSwitch

The user can modify the switch, but it should be read-only by setting writeAccessLevel.

ListSwitch {
    id: switchTailscaleEnabled
    //% "Enable Tailscale"
    text: qsTrId("settings_tailscale_enable")
    dataItem.uid: Global.systemSettings.serviceUid + "/Settings/Services/Tailscale/Enabled"
    writeAccessLevel: VenusOS.User_AccessType_Installer
    enabled: isRunning
}

The showAccessLevel is working correctly.

ListTextField

The user can modify the text box, but it should be read-only by setting writeAccessLevel.

ListTextField {
    //% "Hostname"
    text: qsTrId("settings_tailscale_hostname")
    dataItem.uid: Global.systemSettings.serviceUid + "/Settings/Services/Tailscale/Hostname"
    placeholderText: "--"
    enabled: !isEnabled
    writeAccessLevel: VenusOS.User_AccessType_Installer
}

The showAccessLevel is working correctly.

ListButton

The button is shown, but it should not be shown by setting showAccessLevel.

ListButton {
    //% "Logout from Tailscale account"
    text: qsTrId("settings_tailscale_logout_from_tailscale_account")
    //% "Logout"
    button.text: qsTrId("settings_tailscale_logout")
    allowed: isConnected
    showAccessLevel: VenusOS.User_AccessType_Installer
    onClicked: commandItem.setValue ('logout')
}

The writeAccessLevel is hiding the element. It seems that showAccessLevel and writeAccessLevel are swapped.

mr-manuel commented 1 month ago

Can be tested with https://github.com/victronenergy/gui-v2/commit/8510285ff4dad6a484fdca35df217168bf5b6f04.

I solved it now in another way. Can be closed, if this is not relevant.

blammit commented 1 month ago

@mr-manuel The problem is that when you set enabled, it overwrites the binding that controls whether the control is read-only. So when you set enabled, include userHasWriteAccess in the binding, like this:

ListSwitch {
    writeAccessLevel: VenusOS.User_AccessType_Installer
    enabled: isRunning && userHasWriteAccess
}

Similarly, when you set allowed, it overwrites the binding that controls whether the button is visible. Do this instead:

ListButton {
    allowed: defaultAllowed && isConnected
    showAccessLevel: VenusOS.User_AccessType_Installer
}

I can see this is confusing, and ideally, it wouldn't be possible to overwrite these enabled and allowed so easily and lose the default behavior. But, this is the API we have for now, and it can be improved later on.

blammit commented 1 month ago

As discussed, this is not a bug, just an issue with how the enabled/allowed state can be overwritten in the API. Closing.

mr-manuel commented 1 month ago

I was just documenting this, but you were faster. Thanks.