如何在Ubuntu Scope中定义设置变量并读取
1)首先创建一个最基本的Scope
2)加入代码来完成设置功能
com.ubuntu.developer.liu-xiao-guo.settingscope_settingscope-settings.ini
注意这个文件名和Scope的设置文件
com.ubuntu.developer.liu-xiao-guo.settingscope_settingscope.ini
只有细小的差别。只是在它的后面加上“-settings"即可。记住千万不要改变这个规则。注意这个文件名和项目的名称的不同而不同。
configure_file( "com.ubuntu.developer.liu-xiao-guo.settingscope_settingscope-settings.ini" "${CMAKE_BINARY_DIR}/src/com.ubuntu.developer.liu-xiao-guo.settingscope_settingscope-settings.ini" ) INSTALL( FILES "${CMAKE_BINARY_DIR}/src/com.ubuntu.developer.liu-xiao-guo.settingscope_settingscope-settings.ini" DESTINATION "${SCOPE_INSTALL_DIR}" )
这样我们的设置文件就可以安装到目标中了。下面,我们可以对我们的设置文件进行配置。打开我们的设置文件:
[location]
type = string
defaultValue = London
displayName = Location
[distanceUnit]
type = list
defaultValue = 1
displayName = Distance Unit
displayName[de] = Entfernungseinheit
displayValues = Kilometers;Miles
displayValues[de] = Kilometer;Meilen
[age]
type = number
defaultValue = 23
displayName = Age
[enabled]
type = boolean
defaultValue = true
displayName = Enabled
# Setting without a default value
[color]
type = string
displayName = Color
[limit]
type = number
defaultValue = 20
displayName = 搜寻条数
void Query::run(sc::SearchReplyProxy const& reply) { // Read the settings initScope(); try { // Start by getting information about the query const sc::CannedQuery &query(sc::SearchQueryBase::query()); // Trim the query string of whitespace string query_string = alg::trim_copy(query.query_string()); Client::ResultList results; if (query_string.empty()) { // If the string is empty, pick a default results = client_.search("default"); } else { // otherwise, use the search string results = client_.search(query_string); } // Register a category auto cat = reply->register_category("results", "Results", "", sc::CategoryRenderer(CATEGORY_TEMPLATE)); for (const auto &result : results) { sc::CategorisedResult res(cat); cerr << "it comes here: " << m_limit << endl; // We must have a URI res.set_uri(result.uri); // res.set_title(result.title); res.set_title( m_location ); res["subtitle"] = std::to_string(m_limit); // Set the rest of the attributes, art, description, etc res.set_art(result.art); res["description"] = result.description; // Push the result if (!reply->push(res)) { // If we fail to push, it means the query has been cancelled. // So don't continue; return; } } } catch (domain_error &e) { // Handle exceptions being thrown by the client API cerr << e.what() << endl; reply->error(current_exception()); } } void Query::initScope() { unity::scopes::VariantMap config = settings(); // The settings method is provided by the base class if (config.empty()) cerr << "CONFIG EMPTY!" << endl; m_location = config["location"].get_string(); // Prints "London" unless the user changed the value cerr << "location: " << m_location << endl; m_limit = config["limit"].get_double(); cerr << "limit: " << m_limit << endl; }
这里“initScope”在“Run”中被调用。在InitScope中,我们通过“settings()”来读取设置的值。为了显示的方便,我们在“Run”中,也对读取的值进行简单的显示:
// res.set_title(result.title); res.set_title( m_location ); res["subtitle"] = std::to_string(m_limit);
我们重新运行我们的Scope,并可以看到如下的图片:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。