Skip to content

Commit

Permalink
Improve active session dropdown UX
Browse files Browse the repository at this point in the history
  • Loading branch information
bozbez committed Jul 29, 2022
1 parent d049180 commit 74323e6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
43 changes: 35 additions & 8 deletions src/audio-capture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <obs-properties.h>
#include <util/bmem.h>
#include <util/platform.h>
#include <util/dstr.h>
#include <winuser.h>

#include "wil/result.h"
Expand Down Expand Up @@ -390,7 +391,8 @@ static bool mode_callback(obs_properties_t *ps, obs_property_t *p, obs_data_t *s
}

std::tuple<std::string, std::string>
AudioCapture::MakeSessionOptionStrings(std::set<DWORD> pids, const std::string &executable)
AudioCapture::MakeSessionOptionStrings(std::set<DWORD> pids, const std::string &executable,
bool added = false)
{
auto pids_string = std::string();

Expand All @@ -405,7 +407,11 @@ AudioCapture::MakeSessionOptionStrings(std::set<DWORD> pids, const std::string &
pids_string.append("*");
}

return {std::format("[{}] {}", pids_string, executable), executable};
if (!added)
return {std::format("[{}] {}", pids_string, executable), executable};

static int id = 0;
return {std::format("[{}] {} (added)", pids_string, executable), std::format("{}", id++)};
}

static bool executable_list_callback(void *data, obs_properties_t *ps, obs_property_t *p,
Expand Down Expand Up @@ -485,20 +491,41 @@ void AudioCapture::FillActiveSessionList(obs_property_t *session_list, obs_prope
auto executables = GetExecutables(settings);

std::unordered_map<std::string, std::set<DWORD>> session_options;
for (auto &[key, executable] : sessions) {
if (executables.contains(executable))
for (auto &[key, executable] : sessions)
session_options[executable].insert(key.pid);

std::vector<std::tuple<std::string, std::set<DWORD>>> enabled_session_options;
std::vector<std::tuple<std::string, std::set<DWORD>>> disabled_session_options;

for (auto &[executable, pids] : session_options) {
if (executables.contains(executable)) {
disabled_session_options.push_back({executable, pids});
continue;
}

session_options[executable].insert(key.pid);
enabled_session_options.push_back({executable, pids});
}

for (auto &[executable, pids] : session_options) {
auto cmp = [](auto a, auto b) {
return astrcmpi(std::get<0>(a).c_str(), std::get<0>(b).c_str()) < 0;
};

std::sort(enabled_session_options.begin(), enabled_session_options.end(), cmp);
std::sort(disabled_session_options.begin(), disabled_session_options.end(), cmp);

for (auto &[executable, pids] : enabled_session_options) {
auto [name, val] = AudioCapture::MakeSessionOptionStrings(pids, executable);
obs_property_list_add_string(session_list, name.c_str(), val.c_str());
}

obs_property_set_enabled(session_add, session_options.size() != 0);
obs_property_set_enabled(session_list, session_options.size() != 0);
for (auto &[executable, pids] : disabled_session_options) {
auto [name, val] = AudioCapture::MakeSessionOptionStrings(pids, executable, true);
auto idx = obs_property_list_add_string(session_list, name.c_str(), val.c_str());
obs_property_list_item_disable(session_list, idx, true);
}

obs_property_set_enabled(session_add, enabled_session_options.size() != 0);
obs_property_set_enabled(session_list, enabled_session_options.size() != 0);

obs_data_release(settings);
}
Expand Down
4 changes: 2 additions & 2 deletions src/audio-capture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ class AudioCapture {

void Update(obs_data_t *settings);

static std::tuple<std::string, std::string>
MakeSessionOptionStrings(std::set<DWORD> pids, const std::string &executable);
std::tuple<std::string, std::string>
MakeSessionOptionStrings(std::set<DWORD> pids, const std::string &executable, bool added);

void FillActiveSessionList(obs_property_t *session_list, obs_property_t *session_add);
std::set<std::string> GetExecutables(obs_data_t *settings);
Expand Down

0 comments on commit 74323e6

Please sign in to comment.