unity.scopes.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 &) | |
CategoryRenderer & | operator= (CategoryRenderer const &) |
CategoryRenderer (CategoryRenderer &&) | |
CategoryRenderer & | operator= (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 a mapping that specifies which fields of the results are used by the renderer. The keys of the dictionary are understood by Unity and the values specify a field name of the results. For example, {"title": "album_name"}
means that Unity will use result["album_name"]
as a title for the grid tile.r
A value also can specify extra hints for the renderer, such as the result field name and a fallback image. For example, {"art": {"field": "icon", "aspect-ratio": 1.3, "fallback": "file:///path_to_fallback_image}}
. The fallback image is shown by Unity if no image URL is provided by the result, but the card requires an image. The fallback image is also shown if the result provides an empty URL for an image, the image does not load due to an error, or if loading results in an empty image. If a result does not specify a fallback image and the actual image is empty or cannot be loaded, Unity substitutes a generic fallback image.
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 38overlay
Overlay text data on top of the art; boolean, default falsecollapsed-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 formatcolor://.md#rrggbb
orcolor:///color_name
orgradient://.md#rrggbb.md#rrggbb
or an image URI (will be stretched)quick-preview-type
The type of media content represented by result cards, for use with inline playback; the only currently supported type is "audio".
components keys
title
String specifying card's titlesubtitle
String specifying subtitle of a cardart
URI specifying card's art (primary graphics), can contain subkeys:"aspect-ratio"
(double specifying the aspect ratio of the graphics, default: 1.0),"field"
(specifying the result's field name that contains the URI), and"fallback"
(fallback image to be used if the URI for a result's artwork cannot be retrieved).mascot
URI specifying card's mascot (secondary graphics), can contain subkeys:"aspect-ratio"
(double specifying the aspect ratio of the graphics, default: 1.0),"field"
(specifying the result's field name that contains the URI), and"fallback"
(fallback image to be used if the URI for a result's mascot cannot be retrieved).emblem
URI specifying card's emblemsummary
String specifying text summarybackground
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 overlayquick-preview-data
A dictionary with the following keys:"uri"
(an uri of audio stream or file),"duration"
(duration in seconds),"playlist"
(an array of uris of additional songs to be played in sequence when the main song finishes).
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()).
<span class="comment">// use raw string literal, so we don't have to escape all the quotes</span> std::string CATEGORY_DEFINITION = R<span class="stringliteral">"(</span> <span class="stringliteral">{</span> <span class="stringliteral"> "schema-version" : 1,</span> <span class="stringliteral"> "template" : {</span> <span class="stringliteral"> "category-layout" : "carousel",</span> <span class="stringliteral"> "card-size" : "small"</span> <span class="stringliteral"> },</span> <span class="stringliteral"> "components" : {</span> <span class="stringliteral"> "title" : "title",</span> <span class="stringliteral"> "art" : {</span> <span class="stringliteral"> "field" : "art",</span> <span class="stringliteral"> "aspect-ratio" : 1.3</span> <span class="stringliteral"> "fallback" : "file:///path_to_fallback_image",</span> <span class="stringliteral"> },</span> <span class="stringliteral"> "subtitle" : "publisher"</span> <span class="stringliteral"> }</span> <span class="stringliteral">}</span> <span class="stringliteral">)";</span> <span class="stringliteral"></span> <span class="stringliteral"></span><span class="keywordtype">void</span> MyQuery::run(<a class="code" href="unity.scopes.md#a9cd604d9b842ac3b2b8636c2165dec1f">SearchReplyProxy</a> <span class="keyword">const</span>& reply) { <span class="keyword">auto</span> category = reply->register_category(<span class="stringliteral">"recommended"</span>, <span class="stringliteral">"Recommended"</span>, icon, <a class="code" href="#a046414ae2092968686ee4ee00629054a">CategoryRenderer</a>(CATEGORY_DEFINITION)); <span class="comment">// push a sample result</span> CategorisedResult result(category); <span class="comment">// create a result item in "recommended" category</span> result.set_uri(<span class="stringliteral">"http://www.example.org"</span>); result.set_title(<span class="stringliteral">"Example Result"</span>); result.set_art(<span class="stringliteral">"http://www.example.org/graphics.png"</span>); result.set_dnd_uri(<span class="stringliteral">"http://www.example.org"</span>); result[<span class="stringliteral">"publisher"</span>] = <span class="stringliteral">"Example.org"</span>; reply->push(result); <span class="comment">// send result to the client</span> }
Constructor & Destructor Documentation
|
explicit |
Creates a CategoryRenderer from a JSON string.
- Parameters
-
json_text Renderer template in JSON format
- Exceptions
-
unity::InvalidArgumentException if 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 |
Creates a CategoryRenderer from a text file.
- Returns
- The CategoryRenderer corresponding to the information in the file.