Adding new simple label control and example

This commit is contained in:
Florian Zevedei 2023-09-12 14:14:38 +02:00
parent 105ac9fa5b
commit 243c9c82a8
5 changed files with 213 additions and 0 deletions

BIN
.github/images/label.gif vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 658 KiB

View File

@ -44,6 +44,7 @@ BitTorrent: `TYVZSykaVT1nKZnz9hjDgBRNB9VavU1bpW`
* [PromptDialog](#prompt-dialog)
* [ConfirmDialog](#confirm-dialog)
- [Controls](#controls)
* [Label](#label)
* [ProgressBar](#progress-bar)
* [CalendarPicker](#calendar-picker)
* [MonthPicker](#month-picker)
@ -633,6 +634,13 @@ await this.NavigateTo(cd);
## Controls
### Label
A minimal control which allows to manage a classic "text" message and update its later without having to keep track of the message id.
<img src=".github/images/label.gif?raw=true" />
### Progress Bar
<img src=".github/images/progressbar.PNG?raw=true" />

View File

@ -0,0 +1,84 @@
using System;
using System.Threading.Tasks;
using TelegramBotBase.Args;
using TelegramBotBase.Controls.Hybrid;
using TelegramBotBase.Controls.Inline;
using TelegramBotBase.Enums;
using TelegramBotBase.Form;
namespace TelegramBotBase.Example.Tests.Controls;
public class LabelForm : AutoCleanForm
{
TelegramBotBase.Controls.Inline.LabelControl _label;
ButtonGrid _buttonGrid;
String[] string_options = new string[] { "My test label", "Here is a different text", "*And this looks completely different.*", "Aha! another one.", "_Gotcha!_" };
public LabelForm()
{
DeleteMode = EDeleteMode.OnLeavingForm;
Init += LabelForm_Init;
}
private Task LabelForm_Init(object sender, InitEventArgs e)
{
//The "label" control
_label = new LabelControl("My test label");
AddControl(_label);
//Optional...just for experimentation...
var bf = new ButtonForm();
bf.AddButtonRow("Toggle text", "toggle");
bf.AddButtonRow("Open menu", "menu");
_buttonGrid = new ButtonGrid(bf);
_buttonGrid.KeyboardType = EKeyboardType.ReplyKeyboard;
_buttonGrid.Title = "Choose your options:";
_buttonGrid.ButtonClicked += _buttonGrid_ButtonClicked;
AddControl(_buttonGrid);
return Task.CompletedTask;
}
private async Task _buttonGrid_ButtonClicked(object sender, ButtonClickedEventArgs e)
{
switch (e.Button.Value ?? "")
{
case "menu":
var mn = new Menu();
await NavigateTo(mn);
break;
case "toggle":
//Pick random string from array
var r = new Random((int)DateTime.UtcNow.Ticks);
String random_string;
do
{
random_string = string_options[r.Next(0, string_options.Length)];
if (random_string == null)
continue;
} while (random_string == _label.Text);
_label.Text = random_string;
break;
}
}
}

View File

@ -207,6 +207,14 @@ public class Menu : AutoCleanForm
break;
case "label":
var lf = new LabelForm();
await NavigateTo(lf);
break;
default:
message.Handled = false;
@ -260,6 +268,9 @@ public class Menu : AutoCleanForm
btn.AddButtonRow(new ButtonBase("#19 Notifications", new CallbackData("a", "notifications").Serialize()));
btn.AddButtonRow(new ButtonBase("#20 Label", new CallbackData("a", "label").Serialize()));
await Device.Send("Choose your test:", btn);
}
}

View File

@ -0,0 +1,110 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
using TelegramBotBase.Localizations;
namespace TelegramBotBase.Controls.Inline;
[DebuggerDisplay("{Text}")]
public class LabelControl : ControlBase
{
private bool _renderNecessary = true;
private string _text = string.Empty;
public String Text
{
get
{
return _text;
}
set
{
if (_text == value)
return;
_text = value;
_renderNecessary = true;
}
}
private ParseMode _parseMode = ParseMode.Markdown;
public ParseMode ParseMode
{
get
{
return _parseMode;
}
set
{
_parseMode = value;
_renderNecessary = true;
}
}
public LabelControl()
{
}
public LabelControl(string text)
{
_text = text;
}
public LabelControl(string text, ParseMode parseMode)
{
_text = text;
_parseMode = parseMode;
}
public int? MessageId { get; set; }
public override async Task Render(MessageResult result)
{
if (!_renderNecessary)
{
return;
}
Message m;
//Update ?
if (MessageId != null)
{
m = await Device.Raw(a => a.EditMessageTextAsync(Device.DeviceId, MessageId.Value, Text, _parseMode));
_renderNecessary = false;
return;
}
//New Message
m = await Device.Raw(a => a.SendTextMessageAsync(Device.DeviceId, Text, disableNotification: true, parseMode: _parseMode));
if (m != null)
{
MessageId = m.MessageId;
}
_renderNecessary = false;
}
public override async Task Cleanup()
{
if (this.MessageId == null)
return;
await Device.DeleteMessage(MessageId.Value);
}
}