github.com/ethanhsieh/snapd@v0.0.0-20210615102523-3db9b8e4edc5/usersession/userd/ui/zenity.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2018 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package ui 21 22 import ( 23 "fmt" 24 "os/exec" 25 "time" 26 ) 27 28 // Zenity provides a zenity based UI interface 29 type Zenity struct{} 30 31 // YesNo asks a yes/no question using zenity 32 func (*Zenity) YesNo(primary, secondary string, options *DialogOptions) bool { 33 if options == nil { 34 options = &DialogOptions{} 35 } 36 37 txt := fmt.Sprintf("<big><b>%s</b></big>\n\n%s", primary, secondary) 38 if options.Footer != "" { 39 txt += fmt.Sprintf("\n\n<span size=\"x-small\">%s</span>", options.Footer) 40 } 41 args := []string{"--question", "--modal", "--text=" + txt} 42 if options.Timeout > 0 { 43 args = append(args, fmt.Sprintf("--timeout=%d", int(options.Timeout/time.Second))) 44 } 45 // Gtk is not doing a good job with long labels. It will 46 // create a very narrow and long window (minimal width to fit 47 // the buttons). So force a bigger width here. See also LP: 48 // 1778484. The primary number is lower because the header is 49 // in a bigger font. 50 if len(primary) > 10 || len(secondary) > 20 { 51 args = append(args, "--width=500") 52 } 53 54 cmd := exec.Command("zenity", args...) 55 if err := cmd.Start(); err != nil { 56 return false 57 } 58 59 if err := cmd.Wait(); err != nil { 60 return false 61 } 62 63 return true 64 }