Skip to content

Commit 770713b

Browse files
committed
squashme: Deprecated source handling, feedback, cleanup
1 parent 8dac818 commit 770713b

File tree

7 files changed

+117
-40
lines changed

7 files changed

+117
-40
lines changed

frontend/data/locale/en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ Basic.SourceSelect.AddVisible="Make source visible"
644644
Basic.SourceSelect.NoExisting="No existing %1 sources"
645645
Basic.SourceSelect.Accessible.SourceName="Source Name"
646646
Basic.SourceSelect.Accessible.Existing="Add an Existing Source"
647+
Basic.SourceSelect.Deprecated.Create="This source type is marked as deprecated and does allow creating new sources."
647648

648649
# source box
649650
Basic.Main.Sources.Visibility="Visibility"

frontend/dialogs/OBSBasicSourceSelect.cpp

Lines changed: 71 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -43,37 +43,43 @@ struct AddSourceData {
4343
};
4444

4545
namespace {
46-
const char *getSourceDisplayName(QString type)
46+
QString getSourceDisplayName(QString type)
4747
{
48-
std::string typeId = type.toStdString();
49-
const char *unversionedId = typeId.c_str();
48+
if (type == "scene") {
49+
return QTStr("Basic.Scene");
50+
}
51+
52+
const char *inputChar = obs_get_latest_input_type_id(type.toUtf8().constData());
53+
const char *displayChar = obs_source_get_display_name(inputChar);
54+
std::string displayId = (displayChar) ? displayChar : "";
5055

51-
if (strcmp(unversionedId, "scene") == 0) {
52-
return Str("Basic.Scene");
56+
if (!displayId.empty()) {
57+
return QString::fromStdString(displayId);
58+
} else {
59+
return QString();
5360
}
54-
const char *id = obs_get_latest_input_type_id(unversionedId);
55-
return obs_source_get_display_name(id);
5661
}
57-
} // namespace
5862

59-
char *getNewSourceName(const char *name, const char *format)
63+
std::string getNewSourceName(std::string_view name)
6064
{
61-
struct dstr new_name = {0};
62-
int inc = 0;
65+
std::string newName(name);
6366

64-
dstr_copy(&new_name, name);
67+
int suffix = 1;
6568

6669
for (;;) {
67-
OBSSourceAutoRelease existing_source = obs_get_source_by_name(new_name.array);
70+
OBSSourceAutoRelease existing_source = obs_get_source_by_name(newName.c_str());
6871
if (!existing_source) {
6972
break;
7073
}
7174

72-
dstr_printf(&new_name, format, name, ++inc + 1);
75+
newName = name;
76+
newName += " ";
77+
newName += std::to_string(suffix);
7378
}
7479

75-
return new_name.array;
80+
return newName;
7681
}
82+
} // namespace
7783

