From f238b9d8eec39a58abcd50e81a76e87d13266ef9 Mon Sep 17 00:00:00 2001
From: agsantos <aline.gondimsantos@savoirfairelinux.com>
Date: Wed, 17 Nov 2021 19:11:18 -0500
Subject: [PATCH] x11: window sharing

GitLab: https://git.jami.net/savoirfairelinux/jami-project/-/issues/1294

Change-Id: I6dfaea6c65323d072536518f3ac7abc426d33efd
---
 src/avadapter.cpp                             |  54 +++
 src/avadapter.h                               |  10 +
 src/constant/JamiStrings.qml                  |   3 +
 src/constant/JamiTheme.qml                    |   2 +-
 src/mainview/components/CallActionBar.qml     |   7 +-
 src/mainview/components/CallOverlay.qml       |   5 +-
 .../components/CallViewContextMenu.qml        |   5 +-
 src/mainview/components/SelectScreen.qml      | 406 +++++++++---------
 src/mainview/js/selectscreenwindowcreation.js |   4 +-
 src/videodevices.cpp                          |   2 -
 10 files changed, 289 insertions(+), 209 deletions(-)

diff --git a/src/avadapter.cpp b/src/avadapter.cpp
index 9568fdba0..f0d5a430e 100644
--- a/src/avadapter.cpp
+++ b/src/avadapter.cpp
@@ -239,6 +239,60 @@ AvAdapter::shareScreenArea(unsigned x, unsigned y, unsigned width, unsigned heig
 #endif
 }
 
