Skip to content

Commit

Permalink
UI: Cleanup frontend event handling
Browse files Browse the repository at this point in the history
This adds a function to OBSBasic to call on_event, so every
time a event is called, the api variable doesn't have to be
checked everytime.
  • Loading branch information
cg2121 authored and Lain-B committed Aug 11, 2024
1 parent 0306eff commit 8955411
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 130 deletions.
31 changes: 12 additions & 19 deletions UI/window-basic-main-profiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ bool OBSBasic::CreateProfile(const std::string &newName, bool create_new,
return false;
}

if (api && !rename)
api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGING);
if (!rename)
OnEvent(OBS_FRONTEND_EVENT_PROFILE_CHANGING);

config_set_string(App()->GlobalConfig(), "Basic", "Profile",
newName.c_str());
Expand Down Expand Up @@ -355,9 +355,9 @@ bool OBSBasic::CreateProfile(const std::string &newName, bool create_new,
wizard.exec();
}

if (api && !rename) {
api->on_event(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
if (!rename) {
OnEvent(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
OnEvent(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
}
return true;
}
Expand Down Expand Up @@ -451,8 +451,7 @@ void OBSBasic::DeleteProfile(const QString &profileName)
DeleteProfile(name.c_str(), profileDir);
RefreshProfiles();
config_save_safe(App()->GlobalConfig(), "tmp", nullptr);
if (api)
api->on_event(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
OnEvent(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
}

void OBSBasic::RefreshProfiles()
Expand Down Expand Up @@ -539,8 +538,7 @@ void OBSBasic::on_actionRenameProfile_triggered()
RefreshProfiles();
}

if (api)
api->on_event(OBS_FRONTEND_EVENT_PROFILE_RENAMED);
OnEvent(OBS_FRONTEND_EVENT_PROFILE_RENAMED);
}

void OBSBasic::on_actionRemoveProfile_triggered(bool skipConfirmation)
Expand Down Expand Up @@ -589,8 +587,7 @@ void OBSBasic::on_actionRemoveProfile_triggered(bool skipConfirmation)
return;
}

if (api)
api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGING);
OnEvent(OBS_FRONTEND_EVENT_PROFILE_CHANGING);

newPath.resize(newPath_len);

Expand Down Expand Up @@ -626,10 +623,8 @@ void OBSBasic::on_actionRemoveProfile_triggered(bool skipConfirmation)

Auth::Load();

if (api) {
api->on_event(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
}
OnEvent(OBS_FRONTEND_EVENT_PROFILE_LIST_CHANGED);
OnEvent(OBS_FRONTEND_EVENT_PROFILE_CHANGED);

if (needsRestart) {
QMessageBox::StandardButton button = OBSMessageBox::question(
Expand Down Expand Up @@ -768,8 +763,7 @@ void OBSBasic::ChangeProfile()
return;
}

if (api)
api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGING);
OnEvent(OBS_FRONTEND_EVENT_PROFILE_CHANGING);

path.resize(path_len);

Expand Down Expand Up @@ -811,8 +805,7 @@ void OBSBasic::ChangeProfile()
blog(LOG_INFO, "Switched to profile '%s' (%s)", newName, newDir);
blog(LOG_INFO, "------------------------------------------------");

if (api)
api->on_event(OBS_FRONTEND_EVENT_PROFILE_CHANGED);
OnEvent(OBS_FRONTEND_EVENT_PROFILE_CHANGED);

if (needsRestart) {
QMessageBox::StandardButton button = OBSMessageBox::question(
Expand Down
27 changes: 9 additions & 18 deletions UI/window-basic-main-scene-collections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ bool OBSBasic::AddSceneCollection(bool create_new, const QString &qname)
RefreshSceneCollections();
};

if (api)
api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGING);
OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGING);

new_collection(file, name);

Expand All @@ -179,10 +178,8 @@ bool OBSBasic::AddSceneCollection(bool create_new, const QString &qname)

UpdateTitleBar();

if (api) {
api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
}
OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);

return true;
}
Expand Down Expand Up @@ -294,8 +291,7 @@ void OBSBasic::on_actionRenameSceneCollection_triggered()
UpdateTitleBar();
RefreshSceneCollections();

if (api)
api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_RENAMED);
OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_RENAMED);
}

void OBSBasic::on_actionRemoveSceneCollection_triggered()
Expand Down Expand Up @@ -339,8 +335,7 @@ void OBSBasic::on_actionRemoveSceneCollection_triggered()
return;
}

if (api)
api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGING);
OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGING);

oldFile.insert(0, path);
/* os_rename() overwrites if necessary, only the .bak file will remain. */
Expand All @@ -360,10 +355,8 @@ void OBSBasic::on_actionRemoveSceneCollection_triggered()

UpdateTitleBar();

if (api) {
api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
}
OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_LIST_CHANGED);
OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
}

void OBSBasic::on_actionImportSceneCollection_triggered()
Expand Down Expand Up @@ -461,8 +454,7 @@ void OBSBasic::ChangeSceneCollection()
return;
}

if (api)
api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGING);
OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGING);

SaveProjectNow();

Expand All @@ -480,6 +472,5 @@ void OBSBasic::ChangeSceneCollection()

UpdateTitleBar();

if (api)
api->on_event(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
OnEvent(OBS_FRONTEND_EVENT_SCENE_COLLECTION_CHANGED);
}
4 changes: 1 addition & 3 deletions UI/window-basic-main-screenshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ ScreenshotObj::~ScreenshotObj()

main->lastScreenshot = path;