7884
static void AddSource(void *_data, obs_scene_t *scene)
7985
{
@@ -111,10 +117,9 @@ static void AddExisting(OBSSource source, bool visible, bool duplicate, obs_tran
111117

112118
if (duplicate) {
113119
OBSSource from = source;
114-
char *new_name = getNewSourceName(obs_source_get_name(source), "%s %d");
115-
source = obs_source_duplicate(from, new_name, false);
120+
std::string new_name = getNewSourceName(obs_source_get_name(source));
121+
source = obs_source_duplicate(from, new_name.c_str(), false);
116122
obs_source_release(source);
117-
bfree(new_name);
118123

119124
if (!source) {
120125
return;
@@ -217,6 +222,8 @@ OBSBasicSourceSelect::OBSBasicSourceSelect(OBSBasic *parent, undo_stack &undo_s)
217222
&OBSBasicSourceSelect::checkSourceVisibility);
218223

219224
ui->createNewFrame->setVisible(false);
225+
ui->deprecatedCreateLabel->setVisible(false);
226+
ui->deprecatedCreateLabel->setProperty("class", "text-muted");
220227

221228
getSourceTypes();
222229
getSources();
@@ -251,8 +258,8 @@ void OBSBasicSourceSelect::checkSourceVisibility()
251258

252259
// Allow some room for previous/next rows to make scrolling a bit more seamless
253260
QRect scrollAreaRect(QPoint(0, 0), ui->existingScrollArea->size());
254-
scrollAreaRect.setTop(scrollAreaRect.top() - Thumbnail::cy);
255-
scrollAreaRect.setBottom(scrollAreaRect.bottom() + Thumbnail::cy);
261+
scrollAreaRect.setTop(scrollAreaRect.top() - Thumbnail::size.width());
262+
scrollAreaRect.setBottom(scrollAreaRect.bottom() + Thumbnail::size.height());
256263

257264
for (QAbstractButton *button : buttons) {
258265
SourceSelectButton *sourceButton = qobject_cast<SourceSelectButton *>(button->parent());
@@ -315,6 +322,10 @@ void OBSBasicSourceSelect::updateExistingSources(int limit)
315322
break;
316323
}
317324

325+
if (!source || obs_source_removed(source)) {
326+
continue;
327+
}
328+
318329
const char *id = obs_source_get_unversioned_id(source);
319330
QString stringId = QString(id);
320331

@@ -406,29 +417,50 @@ void OBSBasicSourceSelect::getSourceTypes()
406417
continue;
407418
}
408419

409-
QListWidgetItem *newItem = new QListWidgetItem();
420+
QListWidgetItem *newItem = new QListWidgetItem(ui->sourceTypeList);
410421
newItem->setData(Qt::DisplayRole, name);
411422
newItem->setData(UNVERSIONED_ID_ROLE, unversioned_type);
412423

413-
QIcon icon;
414-
icon = main->GetSourceIcon(type);
415-
newItem->setIcon(icon);
424+
if ((caps & OBS_SOURCE_DEPRECATED) != 0) {
425+
newItem->setData(DEPRECATED_ROLE, true);
426+
} else {
427+
newItem->setData(DEPRECATED_ROLE, false);
416428

417-
ui->sourceTypeList->addItem(newItem);
429+
QIcon icon;
430+
icon = main->GetSourceIcon(type);
431+
newItem->setIcon(icon);
432+
}
418433
}
419434

420-
QListWidgetItem *newItem = new QListWidgetItem();
435+
QListWidgetItem *newItem = new QListWidgetItem(ui->sourceTypeList);
421436
newItem->setData(Qt::DisplayRole, Str("Basic.Scene"));
422437
newItem->setData(UNVERSIONED_ID_ROLE, "scene");
423438

424439
QIcon icon;
425440
icon = main->GetSceneIcon();
426441
newItem->setIcon(icon);
427442

428-
ui->sourceTypeList->addItem(newItem);
429-
430443
ui->sourceTypeList->sortItems();
431444

445+
// Shift Deprecated sources to the bottom
446+
QList<QListWidgetItem *> deprecatedItems;
447+
for (int i = 0; i < ui->sourceTypeList->count(); i++) {
448+
QListWidgetItem *item = ui->sourceTypeList->item(i);
449+
if (!item) {
450+
break;
451+
}
452+
453+
bool isDeprecated = item->data(DEPRECATED_ROLE).toBool();
454+
if (isDeprecated) {
455+
ui->sourceTypeList->takeItem(i);
456+
deprecatedItems.append(item);
457+
}
458+
}
459+
460+
for (auto &item : deprecatedItems) {
461+
ui->sourceTypeList->addItem(item);
462+
}
463+
432464
QListWidgetItem *allSources = new QListWidgetItem();
433465
allSources->setData(Qt::DisplayRole, Str("Basic.SourceSelect.Recent"));
434466
allSources->setData(UNVERSIONED_ID_ROLE, QVariant());
@@ -454,26 +486,33 @@ void OBSBasicSourceSelect::setSelectedSourceType(QListWidgetItem *item)
454486
delete child;
455487
}
456488

457-
QVariant data = item->data(UNVERSIONED_ID_ROLE);
489+
QVariant unversionedIdData = item->data(UNVERSIONED_ID_ROLE);
490+
QVariant deprecatedData = item->data(DEPRECATED_ROLE);
458491

