Form1.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. 
  2. namespace Yuuna.Interaction.WinForms.Test
  3. {
  4. using Newtonsoft.Json;
  5. using RestSharp;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Data;
  10. using System.Diagnostics;
  11. using System.Drawing;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16. public partial class Form1 : Form
  17. {
  18. private readonly ListBox _history;
  19. private readonly TextBox _input;
  20. private readonly RestClient client;
  21. public Form1()
  22. {
  23. this.InitializeComponent();
  24. this.Text = "Yuuna's Debugger Form";
  25. this.MaximizeBox = false;
  26. this.MinimizeBox = false;
  27. this.MaximumSize = new Size(320, 640);
  28. this.Opacity = 0.8;
  29. this.DoubleBuffered = true;
  30. this.BackColor = Color.White;
  31. var rx = Screen.PrimaryScreen.WorkingArea;
  32. this.Location = new Point(rx.Width - this.Size.Width, rx.Height - this.Size.Height);
  33. this.StartPosition = FormStartPosition.Manual;
  34. this.Move += delegate
  35. {
  36. this.Visible = false;
  37. this.Location = new Point(rx.Width - this.Size.Width, rx.Height - this.Size.Height);
  38. this.Visible = true;
  39. };
  40. _history = new ListBox();
  41. _history.Dock = DockStyle.Fill;
  42. _history.Enabled = false;
  43. _history.BorderStyle = BorderStyle.None;
  44. this.Controls.Add(_history);
  45. _input = new TextBox();
  46. _input.Dock = DockStyle.Bottom;
  47. _input.KeyDown += (sender, e) =>
  48. {
  49. if (e.KeyCode == Keys.Enter)
  50. {
  51. var req = this._input.Text;
  52. this._input.Clear();
  53. if (string.IsNullOrWhiteSpace(req))
  54. return;
  55. this._history.Items.Add("Me: " + req);
  56. int visibleItems = _history.ClientSize.Height / _history.ItemHeight;
  57. _history.TopIndex = Math.Max(_history.Items.Count - visibleItems + 1, 0);
  58. if (TrySend(req, out var resp))
  59. this._history.Items.Add("Bot: " + resp.message);
  60. else
  61. this._history.Items.Add("Sys: " + resp.message);
  62. visibleItems = _history.ClientSize.Height / _history.ItemHeight;
  63. _history.TopIndex = Math.Max(_history.Items.Count - visibleItems + 1, 0);
  64. }
  65. };
  66. this.Controls.Add(_input);
  67. this.client = new RestClient("http://localhost:5000/");
  68. client.Timeout = 3000;
  69. }
  70. private bool TrySend(string text, out (string mood, string message) m)
  71. {
  72. try
  73. {
  74. var request = new RestRequest(Method.POST);
  75. request.AddHeader("Content-Type", "application/json");
  76. request.AddParameter("application/json", "{\n \"text\": \"" + text + "\"\n}", ParameterType.RequestBody);
  77. var response = client.Execute(request);
  78. var result = JsonConvert.DeserializeAnonymousType(response.Content, new
  79. {
  80. Success = default(bool),
  81. Message = default(string),
  82. Mood = default(string),
  83. Text = default(string),
  84. });
  85. m = (result.Mood, result.Message);
  86. return true;
  87. }
  88. catch
  89. {
  90. m = ("Sad", "無法處理請求");
  91. }
  92. return false;
  93. }
  94. }
  95. }