CategoryRenderer

A category renderer template in JSON format. More...

#include <unity/scopes/CategoryRenderer.h>

Public Member Functions

 CategoryRenderer (std::string const &json_text=DEFAULT_RENDERER)
 Creates a CategoryRenderer from a JSON string. More...
 
std::string data () const
 Returns complete renderer template definition in JSON format. More...
 
Copy and assignment

Copy and assignment operators (move and non-move versions) have the usual value semantics.

 CategoryRenderer (CategoryRenderer const &)
 
CategoryRendereroperator= (CategoryRenderer const &)
 
 CategoryRenderer (CategoryRenderer &&)
 
CategoryRendereroperator= (CategoryRenderer &&)
 
virtual ~CategoryRenderer ()
 

Static Public Member Functions

static CategoryRenderer from_file (std::string const &path)
 Creates a CategoryRenderer from a text file. More...
 

Detailed Description

A category renderer template in JSON format.

This class specifies how is a particular category rendered by Unity. Note that the data is likely to change between major versions of Unity, and therefore the definition isn't strongly typed and provided by a scope author as a JSON string.

A Category contains all the information needed by Unity to render results provided by a scope author (by handling unity::scopes::SearchQueryBase::run()) in a way that gives the user as much useful information as possible. When pushing results to the query originator (unity::scopes::SearchReply::push()), each result needs to have a category associated, and this association determines what will the result look like.

The most important part of a category definition is the unity::scopes::CategoryRenderer instance. If you use the default constructor CategoryRenderer(), the renderer will use the following definition:

{
  "schema-version" : 1,
  "template" : {
    "category-layout" : "grid"
  },
  "components" : {
    "title" : "title",
    "art" : "art"
  }
}

As specified by the "category-layout" key of the "template" dictionary, Unity will render results associated with this category in a grid layout. The "components" dictionary specifies which result fields are used by Unity. In case of this definition, each tile of the grid will map the "title" field from the result (set also by the call to unity::scopes::Result::set_title()) as title for the grid tile, and "art" field from the result (see unity::scopes::Result::set_art()) as the icon for the grid tile.

To sum up, the "template" dictionary contains information to determine the correct renderer and its parameters, and the "components" dictionary contains mapping specifying which fields of the results are used by the renderer, where keys of the dictionary are understood by Unity and values specify either field name of the results directly ({"title": "album_name"} would mean that Unity will use result["album_name"] as a title for the grid tile), or the value can specify extra hints for the renderer as well as the result field name ({"art": {"field": "icon", "aspect-ratio": 1.3}}).

JSON structure (v1)

When using {"schema-version": 1}, the following keys are understood:

template keys

  • category-layout Specifies renderer type; possible values: "grid" (default), "carousel", "vertical-journal", "horizontal-list"
  • card-layout Specifies layout of the individual result cards; possible values: "vertical" (default), "horizontal"
  • card-size Size of the result cards; possible values: "small", "medium" (default), "large"; when using "category-layout": "vertical-journal" any integer between 12 and 38
  • overlay Overlay text data on top of the art; boolean, default false
  • collapsed-rows Number of result rows displayed while the category is collapsed; possible values: any non-negative integer, where 0 fully expands the category (only affects grid and vertical journal)
  • card-background Background color for the cards; string; URI in the format
    color:///#rrggbb 
    or
    color:///color_name
    
    or
    gradient:///#rrggbb/#rrggbb 
    or an image URI (will be stretched)

components keys

  • title String specifying card's title
  • art URI specifying card's art (primary graphics), can contain subkeys: "aspect-ratio" (double specifying the aspect ratio of the graphics, default: 1.0)
  • subtitle String specifying subtitle of a card
  • mascot URI specifying card's mascot (secondary graphics)
  • emblem URI specifying card's emblem
  • summary String specifying text summary
  • background Card background URI, can override the default specified in the card-background field of the template section (same format as for card-background)
  • attributes Array of dictionaries specifying text and an optional icon (keys: "value", "icon")
  • overlay-color Color of overlay for templates with overlay

Example

In the following example a category named "Recommended" containing three components is created (title, art and subtitle), and a result providing values for these components is pushed.

Note that the scope is free to set any other extra result fields even if they are not used by the renderer (and therefore not specified in the "components" dictionary), such fields will be preserved and available to the scope when handling result-specific methods (for example unity::scopes::ScopeBase::preview()).

// use raw string literal, so we don't have to escape all the quotes
std::string CATEGORY_DEFINITION = R"(
{
"schema-version" : 1,
"template" : {
"category-layout" : "carousel",
"card-size": "small"
},
"components" : {
"title" : "title",
"art" : {
"field": "art",
"aspect-ratio": 1.3
},
"subtitle": "publisher"
}
}
)";
void MyQuery::run(SearchReplyProxy const& reply)
{
auto category = reply->register_category("recommended", "Recommended", icon, CategoryRenderer(CATEGORY_DEFINITION));
// push a sample result
CategorisedResult result(category); // create a result item in "recommended" category
result.set_uri("http://www.example.org");
result.set_title("Example Result");
result.set_art("http://www.example.org/graphics.png");
result.set_dnd_uri("http://www.example.org");
result["publisher"] = "Example.org";
reply->push(result); // send result to the client
}

Constructor & Destructor Documentation

unity::scopes::CategoryRenderer::CategoryRenderer ( std::string const &  json_text = DEFAULT_RENDERER)
explicit

Creates a CategoryRenderer from a JSON string.

Parameters
json_textRenderer template in JSON format
Exceptions
unity::InvalidArgumentExceptionif json_text cannot be parsed

Member Function Documentation

std::string unity::scopes::CategoryRenderer::data ( ) const

Returns complete renderer template definition in JSON format.

Returns
The renderer template (JSON).
static CategoryRenderer unity::scopes::CategoryRenderer::from_file ( std::string const &  path)
static

Creates a CategoryRenderer from a text file.

Returns
The CategoryRenderer corresponding to the information in the file.