Files
test/source/blender/editors/interface/interface_string_search.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

119 lines
2.9 KiB
C++
Raw Normal View History

UI: show recently selected items at the top of searches The goal is to make the search faster to use by dynamically adapting to the user. This can be achieved using the simple but common approach of showing recently selected items at the top. Note, that the "matching score" between the query and each search item still has precedence when determining the order. So the last used item is only at the top, if there is no other search item that matches the query better. Besides making the search generally faster to use, my hope is that this can also reduce the need for manually weighting search items in some places. This is because while the ordering might not be perfect the first time, it will always be once the user selected the element that should be at the top once. This patch includes: * Support for taking recent searches into account in string searching. * Keep track of a global list of recent searches. * Store recent searches on disk similar to recently opened files. * A new setting in the user preferences that allows disabling the functionality. This can be used if deterministic key strokes are required, e.g. for automated tests. In the future this could be improved in different ways: * Add some kind of separator in the search list to indicate which elements are at the top because they have been used recently. * Store the recent search items per search, instead of in a global list. This way it could adapt to the user even better. Pull Request: https://projects.blender.org/blender/blender/pulls/110828
2023-09-25 10:56:12 +02:00
/* SPDX-FileCopyrightText: 2023 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
#include "BKE_appdir.hh"
UI: show recently selected items at the top of searches The goal is to make the search faster to use by dynamically adapting to the user. This can be achieved using the simple but common approach of showing recently selected items at the top. Note, that the "matching score" between the query and each search item still has precedence when determining the order. So the last used item is only at the top, if there is no other search item that matches the query better. Besides making the search generally faster to use, my hope is that this can also reduce the need for manually weighting search items in some places. This is because while the ordering might not be perfect the first time, it will always be once the user selected the element that should be at the top once. This patch includes: * Support for taking recent searches into account in string searching. * Keep track of a global list of recent searches. * Store recent searches on disk similar to recently opened files. * A new setting in the user preferences that allows disabling the functionality. This can be used if deterministic key strokes are required, e.g. for automated tests. In the future this could be improved in different ways: * Add some kind of separator in the search list to indicate which elements are at the top because they have been used recently. * Store the recent search items per search, instead of in a global list. This way it could adapt to the user even better. Pull Request: https://projects.blender.org/blender/blender/pulls/110828
2023-09-25 10:56:12 +02:00
#include "DNA_userdef_types.h"
#include "UI_string_search.hh"
#include "BLI_fileops.h"
UI: show recently selected items at the top of searches The goal is to make the search faster to use by dynamically adapting to the user. This can be achieved using the simple but common approach of showing recently selected items at the top. Note, that the "matching score" between the query and each search item still has precedence when determining the order. So the last used item is only at the top, if there is no other search item that matches the query better. Besides making the search generally faster to use, my hope is that this can also reduce the need for manually weighting search items in some places. This is because while the ordering might not be perfect the first time, it will always be once the user selected the element that should be at the top once. This patch includes: * Support for taking recent searches into account in string searching. * Keep track of a global list of recent searches. * Store recent searches on disk similar to recently opened files. * A new setting in the user preferences that allows disabling the functionality. This can be used if deterministic key strokes are required, e.g. for automated tests. In the future this could be improved in different ways: * Add some kind of separator in the search list to indicate which elements are at the top because they have been used recently. * Store the recent search items per search, instead of in a global list. This way it could adapt to the user even better. Pull Request: https://projects.blender.org/blender/blender/pulls/110828
2023-09-25 10:56:12 +02:00
#include "BLI_fileops.hh"
#include "BLI_map.hh"
#include "BLI_path_utils.hh"
UI: show recently selected items at the top of searches The goal is to make the search faster to use by dynamically adapting to the user. This can be achieved using the simple but common approach of showing recently selected items at the top. Note, that the "matching score" between the query and each search item still has precedence when determining the order. So the last used item is only at the top, if there is no other search item that matches the query better. Besides making the search generally faster to use, my hope is that this can also reduce the need for manually weighting search items in some places. This is because while the ordering might not be perfect the first time, it will always be once the user selected the element that should be at the top once. This patch includes: * Support for taking recent searches into account in string searching. * Keep track of a global list of recent searches. * Store recent searches on disk similar to recently opened files. * A new setting in the user preferences that allows disabling the functionality. This can be used if deterministic key strokes are required, e.g. for automated tests. In the future this could be improved in different ways: * Add some kind of separator in the search list to indicate which elements are at the top because they have been used recently. * Store the recent search items per search, instead of in a global list. This way it could adapt to the user even better. Pull Request: https://projects.blender.org/blender/blender/pulls/110828
2023-09-25 10:56:12 +02:00
namespace blender::ui::string_search {
using blender::string_search::RecentCache;
struct RecentCacheStorage {
/**
* Is incremented every time a search item has been selected. This is used to keep track of the
* order of recent searches.
*/
int logical_clock = 0;
RecentCache cache;
};
static RecentCacheStorage &get_recent_cache_storage()
{
BLI_assert((U.flag & USER_FLAG_RECENT_SEARCHES_DISABLE) == 0);
UI: show recently selected items at the top of searches The goal is to make the search faster to use by dynamically adapting to the user. This can be achieved using the simple but common approach of showing recently selected items at the top. Note, that the "matching score" between the query and each search item still has precedence when determining the order. So the last used item is only at the top, if there is no other search item that matches the query better. Besides making the search generally faster to use, my hope is that this can also reduce the need for manually weighting search items in some places. This is because while the ordering might not be perfect the first time, it will always be once the user selected the element that should be at the top once. This patch includes: * Support for taking recent searches into account in string searching. * Keep track of a global list of recent searches. * Store recent searches on disk similar to recently opened files. * A new setting in the user preferences that allows disabling the functionality. This can be used if deterministic key strokes are required, e.g. for automated tests. In the future this could be improved in different ways: * Add some kind of separator in the search list to indicate which elements are at the top because they have been used recently. * Store the recent search items per search, instead of in a global list. This way it could adapt to the user even better. Pull Request: https://projects.blender.org/blender/blender/pulls/110828
2023-09-25 10:56:12 +02:00
static RecentCacheStorage storage;
return storage;
}
void add_recent_search(const StringRef chosen_str)
{
RecentCacheStorage &storage = get_recent_cache_storage();
storage.cache.logical_time_by_str.add_overwrite(chosen_str, storage.logical_clock);
storage.logical_clock++;
}
const RecentCache *get_recent_cache_or_null()
{
if (U.flag & USER_FLAG_RECENT_SEARCHES_DISABLE) {
return nullptr;
}
RecentCacheStorage &storage = get_recent_cache_storage();
return &storage.cache;
}
static std::optional<std::string> get_recent_searches_file_path()
{
const std::optional<std::string> user_config_dir = BKE_appdir_folder_id_create(
BLENDER_USER_CONFIG, nullptr);
if (!user_config_dir.has_value()) {
UI: show recently selected items at the top of searches The goal is to make the search faster to use by dynamically adapting to the user. This can be achieved using the simple but common approach of showing recently selected items at the top. Note, that the "matching score" between the query and each search item still has precedence when determining the order. So the last used item is only at the top, if there is no other search item that matches the query better. Besides making the search generally faster to use, my hope is that this can also reduce the need for manually weighting search items in some places. This is because while the ordering might not be perfect the first time, it will always be once the user selected the element that should be at the top once. This patch includes: * Support for taking recent searches into account in string searching. * Keep track of a global list of recent searches. * Store recent searches on disk similar to recently opened files. * A new setting in the user preferences that allows disabling the functionality. This can be used if deterministic key strokes are required, e.g. for automated tests. In the future this could be improved in different ways: * Add some kind of separator in the search list to indicate which elements are at the top because they have been used recently. * Store the recent search items per search, instead of in a global list. This way it could adapt to the user even better. Pull Request: https://projects.blender.org/blender/blender/pulls/110828
2023-09-25 10:56:12 +02:00
return std::nullopt;
}
char filepath[FILE_MAX];
BLI_path_join(
filepath, sizeof(filepath), user_config_dir->c_str(), BLENDER_RECENT_SEARCHES_FILE);
UI: show recently selected items at the top of searches The goal is to make the search faster to use by dynamically adapting to the user. This can be achieved using the simple but common approach of showing recently selected items at the top. Note, that the "matching score" between the query and each search item still has precedence when determining the order. So the last used item is only at the top, if there is no other search item that matches the query better. Besides making the search generally faster to use, my hope is that this can also reduce the need for manually weighting search items in some places. This is because while the ordering might not be perfect the first time, it will always be once the user selected the element that should be at the top once. This patch includes: * Support for taking recent searches into account in string searching. * Keep track of a global list of recent searches. * Store recent searches on disk similar to recently opened files. * A new setting in the user preferences that allows disabling the functionality. This can be used if deterministic key strokes are required, e.g. for automated tests. In the future this could be improved in different ways: * Add some kind of separator in the search list to indicate which elements are at the top because they have been used recently. * Store the recent search items per search, instead of in a global list. This way it could adapt to the user even better. Pull Request: https://projects.blender.org/blender/blender/pulls/110828
2023-09-25 10:56:12 +02:00
return std::string(filepath);
}
void write_recent_searches_file()
{
if (U.flag & USER_FLAG_RECENT_SEARCHES_DISABLE) {
return;
}
UI: show recently selected items at the top of searches The goal is to make the search faster to use by dynamically adapting to the user. This can be achieved using the simple but common approach of showing recently selected items at the top. Note, that the "matching score" between the query and each search item still has precedence when determining the order. So the last used item is only at the top, if there is no other search item that matches the query better. Besides making the search generally faster to use, my hope is that this can also reduce the need for manually weighting search items in some places. This is because while the ordering might not be perfect the first time, it will always be once the user selected the element that should be at the top once. This patch includes: * Support for taking recent searches into account in string searching. * Keep track of a global list of recent searches. * Store recent searches on disk similar to recently opened files. * A new setting in the user preferences that allows disabling the functionality. This can be used if deterministic key strokes are required, e.g. for automated tests. In the future this could be improved in different ways: * Add some kind of separator in the search list to indicate which elements are at the top because they have been used recently. * Store the recent search items per search, instead of in a global list. This way it could adapt to the user even better. Pull Request: https://projects.blender.org/blender/blender/pulls/110828
2023-09-25 10:56:12 +02:00
const std::optional<std::string> path = get_recent_searches_file_path();
if (!path) {
return;
}
const RecentCacheStorage &storage = get_recent_cache_storage();
/* Avoid creating an empty file. */
if (storage.cache.logical_time_by_str.is_empty() && !BLI_exists((*path).c_str())) {
return;
}
UI: show recently selected items at the top of searches The goal is to make the search faster to use by dynamically adapting to the user. This can be achieved using the simple but common approach of showing recently selected items at the top. Note, that the "matching score" between the query and each search item still has precedence when determining the order. So the last used item is only at the top, if there is no other search item that matches the query better. Besides making the search generally faster to use, my hope is that this can also reduce the need for manually weighting search items in some places. This is because while the ordering might not be perfect the first time, it will always be once the user selected the element that should be at the top once. This patch includes: * Support for taking recent searches into account in string searching. * Keep track of a global list of recent searches. * Store recent searches on disk similar to recently opened files. * A new setting in the user preferences that allows disabling the functionality. This can be used if deterministic key strokes are required, e.g. for automated tests. In the future this could be improved in different ways: * Add some kind of separator in the search list to indicate which elements are at the top because they have been used recently. * Store the recent search items per search, instead of in a global list. This way it could adapt to the user even better. Pull Request: https://projects.blender.org/blender/blender/pulls/110828
2023-09-25 10:56:12 +02:00
Vector<std::pair<int, std::string>> values;
for (const auto item : storage.cache.logical_time_by_str.items()) {
values.append({item.value, item.key});
}
std::sort(values.begin(), values.end());
fstream file(*path, std::ios::out);
for (const auto &item : values) {
file.write(item.second.data(), item.second.size());
file.write("\n", 1);
}
}
void read_recent_searches_file()
{
if (U.flag & USER_FLAG_RECENT_SEARCHES_DISABLE) {
return;
}
UI: show recently selected items at the top of searches The goal is to make the search faster to use by dynamically adapting to the user. This can be achieved using the simple but common approach of showing recently selected items at the top. Note, that the "matching score" between the query and each search item still has precedence when determining the order. So the last used item is only at the top, if there is no other search item that matches the query better. Besides making the search generally faster to use, my hope is that this can also reduce the need for manually weighting search items in some places. This is because while the ordering might not be perfect the first time, it will always be once the user selected the element that should be at the top once. This patch includes: * Support for taking recent searches into account in string searching. * Keep track of a global list of recent searches. * Store recent searches on disk similar to recently opened files. * A new setting in the user preferences that allows disabling the functionality. This can be used if deterministic key strokes are required, e.g. for automated tests. In the future this could be improved in different ways: * Add some kind of separator in the search list to indicate which elements are at the top because they have been used recently. * Store the recent search items per search, instead of in a global list. This way it could adapt to the user even better. Pull Request: https://projects.blender.org/blender/blender/pulls/110828
2023-09-25 10:56:12 +02:00
const std::optional<std::string> path = get_recent_searches_file_path();
if (!path) {
return;
}
RecentCacheStorage &storage = get_recent_cache_storage();
storage.logical_clock = 0;
storage.cache.logical_time_by_str.clear();
fstream file(*path);
std::string line;
while (std::getline(file, line)) {
storage.cache.logical_time_by_str.add_overwrite(line, storage.logical_clock);
storage.logical_clock++;
}
}
} // namespace blender::ui::string_search