demo-entry-search.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. return function(parent, dir)
  2. local coroutine = require 'coroutine'
  3. local lgi = require 'lgi'
  4. local GLib = lgi.GLib
  5. local GObject = lgi.GObject
  6. local Gtk = lgi.Gtk
  7. local window = Gtk.Dialog {
  8. title = "Search Entry",
  9. resizable = false,
  10. on_response = Gtk.Widget.destroy,
  11. buttons = {
  12. { Gtk.STOCK_CLOSE, Gtk.ResponseType.NONE },
  13. },
  14. }
  15. local content = Gtk.Box {
  16. orientation = 'VERTICAL',
  17. spacing = 5,
  18. border_width = 5,
  19. Gtk.Label {
  20. label = "Search entry demo",
  21. use_markup = true,
  22. },
  23. Gtk.Box {
  24. orientation = 'HORIZONTAL',
  25. spacing = 10,
  26. Gtk.Entry {
  27. id = 'entry',
  28. secondary_icon_stock = Gtk.STOCK_CLEAR,
  29. },
  30. Gtk.Notebook {
  31. id = 'buttons',
  32. show_tabs = false,
  33. show_border = false,
  34. Gtk.Button {
  35. id = 'button_find',
  36. label = "Find",
  37. sensitive = false,
  38. },
  39. Gtk.Button {
  40. id = 'button_cancel',
  41. label = "Cancel",
  42. },
  43. },
  44. },
  45. }
  46. window:get_content_area():add(content)
  47. local entry = content.child.entry
  48. local search = {
  49. {
  50. menu = "Search by _name",
  51. stock = Gtk.STOCK_FIND,
  52. placeholder = "name",
  53. tooltip = "Search by name\n"
  54. .. "Click here to change the search type",
  55. },
  56. {
  57. menu = "Search by _description",
  58. stock = Gtk.STOCK_EDIT,
  59. placeholder = "description",
  60. tooltip = "Search by description\n"
  61. .. "Click here to change the search type",
  62. },
  63. {
  64. menu = "Search by _file name",
  65. stock = Gtk.STOCK_OPEN,
  66. placeholder = "filename",
  67. tooltip = "Search by file name\n"
  68. .. "Click here to change the search type",
  69. },
  70. }
  71. local function search_by(method)
  72. entry.primary_icon_stock = method.stock
  73. entry.primary_icon_tooltip_text = method.tooltip
  74. entry.placeholder_text = method.placeholder
  75. end
  76. search_by(search[1])
  77. local function create_search_menu()
  78. local menu = Gtk.Menu { visible = true }
  79. for i = 1, #search do
  80. menu:append(
  81. Gtk.ImageMenuItem {
  82. image = Gtk.Image {
  83. stock = search[i].stock,
  84. icon_size = Gtk.IconSize.MENU
  85. },
  86. label = search[i].menu,
  87. use_underline = true,
  88. visible = true,
  89. always_show_image = true,
  90. on_activate = function()
  91. search_by(search[i])
  92. end,
  93. })
  94. end
  95. return menu
  96. end
  97. local menu = create_search_menu()
  98. menu:attach_to_widget(entry)
  99. function entry:on_populate_popup(menu)
  100. for _, item in ipairs {
  101. Gtk.SeparatorMenuItem {},
  102. Gtk.MenuItem {
  103. label = "C_lear",
  104. use_underline = true,
  105. visible = true,
  106. on_activate = function()
  107. entry.text = ''
  108. end
  109. },
  110. Gtk.MenuItem {
  111. label = "Search by",
  112. submenu = create_search_menu(),
  113. visible = true,
  114. },
  115. } do
  116. item.visible = true
  117. menu:append(item)
  118. end
  119. end
  120. function entry:on_icon_press(position, event)
  121. if position == 'PRIMARY' then
  122. menu:popup(nil, nil, nil, nil, event.button, event.time)
  123. else
  124. entry.text = ''
  125. end
  126. end
  127. function entry.on_notify:text(pspec)
  128. local has_text = entry.text ~= ''
  129. entry.secondary_icon_sensitive = has_text
  130. content.child.button_find.sensitive = has_text
  131. end
  132. local search_progress_id, finish_search_id
  133. local function finish_search()
  134. content.child.buttons.page = 0
  135. if search_progress_id then
  136. GLib.source_remove(search_progress_id)
  137. search_progress_id = nil
  138. end
  139. if finish_search_id then
  140. GLib.source_remove(finish_search_id)
  141. finish_search_id = nil
  142. end
  143. entry.progress_fraction = 0
  144. end
  145. local function start_search()
  146. content.child.buttons.page = 1
  147. search_progress_id =
  148. GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 1,
  149. function()
  150. search_progress_id = GLib.timeout_add(
  151. GLib.PRIORITY_DEFAULT, 100,
  152. function()
  153. entry:progress_pulse()
  154. return true
  155. end)
  156. return false
  157. end)
  158. finish_search_id = GLib.timeout_add_seconds(
  159. GLib.PRIORITY_DEFAULT, 15, finish_search)
  160. end
  161. function entry:on_activate()
  162. if not search_progress_id then
  163. start_search()
  164. end
  165. end
  166. function content.child.button_find:on_clicked()
  167. start_search()
  168. end
  169. function content.child.button_cancel:on_clicked()
  170. finish_search()
  171. end
  172. function window:on_destroy()
  173. finish_search()
  174. end
  175. window:show_all()
  176. return window
  177. end,
  178. "Entry/Search Entry",
  179. table.concat {
  180. "Gtk.Entry allows to display icons and progress information. ",
  181. "This demo shows how to use these features in a search entry."
  182. }