Mercurial Hosting > editor
annotate src/luan_editor/menu.luan @ 65:a4a97e8c204f
mac build
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Fri, 06 Jun 2025 00:29:38 -0600 |
| parents | 1c68552e8ac2 |
| children | f4e393a13d59 |
| rev | line source |
|---|---|
| 37 | 1 local Luan = require "luan:Luan.luan" |
| 2 local error = Luan.error | |
| 40 | 3 local ipairs = Luan.ipairs or error() |
| 37 | 4 local String = require "luan:String.luan" |
| 5 local to_number = String.to_number or error() | |
| 6 local new_menu_item = require("luan:swing/Menu_item.luan").new or error() | |
| 7 local new_check_box_menu_item = require("luan:swing/Check_box_menu_item.luan").new or error() | |
| 8 local new_menu_bar = require("luan:swing/Menu_bar.luan").new or error() | |
| 9 local Menu = require "luan:swing/Menu.luan" | |
| 10 local new_menu = Menu.new or error() | |
| 11 local separator = Menu.separator or error() | |
| 12 local Option_pane = require "luan:swing/Option_pane.luan" | |
| 13 local show_message_dialog = Option_pane.show_message_dialog or error() | |
| 14 local show_input_dialog = Option_pane.show_input_dialog or error() | |
| 40 | 15 local Frame = require "luan:swing/Frame.luan" |
| 16 local get_all_frames = Frame.get_all_frames or error() | |
| 37 | 17 local Spell_checker = require "classpath:luan_editor/Spell_checker.luan" |
| 18 local spell_check = Spell_checker.spell_check or error() | |
| 56 | 19 local Window = require "classpath:luan_editor/Window.luan" |
| 20 local show_list_window = Window.show_list_window or error() | |
| 64 | 21 local new_window = Window.new_window or error() |
| 37 | 22 |
| 23 | |
| 24 local function action_listener(fn) | |
| 25 return function(_) | |
| 26 fn() | |
| 27 end | |
| 28 end | |
| 29 | |
| 30 local function add_menu_bar(window) | |
| 31 local text_area = window.text_area | |
| 32 local document = text_area.document | |
| 33 local status_bar = window.status_bar | |
| 34 local revert = new_menu_item{ | |
| 35 text = "Revert" | |
| 36 enabled = window.has_file | |
| 37 action_listener = action_listener(window.revert) | |
| 38 } | |
| 48 | 39 local view_file_path = new_menu_item{ |
| 40 text = "File Path" | |
| 41 enabled = window.has_file | |
| 42 action_listener = function(_) | |
| 43 status_bar.text = window.title() | |
| 44 end | |
| 45 } | |
| 37 | 46 local undo = new_menu_item{ |
| 47 text = "Undo" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
48 accelerator = "Z" |
| 37 | 49 action_listener = action_listener(document.undo) |
| 50 } | |
| 51 local redo = new_menu_item{ | |
| 52 text = "Redo" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
53 accelerator = "shift Z" |
| 37 | 54 action_listener = action_listener(document.redo) |
| 55 } | |
| 56 local function update_undo_redo() | |
| 57 undo.set_enabled(document.can_undo()) | |
| 58 redo.set_enabled(document.can_redo()) | |
| 59 end | |
| 60 window.update_undo_redo = update_undo_redo -- dont gc | |
| 61 update_undo_redo() | |
| 62 document.add_undo_listener(update_undo_redo) | |
| 63 | |
| 64 local find_menu_item = new_check_box_menu_item{ | |
| 65 text = "Find and Replace" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
66 accelerator = "F" |
| 37 | 67 action_listener = function(event) |
| 68 window.show_find_panel(event.source.state) | |
| 69 end | |
| 70 } | |
| 71 window.find_menu_item = find_menu_item | |
| 72 | |
| 73 local menu_bar = new_menu_bar{ | |
| 74 menus = { | |
| 75 new_menu{ | |
| 76 text = "File" | |
| 77 menu_items = { | |
| 78 new_menu_item{ | |
| 79 text = "New File" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
80 accelerator = "N" |
| 64 | 81 action_listener = action_listener(new_window) |
| 37 | 82 } |
| 83 new_menu_item{ | |
| 84 text = "Open..." | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
85 accelerator = "O" |
| 37 | 86 action_listener = action_listener(window.open) |
| 87 } | |
| 88 new_menu_item{ | |
| 89 text = "Save" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
90 accelerator = "S" |
| 37 | 91 action_listener = function(_) |
| 92 if window.save() then | |
| 93 revert.set_enabled(true) | |
| 48 | 94 view_file_path.set_enabled(true) |
| 37 | 95 end |
| 96 end | |
| 97 } | |
| 98 new_menu_item{ | |
| 99 text = "Print" | |
| 100 action_listener = action_listener(text_area.print) | |
| 101 } | |
| 102 revert | |
| 55 | 103 new_menu_item{ |
| 104 text = "Paste File(s)" | |
| 64 | 105 accelerator = "P" |
| 55 | 106 action_listener = function(_) |
| 107 if not window.paste_files() then | |
| 108 show_message_dialog(nil,"No files to paste") | |
| 109 end | |
| 110 end | |
| 111 } | |
| 37 | 112 --[[ |
| 113 new_menu_item{ | |
| 114 text = "Test" | |
| 115 action_listener = function(_) | |
| 116 local location = window.frame.location | |
| 117 location.y = location.y - 20 | |
| 118 window.frame.location = location | |
| 119 end | |
| 120 } | |
| 121 ]] | |
| 122 } | |
| 123 } | |
| 124 new_menu{ | |
| 125 text = "Edit" | |
| 126 menu_items = { | |
| 127 undo | |
| 128 redo | |
| 129 separator | |
| 130 new_menu_item{ | |
| 131 text = "Cut" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
132 accelerator = "X" |
| 37 | 133 action_listener = action_listener(text_area.cut) |
| 134 } | |
| 135 new_menu_item{ | |
| 136 text = "Copy" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
137 accelerator = "C" |
| 37 | 138 action_listener = action_listener(text_area.copy) |
| 139 } | |
| 140 new_menu_item{ | |
| 141 text = "Paste" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
142 accelerator = "V" |
| 37 | 143 action_listener = action_listener(text_area.paste) |
| 144 } | |
| 145 separator | |
| 146 new_menu_item{ | |
| 147 text = "Indent" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
148 accelerator = "CLOSE_BRACKET" |
| 37 | 149 action_listener = action_listener(window.indent) |
| 150 } | |
| 151 new_menu_item{ | |
| 152 text = "Unindent" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
153 accelerator = "OPEN_BRACKET" |
| 37 | 154 action_listener = action_listener(window.unindent) |
| 155 } | |
| 156 separator | |
| 157 new_menu_item{ | |
| 158 text = "Select All" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
159 accelerator = "A" |
| 37 | 160 action_listener = action_listener(text_area.select_all) |
| 161 } | |
| 162 } | |
| 163 } | |
| 164 new_menu{ | |
| 165 text = "Find" | |
| 166 menu_items = { | |
| 167 find_menu_item | |
| 168 new_menu_item{ | |
| 169 text = "Find Case Insensitive" | |
| 170 action_listener = window.find_case_insensitive | |
| 171 } | |
| 172 new_menu_item{ | |
| 173 text = "Convert Leading Tabs to Spaces" | |
| 174 action_listener = window.tabs_to_spaces | |
| 175 } | |
| 176 new_menu_item{ | |
| 177 text = "Convert Leading Spaces to Tabs" | |
| 178 action_listener = window.spaces_to_tabs | |
| 179 } | |
| 180 } | |
| 181 } | |
| 182 new_menu{ | |
| 183 text = "View" | |
| 184 menu_items = { | |
| 185 new_check_box_menu_item{ | |
| 186 text = "Word Wrap" | |
| 187 state = text_area.line_wrap | |
| 188 action_listener = function(event) | |
| 189 window.set_line_wrap(event.source.state) | |
| 190 end | |
| 191 } | |
| 192 new_check_box_menu_item{ | |
| 193 text = "Show Whitespace" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
194 accelerator = "W" |
| 37 | 195 state = text_area.whitespace_visible |
| 196 action_listener = function(event) | |
| 197 window.set_whitespace_visible(event.source.state) | |
| 198 end | |
| 199 } | |
| 200 new_check_box_menu_item{ | |
| 201 text = "Spell Check" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
202 accelerator = "SEMICOLON" |
| 37 | 203 action_listener = function(event) |
| 204 spell_check(text_area,event.source.state) | |
| 205 end | |
| 206 } | |
| 207 new_menu_item{ | |
| 208 text = "Cursor Column" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
209 accelerator = "B" |
| 37 | 210 action_listener = function(_) |
| 211 status_bar.text = "Cursor Column: "..window.cursor_column() | |
| 212 end | |
| 213 } | |
| 214 new_menu_item{ | |
| 215 text = "Goto Line" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
216 accelerator = "G" |
| 37 | 217 action_listener = function(_) |
| 218 local input = show_input_dialog( window.frame, "Goto line" ) | |
| 219 if input == nil then | |
| 220 return | |
| 221 end | |
| 222 local line = to_number(input) | |
| 223 try | |
| 224 window.goto(line) | |
| 225 status_bar.text = "Went to line "..line | |
| 226 catch e | |
| 227 status_bar.text = "Invalid line: "..input | |
| 228 end | |
| 229 end | |
| 230 } | |
| 231 new_menu_item{ | |
| 232 text = "Tab Size" | |
| 233 action_listener = function(_) | |
| 234 local input = show_input_dialog( window.frame, "Tab size", text_area.tab_size ) | |
| 235 if input == nil then | |
| 236 return | |
| 237 end | |
| 238 local size = to_number(input) | |
| 239 try | |
| 240 window.set_tab_size(size) | |
| 241 status_bar.text = "Set tab size to "..size | |
| 242 catch e | |
| 243 status_bar.text = "Invalid tab size: "..input | |
| 244 end | |
| 245 end | |
| 246 } | |
| 48 | 247 view_file_path |
| 37 | 248 } |
| 249 } | |
| 38 | 250 new_menu{ |
| 251 text = "Window" | |
| 252 menu_items = { | |
| 253 new_menu_item{ | |
| 254 text = "Duplicate Window" | |
| 255 action_listener = action_listener(window.duplicate) | |
| 256 } | |
| 40 | 257 new_menu_item{ |
| 258 text = "Align Windows" | |
| 259 action_listener = function(_) | |
| 260 local this_frame = window.frame | |
| 261 local location = this_frame.location | |
| 262 local size = this_frame.size | |
| 263 for _, frame in ipairs(get_all_frames()) do | |
| 264 frame.location = location | |
| 265 frame.size = size | |
| 266 end | |
| 267 end | |
| 268 } | |
| 41 | 269 new_menu_item{ |
| 270 text = "List Windows" | |
|
58
7e2d6426c155
cross-platform accelerators
Franklin Schmidt <fschmidt@gmail.com>
parents:
56
diff
changeset
|
271 accelerator = "L" |
| 56 | 272 action_listener = action_listener(show_list_window) |
| 41 | 273 } |
| 38 | 274 } |
| 275 } | |
| 37 | 276 } |
| 277 } | |
| 278 window.frame.set_menu_bar(menu_bar) | |
| 279 end | |
| 280 | |
| 281 return add_menu_bar |
