demo-assistant.lua 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. return function(parent, dir)
  2. local lgi = require 'lgi'
  3. local GLib = lgi.GLib
  4. local Gtk = lgi.Gtk
  5. local assistant = Gtk.Assistant {
  6. default_height = 300,
  7. {
  8. title = "Page 1", type = 'INTRO',
  9. Gtk.Grid {
  10. id = 'page1',
  11. orientation = 'HORIZONTAL',
  12. Gtk.Label { label = "You must fill out this entry to continue:" },
  13. Gtk.Entry {
  14. vexpand = true,
  15. id = 'entry',
  16. activates_default = true,
  17. },
  18. },
  19. },
  20. {
  21. title = "Page 2", complete = true,
  22. Gtk.Grid {
  23. orientation = 'VERTICAL',
  24. Gtk.CheckButton { label = "This is optional data, you may continue " ..
  25. "even if you do not check this" }
  26. },
  27. },
  28. {
  29. title = "Confirmation", type = 'CONFIRM', complete = true,
  30. Gtk.Label { label = "This is a confirmation page, press 'Apply' " ..
  31. "to apply changes" },
  32. },
  33. {
  34. title = "Applying changes", type = 'PROGRESS',
  35. Gtk.ProgressBar { id = 'progressbar',
  36. halign = 'CENTER', valign = 'CENTER' },
  37. },
  38. }
  39. function assistant.child.entry:on_changed()
  40. assistant.property.page1.complete = (self.text ~= '')
  41. end
  42. function assistant:on_cancel() self:destroy() end
  43. function assistant:on_close() self:destroy() end
  44. function assistant:on_prepare()
  45. self.title = ("Sample assistant (%d of %d)"):format(
  46. self:get_current_page() + 1, self:get_n_pages())
  47. if self:get_current_page() == 3 then
  48. -- The changes are permanent and cannot be revisited.
  49. assistant:commit()
  50. end
  51. end
  52. local progressbar = assistant.child.progressbar
  53. function assistant:on_apply()
  54. GLib.timeout_add(GLib.PRIORITY_DEFAULT, 100,
  55. function()
  56. -- Simulate work.
  57. local fraction = progressbar.fraction + 0.05
  58. if fraction < 1 then
  59. progressbar.fraction = fraction
  60. return true
  61. else
  62. assistant:destroy()
  63. return false
  64. end
  65. end)
  66. end
  67. assistant.child.page1:show_all()
  68. --assistant:set_screen(parent:get_screen())
  69. assistant:show_all()
  70. return assistant
  71. end,
  72. "Assistant",
  73. table.concat {
  74. "Demonstrates a sample multi-step assistant.\n",
  75. "Assistants are used to divide an operation into several simpler ",
  76. "sequential steps, and to guide the user through these steps."
  77. }