if (main->api)
main->api->on_event(
OBS_FRONTEND_EVENT_SCREENSHOT_TAKEN);
main->OnEvent(OBS_FRONTEND_EVENT_SCREENSHOT_TAKEN);
}
}
}
Expand Down
40 changes: 12 additions & 28 deletions UI/window-basic-main-transitions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,8 @@ void OBSBasic::TransitionStopped()
EnableTransitionWidgets(true);
UpdatePreviewProgramIndicators();

if (api) {
api->on_event(OBS_FRONTEND_EVENT_TRANSITION_STOPPED);
api->on_event(OBS_FRONTEND_EVENT_SCENE_CHANGED);
}
OnEvent(OBS_FRONTEND_EVENT_TRANSITION_STOPPED);
OnEvent(OBS_FRONTEND_EVENT_SCENE_CHANGED);

swapScene = nullptr;
}
Expand Down Expand Up @@ -371,8 +369,7 @@ void OBSBasic::TransitionToScene(OBSSource source, bool force,

if (force) {
obs_transition_set(transition, source);
if (api)
api->on_event(OBS_FRONTEND_EVENT_SCENE_CHANGED);
OnEvent(OBS_FRONTEND_EVENT_SCENE_CHANGED);
} else {
int duration = ui->transitionDuration->value();

Expand Down Expand Up @@ -450,8 +447,7 @@ void OBSBasic::SetTransition(OBSSource transition)
ui->transitionRemove->setEnabled(configurable);
ui->transitionProps->setEnabled(configurable);

if (api)
api->on_event(OBS_FRONTEND_EVENT_TRANSITION_CHANGED);
OnEvent(OBS_FRONTEND_EVENT_TRANSITION_CHANGED);
}

OBSSource OBSBasic::GetCurrentTransition()
Expand Down Expand Up @@ -509,9 +505,7 @@ void OBSBasic::AddTransition(const char *id)
CreatePropertiesWindow(source);
obs_source_release(source);

if (api)
api->on_event(
OBS_FRONTEND_EVENT_TRANSITION_LIST_CHANGED);
OnEvent(OBS_FRONTEND_EVENT_TRANSITION_LIST_CHANGED);

ClearQuickTransitionWidgets();
RefreshQuickTransitions();
Expand Down Expand Up @@ -566,8 +560,7 @@ void OBSBasic::on_transitionRemove_clicked()

ui->transitions->removeItem(idx);

if (api)
api->on_event(OBS_FRONTEND_EVENT_TRANSITION_LIST_CHANGED);
OnEvent(OBS_FRONTEND_EVENT_TRANSITION_LIST_CHANGED);

ClearQuickTransitionWidgets();
RefreshQuickTransitions();
Expand Down Expand Up @@ -607,9 +600,7 @@ void OBSBasic::RenameTransition(OBSSource transition)
if (idx != -1) {
ui->transitions->setItemText(idx, QT_UTF8(name.c_str()));

if (api)
api->on_event(
OBS_FRONTEND_EVENT_TRANSITION_LIST_CHANGED);
OnEvent(OBS_FRONTEND_EVENT_TRANSITION_LIST_CHANGED);

ClearQuickTransitionWidgets();
RefreshQuickTransitions();
Expand Down Expand Up @@ -643,9 +634,7 @@ void OBSBasic::on_transitionProps_clicked()

void OBSBasic::on_transitionDuration_valueChanged()
{
if (api) {
api->on_event(OBS_FRONTEND_EVENT_TRANSITION_DURATION_CHANGED);
}
OnEvent(OBS_FRONTEND_EVENT_TRANSITION_DURATION_CHANGED);
}

QuickTransition *OBSBasic::GetQuickTransition(int id)
Expand Down Expand Up @@ -714,9 +703,7 @@ void OBSBasic::SetCurrentScene(OBSSource scene, bool force)
outputHandler
->UpdateVirtualCamOutputSource();

if (api)
api->on_event(
OBS_FRONTEND_EVENT_PREVIEW_SCENE_CHANGED);
OnEvent(OBS_FRONTEND_EVENT_PREVIEW_SCENE_CHANGED);
break;
}
}
Expand Down Expand Up @@ -968,8 +955,7 @@ void OBSBasic::TBarChanged(int value)
obs_transition_set_manual_time(transition,
(float)value / T_BAR_PRECISION_F);

if (api)
api->on_event(OBS_FRONTEND_EVENT_TBAR_VALUE_CHANGED);
OnEvent(OBS_FRONTEND_EVENT_TBAR_VALUE_CHANGED);
}

int OBSBasic::GetTbarPosition()
Expand Down Expand Up @@ -1668,8 +1654,7 @@ void OBSBasic::SetPreviewProgramMode(bool enabled)
ui->previewLayout->setAlignment(programOptions,
Qt::AlignCenter);

if (api)
api->on_event(OBS_FRONTEND_EVENT_STUDIO_MODE_ENABLED);
OnEvent(OBS_FRONTEND_EVENT_STUDIO_MODE_ENABLED);

blog(LOG_INFO, "Switched to Preview/Program mode");
blog(LOG_INFO, "-----------------------------"
Expand Down Expand Up @@ -1707,8 +1692,7 @@ void OBSBasic::SetPreviewProgramMode(bool enabled)
ui->transitions->setEnabled(true);
tBarActive = false;

if (api)
api->on_event(OBS_FRONTEND_EVENT_STUDIO_MODE_DISABLED);
OnEvent(OBS_FRONTEND_EVENT_STUDIO_MODE_DISABLED);

blog(LOG_INFO, "Switched to regular Preview mode");
blog(LOG_INFO, "-----------------------------"
Expand Down
Loading

0 comments on commit 8955411

Please sign in to comment.