Skip to content
This repository has been archived by the owner on Jun 21, 2024. It is now read-only.

Commit

Permalink
Implement simple X11 screen/window picker
Browse files Browse the repository at this point in the history
  • Loading branch information
maltejur committed Feb 10, 2024
1 parent 0bf538a commit 0372b1f
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ set(discord-screenaudio_SRC
src/userscript.cpp
src/centralwidget.cpp
src/localserver.cpp
src/mediapicker.cpp
resources.qrc
)

Expand Down
12 changes: 11 additions & 1 deletion src/discordpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "discordpage.h"
#include "log.h"
#include "mainwindow.h"
#include "src/mediapicker.h"
#include "virtmic.h"

#include <QApplication>
Expand All @@ -16,17 +17,21 @@
#include <QTemporaryFile>
#include <QTimer>
#include <QWebChannel>
#include <QWebEngineProfile>
#include <QWebEngineScript>
#include <QWebEngineScriptCollection>
#include <QWebEngineSettings>

DiscordPage::DiscordPage(QWidget *parent) : QWebEnginePage(parent) {
DiscordPage::DiscordPage(QWidget *parent)
: QWebEnginePage(new QWebEngineProfile("discord-screenaudio"), parent) {
setBackgroundColor(QColor("#313338"));

connect(this, &QWebEnginePage::featurePermissionRequested, this,
&DiscordPage::featurePermissionRequested);
connect(this, &DiscordPage::fullScreenRequested, MainWindow::instance(),
&MainWindow::fullScreenRequested);
connect(this, &DiscordPage::desktopMediaRequested, this,
&DiscordPage::handleDesktopMediaRequest);

setupPermissions();

Expand Down Expand Up @@ -339,3 +344,8 @@ void DiscordPage::setupArrpc() {
":/assets/arrpc/arrpc_bridge_mod.js");
}
}

void DiscordPage::handleDesktopMediaRequest(
const QWebEngineDesktopMediaRequest &request) {
(new MediaPicker(request, MainWindow::instance()))->exec();
}
1 change: 1 addition & 0 deletions src/discordpage.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class DiscordPage : public QWebEnginePage {
private Q_SLOTS:
void featurePermissionRequested(const QUrl &securityOrigin,
QWebEnginePage::Feature feature);
void handleDesktopMediaRequest(const QWebEngineDesktopMediaRequest &request);

public Q_SLOTS:
void getUserStyles(QString url);
Expand Down
70 changes: 70 additions & 0 deletions src/mediapicker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "mediapicker.h"

#include <QBoxLayout>
#include <QLabel>
#include <QListView>
#include <QPushButton>
#include <qicon.h>

MediaPicker::MediaPicker(const QWebEngineDesktopMediaRequest &request,
QWidget *parent)
: QDialog(parent), m_request(request) {
auto layout = new QVBoxLayout(this);
layout->setSizeConstraint(QLayout::SetFixedSize);

auto screensLabel = new QLabel("Screens", this);
layout->addWidget(screensLabel);

auto screensView = new QListView(this);
screensView->setModel(request.screensModel());
screensView->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
layout->addWidget(screensView);

auto windowsLabel = new QLabel("Windows", this);
layout->addWidget(windowsLabel);

auto windowsView = new QListView(this);
windowsView->setModel(request.windowsModel());
windowsView->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
windowsView->setMaximumWidth(300);
layout->addWidget(windowsView);

auto startButton = new QPushButton("Start Stream", this);
layout->addWidget(startButton);

setWindowTitle("discord-screenaudio Media Picker");

connect(screensView, &QListView::clicked,
[&, windowsView](const QModelIndex &index) {
m_selectedWindow = false;
m_selectedIndex = index;
windowsView->clearSelection();
});

connect(windowsView, &QListView::clicked,
[&, screensView](const QModelIndex &index) {
m_selectedWindow = true;
m_selectedIndex = index;
screensView->clearSelection();
});

connect(startButton, &QPushButton::clicked, this, &MediaPicker::startStream);
connect(screensView, &QListView::doubleClicked, this,
&MediaPicker::startStream);
connect(windowsView, &QListView::doubleClicked, this,
&MediaPicker::startStream);
}

void MediaPicker::startStream() {
if (m_selectedWindow) {
m_request.selectWindow(m_selectedIndex);
} else {
m_request.selectScreen(m_selectedIndex);
}
accept();
}

void MediaPicker::reject() {
m_request.cancel();
QDialog::reject();
}
25 changes: 25 additions & 0 deletions src/mediapicker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-FileCopyrightText: 2022 Malte Jürgens and contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later

#pragma once

#include <QDialog>
#include <QWebEngineDesktopMediaRequest>

class MediaPicker : public QDialog {
Q_OBJECT

public:
explicit MediaPicker(const QWebEngineDesktopMediaRequest &request,
QWidget *parent = nullptr);

private:
bool m_selectedWindow;
QModelIndex m_selectedIndex;
const QWebEngineDesktopMediaRequest &m_request;

private slots:
void startStream();
void reject() override;
};
4 changes: 3 additions & 1 deletion src/streamdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <QComboBox>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QIcon>
#include <QLabel>
#include <QPushButton>
#include <QSizePolicy>
Expand Down Expand Up @@ -97,7 +98,8 @@ StreamDialog::StreamDialog(QWidget *parent) : QDialog(parent) {
}

auto button = new QPushButton(this);
button->setText("Start Stream");
button->setText("Select Screen Area");
button->setIcon(QIcon::fromTheme("next"));
connect(button, &QPushButton::clicked, this, &StreamDialog::startStream);
layout->addWidget(button, Qt::AlignRight | Qt::AlignBottom);
}
Expand Down

0 comments on commit 0372b1f

Please sign in to comment.