+void
+AvAdapter::shareWindow(const QString& windowId)
+{
+    auto resource = lrcInstance_->getCurrentCallModel()->getDisplay(windowId);
+    auto callId = lrcInstance_->getCurrentCallId();
+    lrcInstance_->getCurrentCallModel()
+        ->requestMediaChange(callId,
+                             "video_0",
+                             resource,
+                             lrc::api::NewCallModel::MediaRequestType::SCREENSHARING,
+                             false);
+    set_currentRenderingDeviceType(
+        lrcInstance_->getCurrentCallModel()->getCurrentRenderedDevice(callId).type);
+}
+
+QString
+AvAdapter::getSharingResource(int screenId = -2, const QString& windowId = "")
+{
+    if (screenId == -1) {
+        const auto arrangementRect = getAllScreensBoundingRect();
+
+        return lrcInstance_->getCurrentCallModel()->getDisplay(getScreenNumber(),
+                                                               arrangementRect.x(),
+                                                               arrangementRect.y(),
+                                                               arrangementRect.width(),
+                                                               arrangementRect.height());
+    } else if (screenId > -1) {
+        QScreen* screen = QGuiApplication::screens().at(screenId);
+        if (!screen)
+            return "";
+        QRect rect = screen->geometry();
+
+        return lrcInstance_->getCurrentCallModel()->getDisplay(getScreenNumber(),
+                                                               rect.x(),
+                                                               rect.y(),
+                                                               rect.width()
+                                                                   * screen->devicePixelRatio(),
+                                                               rect.height()
+                                                                   * screen->devicePixelRatio());
+    } else if (!windowId.isEmpty()) {
+        return lrcInstance_->getCurrentCallModel()->getDisplay(windowId);
+    }
+
+    return "";
+}
+
+void
+AvAdapter::getListWindows()
+{
+    auto map = lrcInstance_->avModel().getListWindows();
+    set_windowsNames(map.keys());
+    set_windowsIds(map.values());
+}
+
 void
 AvAdapter::stopSharing()
 {
diff --git a/src/avadapter.h b/src/avadapter.h
index 68e3a893d..ecf1b0abf 100644
--- a/src/avadapter.h
+++ b/src/avadapter.h
@@ -31,6 +31,8 @@ class AvAdapter final : public QmlAdapterBase
     Q_OBJECT
     QML_RO_PROPERTY(lrc::api::video::DeviceType, currentRenderingDeviceType)
     QML_PROPERTY(bool, muteCamera)
+    QML_RO_PROPERTY(QStringList, windowsNames)
+    QML_RO_PROPERTY(QList<QVariant>, windowsIds)
 
 public:
     explicit AvAdapter(LRCInstance* instance, QObject* parent = nullptr);
@@ -62,6 +64,14 @@ protected:
     // Select screen area to display (from all screens).
     Q_INVOKABLE void shareScreenArea(unsigned x, unsigned y, unsigned width, unsigned height);
 
+    // Select window to display.
+    Q_INVOKABLE void shareWindow(const QString& windowId);
+
+    // Returns the screensharing resource
+    Q_INVOKABLE QString getSharingResource(int screenId, const QString& key);
+
+    Q_INVOKABLE void getListWindows();
+
     // Stop sharing the screen or file
     Q_INVOKABLE void stopSharing();
 
diff --git a/src/constant/JamiStrings.qml b/src/constant/JamiStrings.qml
index 503c4c360..2a8f07910 100644
--- a/src/constant/JamiStrings.qml
+++ b/src/constant/JamiStrings.qml
@@ -495,6 +495,9 @@ Item {
 
     // SelectScreen
     property string selectScreen: qsTr("Select a screen to share")
+    property string allScreens: qsTr("All Screens")
+    property string screens: qsTr("Screens")
+    property string windows: qsTr("Windows")
 
     // UserProfile
     property string qrCode: qsTr("QR code")
diff --git a/src/constant/JamiTheme.qml b/src/constant/JamiTheme.qml
index 74e28bea3..7e847d064 100644
--- a/src/constant/JamiTheme.qml
+++ b/src/constant/JamiTheme.qml
@@ -132,7 +132,7 @@ Item {
     property color recordIconColor: "#dc2719"
     property color successLabelColor: "#2b5084"
     property color rubberBandSelectionBlue: "steelblue"
-    property color screenSelectionBorderGreen: "green"
+    property color screenSelectionBorderColor: raiseHandColor
     property color separationLine: darkTheme ? selectedColor : backgroundColor
 
     // Plugin Preferences View
diff --git a/src/mainview/components/CallActionBar.qml b/src/mainview/components/CallActionBar.qml
index 62d8981bb..c87b4ef38 100644
--- a/src/mainview/components/CallActionBar.qml
+++ b/src/mainview/components/CallActionBar.qml
@@ -184,12 +184,7 @@ Control {
         },
         Action {
             id: muteVideoAction
-            onTriggered: {
-                if (AvAdapter.currentRenderingDeviceType === Video.DeviceType.DISPLAY || AvAdapter.currentRenderingDeviceType === Video.DeviceType.FILE) {
-                    AvAdapter.stopSharing()
-                } else
-                    CallAdapter.videoPauseThisCallToggle(!isVideoMuted)
-            }
+            onTriggered: CallAdapter.videoPauseThisCallToggle(!isVideoMuted)
             checkable: true
             icon.source: checked ?
                              JamiResources.videocam_off_24dp_svg :
diff --git a/src/mainview/components/CallOverlay.qml b/src/mainview/components/CallOverlay.qml
index f946c6a73..1e48f3a7c 100644
--- a/src/mainview/components/CallOverlay.qml
+++ b/src/mainview/components/CallOverlay.qml
@@ -169,11 +169,12 @@ Item {
         if (AvAdapter.currentRenderingDeviceType !== Video.DeviceType.DISPLAY && AvAdapter.currentRenderingDeviceType !== Video.DeviceType.FILE) {
             AvAdapter.muteCamera = root.isVideoMuted
         }
-        if (Qt.application.screens.length === 1) {
+        AvAdapter.getListWindows()
+        if (Qt.application.screens.length + AvAdapter.windowsNames.length === 1) {
             AvAdapter.shareEntireScreen(0)
         } else {
             SelectScreenWindowCreation.createSelectScreenWindowObject()
-            SelectScreenWindowCreation.showSelectScreenWindow()
+            SelectScreenWindowCreation.showSelectScreenWindow(callPreviewId)
         }
     }
 
diff --git a/src/mainview/components/CallViewContextMenu.qml b/src/mainview/components/CallViewContextMenu.qml
index 69183eb82..e43c8a31f 100644
--- a/src/mainview/components/CallViewContextMenu.qml
+++ b/src/mainview/components/CallViewContextMenu.qml
@@ -119,11 +119,12 @@ ContextMenuAutoLoader {
                 if (AvAdapter.currentRenderingDeviceType !== Video.DeviceType.DISPLAY && AvAdapter.currentRenderingDeviceType !== Video.DeviceType.FILE) {
                     AvAdapter.muteCamera = root.isVideoMuted
                 }
-                if (Qt.application.screens.length === 1) {
+                AvAdapter.getListWindows()
+                if (Qt.application.screens.length + AvAdapter.windowsNames().length === 1) {
                     AvAdapter.shareEntireScreen(0)
                 } else {
                     SelectScreenWindowCreation.createSelectScreenWindowObject()
-                    SelectScreenWindowCreation.showSelectScreenWindow()
+                    SelectScreenWindowCreation.showSelectScreenWindow(callPreviewId)
                 }
             }
         },
diff --git a/src/mainview/components/SelectScreen.qml b/src/mainview/components/SelectScreen.qml
index 3b96f4db2..2e4b26273 100644
--- a/src/mainview/components/SelectScreen.qml
+++ b/src/mainview/components/SelectScreen.qml
@@ -1,6 +1,7 @@
 /*
  * Copyright (C) 2020 by Savoir-faire Linux
  * Author: Mingrui Zhang <mingrui.zhang@savoirfairelinux.com>
+ *         Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -31,25 +32,42 @@ import "../../commoncomponents"
 // is to make user aware of which screen they want to share,
 // during the video call, if the context menu item is selected.
 Window {
-    id: selectScreenWindow
+    id: root
 
     property int minWidth: 650
     property int minHeight: 500
 
     property int selectedScreenNumber: -1
     property bool selectAllScreens: false
+    property string currentPreview: ""
+    property var screens: []
 
     // How many rows the ScrollView should have.
     function calculateRepeaterModel() {
-        var numberOfScreens = Qt.application.screens.length
+        screens = []
+        for (var idx in Qt.application.screens) {
+            screens.push(qsTr("Screen") + " " + idx)
+        }
+        AvAdapter.getListWindows()
+        for (var idx in AvAdapter.windowsNames) {
+            screens.push(AvAdapter.windowsNames[idx])
+        } 
 
-        return Math.ceil(numberOfScreens / 2)
+        return screens.length
     }
 
-    function calculateScreenNumber(index, isEven) {
-        return index * 2 + (isEven ? 2 : 1)
+    onActiveChanged: {
+        if (!active) {
+            selectedScreenNumber = -1
+            selectAllScreens = false
+        }
+        screenInfo.model = {}
+        screenInfo2.model = {}
+        calculateRepeaterModel()
+        screenInfo.model = screens.length
+        screenInfo2.model = screens.length
+        windowsText.visible = screens.length > Qt.application.screens.length
     }
-
     minimumWidth: minWidth
     minimumHeight: minHeight
 
@@ -60,6 +78,8 @@ Window {
 
     modality: Qt.ApplicationModal
 
+    title: JamiStrings.selectScreen
+
     Rectangle {
         id: selectScreenWindowRect
 
@@ -67,204 +87,116 @@ Window {
 
         color: JamiTheme.backgroundColor
 
-        Text {
-            id: screenListText
-
-            anchors.top: selectScreenWindowRect.top
-            anchors.topMargin: JamiTheme.preferredMarginSize
-            anchors.horizontalCenter: selectScreenWindowRect.horizontalCenter
-
-            font.pointSize: JamiTheme.textFontSize + 2
-            font.bold: true
-            text: JamiStrings.selectScreen
-            color: JamiTheme.textColor
-        }
-
         ScrollView {
             id: screenSelectionScrollView
 
-            anchors.top: screenListText.bottom
             anchors.topMargin: JamiTheme.preferredMarginSize
             anchors.horizontalCenter: selectScreenWindowRect.horizontalCenter
 
             width: selectScreenWindowRect.width
             height: selectScreenWindowRect.height -
-                    (screenListText.height + selectButton.height + JamiTheme.preferredMarginSize * 4)
+                    (selectButton.height + JamiTheme.preferredMarginSize * 4)
 
             clip: true
 
             ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
             ScrollBar.vertical.policy: ScrollBar.AlwaysOn
 
-            // Column of rows repeater (two screen captures in a row).
-            Column {
-                id: screenSelectionScrollViewColumn
-
-                spacing: 10
-
-                Repeater {
-                    id: screenInfo
-
-                    model: calculateRepeaterModel()
-
-                    Row {
-                        id: screenInfoRow
-
-                        leftPadding: JamiTheme.preferredMarginSize
-                        rightPadding: JamiTheme.preferredMarginSize
-                        spacing: screenSelectionScrollViewColumn.spacing
-
-                        Connections {
-                            target: selectScreenWindow
-
-                            function onSelectedScreenNumberChanged() {
-                                // Recover from green state.
-                                screenSelectionRectOdd.borderColor = JamiTheme.tabbarBorderColor
-                                screenSelectionRectEven.borderColor = JamiTheme.tabbarBorderColor
-                            }
-                        }
-
-                        Connections {
-                            target: AvAdapter
+            Flow {
+                id: screenSelectionScrollViewFlow
 
-                            function onScreenCaptured(screenNumber, source) {
-                                if (screenNumber === -1)
-                                    screenShotAll.source = JamiQmlUtils.base64StringTitle + source
-                                if (screenNumber !== index && screenNumber !== index + 1)
-                                    return
-                                if (screenNumber % 2 !== 1)
-                                    screenShotOdd.source = JamiQmlUtils.base64StringTitle + source
-                                else
-                                    screenShotEven.source = JamiQmlUtils.base64StringTitle + source
-                            }
-                        }
+                anchors.fill: parent
+                topPadding: JamiTheme.preferredMarginSize
+                rightPadding: JamiTheme.preferredMarginSize
+                leftPadding: JamiTheme.preferredMarginSize
 
-                        // To make sure that two screen captures in one row,
-                        // a repeater of two rect is needed, which one in charge
-                        // of odd number screen, one in charge of even number screen.
-                        Rectangle {
-                            id: screenSelectionRectOdd
+                spacing: 10
 
-                            property string borderColor: JamiTheme.tabbarBorderColor
+                Text {
+                    width: screenSelectionScrollView.width
+                    height: JamiTheme.preferredFieldHeight
 
-                            color: JamiTheme.secondaryBackgroundColor
+                    font.pointSize: JamiTheme.menuFontSize
+                    font.bold: true
+                    text: JamiStrings.screens
+                    verticalAlignment: Text.AlignBottom
+                    color: JamiTheme.textColor
+                }
 
-                            height: screenSelectionScrollView.height
-                            width: screenSelectionScrollView.width / 2 -
-                                   screenInfoRow.spacing / 2 - JamiTheme.preferredMarginSize
+                Repeater {
+                    id: screenInfo
 
-                            border.color: borderColor
+                    model: screens ? screens.length : 0
 
-                            Image {
-                                id: screenShotOdd
+                    delegate: Rectangle {
+                        id: screenItem
 
-                                anchors.top: screenSelectionRectOdd.top
-                                anchors.topMargin: 10
-                                anchors.horizontalCenter: screenSelectionRectOdd.horizontalCenter
+                        color: JamiTheme.secondaryBackgroundColor
 
-                                height: screenSelectionRectOdd.height - 50
-                                width: screenSelectionRectOdd.width - 50
+                        width: screenSelectionScrollView.width / 2 -
+                                screenSelectionScrollViewFlow.spacing / 2 - JamiTheme.preferredMarginSize
+                        height: 3 * width / 4
 
-                                fillMode: Image.PreserveAspectFit
-                                mipmap: true
+                        border.color: selectedScreenNumber === index ? JamiTheme.screenSelectionBorderColor : JamiTheme.tabbarBorderColor
+                        visible: JamiStrings.selectScreen !== screens[index] && index < Qt.application.screens.length
 
-                                Component.onCompleted: AvAdapter.captureScreen(
-                                                           calculateScreenNumber(index, false) - 1)
-                            }
+                        PreviewRenderer {
+                            id: screenPreview
 
-                            Text {
-                                id: screenNameOdd
+                            anchors.top: screenItem.top
+                            anchors.topMargin: 10
+                            anchors.horizontalCenter: screenItem.horizontalCenter
+                            height: screenItem.height - 50
+                            width: screenItem.width - 50
 
-                                anchors.top: screenShotOdd.bottom
-                                anchors.topMargin: 10
-                                anchors.horizontalCenter: screenSelectionRectOdd.horizontalCenter
+                            lrcInstance: LRCInstance
 
-                                font.pointSize: JamiTheme.textFontSize - 2
-                                text: qsTr("Screen") + " " + calculateScreenNumber(index, false)
-                                color: JamiTheme.textColor
+                            Component.onDestruction: {
+                                if (screenPreview.rendererId !== "" && screenPreview.rendererId !== currentPreview)
+                                    VideoDevices.stopDevice(screenPreview.rendererId, true)
                             }
-
-                            MouseArea {
-                                anchors.fill: parent
-                                acceptedButtons: Qt.LeftButton
-
-                                onClicked: {
-                                    if (selectedScreenNumber == -1
-                                            || selectedScreenNumber !==
-                                            calculateScreenNumber(index, false)) {
-                                        selectedScreenNumber = calculateScreenNumber(index, false)
-                                        screenSelectionRectOdd.borderColor
-                                                = JamiTheme.screenSelectionBorderGreen
-                                    }
+                            Component.onCompleted: {
+                                if (visible) {
+                                    var rendId = AvAdapter.getSharingResource(index, "")
+                                    if (rendId !== "")
+                                        screenPreview.rendererId = VideoDevices.startDevice(rendId, true)
                                 }
                             }
                         }
 
-                        Rectangle {
-                            id: screenSelectionRectEven
-
-                            property string borderColor: JamiTheme.tabbarBorderColor
-
-                            color: JamiTheme.secondaryBackgroundColor
-
-                            height: screenSelectionScrollView.height
-                            width: screenSelectionScrollView.width / 2 -
-                                   screenInfoRow.spacing / 2 - JamiTheme.preferredMarginSize
-
-                            border.color: borderColor
-
-                            visible: {
-                                if (calculateScreenNumber(index, true) >=
-                                        Qt.application.screens.length)
-                                    return (Qt.application.screens.length) % 2 != 1
-                                return true
-                            }
-
-                            Image {
-                                id: screenShotEven
-
-                                anchors.top: screenSelectionRectEven.top
-                                anchors.topMargin: 10
-                                anchors.horizontalCenter: screenSelectionRectEven.horizontalCenter
-
-                                height: screenSelectionRectEven.height - 50
-                                width: screenSelectionRectEven.width - 50
+                        Text {
+                            id: screenName
+
+                            anchors.top: screenPreview.bottom
+                            anchors.topMargin: 10
+                            anchors.horizontalCenter: screenItem.horizontalCenter
+                            width: parent.width
+                            font.pointSize: JamiTheme.textFontSize
+                            text: screens[index] ? screens[index] : ""
+                            elide: Text.ElideMiddle
+                            horizontalAlignment: Text.AlignHCenter
+                            color: JamiTheme.textColor
+                        }
 
-                                fillMode: Image.PreserveAspectFit
-                                mipmap: true
+                        MouseArea {
+                            anchors.fill: screenItem
+                            acceptedButtons: Qt.LeftButton
 
-                                Component.onCompleted: {
-                                    if (screenSelectionRectEven.visible)
-                                        AvAdapter.captureScreen(
-                                                    calculateScreenNumber(index, true) - 1)
+                            onClicked: {
+                                selectAllScreens = false
+                                if (selectedScreenNumber == -1
+                                        || selectedScreenNumber !== index) {
+                                    selectedScreenNumber = index
                                 }
                             }
+                        }
 
-                            Text {
-                                id: screenNameEven
-
-                                anchors.top: screenShotEven.bottom
-                                anchors.topMargin: 10
-                                anchors.horizontalCenter: screenSelectionRectEven.horizontalCenter
-
-                                font.pointSize: JamiTheme.textFontSize - 2
-                                text: qsTr("Screen") + " " + (calculateScreenNumber(index, true))
-                                color: JamiTheme.textColor
-                            }
+                        Connections {
+                            target: AvAdapter
 
-                            MouseArea {
-                                anchors.fill: parent
-                                acceptedButtons: Qt.LeftButton
-
-                                onClicked: {
-                                    if (selectedScreenNumber == -1
-                                            || selectedScreenNumber !==
-                                            calculateScreenNumber(index, true)) {
-                                        selectedScreenNumber = calculateScreenNumber(index, true)
-                                        screenSelectionRectEven.borderColor
-                                                = JamiTheme.screenSelectionBorderGreen
-                                    }
-                                }
+                            function onScreenCaptured(screenNumber, source) {
+                                if (screenNumber === -1)
+                                    screenShotAll.source = JamiQmlUtils.base64StringTitle + source
                             }
                         }
                     }
@@ -273,41 +205,38 @@ Window {
                 Rectangle {
                     id: screenSelectionRectAll
 
-                    property string borderColor: JamiTheme.tabbarBorderColor
-
-                    anchors.horizontalCenter: screenSelectionScrollViewColumn.horizontalCenter
-
                     color: JamiTheme.secondaryBackgroundColor
 
-                    height: screenSelectionScrollView.height
-                    width: screenSelectionScrollView.width - 2 * JamiTheme.preferredMarginSize
-
-                    border.color: borderColor
+                    width: screenSelectionScrollView.width / 2 -
+                                screenSelectionScrollViewFlow.spacing / 2 - JamiTheme.preferredMarginSize
+                    height: 3 * width / 4
 
-                    Connections {
-                        target: selectScreenWindow
+                    border.color: selectAllScreens ? JamiTheme.screenSelectionBorderColor : JamiTheme.tabbarBorderColor
 
-                        function onSelectedScreenNumberChanged() {
-                            // Recover from green state.
-                            selectAllScreens = false
-                            screenSelectionRectAll.borderColor = JamiTheme.tabbarBorderColor
-                        }
-                    }
+                    visible: Qt.application.screens.length > 1
 
-                    Image {
+                    PreviewRenderer {
                         id: screenShotAll
 
                         anchors.top: screenSelectionRectAll.top
                         anchors.topMargin: 10
                         anchors.horizontalCenter: screenSelectionRectAll.horizontalCenter
-
                         height: screenSelectionRectAll.height - 50
                         width: screenSelectionRectAll.width - 50
 
-                        fillMode: Image.PreserveAspectFit
-                        mipmap: true
+                        lrcInstance: LRCInstance
 
-                        Component.onCompleted: AvAdapter.captureAllScreens()
+                        Component.onDestruction: {
+                            if (screenShotAll.rendererId !== "" && screenShotAll.rendererId !== currentPreview)
+                                VideoDevices.stopDevice(screenShotAll.rendererId, true)
+                        }
+                        Component.onCompleted: {
+                            if (visible) {
+                                var rendId = AvAdapter.getSharingResource(-1, "")
+                                if (rendId !== "")
+                                    screenShotAll.rendererId = VideoDevices.startDevice(rendId, true)
+                            }
+                        }
                     }
 
                     Text {
@@ -317,8 +246,8 @@ Window {
                         anchors.topMargin: 10
                         anchors.horizontalCenter: screenSelectionRectAll.horizontalCenter
 
-                        font.pointSize: JamiTheme.textFontSize - 2
-                        text: qsTr("All Screens")
+                        font.pointSize: JamiTheme.textFontSize
+                        text: JamiStrings.allScreens
                         color: JamiTheme.textColor
                     }
 
@@ -329,8 +258,90 @@ Window {
                         onClicked: {
                             selectedScreenNumber = -1
                             selectAllScreens = true
-                            screenSelectionRectAll.borderColor
-                                    = JamiTheme.screenSelectionBorderGreen
+                        }
+                    }
+                }
+
+                Text {
+                    id: windowsText
+                    width: screenSelectionScrollView.width
+                    height: JamiTheme.preferredFieldHeight
+
+                    font.pointSize: JamiTheme.menuFontSize
+                    font.bold: true
+                    text: JamiStrings.windows
+                    verticalAlignment: Text.AlignBottom
+                    color: JamiTheme.textColor
+                }
+
+                Repeater {
+                    id: screenInfo2
+
+                    model: screens ? screens.length : 0
+
+                    delegate: Rectangle {
+                        id: screenItem2
+
+                        color: JamiTheme.secondaryBackgroundColor
+
+                        width: screenSelectionScrollView.width / 2 -
+                                screenSelectionScrollViewFlow.spacing / 2 - JamiTheme.preferredMarginSize
+                        height: 3 * width / 4
+
+                        border.color: selectedScreenNumber === index ? JamiTheme.screenSelectionBorderColor : JamiTheme.tabbarBorderColor
+                        visible: JamiStrings.selectScreen !== screens[index] && index >= Qt.application.screens.length
+
+                        PreviewRenderer {
+                            id: screenPreview2
+
+                            anchors.top: screenItem2.top
+                            anchors.topMargin: 10
+                            anchors.horizontalCenter: screenItem2.horizontalCenter
+                            anchors.leftMargin: 25
+                            anchors.rightMargin: 25
+                            height: screenItem2.height - 60
+                            width: screenItem2.width - 50
+
+                            lrcInstance: LRCInstance
+
+                            Component.onDestruction: {
+                                if (screenPreview2.rendererId !== "" && screenPreview2.rendererId !== currentPreview)
+                                    VideoDevices.stopDevice(screenPreview2.rendererId, true)
+                            }
+                            Component.onCompleted: {
+                                if (visible) {
+                                    var rendId = AvAdapter.getSharingResource(-2, AvAdapter.windowsIds[index - Qt.application.screens.length])
+                                    if (rendId !== "")
+                                        screenPreview2.rendererId = VideoDevices.startDevice(rendId, true)
+                                }
+                            }
+                        }
+
+                        Text {
+                            id: screenName2
+
+                            anchors.top: screenPreview2.bottom
+                            anchors.topMargin: 10
+                            anchors.horizontalCenter: screenItem2.horizontalCenter
+                            width: parent.width
+                            font.pointSize: JamiTheme.textFontSize
+                            text: screens[index] ? screens[index] : ""
+                            elide: Text.ElideMiddle
+                            horizontalAlignment: Text.AlignHCenter
+                            color: JamiTheme.textColor
+                        }
+
+                        MouseArea {
+                            anchors.fill: screenItem2
+                            acceptedButtons: Qt.LeftButton
+
+                            onClicked: {
+                                selectAllScreens = false
+                                if (selectedScreenNumber == -1
+                                        || selectedScreenNumber !== index) {
+                                    selectedScreenNumber = index
+                                }
+                            }
                         }
                     }
                 }
@@ -347,22 +358,27 @@ Window {
 
         preferredWidth: 200
 
-        visible: selectedScreenNumber != -1 || selectAllScreens
+        enabled: selectedScreenNumber != -1 || selectAllScreens
+        opacity: enabled ? 1.0 : 0.5
 
         color: JamiTheme.buttonTintedBlack
         hoveredColor: JamiTheme.buttonTintedBlackHovered
         pressedColor: JamiTheme.buttonTintedBlackPressed
         outlined: true
-        enabled: true
 
         text: JamiStrings.shareScreen
 
         onClicked: {
             if (selectAllScreens)
                 AvAdapter.shareAllScreens()
-            else
-                AvAdapter.shareEntireScreen(selectedScreenNumber - 1)
-            selectScreenWindow.close()
+            else {
+                if (selectedScreenNumber < Qt.application.screens.length)
+                    AvAdapter.shareEntireScreen(selectedScreenNumber)
+                else {
+                    AvAdapter.shareWindow(AvAdapter.windowsIds[selectedScreenNumber - Qt.application.screens.length])
+                }
+            }
+            root.close()
         }
     }
 }
diff --git a/src/mainview/js/selectscreenwindowcreation.js b/src/mainview/js/selectscreenwindowcreation.js
index d4bbed83e..581859ade 100644
--- a/src/mainview/js/selectscreenwindowcreation.js
+++ b/src/mainview/js/selectscreenwindowcreation.js
@@ -43,7 +43,9 @@ function finishCreation() {
     selectScreenWindowObject.onClosing.connect(destroySelectScreenWindow)
 }
 
-function showSelectScreenWindow() {
+function showSelectScreenWindow(previewId) {
+    console.log("previewId", previewId)
+    selectScreenWindowObject.currentPreview = previewId
     selectScreenWindowObject.show()
 
     var screen = selectScreenWindowObject.screen
diff --git a/src/videodevices.cpp b/src/videodevices.cpp
index 3783bcff6..c42bf552a 100644
--- a/src/videodevices.cpp
+++ b/src/videodevices.cpp
@@ -290,7 +290,6 @@ VideoDevices::getDefaultDevice()
     return rendererId;
 }
 
-#pragma optimize("", off)
 QString
 VideoDevices::startDevice(const QString& deviceId, bool force)
 {
@@ -310,7 +309,6 @@ VideoDevices::stopDevice(const QString& deviceId, bool force)
         deviceOpen_ = false;
     }
 }
-#pragma optimize("", on)
 
 void
 VideoDevices::setDefaultDeviceRes(int index)
-- 
GitLab