459-
if (data.isNull()) {
492+
if (unversionedIdData.isNull()) {
460493
setSelectedSource(nullptr);
461494
sourceTypeId.clear();
462495
ui->createNewFrame->setVisible(false);
463496
updateExistingSources(16);
464497
return;
465498
}
466499

467-
QString type = data.toString();
500+
QString type = unversionedIdData.toString();
468501
if (type.compare(sourceTypeId) == 0) {
469502
return;
470503
}
471504

472505
ui->createNewFrame->setVisible(true);
473506

507+
bool isDeprecatedType = deprecatedData.toBool();
508+
ui->newSourceName->setVisible(!isDeprecatedType);
509+
ui->createNewSource->setVisible(!isDeprecatedType);
510+
511+
ui->deprecatedCreateLabel->setVisible(isDeprecatedType);
512+
474513
sourceTypeId = type;
475514

476-
QString placeHolderText{QT_UTF8(getSourceDisplayName(sourceTypeId))};
515+
QString placeHolderText{getSourceDisplayName(sourceTypeId)};
477516

478517
QString text{placeHolderText};
479518
int i = 2;

frontend/dialogs/OBSBasicSourceSelect.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <QDialog>
3131

3232
constexpr int UNVERSIONED_ID_ROLE = Qt::UserRole + 1;
33+
constexpr int DEPRECATED_ROLE = Qt::UserRole + 2;
3334

3435
class OBSBasicSourceSelect : public QDialog {
3536
Q_OBJECT

frontend/forms/OBSBasicSourceSelect.ui

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,22 @@
492492
</property>
493493
</widget>
494494
</item>
495+
<item>
496+
<widget class="QLabel" name="deprecatedCreateLabel">
497+
<property name="frameShape">
498+
<enum>QFrame::NoFrame</enum>
499+
</property>
500+
<property name="frameShadow">
501+
<enum>QFrame::Plain</enum>
502+
</property>
503+
<property name="lineWidth">
504+
<number>0</number>
505+
</property>
506+
<property name="text">
507+
<string>Basic.SourceSelect.Deprecated.Create</string>
508+
</property>
509+
</widget>
510+
</item>
495511
<item>
496512
<widget class="QFrame" name="frame">
497513
<property name="frameShape">

frontend/utility/ThumbnailManager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ bool ThumbnailManager::updatePixmap(std::shared_ptr<ThumbnailItem> &sharedPointe
129129

130130
auto obj = new ScreenshotObj(source);
131131
obj->setSaveToFile(false);
132-
obj->setSize(Thumbnail::cx, Thumbnail::cy);
132+
obj->setSize(Thumbnail::size);
133133

134134
connect(obj, &ScreenshotObj::imageReady, item, &ThumbnailItem::imageUpdated);
135135
}
@@ -225,7 +225,7 @@ void ThumbnailManager::preloadThumbnail(OBSSource source, QObject *object, std::
225225

226226
auto obj = new ScreenshotObj(source);
227227
obj->setSaveToFile(false);
228-
obj->setSize(Thumbnail::cx, Thumbnail::cy);
228+
obj->setSize(Thumbnail::size);
229229

230230
QPointer<QObject> safeObject = qobject_cast<QObject *>(object);
231231
connect(obj, &ScreenshotObj::imageReady, this, [=](QImage image) {

frontend/utility/ThumbnailManager.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ private slots:
6767

6868
inline QPixmap getPixmap() const { return item->pixmap; }
6969

70-
static constexpr int cx = 320;
71-
static constexpr int cy = 180;
70+
static constexpr QSize size = {320, 180};
7271

7372
signals:
7473
void updateThumbnail(QPixmap pixmap);

frontend/widgets/OBSBasic_SceneItems.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,29 @@
3535

3636
using namespace std;
3737

38+
namespace {
39+
std::string getNewSourceName(std::string_view name)
40+
{
41+
std::string newName(name);
42+
43+
int suffix = 1;
44+
45+
for (;;) {
46+
OBSSourceAutoRelease existing_source = obs_get_source_by_name(newName.c_str());
47+
if (!existing_source) {
48+
break;
49+
}
50+
51+
newName = name;
52+
newName += " (";
53+
newName += std::to_string(suffix);
54+
newName += ")";
55+
}
56+
57+
return newName;
58+
}
59+
} // namespace
60+
3861
static inline bool HasAudioDevices(const char *source_id)
3962
{
4063
const char *output_id = source_id;
@@ -330,8 +353,6 @@ void OBSBasic::SourceRenamed(void *data, calldata_t *params)
330353
blog(LOG_INFO, "Source '%s' renamed to '%s'", prevName, newName);
331354
}
332355

333-
extern char *getNewSourceName(const char *name, const char *format);
334-
335356
void OBSBasic::ResetAudioDevice(const char *sourceId, const char *deviceId, const char *deviceDesc, int channel)
336357
{
337358
bool disable = deviceId && strcmp(deviceId, "disabled") == 0;
@@ -352,11 +373,11 @@ void OBSBasic::ResetAudioDevice(const char *sourceId, const char *deviceId, cons
352373
}
353374

354375
} else if (!disable) {
355-
BPtr<char> name = getNewSourceName(deviceDesc, "%s (%d)");
376+
std::string name = getNewSourceName(deviceDesc);
356377

357378
settings = obs_data_create();
358379
obs_data_set_string(settings, "device_id", deviceId);
359-
source = obs_source_create(sourceId, name, settings, nullptr);
380+
source = obs_source_create(sourceId, name.c_str(), settings, nullptr);
360381

361382
obs_set_output_source(channel, source);
362383
}

0 commit comments

Comments
 (0)