File: dialog.go

package info (click to toggle)
termshark 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,800 kB
  • sloc: sh: 98; makefile: 9
file content (193 lines) | stat: -rw-r--r-- 4,862 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright 2019-2021 Graham Clark. All rights reserved.  Use of this source
// code is governed by the MIT license that can be found in the LICENSE
// file.

// Package ui contains user-interface functions and helpers for termshark.
package ui

import (
	"strings"

	"github.com/gcla/gowid"
	"github.com/gcla/gowid/widgets/dialog"
	"github.com/gcla/gowid/widgets/framed"
	"github.com/gcla/gowid/widgets/hpadding"
	"github.com/gcla/gowid/widgets/selectable"
	"github.com/gcla/gowid/widgets/styled"
	"github.com/gcla/gowid/widgets/text"
	"github.com/gcla/termshark/v2"
	"github.com/gcla/termshark/v2/widgets/appkeys"
	"github.com/gcla/termshark/v2/widgets/minibuffer"
	"github.com/gcla/termshark/v2/widgets/scrollabletext"
	"github.com/gcla/termshark/v2/widgets/withscrollbar"
)

//======================================================================

var (
	fixed      gowid.RenderFixed
	flow       gowid.RenderFlow
	hmiddle    gowid.HAlignMiddle
	hleft      gowid.HAlignLeft
	vmiddle    gowid.VAlignMiddle
	YesNo      *dialog.Widget
	MiniBuffer *minibuffer.Widget
	PleaseWait *dialog.Widget
)

type textID string

func (t textID) ID() interface{} {
	return string(t)
}

// So that I can capture ctrl-c etc before the dialog
type copyable struct {
	*dialog.Widget
	wrapper gowid.IWidget
}

func (w *copyable) UserInput(ev interface{}, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) bool {
	return w.wrapper.UserInput(ev, size, focus, app)
}

func OpenMessage(msgt string, openOver gowid.ISettableComposite, app gowid.IApp) *dialog.Widget {
	return openMessage(msgt, openOver, false, false, app)
}

func OpenLongMessage(msgt string, openOver gowid.ISettableComposite, app gowid.IApp) *dialog.Widget {
	return openMessage(msgt, openOver, false, true, app)
}

func OpenMessageForCopy(msgt string, openOver gowid.ISettableComposite, app gowid.IApp) *dialog.Widget {
	return openMessage(msgt, openOver, true, false, app)
}

func openMessage(msgt string, openOver gowid.ISettableComposite, focusOnWidget bool, doFlow bool, app gowid.IApp) *dialog.Widget {
	var dh gowid.IWidgetDimension = fixed
	var dw gowid.IWidgetDimension = fixed

	if doFlow {
		dh = flow
		dw = ratio(0.7)
	}

	var al gowid.IHAlignment = hmiddle
	if strings.Count(msgt, "\n") > 0 || doFlow {
		al = hleft
	}

	var view gowid.IWidget = text.NewCopyable(msgt, textID(msgt),
		styled.UsePaletteIfSelectedForCopy{Entry: "copy-mode-alt"},
		text.Options{
			Align: al,
		},
	)

	view = selectable.New(view)

	view = hpadding.New(
		view,
		hmiddle,
		dh,
	)

	view = framed.NewSpace(view)

	YesNo = dialog.New(
		view,
		dialog.Options{
			Buttons:         dialog.CloseOnly,
			NoShadow:        true,
			BackgroundStyle: gowid.MakePaletteRef("dialog"),
			BorderStyle:     gowid.MakePaletteRef("dialog"),
			ButtonStyle:     gowid.MakePaletteRef("dialog-button"),
			FocusOnWidget:   focusOnWidget,
		},
	)

	wrapper := appkeys.New(
		appkeys.New(
			YesNo,
			copyModeExitKeys20,
			appkeys.Options{
				ApplyBefore: true,
			},
		),
		copyModeEnterKeys,
		appkeys.Options{
			ApplyBefore: true,
		},
	)

	dialog.OpenExt(
		&copyable{
			Widget:  YesNo,
			wrapper: wrapper,
		}, openOver, dw, dh, app,
	)

	return YesNo
}

func OpenTemplatedDialog(container gowid.ISettableComposite, tmplName string, app gowid.IApp) *dialog.Widget {
	YesNo = dialog.New(
		framed.NewSpace(
			text.New(termshark.TemplateToString(Templates, tmplName, TemplateData)),
		),
		dialog.Options{
			Buttons:         dialog.CloseOnly,
			NoShadow:        true,
			BackgroundStyle: gowid.MakePaletteRef("dialog"),
			BorderStyle:     gowid.MakePaletteRef("dialog"),
			ButtonStyle:     gowid.MakePaletteRef("dialog-button"),
		},
	)

	YesNo.Open(container, ratio(0.5), app)

	return YesNo
}

func OpenTemplatedDialogExt(container gowid.ISettableComposite, tmplName string, width gowid.IWidgetDimension, height gowid.IWidgetDimension, app gowid.IApp) *dialog.Widget {
	YesNo = dialog.New(framed.NewSpace(
		withscrollbar.New(
			scrollabletext.New(
				termshark.TemplateToString(Templates, tmplName, TemplateData),
			),
			withscrollbar.Options{
				HideIfContentFits: true,
			},
		),
	),
		dialog.Options{
			Buttons:         dialog.CloseOnly,
			NoShadow:        true,
			BackgroundStyle: gowid.MakePaletteRef("dialog"),
			BorderStyle:     gowid.MakePaletteRef("dialog"),
			ButtonStyle:     gowid.MakePaletteRef("dialog-button"),
		},
	)
	dialog.OpenExt(YesNo, container, width, height, app)
	return YesNo
}

func OpenPleaseWait(container gowid.ISettableComposite, app gowid.IApp) {
	PleaseWait.Open(container, fixed, app)
	if Fin != nil {
		Fin.Activate()
	}
}

func ClosePleaseWait(app gowid.IApp) {
	PleaseWait.Close(app)
	if Fin != nil {
		Fin.Deactivate()
	}
}

//======================================================================
// Local Variables:
// mode: Go
// fill-column: 110
// End: