這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
在我們之前的C++文章“利用rating-input PreviewWidget來對事物進行評價及打分”,我們已經展示了如何使用C++來在Scope中的Preview中對事物進行評價或打分。在今天的這篇文章中,我們將介紹如何在Go Scope中來做同樣的事。我們可以通過這個例子來展示如何捕獲在Go Preview中的按鈕並得到它們的action id以進行分別的處理。
在Go檔案中的Preview方法中:
unc (s *MyScope) Preview(result *scopes.Result, metadata *scopes.ActionMetadata, reply *scopes.PreviewReply, cancelled <-chan bool) error {layout1col := scopes.NewColumnLayout(1)layout2col := scopes.NewColumnLayout(2)layout3col := scopes.NewColumnLayout(3)// Single column layoutlayout1col.AddColumn("image", "header", "summary", "rating", "actions")// Two column layoutlayout2col.AddColumn("image")layout2col.AddColumn("header", "summary", "rating", "actions")// Three cokumn layoutlayout3col.AddColumn("image")layout3col.AddColumn("header", "summary", "rating", "actions")layout3col.AddColumn()// Register the layouts we just createdreply.RegisterLayout(layout1col, layout2col, layout3col)header := scopes.NewPreviewWidget("header", "header")// It has title and a subtitle propertiesheader.AddAttributeMapping("title", "title")header.AddAttributeMapping("subtitle", "subtitle")// Define the image sectionimage := scopes.NewPreviewWidget("image", "image")// It has a single source property, mapped to the result's art propertyimage.AddAttributeMapping("source", "art")// Define the summary sectiondescription := scopes.NewPreviewWidget("summary", "text")// It has a text property, mapped to the result's description propertydescription.AddAttributeMapping("text", "description")actions := scopes.NewPreviewWidget("actions", "actions")actions.AddAttributeValue("actions", []actionInfo{actionInfo{Id: "my_action", Label: "Close"},actionInfo{Id: "my_action2", Label: "Refresh"},})rating := scopes.NewPreviewWidget("rating", "rating-input")rating.AddAttributeValue("visible", "both")rating.AddAttributeValue("required", "rating")var scope_data stringmetadata.ScopeData(scope_data)if len(scope_data) > 0 {extra := scopes.NewPreviewWidget("extra", "text")extra.AddAttributeValue("text", "test Text")reply.PushWidgets(header, image, description, actions, rating, extra)} else {reply.PushWidgets(header, image, description, rating, actions)}return nil}
我們可以在上面的文字中,看到:
rating := scopes.NewPreviewWidget("rating", "rating-input")rating.AddAttributeValue("visible", "both")rating.AddAttributeValue("required", "rating")
它用來產生一個rating的PreviewWidget:即有rating,也有review,所以在設定“visible”中為both。
func (sc *MyScope) PerformAction(result *scopes.Result, metadata *scopes.ActionMetadata, widgetId, actionId string) (*scopes.ActivationResponse, error) {log.Printf("Perform action for widget=%s, action=%s\n", widgetId, actionId)// var scope_data interface{}var scope_data map[string]interface{}metadata.ScopeData(&scope_data)log.Println("rating: ", scope_data["rating"])log.Println("review: ", scope_data["review"])for key, value := range scope_data {log.Println("key: ", key)log.Println("value: ", value)}if widgetId == "actions" && actionId == "my_action" {resp := scopes.NewActivationResponse(scopes.ActivationHideDash)resp.SetScopeData([]string{"hello", "world"})return resp, nil} return scopes.NewActivationResponse(scopes.ActivationShowPreview), nil}
在Go 檔案中實現上面的PerformAction就可以捕獲在Preview中的按鈕事件。我們在上面的代碼中列印出rating及review的值。
運行我們的代碼:
在運行時的輸出:
整個項目的源碼在:git clone https://gitcafe.com/ubuntu/goscope_rating.git
我們可以在Terminal中:
$chmod +x *.sh
通過上述命令來使得.sh檔案變為可以執行的檔案
$./run.sh
通過上面的指令碼執行,在Desktop上運行我們的Scope
$./build.sh -d
通過我們上面的指令碼執行,可以編譯goscope,並部署到手機中去
$./clean.sh
通過上面的指令碼執行,清除所有的編譯的中間檔案