-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChoiceModal.cs
More file actions
75 lines (64 loc) · 2.44 KB
/
Copy pathChoiceModal.cs
File metadata and controls
75 lines (64 loc) · 2.44 KB
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
namespace TUIKit.Example
{
using System;
using System.Collections.Generic;
using TUIKit;
using TUIKit.Input;
using TUIKit.Layout;
using TUIKit.Modals;
using TUIKit.Widgets;
/// <summary>
/// A modal that presents a scrollable list of choices (used as the command palette). The result is
/// the zero-based index chosen, or -1 when cancelled. Demonstrates hosting a list widget inside a
/// modal.
/// </summary>
internal sealed class ChoiceModal : Modal
{
private readonly string _Title;
private readonly ListView _List = new ListView();
internal ChoiceModal(string title, IReadOnlyList<string> options)
{
_Title = title ?? throw new ArgumentNullException(nameof(title));
if (options == null)
throw new ArgumentNullException(nameof(options));
_List.SetItems(options);
}
public override bool HandleKey(KeyEvent key)
{
if (key.Code == KeyCode.Enter)
{
Close(_List.SelectedIndex);
return true;
}
if (key.Code == KeyCode.Escape)
{
RequestClose(-1);
return true;
}
_List.HandleKey(key);
return true;
}
public override void Render(ISurface surface)
{
if (surface == null)
throw new ArgumentNullException(nameof(surface));
Padding pad = ContentPadding;
int innerWidth = Math.Min(46, surface.Size.Width - 4 - pad.Horizontal);
int innerHeight = Math.Min(_List.Items.Count, surface.Size.Height - 4 - pad.Vertical);
if (innerWidth < 4 || innerHeight < 1)
return;
int width = innerWidth + 2 + pad.Horizontal;
int height = innerHeight + 2 + pad.Vertical;
int x = (surface.Size.Width - width) / 2;
int y = (surface.Size.Height - height) / 2;
Rect box = new Rect(x, y, width, height);
surface.Fill(box, Cell.Blank(CellStyle.Default));
surface.DrawBox(box, CellStyle.Default.WithForeground(Color.FromPalette(6)), _Title);
if (surface is BufferSurface buffer)
{
BufferSurface inner = buffer.CreateView(new Rect(x + 1 + pad.Left, y + 1 + pad.Top, innerWidth, innerHeight));
_List.Render(inner);
}
}
}
}