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