stickynote_app.lua 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. local store_area = 'stickynote:files'
  2. laptop.register_app("stickynote", {
  3. app_name = "Notepad",
  4. app_icon = "laptop_notes_pad.png",
  5. app_info = "Write Text Documents",
  6. formspec_func = function(app, mtos)
  7. local data = mtos.bdev:get_app_storage('system', 'stickynote')
  8. data.files = data.files or {}
  9. data.text = data.text or ""
  10. if data.selected_disk_name and data.selected_file_name then
  11. app.app_info = app.app_info..' - Open File: '..data.selected_disk_name..' / '..data.selected_file_name
  12. end
  13. -- cache sorted files list
  14. if not data.fileslist_sorted then
  15. data.fileslist_sorted = {}
  16. for filename,_ in pairs(data.files) do
  17. table.insert(data.fileslist_sorted, filename)
  18. end
  19. table.sort(data.fileslist_sorted)
  20. end
  21. local formspec = "background[0,1.35;15.2,8.35;"..mtos.theme.contrast_background.."]"..
  22. "textarea[0.35,1.35;15.08,9.5;text;;"..minetest.formspec_escape(data.text).."]"..
  23. mtos.theme:get_button('0,0.5;1.5,0.8', 'minor', 'clear', 'New', 'New file')..
  24. mtos.theme:get_button('2,0.5;1.5,0.8', 'minor', 'load', 'Load', 'Load file')..
  25. mtos.theme:get_button('4,0.5;1.5,0.8', 'minor', 'save', 'Save', 'Save file')..
  26. mtos.theme:get_button('8,0.5;1.5,0.8', 'minor', 'print', 'Print', 'Print file')
  27. return formspec
  28. end,
  29. receive_fields_func = function(app, mtos, sender, fields)
  30. local data = mtos.bdev:get_app_storage('system', 'stickynote')
  31. if fields.text then
  32. data.text = fields.text
  33. end
  34. if fields.load then
  35. mtos:select_file_dialog({
  36. mode = 'open',
  37. allowed_disks = {'hdd', 'removable'},
  38. selected_disk_name = data.selected_disk_name,
  39. selected_file_name = data.selected_file_name,
  40. store_name = store_area,
  41. prefix = 'open_',
  42. })
  43. elseif fields.open_selected_disk and fields.open_selected_file then
  44. data.selected_disk_name = fields.open_selected_disk
  45. data.selected_file_name = fields.open_selected_file
  46. local store = mtos.bdev:get_app_storage(data.selected_disk_name, store_area)
  47. if store then
  48. data.text = store[data.selected_file_name].content
  49. end
  50. elseif fields.save then
  51. mtos:select_file_dialog({
  52. mode = 'save',
  53. allowed_disks = {'hdd', 'removable'},
  54. selected_disk_name = data.selected_disk_name,
  55. selected_file_name = data.selected_file_name,
  56. store_name = store_area,
  57. prefix = 'save_',
  58. })
  59. elseif fields.save_selected_disk and fields.save_selected_file then
  60. data.selected_disk_name = fields.save_selected_disk
  61. data.selected_file_name = fields.save_selected_file
  62. local store = mtos.bdev:get_app_storage(data.selected_disk_name, store_area)
  63. if store then
  64. store[data.selected_file_name] = { content = data.text, ctime = os.time(), owner = sender:get_player_name() }
  65. end
  66. elseif fields.clear then
  67. data.selected_disk_name = nil
  68. data.selected_file_name = nil
  69. data.text = ""
  70. elseif fields.print then
  71. mtos:print_file_dialog({
  72. label = data.selected_file_name,
  73. text = data.text,
  74. })
  75. end
  76. end
  77. })